This repository was archived by the owner on May 6, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 68
fix: prevent mismatch error with default namespace on ancestor queries #604
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -161,6 +161,56 @@ class SomeKind(ndb.Model): | |
| assert [entity.foo for entity in results] == [-1, 0, 1, 2, 3, 4] | ||
|
|
||
|
|
||
| def test_ancestor_query_with_namespace(client_context, dispose_of, other_namespace): | ||
| class Dummy(ndb.Model): | ||
| foo = ndb.StringProperty(default="") | ||
|
|
||
| entity1 = Dummy(foo="bar", namespace="xyz") | ||
| parent_key = entity1.put() | ||
| dispose_of(entity1.key._key) | ||
|
|
||
| entity2 = Dummy(foo="child", parent=parent_key, namespace=None) | ||
| entity2.put() | ||
| dispose_of(entity2.key._key) | ||
|
|
||
| entity3 = Dummy(foo="childless", namespace="xyz") | ||
| entity3.put() | ||
| dispose_of(entity3.key._key) | ||
|
|
||
| with client_context.new(namespace=other_namespace).use(): | ||
| query = Dummy.query(ancestor=parent_key, namespace="xyz") | ||
| results = eventually(query.fetch, length_equals(2)) | ||
|
|
||
| assert results[0].foo == "bar" | ||
| assert results[1].foo == "child" | ||
|
|
||
|
|
||
| def test_ancestor_query_with_default_namespace( | ||
| client_context, dispose_of, other_namespace | ||
| ): | ||
| class Dummy(ndb.Model): | ||
| foo = ndb.StringProperty(default="") | ||
|
|
||
| entity1 = Dummy(foo="bar", namespace="") | ||
| parent_key = entity1.put() | ||
| dispose_of(entity1.key._key) | ||
|
|
||
| entity2 = Dummy(foo="child", parent=parent_key, namespace=None) | ||
| entity2.put() | ||
| dispose_of(entity2.key._key) | ||
|
|
||
| entity3 = Dummy(foo="childless", namespace="") | ||
| entity3.put() | ||
| dispose_of(entity3.key._key) | ||
|
|
||
| with client_context.new(namespace=other_namespace).use(): | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. FWIW, tests are not, by default, run in the default namespace, so you don't really have to switch namespace here. It's already not the default. Although, I guess it might make it more readable to show that the namespace is different explicitly. |
||
| query = Dummy.query(ancestor=parent_key, namespace="") | ||
| results = eventually(query.fetch, length_equals(2)) | ||
|
|
||
| assert results[0].foo == "bar" | ||
| assert results[1].foo == "child" | ||
|
|
||
|
|
||
| @pytest.mark.usefixtures("client_context") | ||
| def test_projection(ds_entity): | ||
| entity_id = test_utils.system.unique_resource_id() | ||
|
|
||
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I noticed in the customer's example that they don't explicitly pass
namespace. I would think that the namespace would be implied by passing inparent. For grins, I tried removing thenamespacearg from this test and I get a namespace mismatch exception in the model constructor. Is there maybe a little more work to do here?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, I confess I added this to make the test pass, but the customer's code does run unchanged if using a python script outside of the test machinery. Maybe I'm missing something on the test setup?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks like it's just because in the customer's example the context's namespace is still the default namespace when that entity is created, so there won't be a mismatch between entity2 and its parent. Does still seem like specifying parent should imply the namespace, though.
I know you're starting your new job. Would you like me to take this one?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, please take this. I appreciate the offer.
FWIW, the following code caused the mismatch error before the fix was applied, and it works fine with the fix in:
I couldn't quite duplicate that in the text.