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

feat(datastore): Support "IN" query filter #760

Merged
merged 6 commits into from
Jun 14, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 2 additions & 0 deletions datastore/gcloud/aio/datastore/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ class MyGQLQuery(gcloud.aio.datastore.GQLQuery):
"""
import importlib.metadata

from .array import Array
from .constants import CompositeFilterOperator
from .constants import Consistency
from .constants import Direction
Expand Down Expand Up @@ -215,6 +216,7 @@ class MyGQLQuery(gcloud.aio.datastore.GQLQuery):

__version__ = importlib.metadata.version('gcloud-aio-datastore')
__all__ = [
'Array',
'CompositeFilter',
'CompositeFilterOperator',
'Consistency',
Expand Down
1 change: 1 addition & 0 deletions datastore/gcloud/aio/datastore/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ class PropertyFilterOperator(enum.Enum):
LESS_THAN_OR_EQUAL = 'LESS_THAN_OR_EQUAL'
NOT_EQUAL = 'NOT_EQUAL'
UNSPECIFIED = 'OPERATOR_UNSPECIFIED'
IN = 'IN'
onurdialpad marked this conversation as resolved.
Show resolved Hide resolved


class ResultType(enum.Enum):
Expand Down
15 changes: 11 additions & 4 deletions datastore/gcloud/aio/datastore/filter.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from typing import Any
from typing import Dict
from typing import List
from typing import Union

from .array import Array
from .constants import CompositeFilterOperator
from .constants import PropertyFilterOperator
from .value import Value
Expand Down Expand Up @@ -89,7 +91,7 @@ class PropertyFilter(BaseFilter):

def __init__(
self, prop: str, operator: PropertyFilterOperator,
value: Value,
value: Union[Value, Array]
) -> None:
self.prop = prop
self.operator = operator
Expand All @@ -113,8 +115,13 @@ def from_repr(cls, data: Dict[str, Any]) -> 'PropertyFilter':
return cls(prop=prop, operator=operator, value=value)

def to_repr(self) -> Dict[str, Any]:
return {
rep = {
'op': self.operator.value,
'property': {'name': self.prop},
'value': self.value.to_repr(),
'property': {'name': self.prop}
}
# Temporary workaround for handling arrayValue with PropertyFilter
TheKevJames marked this conversation as resolved.
Show resolved Hide resolved
if isinstance(self.value, Array):
rep['value'] = {'arrayValue': self.value.to_repr()}
else:
rep['value'] = self.value.to_repr()
return rep
80 changes: 80 additions & 0 deletions datastore/tests/integration/smoke_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import pytest
from gcloud.aio.auth import BUILD_GCLOUD_REST # pylint: disable=no-name-in-module
from gcloud.aio.datastore import Array
from gcloud.aio.datastore import Datastore
from gcloud.aio.datastore import Filter
from gcloud.aio.datastore import GQLCursor
Expand Down Expand Up @@ -272,6 +273,46 @@ async def test_query(creds: str, kind: str, project: str) -> None:
assert len(after.entity_results) == num_results + 2


@pytest.mark.asyncio
@pytest.mark.xfail(strict=False)
async def test_query_with_in_filter(
creds: str, kind: str, project: str) -> None:
async with Session() as s:
ds = Datastore(project=project, service_file=creds, session=s)

property_filter = PropertyFilter(
prop='value', operator=PropertyFilterOperator.IN,
value=Array([Value(99), Value(100)])
)
query = Query(kind=kind, query_filter=Filter(property_filter))

before = await ds.runQuery(query, session=s)
num_results = len(before.entity_results)

transaction = await ds.beginTransaction(session=s)
mutations = [
ds.make_mutation(
Operation.INSERT,
Key(project, [PathElement(kind)]),
properties={'value': 99},
),
ds.make_mutation(
Operation.INSERT,
Key(project, [PathElement(kind)]),
properties={'value': 100},
),
ds.make_mutation(
Operation.INSERT,
Key(project, [PathElement(kind)]),
properties={'value': 101},
)
]
await ds.commit(mutations, transaction=transaction, session=s)

after = await ds.runQuery(query, session=s)
assert len(after.entity_results) == num_results + 2


@pytest.mark.asyncio
@pytest.mark.xfail(strict=False)
async def test_gql_query(creds: str, kind: str, project: str) -> None:
Expand Down Expand Up @@ -310,6 +351,45 @@ async def test_gql_query(creds: str, kind: str, project: str) -> None:
assert len(after.entity_results) == num_results + 3


@pytest.mark.asyncio
@pytest.mark.xfail(strict=False)
async def test_gql_query_with_in_filter(
creds: str, kind: str, project: str) -> None:
async with Session() as s:
ds = Datastore(project=project, service_file=creds, session=s)

query = GQLQuery(
f'SELECT * FROM {kind} WHERE value IN @values',
named_bindings={'values': Array([Value(99), Value(100)])},
)

before = await ds.runQuery(query, session=s)
num_results = len(before.entity_results)

transaction = await ds.beginTransaction(session=s)
mutations = [
ds.make_mutation(
Operation.INSERT,
Key(project, [PathElement(kind)]),
properties={'value': 99},
),
ds.make_mutation(
Operation.INSERT,
Key(project, [PathElement(kind)]),
properties={'value': 100},
),
ds.make_mutation(
Operation.INSERT,
Key(project, [PathElement(kind)]),
properties={'value': 101},
),
]
await ds.commit(mutations, transaction=transaction, session=s)

after = await ds.runQuery(query, session=s)
assert len(after.entity_results) == num_results + 2


@pytest.mark.asyncio
@pytest.mark.xfail(strict=False)
async def test_gql_query_pagination(
Expand Down