How to use jsonmodels - 10 common examples

To help you get started, we’ve selected a few jsonmodels examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github jazzband / jsonmodels / tests / test_name.py View on Github external
def test_fields_can_have_different_names():

    class Human(models.Base):

        name = fields.StringField()
        surname = fields.StringField(name='second-name')

    chuck = Human(name='Chuck', surname='Testa')
    assert chuck.to_struct() == {'name': 'Chuck', 'second-name': 'Testa'}
github jazzband / jsonmodels / tests / test_schema.py View on Github external
def test_model2(self):

        class Car(models.Base):

            brand = fields.StringField(required=True)
            registration = fields.StringField(required=True)

        class Toy(models.Base):

            name = fields.StringField(required=True)

        class Kid(models.Base):

            name = fields.StringField(required=True)
            surname = fields.StringField(required=True)
            age = fields.IntField()
            toys = fields.ListField(Toy)

        class Person(models.Base):

            name = fields.StringField(required=True)
            surname = fields.StringField(required=True)
github jazzband / jsonmodels / tests / test_validation.py View on Github external
def test_validation():

    validator1 = FakeValidator()
    validator2 = FakeValidator()

    called = []
    arg = []

    def validator3(value):
        called.append(1)
        arg.append(value)

    class Person(models.Base):

        name = fields.StringField(
            required=True, validators=[validator1, validator2])
        surname = fields.StringField(required=True)
        age = fields.IntField(validators=validator3)
        cash = fields.FloatField()

    person = Person()
    person.name = 'John'
    person.surname = 'Smith'
    person.age = 33
    person.cash = 123567.89

    validator1.assert_called_once_with('John')
    validator2.assert_called_once_with('John')

    assert 1 == sum(called)
    assert 33 == arg.pop()
github jazzband / jsonmodels / tests / test_schema.py View on Github external
def test_regex_validator(self):

        class Person(models.Base):

            name = fields.StringField(
                validators=validators.Regex('^some pattern$'))

        schema = Person.to_json_schema()

        pattern = get_fixture('schema_pattern.json')
        self.assertTrue(compare_schemas(pattern, schema))
github jazzband / jsonmodels / tests / test_schema.py View on Github external
def test_model_with_constructors(self):

        class Car(models.Base):

            def __init__(self, some_value):
                pass

            brand = fields.StringField(required=True)
            registration = fields.StringField(required=True)

        class Toy(models.Base):

            def __init__(self, some_value):
                pass

            name = fields.StringField(required=True)

        class Kid(models.Base):

            def __init__(self, some_value):
                pass

            name = fields.StringField(required=True)
            surname = fields.StringField(required=True)
github jazzband / jsonmodels / tests / test_schema.py View on Github external
def __init__(self, some_value):
                pass

            name = fields.StringField(required=True)
            surname = fields.StringField(required=True)
            age = fields.IntField()
            toys = fields.ListField(Toy)

        class Person(models.Base):

            def __init__(self, some_value):
                pass

            name = fields.StringField(required=True)
            surname = fields.StringField(required=True)
            age = fields.IntField()
            kids = fields.ListField(Kid)
            car = fields.EmbeddedField(Car)

        schema = Person.to_json_schema()

        pattern = get_fixture('schema2.json')
        self.assertTrue(compare_schemas(pattern, schema))
github jazzband / jsonmodels / tests / test_data_initialization.py View on Github external
def test_initialization():

    class Person(models.Base):

        name = fields.StringField()
        surname = fields.StringField()
        age = fields.IntField()
        cash = fields.FloatField()

    data = dict(
        name='Alan',
        surname='Wake',
        age=24,
        cash=2445.45,
        trash='123qwe',
    )

    alan1 = Person(**data)
    alan2 = Person()
    alan2.populate(**data)
    for alan in [alan1, alan2]:
        assert alan.name == 'Alan'
        assert alan.surname == 'Wake'
github jazzband / jsonmodels / tests / test_schema.py View on Github external
def __init__(self, some_value):
                pass

            name = fields.StringField(required=True)
            surname = fields.StringField(required=True)
            age = fields.IntField()
            toys = fields.ListField(Toy)

        class Person(models.Base):

            def __init__(self, some_value):
                pass

            name = fields.StringField(required=True)
            surname = fields.StringField(required=True)
            age = fields.IntField()
            kids = fields.ListField(Kid)
            car = fields.EmbeddedField(Car)

        schema = Person.to_json_schema()

        pattern = get_fixture('schema2.json')
        self.assertTrue(compare_schemas(pattern, schema))
github jazzband / jsonmodels / tests / test_fields.py View on Github external
def test_bool_field():

    field = fields.BoolField()

    class Person(models.Base):

        is_programmer = field

    person = Person()
    assert person.is_programmer is None

    person.is_programmer = True
    assert person.is_programmer is True

    person.is_programmer = False
    assert person.is_programmer is False

    assert field.parse_value(True) is True
    assert field.parse_value('something') is True
github jazzband / jsonmodels / tests / test_schema.py View on Github external
def test_model1(self):

        class Person(models.Base):

            name = fields.StringField(required=True)
            surname = fields.StringField(required=True)
            age = fields.IntField()

        alan = Person()
        schema = alan.to_json_schema()

        pattern = get_fixture('schema1.json')
        self.assertTrue(compare_schemas(pattern, schema))