-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
121 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
from collections.abc import Iterator | ||
|
||
from mypy.nodes import Expression | ||
|
||
from edgeql_qb.render.condition import render_conditions | ||
from edgeql_qb.render.select import render_select | ||
from edgeql_qb.render.tools import combine_many_renderers | ||
from edgeql_qb.render.types import RenderedQuery | ||
|
||
|
||
def render_count(inner: RenderedQuery) -> RenderedQuery: | ||
return combine_many_renderers( | ||
RenderedQuery('select count('), | ||
inner, | ||
RenderedQuery(')'), | ||
) | ||
|
||
def render_count_inner(model_name, filters: tuple[Expression, ...], gen: Iterator[int]): | ||
match filters: | ||
case (): | ||
return RenderedQuery(model_name) | ||
case conditions: | ||
rendered_select = render_select( | ||
model_name, | ||
select=(), | ||
generator=gen, | ||
) | ||
rendered_filters = render_conditions(conditions, gen) | ||
return combine_many_renderers( | ||
RenderedQuery('('), | ||
rendered_select, | ||
rendered_filters, | ||
RenderedQuery(')'), | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
from multiprocessing.connection import Client | ||
|
||
from edgeql_qb import EdgeDBModel | ||
from edgeql_qb.frozendict import FrozenDict | ||
from edgeql_qb.types import int16 | ||
|
||
A = EdgeDBModel('A') | ||
|
||
|
||
def test_count_wo_filters() -> None: | ||
rendered = A.count.build() | ||
assert rendered.query == 'select count(A)' | ||
|
||
|
||
def test_count_filter(client: Client) -> None: | ||
insert1 = A.insert.values(p_int16=int16(1)).build() | ||
insert2 = A.insert.values(p_int16=int16(2)).build() | ||
insert3 = A.insert.values(p_int16=int16(3)).build() | ||
client.query(insert1.query, **insert1.context) | ||
client.query(insert2.query, **insert2.context) | ||
client.query(insert3.query, **insert3.context) | ||
rendered = A.count.where(A.c.p_int16 <= int16(2)).build() | ||
assert rendered.query == 'select count((select A filter .p_int16 <= <int16>$filter_0))' | ||
assert rendered.context == FrozenDict(filter_0=2) | ||
result = client.query(rendered.query, **rendered.context) | ||
assert result == [2] |