diff --git a/jsonmodels/fields.py b/jsonmodels/fields.py index 722c833..07e8ccf 100644 --- a/jsonmodels/fields.py +++ b/jsonmodels/fields.py @@ -187,7 +187,7 @@ class ListField(BaseField): types = (list,) - def __init__(self, items_types=None, *args, **kwargs): + def __init__(self, items_types, *args, **kwargs): """Init. `ListField` is **always not required**. If you want to control number diff --git a/tests/test_jsonmodels.py b/tests/test_jsonmodels.py index ab7aad5..00d5f5f 100644 --- a/tests/test_jsonmodels.py +++ b/tests/test_jsonmodels.py @@ -3,16 +3,18 @@ from jsonmodels import models, fields, errors -def test_model1(): +class Person(models.Base): - class Person(models.Base): + name = fields.StringField() + surname = fields.StringField() + age = fields.IntField() + cash = fields.FloatField() + children = fields.ListField(["Person"]) - name = fields.StringField() - surname = fields.StringField() - age = fields.IntField() - alan = Person() +def test_model1(): + alan = Person() alan.name = 'Alan' alan.surname = 'Wake' alan.age = 34 @@ -20,39 +22,30 @@ class Person(models.Base): def test_required(): - class Person(models.Base): - - name = fields.StringField(required=True) - surname = fields.StringField() - age = fields.IntField() + class Foo(models.Base): + bar = fields.StringField(required=True) - alan = Person() + alan = Foo() with pytest.raises(errors.ValidationError): alan.validate() - alan.name = 'Chuck' + alan.bar = 'Chuck' alan.validate() def test_type_validation(): - class Person(models.Base): - - name = fields.StringField() - age = fields.IntField() - alan = Person() - alan.age = 42 def test_base_field_should_not_be_usable(): - class Person(models.Base): + class Foo(models.Base): name = fields.BaseField() - alan = Person() + alan = Foo() with pytest.raises(errors.ValidationError): alan.name = 'some name' @@ -63,13 +56,6 @@ class Person(models.Base): def test_value_replacements(): - class Person(models.Base): - - name = fields.StringField() - age = fields.IntField() - cash = fields.FloatField() - children = fields.ListField() - alan = Person() assert alan.name is None assert alan.age is None @@ -81,7 +67,7 @@ def test_list_field(): class Car(models.Base): - wheels = fields.ListField() + wheels = fields.ListField([str]) viper = Car() @@ -167,9 +153,12 @@ class Garage2(models.Base): def test_list_validation(): + class Car(models.Base): + pass + class Garage(models.Base): - cars = fields.ListField() + cars = fields.ListField([Car]) garage = Garage() @@ -401,25 +390,22 @@ class Person2(object): def test_items_types(): - class Person(object): + class Foo(object): pass - class Person2(object): + class Bar(object): pass - allowed_types = (Person,) + allowed_types = (Foo,) field = fields.ListField(allowed_types) assert allowed_types == field.items_types - allowed_types = (Person, Person2) + allowed_types = (Foo, Bar) field = fields.ListField(allowed_types) assert allowed_types == field.items_types - field = fields.ListField() - assert tuple() == field.items_types - def test_required_embedded_field(): diff --git a/tests/test_validation.py b/tests/test_validation.py index e22c81f..7403048 100644 --- a/tests/test_validation.py +++ b/tests/test_validation.py @@ -53,23 +53,18 @@ class Person(models.Base): assert 33 == arg.pop() -def test_validators_are_always_iterable(): +class Person(models.Base): + children = fields.ListField(["Person"]) - class Person(models.Base): - children = fields.ListField() +def test_validators_are_always_iterable(): alan = Person() - assert isinstance(alan.get_field('children').validators, list) def test_get_field_not_found(): - class Person(models.Base): - - children = fields.ListField() - alan = Person() with pytest.raises(errors.FieldNotFound):