From 8175b15b9e1a76d02b1477cca7a8e613433e7346 Mon Sep 17 00:00:00 2001 From: Avraham Shukron Date: Tue, 10 Oct 2017 01:08:11 +0300 Subject: [PATCH] ListField: removed default `None` value for the `items_type` parameter. This will create more consistent code since `item_types` _is_ required. Tests had to be modified a bit. Specifically tests which used Model class with circular reference to itself. It was easier to just extract the dummy class outside the function, in order to avoid import path hell. This change breaks the current API, so if merged it should be into a new major version. --- jsonmodels/fields.py | 2 +- tests/test_jsonmodels.py | 60 +++++++++++++++------------------------- tests/test_validation.py | 11 ++------ 3 files changed, 27 insertions(+), 46 deletions(-) diff --git a/jsonmodels/fields.py b/jsonmodels/fields.py index dd87bcb..d9bc724 100644 --- a/jsonmodels/fields.py +++ b/jsonmodels/fields.py @@ -162,7 +162,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 938422b..bfe3feb 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):