finish release notes, add more test

This commit is contained in:
collerek
2021-04-20 11:52:41 +02:00
parent 7a27778b44
commit 5e38426694
12 changed files with 242 additions and 95 deletions

View File

@ -567,8 +567,14 @@ class ModelMetaclass(pydantic.main.ModelMetaclass):
field = self.Meta.model_fields.get(item)
if field.is_relation:
return FieldAccessor(
source_model=self, model=field.to, access_chain=item
source_model=cast(Type["Model"], self),
model=field.to,
access_chain=item,
)
else:
return FieldAccessor(source_model=self, field=field, access_chain=item)
return FieldAccessor(
source_model=cast(Type["Model"], self),
field=field,
access_chain=item,
)
return object.__getattribute__(self, item)

View File

@ -176,5 +176,6 @@ class FilterAction(QueryAction):
clause_text = clause_text.replace(
f"{self.table.name}.{self.column.name}", aliased_name
)
clause_text.replace("%%", "%") # remove doubles in some dialects
clause = text(clause_text)
return clause

View File

@ -25,10 +25,11 @@ class FilterGroup:
"""
def __init__(
self, *args: Any,
_filter_type: FilterType = FilterType.AND,
_exclude: bool = False,
**kwargs: Any,
self,
*args: Any,
_filter_type: FilterType = FilterType.AND,
_exclude: bool = False,
**kwargs: Any,
) -> None:
self.filter_type = _filter_type
self.exclude = _exclude
@ -41,7 +42,7 @@ class FilterGroup:
def __and__(self, other: "FilterGroup") -> "FilterGroup":
return FilterGroup(self, other)
def __or__(self, other) -> "FilterGroup":
def __or__(self, other: "FilterGroup") -> "FilterGroup":
return FilterGroup(self, other, _filter_type=FilterType.OR)
def __invert__(self) -> "FilterGroup":
@ -49,10 +50,10 @@ class FilterGroup:
return self
def resolve(
self,
model_cls: Type["Model"],
select_related: List = None,
filter_clauses: List = None,
self,
model_cls: Type["Model"],
select_related: List = None,
filter_clauses: List = None,
) -> Tuple[List[FilterAction], List[str]]:
"""
Resolves the FilterGroups actions to use proper target model, replace
@ -123,13 +124,15 @@ class FilterGroup:
prefix = " NOT " if self.exclude else ""
if self.filter_type == FilterType.AND:
clause = sqlalchemy.text(
f"{prefix}( " + str(
sqlalchemy.sql.and_(*self._get_text_clauses())) + " )"
f"{prefix}( "
+ str(sqlalchemy.sql.and_(*self._get_text_clauses()))
+ " )"
)
else:
clause = sqlalchemy.text(
f"{prefix}( " + str(
sqlalchemy.sql.or_(*self._get_text_clauses())) + " )"
f"{prefix}( "
+ str(sqlalchemy.sql.or_(*self._get_text_clauses()))
+ " )"
)
return clause
@ -182,7 +185,7 @@ class QueryClause:
"""
def __init__(
self, model_cls: Type["Model"], filter_clauses: List, select_related: List,
self, model_cls: Type["Model"], filter_clauses: List, select_related: List,
) -> None:
self._select_related = select_related[:]
@ -192,7 +195,7 @@ class QueryClause:
self.table = self.model_cls.Meta.table
def prepare_filter( # noqa: A003
self, _own_only: bool = False, **kwargs: Any
self, _own_only: bool = False, **kwargs: Any
) -> Tuple[List[FilterAction], List[str]]:
"""
Main external access point that processes the clauses into sqlalchemy text
@ -217,7 +220,7 @@ class QueryClause:
return filter_clauses, select_related
def _populate_filter_clauses(
self, _own_only: bool, **kwargs: Any
self, _own_only: bool, **kwargs: Any
) -> Tuple[List[FilterAction], List[str]]:
"""
Iterates all clauses and extracts used operator and field from related
@ -298,7 +301,7 @@ class QueryClause:
return prefixes
def _switch_filter_action_prefixes(
self, filter_clauses: List[FilterAction]
self, filter_clauses: List[FilterAction]
) -> List[FilterAction]:
"""
Substitutes aliases for filter action if the complex key (whole relation str) is

