<s> import pytest <EOL> from jsonmodels import models , fields , validators , errors <EOL> from jsonmodels . utilities import compare_schemas <EOL> from . utilities import get_fixture <EOL> def test_model1 ( ) : <EOL> class Person ( models . Base ) : <EOL> name = fields . StringField ( required = True ) <EOL> surname = fields . StringField ( required = True ) <EOL> age = fields . IntField ( ) <EOL> alan = Person ( ) <EOL> schema = alan . to_json_schema ( ) <EOL> pattern = get_fixture ( '<STR_LIT>' ) <EOL> assert compare_schemas ( pattern , schema ) is True <EOL> def test_model2 ( ) : <EOL> class Car ( models . Base ) : <EOL> brand = fields . StringField ( required = True ) <EOL> registration = fields . StringField ( required = True ) <EOL> class Toy ( models . Base ) : <EOL> name = fields . StringField ( required = True ) <EOL> class Kid ( models . Base ) : <EOL> name = fields . StringField ( required = True ) <EOL> surname = fields . StringField ( required = True ) <EOL> age = fields . IntField ( ) <EOL> toys = fields . ListField ( Toy ) <EOL> class Person ( models . Base ) : <EOL> name = fields . StringField ( required = True ) <EOL> surname = fields . StringField ( required = True ) <EOL> age = fields . IntField ( ) <EOL> kids = fields . ListField ( Kid ) <EOL> car = fields . EmbeddedField ( Car ) <EOL> chuck = Person ( ) <EOL> schema = chuck . to_json_schema ( ) <EOL> pattern = get_fixture ( '<STR_LIT>' ) <EOL> assert compare_schemas ( pattern , schema ) is True <EOL> def test_model3 ( ) : <EOL> class Viper ( models . Base ) : <EOL> brand = fields . StringField ( ) <EOL> capacity = fields . FloatField ( ) <EOL> class Lamborghini ( models . Base ) : <EOL> brand = fields . StringField ( ) <EOL> velocity = fields . FloatField ( ) <EOL> class PC ( models . Base ) : <EOL> name = fields . StringField ( ) <EOL> ports = fields . StringField ( ) <EOL> class Laptop ( models . Base ) : <EOL> name = fields . StringField ( ) <EOL> battery_voltage = fields . FloatField ( ) <EOL> class Tablet ( models . Base ) : <EOL> name = fields . StringField ( ) <EOL> os = fields . StringField ( ) <EOL> class Person ( models . Base ) : <EOL> name = fields . StringField ( required = True ) <EOL> surname = fields . StringField ( required = True ) <EOL> age = fields . IntField ( ) <EOL> car = fields . EmbeddedField ( [ Viper , Lamborghini ] ) <EOL> computer = fields . ListField ( [ PC , Laptop , Tablet ] ) <EOL> chuck = Person ( ) <EOL> schema = chuck . to_json_schema ( ) <EOL> pattern = get_fixture ( '<STR_LIT>' ) <EOL> assert compare_schemas ( pattern , schema ) is True <EOL> def test_model_with_constructors ( ) : <EOL> class Car ( models . Base ) : <EOL> def __init__ ( self , some_value ) : <EOL> pass <EOL> brand = fields . StringField ( required = True ) <EOL> registration = fields . StringField ( required = True ) <EOL> class Toy ( models . Base ) : <EOL> def __init__ ( self , some_value ) : <EOL> pass <EOL> name = fields . StringField ( required = True ) <EOL> class Kid ( models . Base ) : <EOL> def __init__ ( self , some_value ) : <EOL> pass <EOL> name = fields . StringField ( required = True ) <EOL> surname = fields . StringField ( required = True ) <EOL> age = fields . IntField ( ) <EOL> toys = fields . ListField ( Toy ) <EOL> class Person ( models . Base ) : <EOL> def __init__ ( self , some_value ) : <EOL> pass <EOL> name = fields . StringField ( required = True ) <EOL> surname = fields . StringField ( required = True ) <EOL> age = fields . IntField ( ) <EOL> kids = fields . ListField ( Kid ) <EOL> car = fields . EmbeddedField ( Car ) <EOL> schema = Person . to_json_schema ( ) <EOL> pattern = get_fixture ( '<STR_LIT>' ) <EOL> assert compare_schemas ( pattern , schema ) is True <EOL> def test_datetime_fields ( ) : <EOL> class Event ( models . Base ) : <EOL> time = fields . TimeField ( ) <EOL> date = fields . DateField ( ) <EOL> end = fields . DateTimeField ( ) <EOL> schema = Event . to_json_schema ( ) <EOL> pattern = get_fixture ( '<STR_LIT>' ) <EOL> assert compare_schemas ( pattern , schema ) is True <EOL> def test_bool_field ( ) : <EOL> class Person ( models . Base ) : <EOL> has_childen = fields . BoolField ( ) <EOL> schema = Person . to_json_schema ( ) <EOL> pattern = get_fixture ( '<STR_LIT>' ) <EOL> assert compare_schemas ( pattern , schema ) is True <EOL> def test_unsupported_field ( ) : <EOL> class NewField ( fields . BaseField ) : <EOL> pass <EOL> class Person ( models . Base ) : <EOL> some_property = NewField ( ) <EOL> with pytest . raises ( errors . FieldNotSupported ) : <EOL> Person . to_json_schema ( ) <EOL> def test_validators_can_modify_schema ( ) : <EOL> class ClassBasedValidator ( object ) : <EOL> def validate ( self , value ) : <EOL> raise RuntimeError ( ) <EOL> def modify_schema ( self , field_schema ) : <EOL> field_schema [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> def function_validator ( value ) : <EOL> raise RuntimeError ( ) <EOL> class Person ( models . Base ) : <EOL> name = fields . StringField ( validators = ClassBasedValidator ( ) ) <EOL> surname = fields . StringField ( validators = function_validator ) <EOL> for person in [ Person , Person ( ) ] : <EOL> schema = person . to_json_schema ( ) <EOL> pattern = get_fixture ( '<STR_LIT>' ) <EOL> assert compare_schemas ( pattern , schema ) is True <EOL> def test_min_validator ( ) : <EOL> class Person ( models . Base ) : <EOL> name = fields . StringField ( ) <EOL> surname = fields . StringField ( ) <EOL> age = fields . IntField ( validators = validators . Min ( <NUM_LIT> ) ) <EOL> schema = Person . to_json_schema ( ) <EOL> pattern = get_fixture ( '<STR_LIT>' ) <EOL> assert compare_schemas ( pattern , schema ) <EOL> def test_min_validator_with_exclusive ( ) : <EOL> class Person ( models . Base ) : <EOL> name = fields . StringField ( ) <EOL> surname = fields . StringField ( ) <EOL> age = fields . IntField ( validators = validators . Min ( <NUM_LIT> , True ) ) <EOL> schema = Person . to_json_schema ( ) <EOL> pattern = get_fixture ( '<STR_LIT>' ) <EOL> assert compare_schemas ( pattern , schema ) <EOL> def test_max_validator ( ) : <EOL> class Person ( models . Base ) : <EOL> name = fields . StringField ( ) <EOL> surname = fields . StringField ( ) <EOL> age = fields . IntField ( validators = validators . Max ( <NUM_LIT> ) ) <EOL> schema = Person . to_json_schema ( ) <EOL> pattern = get_fixture ( '<STR_LIT>' ) <EOL> assert compare_schemas ( pattern , schema ) <EOL> def test_max_validator_with_exclusive ( ) : <EOL> class Person ( models . Base ) : <EOL> name = fields . StringField ( ) <EOL> surname = fields . StringField ( ) <EOL> age = fields . IntField ( validators = validators . Max ( <NUM_LIT> , True ) ) <EOL> schema = Person . to_json_schema ( ) <EOL> pattern = get_fixture ( '<STR_LIT>' ) <EOL> assert compare_schemas ( pattern , schema ) <EOL> def test_regex_validator ( ) : <EOL> class Person ( models . Base ) : <EOL> name = fields . StringField ( <EOL> validators = validators . Regex ( '<STR_LIT>' ) ) <EOL> schema = Person . to_json_schema ( ) <EOL> pattern = get_fixture ( '<STR_LIT>' ) <EOL> assert compare_schemas ( pattern , schema ) <EOL> def test_regex_validator_when_ecma_regex_given ( ) : <EOL> class Person ( models . Base ) : <EOL> name = fields . StringField ( <EOL> validators = validators . Regex ( '<STR_LIT>' ) ) <EOL> schema = Person . to_json_schema ( ) <EOL> pattern = get_fixture ( '<STR_LIT>' ) <EOL> assert compare_schemas ( pattern , schema ) <EOL> def test_regex_validator_with_flag ( ) : <EOL> class Person ( models . Base ) : <EOL> name = fields . StringField ( <EOL> validators = validators . Regex ( <EOL> '<STR_LIT>' , ignorecase = True ) ) <EOL> schema = Person . to_json_schema ( ) <EOL> pattern = get_fixture ( '<STR_LIT>' ) <EOL> assert compare_schemas ( pattern , schema ) <EOL> def test_length_validator_min ( ) : <EOL> class Person ( models . Base ) : <EOL> name = fields . StringField ( validators = validators . Length ( <NUM_LIT:5> ) ) <EOL> surname = fields . StringField ( ) <EOL> age = fields . IntField ( ) <EOL> schema = Person . to_json_schema ( ) <EOL> pattern = get_fixture ( '<STR_LIT>' ) <EOL> assert compare_schemas ( pattern , schema ) <EOL> def test_length_validator ( ) : <EOL> class Person ( models . Base ) : <EOL> name = fields . StringField ( validators = validators . Length ( <NUM_LIT:5> , <NUM_LIT:20> ) ) <EOL> surname = fields . StringField ( ) <EOL> age = fields . IntField ( ) <EOL> schema = Person . to_json_schema ( ) <EOL> pattern = get_fixture ( '<STR_LIT>' ) <EOL> assert compare_schemas ( pattern , schema ) <EOL> def test_max_only_validator ( ) : <EOL> class Person ( models . Base ) : <EOL> name = fields . StringField ( <EOL> validators = validators . Length ( maximum_value = <NUM_LIT:20> ) <EOL> ) <EOL> surname = fields . StringField ( ) <EOL> age = fields . IntField ( ) <EOL> schema = Person . to_json_schema ( ) <EOL> pattern = get_fixture ( '<STR_LIT>' ) <EOL> assert compare_schemas ( pattern , schema ) <EOL> def test_schema_for_list_and_primitives ( ) : <EOL> class Event ( models . Base ) : <EOL> time = fields . TimeField ( ) <EOL> date = fields . DateField ( ) <EOL> end = fields . DateTimeField ( ) <EOL> class Person ( models . Base ) : <EOL> names = fields . ListField ( [ str , int , float , bool , Event ] ) <EOL> schema = Person . to_json_schema ( ) <EOL> pattern = get_fixture ( '<STR_LIT>' ) <EOL> assert compare_schemas ( pattern , schema ) <EOL> def test_schema_for_unsupported_primitive ( ) : <EOL> class Person ( models . Base ) : <EOL> names = fields . ListField ( [ str , object ] ) <EOL> with pytest . raises ( errors . FieldNotSupported ) : <EOL> Person . to_json_schema ( ) </s>
