refactor and move tests
This commit is contained in:
@ -274,7 +274,9 @@ def choices_validator(cls: Type["Model"], values: Dict[str, Any]) -> Dict[str, A
|
||||
return values
|
||||
|
||||
|
||||
def populate_choices_validators(model: Type["Model"], attrs: Dict) -> None: # noqa CCR001
|
||||
def populate_choices_validators( # noqa CCR001
|
||||
model: Type["Model"], attrs: Dict
|
||||
) -> None:
|
||||
if model_initialized_and_has_model_fields(model):
|
||||
for _, field in model.Meta.model_fields.items():
|
||||
if check_if_field_has_choices(field):
|
||||
|
||||
@ -53,7 +53,7 @@ class Organisation(ormar.Model):
|
||||
database = database
|
||||
|
||||
id: ormar.Integer(primary_key=True)
|
||||
ident: ormar.String(max_length=100, choices=['ACME Ltd', 'Other ltd'])
|
||||
ident: ormar.String(max_length=100, choices=["ACME Ltd", "Other ltd"])
|
||||
|
||||
|
||||
class Team(ormar.Model):
|
||||
@ -78,25 +78,6 @@ class Member(ormar.Model):
|
||||
email: ormar.String(max_length=100)
|
||||
|
||||
|
||||
country_name_choices = ("Canada", "Algeria", "United States")
|
||||
country_taxed_choices = (True,)
|
||||
country_country_code_choices = (-10, 1, 213, 1200)
|
||||
|
||||
|
||||
class Country(ormar.Model):
|
||||
class Meta:
|
||||
tablename = "country"
|
||||
metadata = metadata
|
||||
database = database
|
||||
|
||||
id: ormar.Integer(primary_key=True)
|
||||
name: ormar.String(max_length=9, choices=country_name_choices, default="Canada", )
|
||||
taxed: ormar.Boolean(choices=country_taxed_choices, default=True)
|
||||
country_code: ormar.Integer(
|
||||
minimum=0, maximum=1000, choices=country_country_code_choices, default=1
|
||||
)
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True, scope="module")
|
||||
def create_test_database():
|
||||
engine = sqlalchemy.create_engine(DATABASE_URL)
|
||||
@ -129,108 +110,6 @@ async def test_related_name():
|
||||
assert len(album.cover_pictures) == 1
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_model_choices():
|
||||
"""Test that choices work properly for various types of fields."""
|
||||
async with database:
|
||||
# Test valid choices.
|
||||
await asyncio.gather(
|
||||
Country.objects.create(name="Canada", taxed=True, country_code=1),
|
||||
Country.objects.create(name="Algeria", taxed=True, country_code=213),
|
||||
Country.objects.create(name="Algeria"),
|
||||
)
|
||||
|
||||
with pytest.raises(ValueError):
|
||||
name, taxed, country_code = "Saudi Arabia", True, 1
|
||||
assert all(
|
||||
(
|
||||
name not in country_name_choices,
|
||||
taxed in country_taxed_choices,
|
||||
country_code in country_country_code_choices,
|
||||
)
|
||||
)
|
||||
await Country.objects.create(
|
||||
name=name, taxed=taxed, country_code=country_code
|
||||
)
|
||||
|
||||
with pytest.raises(ValueError):
|
||||
name, taxed, country_code = "Algeria", False, 1
|
||||
assert all(
|
||||
(
|
||||
name in country_name_choices,
|
||||
taxed not in country_taxed_choices,
|
||||
country_code in country_country_code_choices,
|
||||
)
|
||||
)
|
||||
await Country.objects.create(
|
||||
name=name, taxed=taxed, country_code=country_code
|
||||
)
|
||||
|
||||
with pytest.raises(ValueError):
|
||||
name, taxed, country_code = "Algeria", True, 967
|
||||
assert all(
|
||||
(
|
||||
name in country_name_choices,
|
||||
taxed in country_taxed_choices,
|
||||
country_code not in country_country_code_choices,
|
||||
)
|
||||
)
|
||||
await Country.objects.create(
|
||||
name=name, taxed=taxed, country_code=country_code
|
||||
)
|
||||
|
||||
with pytest.raises(ValueError):
|
||||
name, taxed, country_code = (
|
||||
"United States",
|
||||
True,
|
||||
1,
|
||||
) # name is too long but is a valid choice
|
||||
assert all(
|
||||
(
|
||||
name in country_name_choices,
|
||||
taxed in country_taxed_choices,
|
||||
country_code in country_country_code_choices,
|
||||
)
|
||||
)
|
||||
await Country.objects.create(
|
||||
name=name, taxed=taxed, country_code=country_code
|
||||
)
|
||||
|
||||
with pytest.raises(ValueError):
|
||||
name, taxed, country_code = (
|
||||
"Algeria",
|
||||
True,
|
||||
-10,
|
||||
) # country code is too small but is a valid choice
|
||||
assert all(
|
||||
(
|
||||
name in country_name_choices,
|
||||
taxed in country_taxed_choices,
|
||||
country_code in country_country_code_choices,
|
||||
)
|
||||
)
|
||||
await Country.objects.create(
|
||||
name=name, taxed=taxed, country_code=country_code
|
||||
)
|
||||
|
||||
with pytest.raises(ValueError):
|
||||
name, taxed, country_code = (
|
||||
"Algeria",
|
||||
True,
|
||||
1200,
|
||||
) # country code is too large but is a valid choice
|
||||
assert all(
|
||||
(
|
||||
name in country_name_choices,
|
||||
taxed in country_taxed_choices,
|
||||
country_code in country_country_code_choices,
|
||||
)
|
||||
)
|
||||
await Country.objects.create(
|
||||
name=name, taxed=taxed, country_code=country_code
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_model_crud():
|
||||
async with database:
|
||||
@ -347,14 +226,18 @@ async def test_fk_filter():
|
||||
await Track.objects.create(album=malibu, title="The Waters", position=3)
|
||||
|
||||
fantasies = await Album.objects.create(name="Fantasies")
|
||||
await Track.objects.create(album=fantasies, title="Help I'm Alive", position=1)
|
||||
await Track.objects.create(
|
||||
album=fantasies, title="Help I'm Alive", position=1
|
||||
)
|
||||
await Track.objects.create(album=fantasies, title="Sick Muse", position=2)
|
||||
await Track.objects.create(album=fantasies, title="Satellite Mind", position=3)
|
||||
await Track.objects.create(
|
||||
album=fantasies, title="Satellite Mind", position=3
|
||||
)
|
||||
|
||||
tracks = (
|
||||
await Track.objects.select_related("album")
|
||||
.filter(album__name="Fantasies")
|
||||
.all()
|
||||
.filter(album__name="Fantasies")
|
||||
.all()
|
||||
)
|
||||
assert len(tracks) == 3
|
||||
for track in tracks:
|
||||
@ -362,8 +245,8 @@ async def test_fk_filter():
|
||||
|
||||
tracks = (
|
||||
await Track.objects.select_related("album")
|
||||
.filter(album__name__icontains="fan")
|
||||
.all()
|
||||
.filter(album__name__icontains="fan")
|
||||
.all()
|
||||
)
|
||||
assert len(tracks) == 3
|
||||
for track in tracks:
|
||||
@ -377,7 +260,9 @@ async def test_fk_filter():
|
||||
tracks = await Track.objects.filter(album__name__contains="Malibu%").all()
|
||||
assert len(tracks) == 3
|
||||
|
||||
tracks = await Track.objects.filter(album=malibu).select_related("album").all()
|
||||
tracks = (
|
||||
await Track.objects.filter(album=malibu).select_related("album").all()
|
||||
)
|
||||
assert len(tracks) == 3
|
||||
for track in tracks:
|
||||
assert track.album.name == "Malibu%"
|
||||
@ -406,8 +291,8 @@ async def test_multiple_fk():
|
||||
|
||||
members = (
|
||||
await Member.objects.select_related("team__org")
|
||||
.filter(team__org__ident="ACME Ltd")
|
||||
.all()
|
||||
.filter(team__org__ident="ACME Ltd")
|
||||
.all()
|
||||
)
|
||||
assert len(members) == 4
|
||||
for member in members:
|
||||
@ -427,16 +312,20 @@ async def test_pk_filter():
|
||||
async with database:
|
||||
async with database.transaction(force_rollback=True):
|
||||
fantasies = await Album.objects.create(name="Test")
|
||||
track = await Track.objects.create(album=fantasies, title="Test1", position=1)
|
||||
track = await Track.objects.create(
|
||||
album=fantasies, title="Test1", position=1
|
||||
)
|
||||
await Track.objects.create(album=fantasies, title="Test2", position=2)
|
||||
await Track.objects.create(album=fantasies, title="Test3", position=3)
|
||||
tracks = await Track.objects.select_related("album").filter(pk=track.pk).all()
|
||||
tracks = (
|
||||
await Track.objects.select_related("album").filter(pk=track.pk).all()
|
||||
)
|
||||
assert len(tracks) == 1
|
||||
|
||||
tracks = (
|
||||
await Track.objects.select_related("album")
|
||||
.filter(position=2, album__name="Test")
|
||||
.all()
|
||||
.filter(position=2, album__name="Test")
|
||||
.all()
|
||||
)
|
||||
assert len(tracks) == 1
|
||||
|
||||
@ -446,7 +335,9 @@ async def test_limit_and_offset():
|
||||
async with database:
|
||||
async with database.transaction(force_rollback=True):
|
||||
fantasies = await Album.objects.create(name="Limitless")
|
||||
await Track.objects.create(id=None, album=fantasies, title="Sample", position=1)
|
||||
await Track.objects.create(
|
||||
id=None, album=fantasies, title="Sample", position=1
|
||||
)
|
||||
await Track.objects.create(album=fantasies, title="Sample2", position=2)
|
||||
await Track.objects.create(album=fantasies, title="Sample3", position=3)
|
||||
|
||||
|
||||
@ -126,7 +126,9 @@ async def test_quering_of_the_m2m_models(cleanup):
|
||||
category = await Category.objects.filter(posts__author=guido).get()
|
||||
assert category == news
|
||||
# or:
|
||||
category2 = await Category.objects.filter(posts__author__first_name="Guido").get()
|
||||
category2 = await Category.objects.filter(
|
||||
posts__author__first_name="Guido"
|
||||
).get()
|
||||
assert category2 == news
|
||||
|
||||
|
||||
|
||||
@ -56,7 +56,11 @@ class ExampleModel2(Model):
|
||||
@pytest.fixture()
|
||||
def example():
|
||||
return ExampleModel(
|
||||
pk=1, test_string="test", test_bool=True, test_decimal=decimal.Decimal(3.5), test_decimal2=decimal.Decimal(5.5)
|
||||
pk=1,
|
||||
test_string="test",
|
||||
test_bool=True,
|
||||
test_decimal=decimal.Decimal(3.5),
|
||||
test_decimal2=decimal.Decimal(5.5),
|
||||
)
|
||||
|
||||
|
||||
@ -115,6 +119,7 @@ def test_sqlalchemy_table_is_created(example):
|
||||
|
||||
def test_no_pk_in_model_definition():
|
||||
with pytest.raises(ModelDefinitionError):
|
||||
|
||||
class ExampleModel2(Model):
|
||||
class Meta:
|
||||
tablename = "example3"
|
||||
@ -125,6 +130,7 @@ def test_no_pk_in_model_definition():
|
||||
|
||||
def test_two_pks_in_model_definition():
|
||||
with pytest.raises(ModelDefinitionError):
|
||||
|
||||
class ExampleModel2(Model):
|
||||
class Meta:
|
||||
tablename = "example3"
|
||||
@ -136,6 +142,7 @@ def test_two_pks_in_model_definition():
|
||||
|
||||
def test_setting_pk_column_as_pydantic_only_in_model_definition():
|
||||
with pytest.raises(ModelDefinitionError):
|
||||
|
||||
class ExampleModel2(Model):
|
||||
class Meta:
|
||||
tablename = "example4"
|
||||
@ -146,6 +153,7 @@ def test_setting_pk_column_as_pydantic_only_in_model_definition():
|
||||
|
||||
def test_decimal_error_in_model_definition():
|
||||
with pytest.raises(ModelDefinitionError):
|
||||
|
||||
class ExampleModel2(Model):
|
||||
class Meta:
|
||||
tablename = "example5"
|
||||
@ -156,6 +164,7 @@ def test_decimal_error_in_model_definition():
|
||||
|
||||
def test_string_error_in_model_definition():
|
||||
with pytest.raises(ModelDefinitionError):
|
||||
|
||||
class ExampleModel2(Model):
|
||||
class Meta:
|
||||
tablename = "example6"
|
||||
|
||||
@ -1,5 +1,6 @@
|
||||
import asyncio
|
||||
from datetime import datetime
|
||||
from typing import List
|
||||
|
||||
import databases
|
||||
import pydantic
|
||||
@ -47,6 +48,27 @@ class Product(ormar.Model):
|
||||
last_delivery: ormar.Date(default=datetime.now)
|
||||
|
||||
|
||||
country_name_choices = ("Canada", "Algeria", "United States")
|
||||
country_taxed_choices = (True,)
|
||||
country_country_code_choices = (-10, 1, 213, 1200)
|
||||
|
||||
|
||||
class Country(ormar.Model):
|
||||
class Meta:
|
||||
tablename = "country"
|
||||
metadata = metadata
|
||||
database = database
|
||||
|
||||
id: ormar.Integer(primary_key=True)
|
||||
name: ormar.String(
|
||||
max_length=9, choices=country_name_choices, default="Canada",
|
||||
)
|
||||
taxed: ormar.Boolean(choices=country_taxed_choices, default=True)
|
||||
country_code: ormar.Integer(
|
||||
minimum=0, maximum=1000, choices=country_country_code_choices, default=1
|
||||
)
|
||||
|
||||
|
||||
@pytest.fixture(scope="module")
|
||||
def event_loop():
|
||||
loop = asyncio.get_event_loop()
|
||||
@ -242,7 +264,9 @@ async def test_model_limit_with_filter():
|
||||
await User.objects.create(name="Tom")
|
||||
await User.objects.create(name="Tom")
|
||||
|
||||
assert len(await User.objects.limit(2).filter(name__iexact="Tom").all()) == 2
|
||||
assert (
|
||||
len(await User.objects.limit(2).filter(name__iexact="Tom").all()) == 2
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@ -268,3 +292,85 @@ async def test_model_first():
|
||||
assert await User.objects.filter(name="Jane").first() == jane
|
||||
with pytest.raises(NoMatch):
|
||||
await User.objects.filter(name="Lucy").first()
|
||||
|
||||
|
||||
def not_contains(a, b):
|
||||
return a not in b
|
||||
|
||||
|
||||
def contains(a, b):
|
||||
return a in b
|
||||
|
||||
|
||||
def check_choices(values: tuple, ops: List):
|
||||
ops_dict = {"in": contains, "out": not_contains}
|
||||
checks = (country_name_choices, country_taxed_choices, country_country_code_choices)
|
||||
assert all(
|
||||
[ops_dict[op](value, check) for value, op, check in zip(values, ops, checks)]
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_model_choices():
|
||||
"""Test that choices work properly for various types of fields."""
|
||||
async with database:
|
||||
# Test valid choices.
|
||||
await asyncio.gather(
|
||||
Country.objects.create(name="Canada", taxed=True, country_code=1),
|
||||
Country.objects.create(name="Algeria", taxed=True, country_code=213),
|
||||
Country.objects.create(name="Algeria"),
|
||||
)
|
||||
|
||||
with pytest.raises(ValueError):
|
||||
name, taxed, country_code = "Saudi Arabia", True, 1
|
||||
check_choices((name, taxed, country_code), ["out", "in", "in"])
|
||||
await Country.objects.create(
|
||||
name=name, taxed=taxed, country_code=country_code
|
||||
)
|
||||
|
||||
with pytest.raises(ValueError):
|
||||
name, taxed, country_code = "Algeria", False, 1
|
||||
check_choices((name, taxed, country_code), ["in", "out", "in"])
|
||||
await Country.objects.create(
|
||||
name=name, taxed=taxed, country_code=country_code
|
||||
)
|
||||
|
||||
with pytest.raises(ValueError):
|
||||
name, taxed, country_code = "Algeria", True, 967
|
||||
check_choices((name, taxed, country_code), ["in", "in", "out"])
|
||||
await Country.objects.create(
|
||||
name=name, taxed=taxed, country_code=country_code
|
||||
)
|
||||
|
||||
with pytest.raises(ValueError):
|
||||
name, taxed, country_code = (
|
||||
"United States",
|
||||
True,
|
||||
1,
|
||||
) # name is too long but is a valid choice
|
||||
check_choices((name, taxed, country_code), ["in", "in", "in"])
|
||||
await Country.objects.create(
|
||||
name=name, taxed=taxed, country_code=country_code
|
||||
)
|
||||
|
||||
with pytest.raises(ValueError):
|
||||
name, taxed, country_code = (
|
||||
"Algeria",
|
||||
True,
|
||||
-10,
|
||||
) # country code is too small but is a valid choice
|
||||
check_choices((name, taxed, country_code), ["in", "in", "in"])
|
||||
await Country.objects.create(
|
||||
name=name, taxed=taxed, country_code=country_code
|
||||
)
|
||||
|
||||
with pytest.raises(ValueError):
|
||||
name, taxed, country_code = (
|
||||
"Algeria",
|
||||
True,
|
||||
1200,
|
||||
) # country code is too large but is a valid choice
|
||||
check_choices((name, taxed, country_code), ["in", "in", "in"])
|
||||
await Country.objects.create(
|
||||
name=name, taxed=taxed, country_code=country_code
|
||||
)
|
||||
|
||||
@ -94,6 +94,7 @@ async def create_data():
|
||||
await Student.objects.create(name="Jack", category=category2, schoolclass=class2)
|
||||
await Teacher.objects.create(name="Joe", category=category2, schoolclass=class1)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_model_multiple_instances_of_same_table_in_schema():
|
||||
async with database:
|
||||
|
||||
@ -44,4 +44,4 @@ async def test_pk_1():
|
||||
async def test_pk_2():
|
||||
async with database:
|
||||
model = await Model.objects.create(name="NAME")
|
||||
assert await Model.objects.all() == [model]
|
||||
assert await Model.objects.all() == [model]
|
||||
|
||||
@ -112,10 +112,14 @@ async def test_model_multiple_instances_of_same_table_in_schema():
|
||||
assert classes[0].students[0].schoolclass.name == "Math"
|
||||
assert classes[0].students[0].schoolclass.department.name is None
|
||||
await classes[0].students[0].schoolclass.department.load()
|
||||
assert classes[0].students[0].schoolclass.department.name == "Math Department"
|
||||
assert (
|
||||
classes[0].students[0].schoolclass.department.name == "Math Department"
|
||||
)
|
||||
|
||||
await classes[1].students[0].schoolclass.department.load()
|
||||
assert classes[1].students[0].schoolclass.department.name == "Law Department"
|
||||
assert (
|
||||
classes[1].students[0].schoolclass.department.name == "Law Department"
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
|
||||
Reference in New Issue
Block a user