Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PYG-350] 😣 Fix aggregate empty filter #440

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 26 additions & 12 deletions tests/test_integration/test_query_execution.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
from cognite.client.data_classes import filters
from omni import OmniClient

from cognite.pygen import _QueryExecutor
from cognite.pygen._query.interface import QueryExecutor


def test_query_reverse_direct_relation(cognite_client: CogniteClient, omni_views: dict[str, dm.View]) -> None:
item_e = omni_views["ConnectionItemE"]
item_d = omni_views["ConnectionItemD"]
executor = _QueryExecutor(cognite_client, views=[item_e, item_d])
executor = QueryExecutor(cognite_client, views=[item_e, item_d])
properties: list[str | dict[str, Any]] = [
"externalId",
"name",
Expand Down Expand Up @@ -44,7 +44,7 @@ def test_query_reverse_direct_relation(cognite_client: CogniteClient, omni_views
def test_query_direct_relation(cognite_client: CogniteClient, omni_views: dict[str, dm.View]) -> None:
item_e = omni_views["ConnectionItemE"]
item_d = omni_views["ConnectionItemD"]
executor = _QueryExecutor(cognite_client, views=[item_e, item_d])
executor = QueryExecutor(cognite_client, views=[item_e, item_d])
properties: list[str | dict[str, Any]] = [
"externalId",
"name",
Expand Down Expand Up @@ -82,7 +82,7 @@ def test_query_direct_relation(cognite_client: CogniteClient, omni_views: dict[s
def test_query_edge_outwards(cognite_client: CogniteClient, omni_views: dict[str, dm.View]) -> None:
item_a = omni_views["ConnectionItemA"]
item_b = omni_views["ConnectionItemB"]
executor = _QueryExecutor(cognite_client, views=[item_a, item_b])
executor = QueryExecutor(cognite_client, views=[item_a, item_b])
properties: list[str | dict[str, Any]] = [
"externalId",
"name",
Expand All @@ -107,7 +107,7 @@ def test_query_edge_outwards(cognite_client: CogniteClient, omni_views: dict[str
def test_query_edge_outwards_skip_edge(cognite_client: CogniteClient, omni_views: dict[str, dm.View]) -> None:
item_a = omni_views["ConnectionItemA"]
item_b = omni_views["ConnectionItemB"]
executor = _QueryExecutor(cognite_client, views=[item_a, item_b])
executor = QueryExecutor(cognite_client, views=[item_a, item_b])
executor._unpack_edges = "skip"
properties: list[str | dict[str, Any]] = [
"externalId",
Expand All @@ -131,7 +131,7 @@ def test_query_edge_outwards_skip_edge(cognite_client: CogniteClient, omni_views

def test_query_list_primitive_properties(cognite_client: CogniteClient, omni_views: dict[str, dm.View]) -> None:
view = omni_views["PrimitiveNullable"]
executor = _QueryExecutor(cognite_client, views=[view])
executor = QueryExecutor(cognite_client, views=[view])
properties: list[str | dict[str, Any]] = ["text", "boolean", "date"]
result = executor.list(view.as_id(), properties, limit=5)

Expand All @@ -144,16 +144,30 @@ def test_query_list_primitive_properties(cognite_client: CogniteClient, omni_vie

def test_aggregate_count(cognite_client: CogniteClient, omni_views: dict[str, dm.View]) -> None:
view = omni_views["PrimitiveRequired"]
executor = _QueryExecutor(cognite_client, views=[view])
executor = QueryExecutor(cognite_client, views=[view])
result = executor.aggregate(view.as_id(), aggregates=dm.aggregations.Count(property="externalId"))

assert isinstance(result, dict)
assert "count" in result


def test_aggregate_count_filter_no_results(cognite_client: CogniteClient, omni_views: dict[str, dm.View]) -> None:
view = omni_views["PrimitiveRequired"]
executor = QueryExecutor(cognite_client, views=[view])
result = executor.aggregate(
view.as_id(),
aggregates=dm.aggregations.Avg(property="int64"),
filter=filters.Equals(["node", "externalId"], "non_existing_id"),
)

assert isinstance(result, dict)
assert "count" in result
assert result["count"] == 0


def test_aggregate_count_with_group_by(cognite_client: CogniteClient, omni_views: dict[str, dm.View]) -> None:
view = omni_views["PrimitiveRequired"]
executor = _QueryExecutor(cognite_client, views=[view])
executor = QueryExecutor(cognite_client, views=[view])
result = executor.aggregate(
view.as_id(), aggregates=dm.aggregations.Count(property="externalId"), group_by="boolean"
)
Expand All @@ -163,7 +177,7 @@ def test_aggregate_count_with_group_by(cognite_client: CogniteClient, omni_views

def test_histogram(cognite_client: CogniteClient, omni_views: dict[str, dm.View]) -> None:
view = omni_views["PrimitiveRequired"]
executor = _QueryExecutor(cognite_client, views=[view])
executor = QueryExecutor(cognite_client, views=[view])
result = executor.aggregate(view.as_id(), aggregates=dm.aggregations.Histogram(property="float32", interval=100.0))

assert isinstance(result, dict)
Expand All @@ -177,7 +191,7 @@ def test_search(cognite_client: CogniteClient, omni_client: OmniClient, omni_vie
word = item.text.split(" ", maxsplit=1)[0]

view = omni_views["PrimitiveRequired"]
executor = _QueryExecutor(cognite_client, views=[view])
executor = QueryExecutor(cognite_client, views=[view])
selected_properties: list[str | dict[str, Any]] = ["text", "boolean", "externalId"]
result = executor.search(view.as_id(), selected_properties, query=word, limit=5)

Expand All @@ -190,7 +204,7 @@ def test_search(cognite_client: CogniteClient, omni_client: OmniClient, omni_vie

def test_search_nested_properties(cognite_client: CogniteClient, omni_views: dict[str, dm.View]) -> None:
view = omni_views["ConnectionItemE"]
executor = _QueryExecutor(cognite_client, views=[view])
executor = QueryExecutor(cognite_client, views=[view])
selected_properties: list[str | dict[str, Any]] = [
"externalId",
"name",
Expand All @@ -214,7 +228,7 @@ def test_search_nested_properties(cognite_client: CogniteClient, omni_views: dic


def test_query_list_root_nodes(cognite_client: CogniteClient) -> None:
executor = _QueryExecutor(cognite_client)
executor = QueryExecutor(cognite_client)
view_id = dm.ViewId("cdf_cdm", "CogniteAsset", "v1")

no_parent = filters.Equals(view_id.as_property_ref("parent"), None) # type: ignore[arg-type]
Expand Down
Loading