mirror of
				https://github.com/enpaul/peewee-plus.git
				synced 2025-11-04 01:08:38 +00:00 
			
		
		
		
	Update json field tests to account for error cases
This commit is contained in:
		@@ -36,6 +36,43 @@ def test_json(fakedb):
 | 
			
		||||
    model = TestModel.get()
 | 
			
		||||
    assert model.some_data == data
 | 
			
		||||
 | 
			
		||||
    with pytest.raises(peewee.IntegrityError):
 | 
			
		||||
        bad = TestModel(some_data=Path("."))
 | 
			
		||||
 | 
			
		||||
def test_errors(fakedb):
 | 
			
		||||
    """Test that errors are raised as expected"""
 | 
			
		||||
 | 
			
		||||
    class GoodModel(peewee.Model):
 | 
			
		||||
        class Meta:
 | 
			
		||||
            database = fakedb
 | 
			
		||||
            # Needs to match the table name below
 | 
			
		||||
            table_name = "one_table"
 | 
			
		||||
 | 
			
		||||
        id = peewee.AutoField()
 | 
			
		||||
        some_data = peewee_plus.JSONField()
 | 
			
		||||
 | 
			
		||||
    class BadModel(peewee.Model):
 | 
			
		||||
        class Meta:
 | 
			
		||||
            database = fakedb
 | 
			
		||||
            # Needs to match the table name above
 | 
			
		||||
            table_name = "one_table"
 | 
			
		||||
 | 
			
		||||
        id = peewee.AutoField()
 | 
			
		||||
        some_data = peewee.TextField()
 | 
			
		||||
 | 
			
		||||
    fakedb.create_tables([GoodModel])
 | 
			
		||||
 | 
			
		||||
    with pytest.raises(ValueError):
 | 
			
		||||
        # The usage of path here is arbitrary, it just needs to be any
 | 
			
		||||
        # non-JSON serializable type
 | 
			
		||||
        bad = GoodModel(some_data=Path("."))
 | 
			
		||||
        bad.save()
 | 
			
		||||
 | 
			
		||||
    good = GoodModel(some_data={"foo": 123})
 | 
			
		||||
    good.save()
 | 
			
		||||
 | 
			
		||||
    # This is overwriting the ``some_data`` on the above object with garbage
 | 
			
		||||
    bad = BadModel.get(good.id)
 | 
			
		||||
    bad.some_data = "This{ string' is not, valid JSON;"
 | 
			
		||||
    bad.save()
 | 
			
		||||
 | 
			
		||||
    with pytest.raises(peewee.IntegrityError):
 | 
			
		||||
        GoodModel.get(good.id)
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user