View File

@ -1,20 +1,27 @@
from typing import Any
from typing import Any, TYPE_CHECKING, Type
from ormar.queryset.actions import OrderAction
from ormar.queryset.actions.filter_action import METHODS_TO_OPERATORS
from ormar.queryset.clause import FilterGroup
if TYPE_CHECKING: # pragma: no cover
from ormar import BaseField, Model
class FieldAccessor:
def __init__(
self, source_model=None, field=None, model=None, access_chain: str = ""
):
self,
source_model: Type["Model"],
field: "BaseField" = None,
model: Type["Model"] = None,
access_chain: str = "",
) -> None:
self._source_model = source_model
self._field = field
self._model = model
self._access_chain = access_chain
def __bool__(self):
def __bool__(self) -> bool:
# hack to avoid pydantic name check from parent model
return False
@ -22,8 +29,8 @@ class FieldAccessor:
if self._field and item == self._field.name:
return self._field
if item in self._model.Meta.model_fields:
field = self._model.Meta.model_fields.get(item)
if self._model and item in self._model.Meta.model_fields:
field = self._model.Meta.model_fields[item]
if field.is_relation:
return FieldAccessor(
source_model=self._source_model,
@ -61,43 +68,43 @@ class FieldAccessor:
def __le__(self, other: Any) -> FilterGroup:
return self._select_operator(op="__le__", other=other)
def __lt__(self, other) -> FilterGroup:
def __lt__(self, other: Any) -> FilterGroup:
return self._select_operator(op="__lt__", other=other)
def __mod__(self, other) -> FilterGroup:
def __mod__(self, other: Any) -> FilterGroup:
return self._select_operator(op="__mod__", other=other)
def __lshift__(self, other) -> FilterGroup:
def __lshift__(self, other: Any) -> FilterGroup:
return self._select_operator(op="in", other=other)
def __rshift__(self, other) -> FilterGroup:
def __rshift__(self, other: Any) -> FilterGroup:
return self._select_operator(op="isnull", other=True)
def in_(self, other) -> FilterGroup:
def in_(self, other: Any) -> FilterGroup:
return self._select_operator(op="in", other=other)
def iexact(self, other) -> FilterGroup:
def iexact(self, other: Any) -> FilterGroup:
return self._select_operator(op="iexact", other=other)
def contains(self, other) -> FilterGroup:
def contains(self, other: Any) -> FilterGroup:
return self._select_operator(op="contains", other=other)
def icontains(self, other) -> FilterGroup:
def icontains(self, other: Any) -> FilterGroup:
return self._select_operator(op="icontains", other=other)
def startswith(self, other) -> FilterGroup:
def startswith(self, other: Any) -> FilterGroup:
return self._select_operator(op="startswith", other=other)
def istartswith(self, other) -> FilterGroup:
def istartswith(self, other: Any) -> FilterGroup:
return self._select_operator(op="istartswith", other=other)
def endswith(self, other) -> FilterGroup:
def endswith(self, other: Any) -> FilterGroup:
return self._select_operator(op="endswith", other=other)
def iendswith(self, other) -> FilterGroup:
def iendswith(self, other: Any) -> FilterGroup:
return self._select_operator(op="iendswith", other=other)
def isnull(self, other) -> FilterGroup:
def isnull(self, other: Any) -> FilterGroup:
return self._select_operator(op="isnull", other=other)
def asc(self) -> OrderAction:

View File

@ -541,7 +541,8 @@ class QuerySet(Generic[T]):
orders_by = [
OrderAction(order_str=x, model_cls=self.model_cls) # type: ignore
if not isinstance(x, OrderAction) else x
if not isinstance(x, OrderAction)
else x
for x in columns
]
@ -672,7 +673,7 @@ class QuerySet(Generic[T]):
)
return await self.database.execute(expr)
async def delete(self, *args, each: bool = False, **kwargs: Any) -> int:
async def delete(self, *args: Any, each: bool = False, **kwargs: Any) -> int:
"""
Deletes from the model table after applying the filters from kwargs.
@ -754,7 +755,7 @@ class QuerySet(Generic[T]):
limit_raw_sql = self.limit_sql_raw if limit_raw_sql is None else limit_raw_sql
return self.rebuild_self(offset=offset, limit_raw_sql=limit_raw_sql,)
async def first(self, *args, **kwargs: Any) -> "T":
async def first(self, *args: Any, **kwargs: Any) -> "T":
"""
Gets the first row from the db ordered by primary key column ascending.
@ -785,7 +786,7 @@ class QuerySet(Generic[T]):
self.check_single_result_rows_count(processed_rows)
return processed_rows[0] # type: ignore
async def get_or_none(self, *args, **kwargs: Any) -> Optional["T"]:
async def get_or_none(self, *args: Any, **kwargs: Any) -> Optional["T"]:
"""
Get's the first row from the db meeting the criteria set by kwargs.
@ -805,7 +806,7 @@ class QuerySet(Generic[T]):
except ormar.NoMatch:
return None
async def get(self, *args, **kwargs: Any) -> "T":
async def get(self, *args: Any, **kwargs: Any) -> "T":
"""
Get's the first row from the db meeting the criteria set by kwargs.
@ -844,7 +845,7 @@ class QuerySet(Generic[T]):
self.check_single_result_rows_count(processed_rows)
return processed_rows[0] # type: ignore
async def get_or_create(self, *args, **kwargs: Any) -> "T":
async def get_or_create(self, *args: Any, **kwargs: Any) -> "T":
"""
Combination of create and get methods.
@ -879,7 +880,7 @@ class QuerySet(Generic[T]):
model = await self.get(pk=kwargs[pk_name])
return await model.update(**kwargs)
async def all(self, *args, **kwargs: Any) -> List[Optional["T"]]: # noqa: A003
async def all(self, *args: Any, **kwargs: Any) -> List[Optional["T"]]: # noqa: A003
"""
Returns all rows from a database for given model for set filter options.

