add possibility to exclude/include fields (refactor to excludableitems), fix for through model only on related side of the relation, fix for exclude of through model related models

This commit is contained in:
collerek
2021-03-01 19:26:33 +01:00
parent 0c781c4d52
commit a99000d2c0
15 changed files with 313 additions and 430 deletions

View File

@ -22,7 +22,7 @@ uuid1 = uuid.uuid4()
uuid2 = uuid.uuid4()
class TestEnum(Enum):
class EnumTest(Enum):
val1 = "Val1"
val2 = "Val2"
@ -56,7 +56,7 @@ class Organisation(ormar.Model):
)
random_json: pydantic.Json = ormar.JSON(choices=["aa", '{"aa":"bb"}'])
random_uuid: uuid.UUID = ormar.UUID(choices=[uuid1, uuid2])
enum_string: str = ormar.String(max_length=100, choices=list(TestEnum))
enum_string: str = ormar.String(max_length=100, choices=list(EnumTest))
@app.on_event("startup")
@ -110,7 +110,7 @@ def test_all_endpoints():
"random_decimal": 12.4,
"random_json": '{"aa":"bb"}',
"random_uuid": str(uuid1),
"enum_string": TestEnum.val1.value,
"enum_string": EnumTest.val1.value,
},
)

View File

@ -1,4 +1,4 @@
from typing import Any
from typing import Any, List, Sequence, cast
import databases
import pytest
@ -131,6 +131,7 @@ async def test_getting_additional_fields_from_queryset() -> Any:
)
await post.categories.all()
assert post.postcategory is None
assert post.categories[0].postcategory.sort_order == 1
assert post.categories[1].postcategory.sort_order == 2
@ -138,8 +139,31 @@ async def test_getting_additional_fields_from_queryset() -> Any:
categories__name="Test category2"
)
assert post2.categories[0].postcategory.sort_order == 2
# if TYPE_CHECKING:
# reveal_type(post2)
@pytest.mark.asyncio
async def test_only_one_side_has_through() -> Any:
async with database:
post = await Post(title="Test post").save()
await post.categories.create(
name="Test category1", postcategory={"sort_order": 1}
)
await post.categories.create(
name="Test category2", postcategory={"sort_order": 2}
)
post2 = await Post.objects.select_related("categories").get()
assert post2.postcategory is None
assert post2.categories[0].postcategory is not None
await post2.categories.all()
assert post2.postcategory is None
assert post2.categories[0].postcategory is not None
categories = await Category.objects.select_related("posts").all()
categories = cast(Sequence[Category], categories)
assert categories[0].postcategory is None
assert categories[0].posts[0].postcategory is not None
@pytest.mark.asyncio
@ -294,7 +318,6 @@ async def test_update_through_from_related() -> Any:
@pytest.mark.asyncio
@pytest.mark.skip # TODO: Restore after finished exclude refactor
async def test_excluding_fields_on_through_model() -> Any:
async with database:
post = await Post(title="Test post").save()
@ -323,6 +346,17 @@ async def test_excluding_fields_on_through_model() -> Any:
assert post2.categories[2].postcategory.param_name is None
assert post2.categories[2].postcategory.sort_order == 3
post3 = (
await Post.objects.select_related("categories")
.fields({"postcategory": ..., "title": ...})
.exclude_fields({"postcategory": {"param_name", "sort_order"}})
.get()
)
assert len(post3.categories) == 3
for category in post3.categories:
assert category.postcategory.param_name is None
assert category.postcategory.sort_order is None
# TODO: check/ modify following
@ -337,9 +371,9 @@ async def test_excluding_fields_on_through_model() -> Any:
# ordering by in order_by (V)
# updating in query (V)
# updating from querysetproxy (V)
# including/excluding in fields?
# modifying from instance (both sides?) (X) <= no, the loaded one doesn't have relations
# including/excluding in fields?
# allowing to change fk fields names in through model?
# make through optional? auto-generated for cases other fields are missing?

View File

@ -8,11 +8,6 @@ from ormar.queryset.utils import translate_list_to_dict, update_dict_from_list,
from tests.settings import DATABASE_URL
def test_empty_excludable():
assert ExcludableMixin.is_included(None, "key") # all fields included if empty
assert not ExcludableMixin.is_excluded(None, "key") # none field excluded if empty
def test_list_to_dict_translation():
tet_list = ["aa", "bb", "cc__aa", "cc__bb", "cc__aa__xx", "cc__aa__yy"]
test = translate_list_to_dict(tet_list)