Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ListField: removed default None value for the items_types parameter. #97

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion jsonmodels/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
60 changes: 23 additions & 37 deletions tests/test_jsonmodels.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,56 +3,49 @@
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


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'
Expand All @@ -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
Expand All @@ -81,7 +67,7 @@ def test_list_field():

class Car(models.Base):

wheels = fields.ListField()
wheels = fields.ListField([str])

viper = Car()

Expand Down Expand Up @@ -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()

Expand Down Expand Up @@ -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():

Expand Down
11 changes: 3 additions & 8 deletions tests/test_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down