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

Allow class member values in projection and distinct queries #214

Merged
merged 7 commits into from
Oct 1, 2019
Merged
Show file tree
Hide file tree
Changes from 6 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
6 changes: 4 additions & 2 deletions google/cloud/ndb/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -1406,7 +1406,8 @@ def __init__(
"projection must be a tuple, list or None; "
"received {}".format(projection)
)
self._check_properties(self._to_property_names(projection))
projection = self._to_property_names(projection)
self._check_properties(projection)
self.projection = tuple(projection)

if distinct_on is not None and group_by is not None:
Expand All @@ -1426,7 +1427,8 @@ def __init__(
"distinct_on must be a tuple, list or None; "
"received {}".format(distinct_on)
)
self._check_properties(self._to_property_names(distinct_on))
distinct_on = self._to_property_names(distinct_on)
self._check_properties(distinct_on)
self.distinct_on = tuple(distinct_on)

def __repr__(self):
Expand Down
36 changes: 36 additions & 0 deletions tests/unit/test_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -1165,6 +1165,42 @@ def test_constructor_with_ancestor_parameterized_thing():
query = query_module.Query(ancestor=query_module.ParameterizedThing())
assert isinstance(query.ancestor, query_module.ParameterizedThing)

@staticmethod
@pytest.mark.usefixtures("in_context")
@unittest.mock.patch("google.cloud.ndb.query._datastore_query")
def test_constructor_with_class_attribute_projection(_datastore_query):
class Foo(model.Model):
string_attr = model.StringProperty()

class Bar(model.Model):
bar_attr = model.StructuredProperty(Foo)

query = Bar.query(projection=[Bar.bar_attr.string_attr])

assert query.projection[0] == ('bar_attr.string_attr',)[0]

query.fetch()

@staticmethod
@pytest.mark.usefixtures("in_context")
@unittest.mock.patch("google.cloud.ndb.query._datastore_query")
def test_constructor_with_class_attribute_projection_and_distinct(_datastore_query):
class Foo(model.Model):
string_attr = model.StringProperty()

class Bar(model.Model):
bar_attr = model.StructuredProperty(Foo)

query = Bar.query(
projection=[Bar.bar_attr.string_attr],
distinct_on=[Bar.bar_attr.string_attr]
)

assert query.projection[0] == ('bar_attr.string_attr',)[0]
assert query.distinct_on[0] == ('bar_attr.string_attr',)[0]

query.fetch()

@staticmethod
@pytest.mark.usefixtures("in_context")
def test_constructor_with_projection():
Expand Down