black, add additional tests with combined fields and exclude_fields, add aliases for column names to tests with fields and exclude_fields
This commit is contained in:
@ -34,7 +34,7 @@ class Car(ormar.Model):
|
||||
name: str = ormar.String(max_length=100)
|
||||
year: int = ormar.Integer(nullable=True)
|
||||
gearbox_type: str = ormar.String(max_length=20, nullable=True)
|
||||
gears: int = ormar.Integer(nullable=True)
|
||||
gears: int = ormar.Integer(nullable=True, name="gears_number")
|
||||
aircon_type: str = ormar.String(max_length=20, nullable=True)
|
||||
|
||||
|
||||
@ -79,7 +79,9 @@ async def test_selecting_subset():
|
||||
|
||||
all_cars = (
|
||||
await Car.objects.select_related("manufacturer")
|
||||
.exclude_fields(["gearbox_type", "gears", "aircon_type", "year", "company__founded"])
|
||||
.exclude_fields(
|
||||
["gearbox_type", "gears", "aircon_type", "year", "company__founded"]
|
||||
)
|
||||
.all()
|
||||
)
|
||||
for car in all_cars:
|
||||
@ -114,6 +116,20 @@ async def test_selecting_subset():
|
||||
assert car.manufacturer.name == "Toyota"
|
||||
assert car.manufacturer.founded == 1937
|
||||
|
||||
all_cars_check2 = (
|
||||
await Car.objects.select_related("manufacturer")
|
||||
.fields(["id", "name", "manufacturer"])
|
||||
.exclude_fields("company__founded")
|
||||
.all()
|
||||
)
|
||||
for car in all_cars_check2:
|
||||
assert all(
|
||||
getattr(car, x) is None
|
||||
for x in ["year", "gearbox_type", "gears", "aircon_type"]
|
||||
)
|
||||
assert car.manufacturer.name == "Toyota"
|
||||
assert car.manufacturer.founded is None
|
||||
|
||||
with pytest.raises(pydantic.error_wrappers.ValidationError):
|
||||
# cannot exclude mandatory model columns - company__name in this example
|
||||
await Car.objects.select_related("manufacturer").exclude_fields(
|
||||
|
||||
@ -32,6 +32,27 @@ class Owner(ormar.Model):
|
||||
name: str = ormar.String(max_length=100)
|
||||
|
||||
|
||||
class AliasNested(ormar.Model):
|
||||
class Meta:
|
||||
tablename = "aliases_nested"
|
||||
metadata = metadata
|
||||
database = database
|
||||
|
||||
id: int = ormar.Integer(name="alias_id", primary_key=True)
|
||||
name: str = ormar.String(name="alias_name", max_length=100)
|
||||
|
||||
|
||||
class AliasTest(ormar.Model):
|
||||
class Meta:
|
||||
tablename = "aliases"
|
||||
metadata = metadata
|
||||
database = database
|
||||
|
||||
id: int = ormar.Integer(name="alias_id", primary_key=True)
|
||||
name: str = ormar.String(name="alias_name", max_length=100)
|
||||
nested: str = ormar.ForeignKey(AliasNested, name="nested_alias")
|
||||
|
||||
|
||||
class Toy(ormar.Model):
|
||||
class Meta:
|
||||
tablename = "toys"
|
||||
@ -275,3 +296,34 @@ async def test_sort_order_on_many_to_many():
|
||||
assert users[1].cars[1].name == "Buggy"
|
||||
assert users[1].cars[2].name == "Ferrari"
|
||||
assert users[1].cars[3].name == "Skoda"
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_sort_order_with_aliases():
|
||||
async with database:
|
||||
al1 = await AliasTest.objects.create(name="Test4")
|
||||
al2 = await AliasTest.objects.create(name="Test2")
|
||||
await AliasTest.objects.create(name="Test1")
|
||||
await AliasTest.objects.create(name="Test3")
|
||||
|
||||
aliases = await AliasTest.objects.order_by("-name").all()
|
||||
assert [alias.name[-1] for alias in aliases] == ["4", "3", "2", "1"]
|
||||
|
||||
nest1 = await AliasNested.objects.create(name="Try1")
|
||||
nest2 = await AliasNested.objects.create(name="Try2")
|
||||
|
||||
al1.nested = nest1
|
||||
await al1.update()
|
||||
|
||||
al2.nested = nest2
|
||||
await al2.update()
|
||||
|
||||
aliases = (
|
||||
await AliasTest.objects.select_related("nested")
|
||||
.order_by("-nested__name")
|
||||
.all()
|
||||
)
|
||||
assert aliases[0].nested.name == "Try2"
|
||||
assert aliases[1].nested.name == "Try1"
|
||||
assert aliases[2].nested is None
|
||||
assert aliases[3].nested is None
|
||||
|
||||
@ -50,17 +50,19 @@ async def test_sort_order_on_main_model():
|
||||
|
||||
songs = await Song.objects.all()
|
||||
song_dict = [song.dict() for song in songs]
|
||||
assert all('sorted_name' in x for x in song_dict)
|
||||
assert all(x['sorted_name'] == f"{x['sort_order']}: {x['name']}" for x in song_dict)
|
||||
assert all("sorted_name" in x for x in song_dict)
|
||||
assert all(
|
||||
x["sorted_name"] == f"{x['sort_order']}: {x['name']}" for x in song_dict
|
||||
)
|
||||
song_json = [song.json() for song in songs]
|
||||
assert all('sorted_name' in x for x in song_json)
|
||||
assert all("sorted_name" in x for x in song_json)
|
||||
|
||||
check_include = songs[0].dict(include={"sample"})
|
||||
assert 'sample' in check_include
|
||||
assert 'sample2' not in check_include
|
||||
assert 'sorted_name' not in check_include
|
||||
assert "sample" in check_include
|
||||
assert "sample2" not in check_include
|
||||
assert "sorted_name" not in check_include
|
||||
|
||||
check_include = songs[0].dict(exclude={"sample"})
|
||||
assert 'sample' not in check_include
|
||||
assert 'sample2' in check_include
|
||||
assert 'sorted_name' in check_include
|
||||
assert "sample" not in check_include
|
||||
assert "sample2" in check_include
|
||||
assert "sorted_name" in check_include
|
||||
|
||||
@ -19,7 +19,7 @@ class Company(ormar.Model):
|
||||
database = database
|
||||
|
||||
id: int = ormar.Integer(primary_key=True)
|
||||
name: str = ormar.String(max_length=100, nullable=False)
|
||||
name: str = ormar.String(max_length=100, nullable=False, name="company_name")
|
||||
founded: int = ormar.Integer(nullable=True)
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user