allow passing a dict and set to fields and exclude_fields, store it as dict
This commit is contained in:
@ -117,8 +117,8 @@ async def test_working_with_aliases():
|
||||
"first_name",
|
||||
"last_name",
|
||||
"born_year",
|
||||
"child__first_name",
|
||||
"child__last_name",
|
||||
"children__first_name",
|
||||
"children__last_name",
|
||||
]
|
||||
)
|
||||
.get()
|
||||
|
||||
@ -80,10 +80,53 @@ async def test_selecting_subset():
|
||||
all_cars = (
|
||||
await Car.objects.select_related("manufacturer")
|
||||
.exclude_fields(
|
||||
["gearbox_type", "gears", "aircon_type", "year", "company__founded"]
|
||||
[
|
||||
"gearbox_type",
|
||||
"gears",
|
||||
"aircon_type",
|
||||
"year",
|
||||
"manufacturer__founded",
|
||||
]
|
||||
)
|
||||
.all()
|
||||
)
|
||||
for car in all_cars:
|
||||
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
|
||||
|
||||
all_cars = (
|
||||
await Car.objects.select_related("manufacturer")
|
||||
.exclude_fields(
|
||||
{
|
||||
"gearbox_type": ...,
|
||||
"gears": ...,
|
||||
"aircon_type": ...,
|
||||
"year": ...,
|
||||
"manufacturer": {"founded": ...},
|
||||
}
|
||||
)
|
||||
.all()
|
||||
)
|
||||
all_cars2 = (
|
||||
await Car.objects.select_related("manufacturer")
|
||||
.exclude_fields(
|
||||
{
|
||||
"gearbox_type": ...,
|
||||
"gears": ...,
|
||||
"aircon_type": ...,
|
||||
"year": ...,
|
||||
"manufacturer": {"founded"},
|
||||
}
|
||||
)
|
||||
.all()
|
||||
)
|
||||
|
||||
assert all_cars == all_cars2
|
||||
|
||||
for car in all_cars:
|
||||
assert all(
|
||||
getattr(car, x) is None
|
||||
@ -119,7 +162,7 @@ async def test_selecting_subset():
|
||||
all_cars_check2 = (
|
||||
await Car.objects.select_related("manufacturer")
|
||||
.fields(["id", "name", "manufacturer"])
|
||||
.exclude_fields("company__founded")
|
||||
.exclude_fields("manufacturer__founded")
|
||||
.all()
|
||||
)
|
||||
for car in all_cars_check2:
|
||||
@ -133,5 +176,5 @@ async def test_selecting_subset():
|
||||
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(
|
||||
["company__name"]
|
||||
["manufacturer__name"]
|
||||
).all()
|
||||
|
||||
98
tests/test_queryset_utils.py
Normal file
98
tests/test_queryset_utils.py
Normal file
@ -0,0 +1,98 @@
|
||||
from ormar.models.excludable import Excludable
|
||||
from ormar.queryset.utils import translate_list_to_dict, update_dict_from_list, update
|
||||
|
||||
|
||||
def test_empty_excludable():
|
||||
assert Excludable.is_included(None, "key") # all fields included if empty
|
||||
assert not Excludable.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)
|
||||
assert test == {
|
||||
"aa": Ellipsis,
|
||||
"bb": Ellipsis,
|
||||
"cc": {"aa": {"xx": Ellipsis, "yy": Ellipsis}, "bb": Ellipsis},
|
||||
}
|
||||
|
||||
|
||||
def test_updating_dict_with_list():
|
||||
curr_dict = {
|
||||
"aa": Ellipsis,
|
||||
"bb": Ellipsis,
|
||||
"cc": {"aa": {"xx": Ellipsis, "yy": Ellipsis}, "bb": Ellipsis},
|
||||
}
|
||||
list_to_update = ["ee", "bb__cc", "cc__aa__xx__oo", "cc__aa__oo"]
|
||||
test = update_dict_from_list(curr_dict, list_to_update)
|
||||
assert test == {
|
||||
"aa": Ellipsis,
|
||||
"bb": {"cc": Ellipsis},
|
||||
"cc": {
|
||||
"aa": {"xx": {"oo": Ellipsis}, "yy": Ellipsis, "oo": Ellipsis},
|
||||
"bb": Ellipsis,
|
||||
},
|
||||
"ee": Ellipsis,
|
||||
}
|
||||
|
||||
|
||||
def test_updating_dict_inc_set_with_list():
|
||||
curr_dict = {
|
||||
"aa": Ellipsis,
|
||||
"bb": Ellipsis,
|
||||
"cc": {"aa": {"xx", "yy"}, "bb": Ellipsis},
|
||||
}
|
||||
list_to_update = ["uu", "bb__cc", "cc__aa__xx__oo", "cc__aa__oo"]
|
||||
test = update_dict_from_list(curr_dict, list_to_update)
|
||||
assert test == {
|
||||
"aa": Ellipsis,
|
||||
"bb": {"cc": Ellipsis},
|
||||
"cc": {
|
||||
"aa": {"xx": {"oo": Ellipsis}, "yy": Ellipsis, "oo": Ellipsis},
|
||||
"bb": Ellipsis,
|
||||
},
|
||||
"uu": Ellipsis,
|
||||
}
|
||||
|
||||
|
||||
def test_updating_dict_inc_set_with_dict():
|
||||
curr_dict = {
|
||||
"aa": Ellipsis,
|
||||
"bb": Ellipsis,
|
||||
"cc": {"aa": {"xx", "yy"}, "bb": Ellipsis},
|
||||
}
|
||||
dict_to_update = {
|
||||
"uu": Ellipsis,
|
||||
"bb": {"cc", "dd"},
|
||||
"cc": {"aa": {"xx": {"oo": Ellipsis}, "oo": Ellipsis}},
|
||||
}
|
||||
test = update(curr_dict, dict_to_update)
|
||||
assert test == {
|
||||
"aa": Ellipsis,
|
||||
"bb": {"cc", "dd"},
|
||||
"cc": {
|
||||
"aa": {"xx": {"oo": Ellipsis}, "yy": Ellipsis, "oo": Ellipsis},
|
||||
"bb": Ellipsis,
|
||||
},
|
||||
"uu": Ellipsis,
|
||||
}
|
||||
|
||||
|
||||
def test_updating_dict_inc_set_with_dict_inc_set():
|
||||
curr_dict = {
|
||||
"aa": Ellipsis,
|
||||
"bb": Ellipsis,
|
||||
"cc": {"aa": {"xx", "yy"}, "bb": Ellipsis},
|
||||
}
|
||||
dict_to_update = {
|
||||
"uu": Ellipsis,
|
||||
"bb": {"cc", "dd"},
|
||||
"cc": {"aa": {"xx", "oo", "zz", "ii"}},
|
||||
}
|
||||
test = update(curr_dict, dict_to_update)
|
||||
assert test == {
|
||||
"aa": Ellipsis,
|
||||
"bb": {"cc", "dd"},
|
||||
"cc": {"aa": {"xx", "yy", "oo", "zz", "ii"}, "bb": Ellipsis},
|
||||
"uu": Ellipsis,
|
||||
}
|
||||
@ -1,4 +1,5 @@
|
||||
from typing import Optional
|
||||
import itertools
|
||||
from typing import Optional, List
|
||||
|
||||
import databases
|
||||
import pydantic
|
||||
@ -12,6 +13,35 @@ database = databases.Database(DATABASE_URL, force_rollback=True)
|
||||
metadata = sqlalchemy.MetaData()
|
||||
|
||||
|
||||
class NickNames(ormar.Model):
|
||||
class Meta:
|
||||
tablename = "nicks"
|
||||
metadata = metadata
|
||||
database = database
|
||||
|
||||
id: int = ormar.Integer(primary_key=True)
|
||||
name: str = ormar.String(max_length=100, nullable=False, name="hq_name")
|
||||
is_lame: bool = ormar.Boolean(nullable=True)
|
||||
|
||||
|
||||
class NicksHq(ormar.Model):
|
||||
class Meta:
|
||||
tablename = "nicks_x_hq"
|
||||
metadata = metadata
|
||||
database = database
|
||||
|
||||
|
||||
class HQ(ormar.Model):
|
||||
class Meta:
|
||||
tablename = "hqs"
|
||||
metadata = metadata
|
||||
database = database
|
||||
|
||||
id: int = ormar.Integer(primary_key=True)
|
||||
name: str = ormar.String(max_length=100, nullable=False, name="hq_name")
|
||||
nicks: List[NickNames] = ormar.ManyToMany(NickNames, through=NicksHq)
|
||||
|
||||
|
||||
class Company(ormar.Model):
|
||||
class Meta:
|
||||
tablename = "companies"
|
||||
@ -21,6 +51,7 @@ class Company(ormar.Model):
|
||||
id: int = ormar.Integer(primary_key=True)
|
||||
name: str = ormar.String(max_length=100, nullable=False, name="company_name")
|
||||
founded: int = ormar.Integer(nullable=True)
|
||||
hq: HQ = ormar.ForeignKey(HQ)
|
||||
|
||||
|
||||
class Car(ormar.Model):
|
||||
@ -51,7 +82,14 @@ def create_test_database():
|
||||
async def test_selecting_subset():
|
||||
async with database:
|
||||
async with database.transaction(force_rollback=True):
|
||||
toyota = await Company.objects.create(name="Toyota", founded=1937)
|
||||
nick1 = await NickNames.objects.create(name="Nippon", is_lame=False)
|
||||
nick2 = await NickNames.objects.create(name="EroCherry", is_lame=True)
|
||||
hq = await HQ.objects.create(name="Japan")
|
||||
await hq.nicks.add(nick1)
|
||||
await hq.nicks.add(nick2)
|
||||
|
||||
toyota = await Company.objects.create(name="Toyota", founded=1937, hq=hq)
|
||||
|
||||
await Car.objects.create(
|
||||
manufacturer=toyota,
|
||||
name="Corolla",
|
||||
@ -78,17 +116,66 @@ async def test_selecting_subset():
|
||||
)
|
||||
|
||||
all_cars = (
|
||||
await Car.objects.select_related("manufacturer")
|
||||
.fields(["id", "name", "company__name"])
|
||||
await Car.objects.select_related(
|
||||
["manufacturer", "manufacturer__hq", "manufacturer__hq__nicks"]
|
||||
)
|
||||
.fields(
|
||||
[
|
||||
"id",
|
||||
"name",
|
||||
"manufacturer__name",
|
||||
"manufacturer__hq__name",
|
||||
"manufacturer__hq__nicks__name",
|
||||
]
|
||||
)
|
||||
.all()
|
||||
)
|
||||
for car in all_cars:
|
||||
|
||||
all_cars2 = (
|
||||
await Car.objects.select_related(
|
||||
["manufacturer", "manufacturer__hq", "manufacturer__hq__nicks"]
|
||||
)
|
||||
.fields(
|
||||
{
|
||||
"id": ...,
|
||||
"name": ...,
|
||||
"manufacturer": {
|
||||
"name": ...,
|
||||
"hq": {"name": ..., "nicks": {"name": ...}},
|
||||
},
|
||||
}
|
||||
)
|
||||
.all()
|
||||
)
|
||||
|
||||
all_cars3 = (
|
||||
await Car.objects.select_related(
|
||||
["manufacturer", "manufacturer__hq", "manufacturer__hq__nicks"]
|
||||
)
|
||||
.fields(
|
||||
{
|
||||
"id": ...,
|
||||
"name": ...,
|
||||
"manufacturer": {
|
||||
"name": ...,
|
||||
"hq": {"name": ..., "nicks": {"name"}},
|
||||
},
|
||||
}
|
||||
)
|
||||
.all()
|
||||
)
|
||||
assert all_cars3 == all_cars
|
||||
|
||||
for car in itertools.chain(all_cars, all_cars2):
|
||||
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
|
||||
assert car.manufacturer.hq.name == "Japan"
|
||||
assert len(car.manufacturer.hq.nicks) == 2
|
||||
assert car.manufacturer.hq.nicks[0].is_lame is None
|
||||
|
||||
all_cars = (
|
||||
await Car.objects.select_related("manufacturer")
|
||||
@ -103,6 +190,7 @@ async def test_selecting_subset():
|
||||
)
|
||||
assert car.manufacturer.name == "Toyota"
|
||||
assert car.manufacturer.founded == 1937
|
||||
assert car.manufacturer.hq.name is None
|
||||
|
||||
all_cars_check = await Car.objects.select_related("manufacturer").all()
|
||||
for car in all_cars_check:
|
||||
@ -116,5 +204,5 @@ async def test_selecting_subset():
|
||||
with pytest.raises(pydantic.error_wrappers.ValidationError):
|
||||
# cannot exclude mandatory model columns - company__name in this example
|
||||
await Car.objects.select_related("manufacturer").fields(
|
||||
["id", "name", "company__founded"]
|
||||
["id", "name", "manufacturer__founded"]
|
||||
).all()
|
||||
|
||||
Reference in New Issue
Block a user