View File

@ -276,7 +276,7 @@ class QuerysetProxy(Generic[T]):
)
return await queryset.delete(**kwargs) # type: ignore
async def first(self, *args, **kwargs: Any) -> "T":
async def first(self, *args: Any, **kwargs: Any) -> "T":
"""
Gets the first row from the db ordered by primary key column ascending.
@ -294,7 +294,7 @@ class QuerysetProxy(Generic[T]):
self._register_related(first)
return first
async def get_or_none(self, *args, **kwargs: Any) -> Optional["T"]:
async def get_or_none(self, *args: Any, **kwargs: Any) -> Optional["T"]:
"""
Get's the first row from the db meeting the criteria set by kwargs.
@ -318,7 +318,7 @@ class QuerysetProxy(Generic[T]):
self._register_related(get)
return get
async def get(self, *args, **kwargs: Any) -> "T":
async def get(self, *args: Any, **kwargs: Any) -> "T":
"""
Get's the first row from the db meeting the criteria set by kwargs.
@ -342,7 +342,7 @@ class QuerysetProxy(Generic[T]):
self._register_related(get)
return get
async def all(self, *args, **kwargs: Any) -> List[Optional["T"]]: # noqa: A003
async def all(self, *args: Any, **kwargs: Any) -> List[Optional["T"]]: # noqa: A003
"""
Returns all rows from a database for given model for set filter options.
@ -425,7 +425,7 @@ class QuerysetProxy(Generic[T]):
)
return len(children)
async def get_or_create(self, *args, **kwargs: Any) -> "T":
async def get_or_create(self, *args: Any, **kwargs: Any) -> "T":
"""
Combination of create and get methods.