def test_custom_root_type_required(): custom_root_type = CustomRootType( fields=[DataModelFieldBase(data_type=DataType(type='str'), required=True)], reference=Reference(name='test_model', path='test_model'), ) assert custom_root_type.name == 'test_model' assert custom_root_type.fields == [ DataModelFieldBase(data_type=DataType(type='str'), required=True) ] assert custom_root_type.render() == ( 'class test_model(BaseModel):\n' ' __root__: str' )
def test_sort_data_models(): reference_a = Reference(path='A', original_name='A', name='A') reference_b = Reference(path='B', original_name='B', name='B') reference_c = Reference(path='C', original_name='C', name='C') data_type_a = DataType(reference=reference_a) data_type_b = DataType(reference=reference_b) data_type_c = DataType(reference=reference_c) reference = [ BaseModel( fields=[ DataModelField(data_type=data_type_a), DataModelFieldBase(data_type=data_type_c), ], reference=reference_a, ), BaseModel( fields=[DataModelField(data_type=data_type_b)], reference=reference_b, ), BaseModel( fields=[DataModelField(data_type=data_type_b)], reference=reference_c, ), ] unresolved, resolved, require_update_action_models = sort_data_models( reference) expected = OrderedDict() expected['B'] = reference[1] expected['C'] = reference[2] expected['A'] = reference[0] assert resolved == expected assert unresolved == [] assert require_update_action_models == ['B', 'A']
def test_custom_root_type_decorator(): custom_root_type = CustomRootType( name='test_model', fields=[ DataModelFieldBase(data_type=DataType(type='str'), required=True) ], decorators=['@validate'], base_classes=['Base'], ) assert custom_root_type.name == 'test_model' assert custom_root_type.fields == [ DataModelFieldBase(data_type=DataType(type='str'), required=True) ] assert custom_root_type.base_class == 'Base' assert (custom_root_type.render() == '@validate\n' 'class test_model(Base):\n' ' __root__: str')
def test_data_class(): field = DataModelFieldBase( name='a', data_types=[DataType(type='str')], required=True ) data_class = DataClass(name='test_model', fields=[field]) assert data_class.name == 'test_model' assert data_class.fields == [field] assert data_class.decorators == [] assert data_class.render() == '@dataclass\n' 'class test_model:\n' ' a: str'
def test_data_class_optional(): field = DataModelFieldBase(name='a', data_type=DataType(type='str'), default="'abc'", required=True) data_class = DataClass(name='test_model', fields=[field]) assert data_class.name == 'test_model' assert data_class.fields == [field] assert data_class.decorators == [] assert (data_class.render() == '@dataclass\n' 'class test_model:\n' ' a: str = \'abc\'')
def test_custom_root_type(): custom_root_type = CustomRootType( fields=[ DataModelFieldBase( name='a', data_type=DataType(type='str'), default='abc', required=False, ) ], reference=Reference(name='test_model', path='test_model'), ) assert custom_root_type.name == 'test_model' assert custom_root_type.fields == [ DataModelFieldBase( name='a', data_type=DataType(type='str'), default='abc', required=False ) ] assert custom_root_type.render() == ( 'class test_model(BaseModel):\n' ' __root__: Optional[str] = \'abc\'' )
def test_data_class_base_class(): field = DataModelFieldBase(name='a', data_type=DataType(type='str'), required=True) data_class = DataClass( fields=[field], base_classes=[Reference(name='Base', original_name='Base', path='Base')], reference=Reference(name='test_model', path='test_model'), ) assert data_class.name == 'test_model' assert data_class.fields == [field] assert data_class.decorators == [] assert ( data_class.render() == '@dataclass\n' 'class test_model(Base):\n' ' a: str' )
def test_sort_data_models_unresolved(): reference_a = Reference(path='A', original_name='A', name='A') reference_b = Reference(path='B', original_name='B', name='B') reference_c = Reference(path='C', original_name='C', name='C') reference_d = Reference(path='D', original_name='D', name='D') reference_v = Reference(path='V', original_name='V', name='V') reference_z = Reference(path='Z', original_name='Z', name='Z') data_type_a = DataType(reference=reference_a) data_type_b = DataType(reference=reference_b) data_type_c = DataType(reference=reference_c) data_type_v = DataType(reference=reference_v) data_type_z = DataType(reference=reference_z) reference = [ BaseModel( fields=[ DataModelField(data_type=data_type_a), DataModelFieldBase(data_type=data_type_c), ], reference=reference_a, ), BaseModel( fields=[DataModelField(data_type=data_type_b)], reference=reference_b, ), BaseModel( fields=[DataModelField(data_type=data_type_b)], reference=reference_c, ), BaseModel( fields=[ DataModelField(data_type=data_type_a), DataModelField(data_type=data_type_c), DataModelField(data_type=data_type_z), ], reference=reference_d, ), BaseModel( fields=[DataModelField(data_type=data_type_v)], reference=reference_z, ), ] with pytest.raises(Exception): sort_data_models(reference)