split tests into packages
This commit is contained in:
0
tests/test_ordering/__init__.py
Normal file
0
tests/test_ordering/__init__.py
Normal file
115
tests/test_ordering/test_default_model_order.py
Normal file
115
tests/test_ordering/test_default_model_order.py
Normal file
@ -0,0 +1,115 @@
|
||||
from typing import Optional
|
||||
|
||||
import databases
|
||||
import pytest
|
||||
import sqlalchemy
|
||||
|
||||
import ormar
|
||||
from tests.settings import DATABASE_URL
|
||||
|
||||
database = databases.Database(DATABASE_URL)
|
||||
metadata = sqlalchemy.MetaData()
|
||||
|
||||
|
||||
class BaseMeta(ormar.ModelMeta):
|
||||
metadata = metadata
|
||||
database = database
|
||||
|
||||
|
||||
class Author(ormar.Model):
|
||||
class Meta(BaseMeta):
|
||||
tablename = "authors"
|
||||
orders_by = ["-name"]
|
||||
|
||||
id: int = ormar.Integer(primary_key=True)
|
||||
name: str = ormar.String(max_length=100)
|
||||
|
||||
|
||||
class Book(ormar.Model):
|
||||
class Meta(BaseMeta):
|
||||
tablename = "books"
|
||||
orders_by = ["year", "-ranking"]
|
||||
|
||||
id: int = ormar.Integer(primary_key=True)
|
||||
author: Optional[Author] = ormar.ForeignKey(Author)
|
||||
title: str = ormar.String(max_length=100)
|
||||
year: int = ormar.Integer(nullable=True)
|
||||
ranking: int = ormar.Integer(nullable=True)
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True, scope="module")
|
||||
def create_test_database():
|
||||
engine = sqlalchemy.create_engine(DATABASE_URL)
|
||||
metadata.drop_all(engine)
|
||||
metadata.create_all(engine)
|
||||
yield
|
||||
metadata.drop_all(engine)
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True, scope="function")
|
||||
async def cleanup():
|
||||
yield
|
||||
async with database:
|
||||
await Book.objects.delete(each=True)
|
||||
await Author.objects.delete(each=True)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_default_orders_is_applied():
|
||||
async with database:
|
||||
tolkien = await Author(name="J.R.R. Tolkien").save()
|
||||
sapkowski = await Author(name="Andrzej Sapkowski").save()
|
||||
king = await Author(name="Stephen King").save()
|
||||
lewis = await Author(name="C.S Lewis").save()
|
||||
|
||||
authors = await Author.objects.all()
|
||||
assert authors[0] == king
|
||||
assert authors[1] == tolkien
|
||||
assert authors[2] == lewis
|
||||
assert authors[3] == sapkowski
|
||||
|
||||
authors = await Author.objects.order_by("name").all()
|
||||
assert authors[3] == king
|
||||
assert authors[2] == tolkien
|
||||
assert authors[1] == lewis
|
||||
assert authors[0] == sapkowski
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_default_orders_is_applied_on_related():
|
||||
async with database:
|
||||
tolkien = await Author(name="J.R.R. Tolkien").save()
|
||||
silmarillion = await Book(
|
||||
author=tolkien, title="The Silmarillion", year=1977
|
||||
).save()
|
||||
lotr = await Book(
|
||||
author=tolkien, title="The Lord of the Rings", year=1955
|
||||
).save()
|
||||
hobbit = await Book(author=tolkien, title="The Hobbit", year=1933).save()
|
||||
|
||||
await tolkien.books.all()
|
||||
assert tolkien.books[0] == hobbit
|
||||
assert tolkien.books[1] == lotr
|
||||
assert tolkien.books[2] == silmarillion
|
||||
|
||||
await tolkien.books.order_by("-title").all()
|
||||
assert tolkien.books[2] == hobbit
|
||||
assert tolkien.books[1] == lotr
|
||||
assert tolkien.books[0] == silmarillion
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_default_orders_is_applied_on_related_two_fields():
|
||||
async with database:
|
||||
sanders = await Author(name="Brandon Sanderson").save()
|
||||
twok = await Book(
|
||||
author=sanders, title="The Way of Kings", year=2010, ranking=10
|
||||
).save()
|
||||
bret = await Author(name="Peter V. Bret").save()
|
||||
tds = await Book(
|
||||
author=bret, title="The Desert Spear", year=2010, ranking=9
|
||||
).save()
|
||||
|
||||
books = await Book.objects.all()
|
||||
assert books[0] == twok
|
||||
assert books[1] == tds
|
||||
151
tests/test_ordering/test_default_relation_order.py
Normal file
151
tests/test_ordering/test_default_relation_order.py
Normal file
@ -0,0 +1,151 @@
|
||||
from typing import List, Optional
|
||||
from uuid import UUID, uuid4
|
||||
|
||||
import databases
|
||||
import pytest
|
||||
import sqlalchemy
|
||||
|
||||
import ormar
|
||||
from tests.settings import DATABASE_URL
|
||||
|
||||
database = databases.Database(DATABASE_URL)
|
||||
metadata = sqlalchemy.MetaData()
|
||||
|
||||
|
||||
class BaseMeta(ormar.ModelMeta):
|
||||
metadata = metadata
|
||||
database = database
|
||||
|
||||
|
||||
class Author(ormar.Model):
|
||||
class Meta(BaseMeta):
|
||||
tablename = "authors"
|
||||
|
||||
id: int = ormar.Integer(primary_key=True)
|
||||
name: str = ormar.String(max_length=100)
|
||||
|
||||
|
||||
class Book(ormar.Model):
|
||||
class Meta(BaseMeta):
|
||||
tablename = "books"
|
||||
|
||||
id: int = ormar.Integer(primary_key=True)
|
||||
author: Optional[Author] = ormar.ForeignKey(
|
||||
Author, orders_by=["name"], related_orders_by=["-year"]
|
||||
)
|
||||
title: str = ormar.String(max_length=100)
|
||||
year: int = ormar.Integer(nullable=True)
|
||||
ranking: int = ormar.Integer(nullable=True)
|
||||
|
||||
|
||||
class Animal(ormar.Model):
|
||||
class Meta(BaseMeta):
|
||||
tablename = "animals"
|
||||
|
||||
id: UUID = ormar.UUID(primary_key=True, default=uuid4)
|
||||
name: str = ormar.String(max_length=200)
|
||||
specie: str = ormar.String(max_length=200)
|
||||
|
||||
|
||||
class Human(ormar.Model):
|
||||
class Meta(BaseMeta):
|
||||
tablename = "humans"
|
||||
|
||||
id: UUID = ormar.UUID(primary_key=True, default=uuid4)
|
||||
name: str = ormar.Text(default="")
|
||||
pets: List[Animal] = ormar.ManyToMany(
|
||||
Animal,
|
||||
related_name="care_takers",
|
||||
orders_by=["specie", "-name"],
|
||||
related_orders_by=["name"],
|
||||
)
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True, scope="module")
|
||||
def create_test_database():
|
||||
engine = sqlalchemy.create_engine(DATABASE_URL)
|
||||
metadata.drop_all(engine)
|
||||
metadata.create_all(engine)
|
||||
yield
|
||||
metadata.drop_all(engine)
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True, scope="function")
|
||||
async def cleanup():
|
||||
yield
|
||||
async with database:
|
||||
await Book.objects.delete(each=True)
|
||||
await Author.objects.delete(each=True)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_default_orders_is_applied_from_reverse_relation():
|
||||
async with database:
|
||||
tolkien = await Author(name="J.R.R. Tolkien").save()
|
||||
hobbit = await Book(author=tolkien, title="The Hobbit", year=1933).save()
|
||||
silmarillion = await Book(
|
||||
author=tolkien, title="The Silmarillion", year=1977
|
||||
).save()
|
||||
lotr = await Book(
|
||||
author=tolkien, title="The Lord of the Rings", year=1955
|
||||
).save()
|
||||
|
||||
tolkien = await Author.objects.select_related("books").get()
|
||||
assert tolkien.books[2] == hobbit
|
||||
assert tolkien.books[1] == lotr
|
||||
assert tolkien.books[0] == silmarillion
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_default_orders_is_applied_from_relation():
|
||||
async with database:
|
||||
bret = await Author(name="Peter V. Bret").save()
|
||||
tds = await Book(
|
||||
author=bret, title="The Desert Spear", year=2010, ranking=9
|
||||
).save()
|
||||
sanders = await Author(name="Brandon Sanderson").save()
|
||||
twok = await Book(
|
||||
author=sanders, title="The Way of Kings", year=2010, ranking=10
|
||||
).save()
|
||||
|
||||
books = await Book.objects.order_by("year").select_related("author").all()
|
||||
assert books[0] == twok
|
||||
assert books[1] == tds
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_default_orders_is_applied_from_relation_on_m2m():
|
||||
async with database:
|
||||
alice = await Human(name="Alice").save()
|
||||
|
||||
spot = await Animal(name="Spot", specie="Cat").save()
|
||||
zkitty = await Animal(name="ZKitty", specie="Cat").save()
|
||||
noodle = await Animal(name="Noodle", specie="Anaconda").save()
|
||||
|
||||
await alice.pets.add(noodle)
|
||||
await alice.pets.add(spot)
|
||||
await alice.pets.add(zkitty)
|
||||
|
||||
await alice.load_all()
|
||||
assert alice.pets[0] == noodle
|
||||
assert alice.pets[1] == zkitty
|
||||
assert alice.pets[2] == spot
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_default_orders_is_applied_from_reverse_relation_on_m2m():
|
||||
async with database:
|
||||
|
||||
max = await Animal(name="Max", specie="Dog").save()
|
||||
joe = await Human(name="Joe").save()
|
||||
zack = await Human(name="Zack").save()
|
||||
julia = await Human(name="Julia").save()
|
||||
|
||||
await max.care_takers.add(joe)
|
||||
await max.care_takers.add(zack)
|
||||
await max.care_takers.add(julia)
|
||||
|
||||
await max.load_all()
|
||||
assert max.care_takers[0] == joe
|
||||
assert max.care_takers[1] == julia
|
||||
assert max.care_takers[2] == zack
|
||||
332
tests/test_ordering/test_default_through_relation_order.py
Normal file
332
tests/test_ordering/test_default_through_relation_order.py
Normal file
@ -0,0 +1,332 @@
|
||||
from typing import Any, Dict, List, Tuple, Type, cast
|
||||
from uuid import UUID, uuid4
|
||||
|
||||
import databases
|
||||
import pytest
|
||||
import sqlalchemy
|
||||
|
||||
import ormar
|
||||
from ormar import ModelDefinitionError, Model, QuerySet, pre_relation_remove, pre_update
|
||||
from ormar import pre_save
|
||||
from tests.settings import DATABASE_URL
|
||||
|
||||
database = databases.Database(DATABASE_URL)
|
||||
metadata = sqlalchemy.MetaData()
|
||||
|
||||
|
||||
class BaseMeta(ormar.ModelMeta):
|
||||
metadata = metadata
|
||||
database = database
|
||||
|
||||
|
||||
class Animal(ormar.Model):
|
||||
class Meta(BaseMeta):
|
||||
tablename = "animals"
|
||||
|
||||
id: UUID = ormar.UUID(primary_key=True, default=uuid4)
|
||||
name: str = ormar.Text(default="")
|
||||
# favoriteHumans
|
||||
|
||||
|
||||
class Link(ormar.Model):
|
||||
class Meta(BaseMeta):
|
||||
tablename = "link_table"
|
||||
|
||||
id: UUID = ormar.UUID(primary_key=True, default=uuid4)
|
||||
animal_order: int = ormar.Integer(nullable=True)
|
||||
human_order: int = ormar.Integer(nullable=True)
|
||||
|
||||
|
||||
class Human(ormar.Model):
|
||||
class Meta(BaseMeta):
|
||||
tablename = "humans"
|
||||
|
||||
id: UUID = ormar.UUID(primary_key=True, default=uuid4)
|
||||
name: str = ormar.Text(default="")
|
||||
favoriteAnimals: List[Animal] = ormar.ManyToMany(
|
||||
Animal,
|
||||
through=Link,
|
||||
related_name="favoriteHumans",
|
||||
orders_by=["link__animal_order"],
|
||||
related_orders_by=["link__human_order"],
|
||||
)
|
||||
|
||||
|
||||
class Human2(ormar.Model):
|
||||
class Meta(BaseMeta):
|
||||
tablename = "humans2"
|
||||
|
||||
id: UUID = ormar.UUID(primary_key=True, default=uuid4)
|
||||
name: str = ormar.Text(default="")
|
||||
favoriteAnimals: List[Animal] = ormar.ManyToMany(
|
||||
Animal, related_name="favoriteHumans2", orders_by=["link__animal_order__fail"]
|
||||
)
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True, scope="module")
|
||||
def create_test_database():
|
||||
engine = sqlalchemy.create_engine(DATABASE_URL)
|
||||
metadata.drop_all(engine)
|
||||
metadata.create_all(engine)
|
||||
yield
|
||||
metadata.drop_all(engine)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_ordering_by_through_fail():
|
||||
async with database:
|
||||
alice = await Human2(name="Alice").save()
|
||||
spot = await Animal(name="Spot").save()
|
||||
await alice.favoriteAnimals.add(spot)
|
||||
with pytest.raises(ModelDefinitionError):
|
||||
await alice.load_all()
|
||||
|
||||
|
||||
def _get_filtered_query(
|
||||
sender: Type[Model], instance: Model, to_class: Type[Model]
|
||||
) -> QuerySet:
|
||||
"""
|
||||
Helper function.
|
||||
Gets the query filtered by the appropriate class name.
|
||||
"""
|
||||
pk = getattr(instance, f"{to_class.get_name()}").pk
|
||||
filter_kwargs = {f"{to_class.get_name()}": pk}
|
||||
query = sender.objects.filter(**filter_kwargs)
|
||||
return query
|
||||
|
||||
|
||||
def _get_through_model_relations(
|
||||
sender: Type[Model], instance: Model
|
||||
) -> Tuple[Type[Model], Type[Model]]:
|
||||
relations = list(instance.extract_related_names())
|
||||
rel_one = sender.Meta.model_fields[relations[0]].to
|
||||
rel_two = sender.Meta.model_fields[relations[1]].to
|
||||
return rel_one, rel_two
|
||||
|
||||
|
||||
async def _populate_order_on_insert(
|
||||
sender: Type[Model], instance: Model, from_class: Type[Model], to_class: Type[Model]
|
||||
):
|
||||
"""
|
||||
Helper function.
|
||||
|
||||
Get max values from database for both orders and adds 1 (0 if max is None) if the
|
||||
order is not provided. If the order is provided it reorders the existing links
|
||||
to match the newly defined order.
|
||||
|
||||
Assumes names f"{model.get_name()}_order" like for Animal: animal_order.
|
||||
"""
|
||||
order_column = f"{from_class.get_name()}_order"
|
||||
if getattr(instance, order_column) is None:
|
||||
query = _get_filtered_query(sender, instance, to_class)
|
||||
max_order = await query.max(order_column)
|
||||
max_order = max_order + 1 if max_order is not None else 0
|
||||
setattr(instance, order_column, max_order)
|
||||
else:
|
||||
await _reorder_on_update(
|
||||
sender=sender,
|
||||
instance=instance,
|
||||
from_class=from_class,
|
||||
to_class=to_class,
|
||||
passed_args={order_column: getattr(instance, order_column)},
|
||||
)
|
||||
|
||||
|
||||
async def _reorder_on_update(
|
||||
sender: Type[Model],
|
||||
instance: Model,
|
||||
from_class: Type[Model],
|
||||
to_class: Type[Model],
|
||||
passed_args: Dict,
|
||||
):
|
||||
"""
|
||||
Helper function.
|
||||
Actually reorders links by given order passed in add/update query to the link
|
||||
model.
|
||||
|
||||
Assumes names f"{model.get_name()}_order" like for Animal: animal_order.
|
||||
"""
|
||||
order = f"{from_class.get_name()}_order"
|
||||
if order in passed_args:
|
||||
query = _get_filtered_query(sender, instance, to_class)
|
||||
to_reorder = await query.exclude(pk=instance.pk).order_by(order).all()
|
||||
new_order = passed_args.get(order)
|
||||
if to_reorder and new_order is not None:
|
||||
# can be more efficient - here we renumber all even if not needed.
|
||||
for ind, link in enumerate(to_reorder):
|
||||
if ind < new_order:
|
||||
setattr(link, order, ind)
|
||||
else:
|
||||
setattr(link, order, ind + 1)
|
||||
await sender.objects.bulk_update(
|
||||
cast(List[Model], to_reorder), columns=[order]
|
||||
)
|
||||
|
||||
|
||||
@pre_save(Link)
|
||||
async def order_link_on_insert(sender: Type[Model], instance: Model, **kwargs: Any):
|
||||
"""
|
||||
Signal receiver registered on Link model, triggered every time before one is created
|
||||
by calling save() on a model. Note that signal functions for pre_save signal accepts
|
||||
sender class, instance and have to accept **kwargs even if it's empty as of now.
|
||||
"""
|
||||
rel_one, rel_two = _get_through_model_relations(sender, instance)
|
||||
await _populate_order_on_insert(
|
||||
sender=sender, instance=instance, from_class=rel_one, to_class=rel_two
|
||||
)
|
||||
await _populate_order_on_insert(
|
||||
sender=sender, instance=instance, from_class=rel_two, to_class=rel_one
|
||||
)
|
||||
|
||||
|
||||
@pre_update(Link)
|
||||
async def reorder_links_on_update(
|
||||
sender: Type[ormar.Model], instance: ormar.Model, passed_args: Dict, **kwargs: Any
|
||||
):
|
||||
"""
|
||||
Signal receiver registered on Link model, triggered every time before one is updated
|
||||
by calling update() on a model. Note that signal functions for pre_update signal
|
||||
accepts sender class, instance, passed_args which is a dict of kwargs passed to
|
||||
update and have to accept **kwargs even if it's empty as of now.
|
||||
"""
|
||||
|
||||
rel_one, rel_two = _get_through_model_relations(sender, instance)
|
||||
await _reorder_on_update(
|
||||
sender=sender,
|
||||
instance=instance,
|
||||
from_class=rel_one,
|
||||
to_class=rel_two,
|
||||
passed_args=passed_args,
|
||||
)
|
||||
await _reorder_on_update(
|
||||
sender=sender,
|
||||
instance=instance,
|
||||
from_class=rel_two,
|
||||
to_class=rel_one,
|
||||
passed_args=passed_args,
|
||||
)
|
||||
|
||||
|
||||
@pre_relation_remove([Animal, Human])
|
||||
async def reorder_links_on_remove(
|
||||
sender: Type[ormar.Model],
|
||||
instance: ormar.Model,
|
||||
child: ormar.Model,
|
||||
relation_name: str,
|
||||
**kwargs: Any,
|
||||
):
|
||||
"""
|
||||
Signal receiver registered on Anima and Human models, triggered every time before
|
||||
relation on a model is removed. Note that signal functions for pre_relation_remove
|
||||
signal accepts sender class, instance, child, relation_name and have to accept
|
||||
**kwargs even if it's empty as of now.
|
||||
|
||||
Note that if classes have many relations you need to check if current one is ordered
|
||||
"""
|
||||
through_class = sender.Meta.model_fields[relation_name].through
|
||||
through_instance = getattr(instance, through_class.get_name())
|
||||
if not through_instance:
|
||||
parent_pk = instance.pk
|
||||
child_pk = child.pk
|
||||
filter_kwargs = {f"{sender.get_name()}": parent_pk, child.get_name(): child_pk}
|
||||
through_instance = await through_class.objects.get(**filter_kwargs)
|
||||
rel_one, rel_two = _get_through_model_relations(through_class, through_instance)
|
||||
await _reorder_on_update(
|
||||
sender=through_class,
|
||||
instance=through_instance,
|
||||
from_class=rel_one,
|
||||
to_class=rel_two,
|
||||
passed_args={f"{rel_one.get_name()}_order": 999999},
|
||||
)
|
||||
await _reorder_on_update(
|
||||
sender=through_class,
|
||||
instance=through_instance,
|
||||
from_class=rel_two,
|
||||
to_class=rel_one,
|
||||
passed_args={f"{rel_two.get_name()}_order": 999999},
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_ordering_by_through_on_m2m_field():
|
||||
async with database:
|
||||
|
||||
def verify_order(instance, expected):
|
||||
field_name = (
|
||||
"favoriteAnimals" if isinstance(instance, Human) else "favoriteHumans"
|
||||
)
|
||||
order_field_name = (
|
||||
"animal_order" if isinstance(instance, Human) else "human_order"
|
||||
)
|
||||
assert [x.name for x in getattr(instance, field_name)] == expected
|
||||
assert [
|
||||
getattr(x.link, order_field_name) for x in getattr(instance, field_name)
|
||||
] == [i for i in range(len(expected))]
|
||||
|
||||
alice = await Human(name="Alice").save()
|
||||
bob = await Human(name="Bob").save()
|
||||
charlie = await Human(name="Charlie").save()
|
||||
|
||||
spot = await Animal(name="Spot").save()
|
||||
kitty = await Animal(name="Kitty").save()
|
||||
noodle = await Animal(name="Noodle").save()
|
||||
|
||||
await alice.favoriteAnimals.add(noodle)
|
||||
await alice.favoriteAnimals.add(spot)
|
||||
await alice.favoriteAnimals.add(kitty)
|
||||
|
||||
await alice.load_all()
|
||||
verify_order(alice, ["Noodle", "Spot", "Kitty"])
|
||||
|
||||
await bob.favoriteAnimals.add(noodle)
|
||||
await bob.favoriteAnimals.add(kitty)
|
||||
await bob.favoriteAnimals.add(spot)
|
||||
|
||||
await bob.load_all()
|
||||
verify_order(bob, ["Noodle", "Kitty", "Spot"])
|
||||
|
||||
await charlie.favoriteAnimals.add(kitty)
|
||||
await charlie.favoriteAnimals.add(noodle)
|
||||
await charlie.favoriteAnimals.add(spot)
|
||||
|
||||
await charlie.load_all()
|
||||
verify_order(charlie, ["Kitty", "Noodle", "Spot"])
|
||||
|
||||
animals = [noodle, kitty, spot]
|
||||
for animal in animals:
|
||||
await animal.load_all()
|
||||
verify_order(animal, ["Alice", "Bob", "Charlie"])
|
||||
|
||||
zack = await Human(name="Zack").save()
|
||||
|
||||
await noodle.favoriteHumans.add(zack, human_order=0)
|
||||
await noodle.load_all()
|
||||
verify_order(noodle, ["Zack", "Alice", "Bob", "Charlie"])
|
||||
|
||||
await zack.load_all()
|
||||
verify_order(zack, ["Noodle"])
|
||||
|
||||
await noodle.favoriteHumans.filter(name="Zack").update(link=dict(human_order=1))
|
||||
await noodle.load_all()
|
||||
verify_order(noodle, ["Alice", "Zack", "Bob", "Charlie"])
|
||||
|
||||
await noodle.favoriteHumans.filter(name="Zack").update(link=dict(human_order=2))
|
||||
await noodle.load_all()
|
||||
verify_order(noodle, ["Alice", "Bob", "Zack", "Charlie"])
|
||||
|
||||
await noodle.favoriteHumans.filter(name="Zack").update(link=dict(human_order=3))
|
||||
await noodle.load_all()
|
||||
verify_order(noodle, ["Alice", "Bob", "Charlie", "Zack"])
|
||||
|
||||
await kitty.favoriteHumans.remove(bob)
|
||||
await kitty.load_all()
|
||||
assert [x.name for x in kitty.favoriteHumans] == ["Alice", "Charlie"]
|
||||
|
||||
bob = await noodle.favoriteHumans.get(pk=bob.pk)
|
||||
assert bob.link.human_order == 1
|
||||
|
||||
await noodle.favoriteHumans.remove(
|
||||
await noodle.favoriteHumans.filter(link__human_order=2).get()
|
||||
)
|
||||
await noodle.load_all()
|
||||
verify_order(noodle, ["Alice", "Bob", "Zack"])
|
||||
80
tests/test_ordering/test_proper_order_of_sorting_apply.py
Normal file
80
tests/test_ordering/test_proper_order_of_sorting_apply.py
Normal file
@ -0,0 +1,80 @@
|
||||
from typing import Optional
|
||||
|
||||
import databases
|
||||
import pytest
|
||||
import sqlalchemy
|
||||
|
||||
import ormar
|
||||
from tests.settings import DATABASE_URL
|
||||
|
||||
database = databases.Database(DATABASE_URL)
|
||||
metadata = sqlalchemy.MetaData()
|
||||
|
||||
|
||||
class BaseMeta(ormar.ModelMeta):
|
||||
metadata = metadata
|
||||
database = database
|
||||
|
||||
|
||||
class Author(ormar.Model):
|
||||
class Meta(BaseMeta):
|
||||
tablename = "authors"
|
||||
|
||||
id: int = ormar.Integer(primary_key=True)
|
||||
name: str = ormar.String(max_length=100)
|
||||
|
||||
|
||||
class Book(ormar.Model):
|
||||
class Meta(BaseMeta):
|
||||
tablename = "books"
|
||||
orders_by = ["-ranking"]
|
||||
|
||||
id: int = ormar.Integer(primary_key=True)
|
||||
author: Optional[Author] = ormar.ForeignKey(
|
||||
Author, orders_by=["name"], related_orders_by=["-year"]
|
||||
)
|
||||
title: str = ormar.String(max_length=100)
|
||||
year: int = ormar.Integer(nullable=True)
|
||||
ranking: int = ormar.Integer(nullable=True)
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True, scope="module")
|
||||
def create_test_database():
|
||||
engine = sqlalchemy.create_engine(DATABASE_URL)
|
||||
metadata.drop_all(engine)
|
||||
metadata.create_all(engine)
|
||||
yield
|
||||
metadata.drop_all(engine)
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True, scope="function")
|
||||
async def cleanup():
|
||||
yield
|
||||
async with database:
|
||||
await Book.objects.delete(each=True)
|
||||
await Author.objects.delete(each=True)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_default_orders_is_applied_from_reverse_relation():
|
||||
async with database:
|
||||
tolkien = await Author(name="J.R.R. Tolkien").save()
|
||||
hobbit = await Book(author=tolkien, title="The Hobbit", year=1933).save()
|
||||
silmarillion = await Book(
|
||||
author=tolkien, title="The Silmarillion", year=1977
|
||||
).save()
|
||||
lotr = await Book(
|
||||
author=tolkien, title="The Lord of the Rings", year=1955
|
||||
).save()
|
||||
|
||||
tolkien = await Author.objects.select_related("books").get()
|
||||
assert tolkien.books[2] == hobbit
|
||||
assert tolkien.books[1] == lotr
|
||||
assert tolkien.books[0] == silmarillion
|
||||
|
||||
tolkien = (
|
||||
await Author.objects.select_related("books").order_by("books__title").get()
|
||||
)
|
||||
assert tolkien.books[0] == hobbit
|
||||
assert tolkien.books[1] == lotr
|
||||
assert tolkien.books[2] == silmarillion
|
||||
Reference in New Issue
Block a user