Exemple #1
0
def test_base_model():
    field = DataModelField(name='a', data_types=[DataType(type='str')], required=True)

    base_model = BaseModel(name='test_model', fields=[field])

    assert base_model.name == 'test_model'
    assert base_model.fields == [field]
    assert base_model.decorators == []
    assert base_model.render() == 'class test_model(BaseModel):\n' '    a: str'
Exemple #2
0
def test_base_model_optional():
    field = DataModelField(name='a',
                           data_types=[DataType(type='str')],
                           default="'abc'",
                           required=False)

    base_model = BaseModel(name='test_model', fields=[field])

    assert base_model.name == 'test_model'
    assert base_model.fields == [field]
    assert base_model.decorators == []
    assert (base_model.render() == 'class test_model(BaseModel):\n'
            '    a: Optional[str] = \'abc\'')
Exemple #3
0
def test_base_model_optional():
    field = DataModelField(name='a',
                           data_type=DataType(type='str'),
                           default='abc',
                           required=False)

    base_model = BaseModel(
        fields=[field],
        reference=Reference(name='test_model', path='test_model'),
    )

    assert base_model.name == 'test_model'
    assert base_model.fields == [field]
    assert base_model.decorators == []
    assert (base_model.render() == 'class test_model(BaseModel):\n'
            '    a: Optional[str] = \'abc\'')
def test_base_model_strict_non_nullable_required():
    field = DataModelField(
        name='a',
        data_type=DataType(type='str'),
        default='abc',
        required=True,
        nullable=False,
    )

    base_model = BaseModel(
        fields=[field], reference=Reference(name='test_model', path='test_model'),
    )

    assert base_model.name == 'test_model'
    assert base_model.fields == [field]
    assert base_model.decorators == []
    assert base_model.render() == 'class test_model(BaseModel):\n' '    a: str'
def test_base_model_decorator():
    field = DataModelField(name='a',
                           data_type=DataType(type='str'),
                           default='abc',
                           required=False)

    base_model = BaseModel(
        name='test_model',
        fields=[field],
        decorators=['@validate'],
        base_classes=['Base'],
    )

    assert base_model.name == 'test_model'
    assert base_model.fields == [field]
    assert base_model.base_class == 'Base'
    assert base_model.decorators == ['@validate']
    assert (base_model.render() == '@validate\n'
            'class test_model(Base):\n'
            '    a: Optional[str] = \'abc\'')
Exemple #6
0
def test_base_model_reserved_name():
    field = DataModelField(name='except',
                           data_type=DataType(type='str'),
                           required=True)

    base_model = BaseModel(
        fields=[field],
        reference=Reference(name='test_model', path='test_model'),
    )

    assert base_model.name == 'test_model'
    assert base_model.fields == [field]
    assert base_model.decorators == []
    assert (base_model.render() == """class test_model(BaseModel):
    except_: str = Field(..., alias='except')""")

    field = DataModelField(name='def',
                           data_type=DataType(type='str'),
                           required=True,
                           alias='def-field')

    base_model = BaseModel(
        fields=[field],
        reference=Reference(name='test_model', path='test_model'),
    )

    assert base_model.name == 'test_model'
    assert base_model.fields == [field]
    assert base_model.decorators == []
    assert (base_model.render() == """class test_model(BaseModel):
    def_: str = Field(..., alias='def-field')""")
Exemple #7
0
def test_base_model_get_data_type():
    assert BaseModel.get_data_type(Types.integer) == DataType(type='int')