Add validators

This commit is contained in:
evilchili
2025-11-01 22:25:15 -07:00
parent b7d7ef9638
commit 64aef7c18b
7 changed files with 795 additions and 481 deletions
+35 -2
View File
@@ -10,7 +10,14 @@ from tinydb.storages import MemoryStorage
from grung import examples
from grung.db import GrungDB
from grung.exceptions import CircularReferenceError, UniqueConstraintError
from grung.exceptions import (
CircularReferenceError,
InvalidFieldTypeError,
InvalidLengthError,
InvalidSizeError,
PatternMatchError,
UniqueConstraintError,
)
@pytest.fixture
@@ -82,7 +89,7 @@ def test_subgroups(db):
# recursion!
with pytest.raises(CircularReferenceError):
tos.members = [tos]
tos.groups = [tos]
db.save(tos)
@@ -201,3 +208,29 @@ def test_file_pointers(db):
location_on_disk = db.path / album._metadata.fields["review"].relpath(album)
assert location_on_disk.read_text() == album.review
@pytest.mark.parametrize(
"updates, expected",
[
({"name": ""}, InvalidLengthError),
({"name": "a name longer than 30 characters is what we have here"}, InvalidLengthError),
({"name": 23}, InvalidFieldTypeError),
({"number": -1}, InvalidSizeError),
({"number": 256}, InvalidSizeError),
({"email": "foo+alias@"}, PatternMatchError),
],
ids=[
"name too short",
"name too long",
"name is not a string",
"number too small",
"number too big",
"invalid email addres",
],
)
def test_validators(updates, expected, db):
user = db.save(examples.User(name="john", email="john@foo", password="fnord", created=datetime.utcnow()))
with pytest.raises(expected):
user.update(**updates)
db.save(user)