Skip to content

Commit d240306

Browse files
committed
Update json field tests to account for error cases
1 parent 7944d4d commit d240306

File tree

1 file changed

+39
-2
lines changed

1 file changed

+39
-2
lines changed

tests/test_json_field.py

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,43 @@ class Meta:
3636
model = TestModel.get()
3737
assert model.some_data == data
3838

39-
with pytest.raises(peewee.IntegrityError):
40-
bad = TestModel(some_data=Path("."))
39+
40+
def test_errors(fakedb):
41+
"""Test that errors are raised as expected"""
42+
43+
class GoodModel(peewee.Model):
44+
class Meta:
45+
database = fakedb
46+
# Needs to match the table name below
47+
table_name = "one_table"
48+
49+
id = peewee.AutoField()
50+
some_data = peewee_plus.JSONField()
51+
52+
class BadModel(peewee.Model):
53+
class Meta:
54+
database = fakedb
55+
# Needs to match the table name above
56+
table_name = "one_table"
57+
58+
id = peewee.AutoField()
59+
some_data = peewee.TextField()
60+
61+
fakedb.create_tables([GoodModel])
62+
63+
with pytest.raises(ValueError):
64+
# The usage of path here is arbitrary, it just needs to be any
65+
# non-JSON serializable type
66+
bad = GoodModel(some_data=Path("."))
4167
bad.save()
68+
69+
good = GoodModel(some_data={"foo": 123})
70+
good.save()
71+
72+
# This is overwriting the ``some_data`` on the above object with garbage
73+
bad = BadModel.get(good.id)
74+
bad.some_data = "This{ string' is not, valid JSON;"
75+
bad.save()
76+
77+
with pytest.raises(peewee.IntegrityError):
78+
GoodModel.get(good.id)

0 commit comments

Comments
 (0)