fix some complexity issues

This commit is contained in:
collerek
2020-08-09 10:58:36 +02:00
parent 22c4a0619c
commit fb5d03d64c
10 changed files with 244 additions and 134 deletions

View File

@ -1,5 +1,4 @@
import datetime
from typing import ClassVar
import pydantic
import pytest
@ -17,7 +16,7 @@ class ExampleModel(Model):
__metadata__ = metadata
test = fields.Integer(primary_key=True)
test_string = fields.String(length=250)
test_text = fields.Text(default='')
test_text = fields.Text(default="")
test_bool = fields.Boolean(nullable=False)
test_float = fields.Float()
test_datetime = fields.DateTime(default=datetime.datetime.now)
@ -28,33 +27,42 @@ class ExampleModel(Model):
test_decimal = fields.Decimal(length=10, precision=2)
fields_to_check = ['test', 'test_text', 'test_string', 'test_datetime', 'test_date', 'test_text', 'test_float',
'test_bigint', 'test_json']
fields_to_check = [
"test",
"test_text",
"test_string",
"test_datetime",
"test_date",
"test_text",
"test_float",
"test_bigint",
"test_json",
]
class ExampleModel2(Model):
__tablename__ = "example2"
__metadata__ = metadata
test = fields.Integer(name='test12', primary_key=True)
test_string = fields.String('test_string2', length=250)
test = fields.Integer(name="test12", primary_key=True)
test_string = fields.String("test_string2", length=250)
@pytest.fixture()
def example():
return ExampleModel(pk=1, test_string='test', test_bool=True)
return ExampleModel(pk=1, test_string="test", test_bool=True)
def test_not_nullable_field_is_required():
with pytest.raises(pydantic.error_wrappers.ValidationError):
ExampleModel(test=1, test_string='test')
ExampleModel(test=1, test_string="test")
def test_model_attribute_access(example):
assert example.test == 1
assert example.test_string == 'test'
assert example.test_string == "test"
assert example.test_datetime.year == datetime.datetime.now().year
assert example.test_date == datetime.date.today()
assert example.test_text == ''
assert example.test_text == ""
assert example.test_float is None
assert example.test_bigint == 0
assert example.test_json == {}
@ -63,7 +71,7 @@ def test_model_attribute_access(example):
assert example.test == 12
example.new_attr = 12
assert 'new_attr' in example.__dict__
assert "new_attr" in example.__dict__
def test_primary_key_access_and_setting(example):
@ -87,44 +95,54 @@ def test_sqlalchemy_table_is_created(example):
def test_double_column_name_in_model_definition():
with pytest.raises(ModelDefinitionError):
class ExampleModel2(Model):
__tablename__ = "example3"
__metadata__ = metadata
test_string = fields.String('test_string2', name='test_string2', length=250)
test_string = fields.String("test_string2", name="test_string2", length=250)
def test_no_pk_in_model_definition():
with pytest.raises(ModelDefinitionError):
class ExampleModel2(Model):
__tablename__ = "example3"
__metadata__ = metadata
test_string = fields.String(name='test_string2', length=250)
test_string = fields.String(name="test_string2", length=250)
def test_setting_pk_column_as_pydantic_only_in_model_definition():
with pytest.raises(ModelDefinitionError):
class ExampleModel2(Model):
__tablename__ = "example4"
__metadata__ = metadata
test = fields.Integer(name='test12', primary_key=True, pydantic_only=True)
test = fields.Integer(name="test12", primary_key=True, pydantic_only=True)
def test_decimal_error_in_model_definition():
with pytest.raises(ModelDefinitionError):
class ExampleModel2(Model):
__tablename__ = "example4"
__metadata__ = metadata
test = fields.Decimal(name='test12', primary_key=True)
test = fields.Decimal(name="test12", primary_key=True)
def test_string_error_in_model_definition():
with pytest.raises(ModelDefinitionError):
class ExampleModel2(Model):
__tablename__ = "example4"
__metadata__ = metadata
test = fields.String(name='test12', primary_key=True)
test = fields.String(name="test12", primary_key=True)
def test_json_conversion_in_model():
with pytest.raises(pydantic.ValidationError):
ExampleModel(test_json=datetime.datetime.now(), test=1, test_string='test', test_bool=True)
ExampleModel(
test_json=datetime.datetime.now(),
test=1,
test_string="test",
test_bool=True,
)