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

NDB: equality checking when using StructuredProperty and multiple contexts is broken #845

Open
aetherknight opened this issue Nov 29, 2022 · 1 comment
Labels
api: datastore Issues related to the googleapis/python-ndb API.

Comments

@aetherknight
Copy link

Environment details

python-ndb 1.11.1
Python 2.7.18
Tested on MacOS 12.6.1

Steps to reproduce

  1. Create an entity in one NDB context
  2. Fetch/get the same entity in another context, or in the same context after having flushed/cleared the caches.
  3. Compare those objects

Code example

from google.cloud import ndb

client = ndb.Client("test")

class SP(ndb.Model):
    some_field = ndb.StringProperty()

class SomeModel(ndb.Model):
    sp = ndb.StructuredProperty(SP)

with client.context():
    obj = SomeModel(sp=SP(some_field="foo"))
    key = obj.put()

with client.context():
    other_obj = key.get()

    print(obj)
    # SomeModel(key=Key(u'SomeModel', 2L), sp=SP(some_field=u'foo'))
    print(other_obj)
    # SomeModel(key=Key(u'SomeModel', 2L), sp=SP(key=Key('SP', None), some_field=u'foo'))

    assert obj == other_obj
    # AssertionErrror...

Stack trace

N/A, it raises on my assertion statement, not deep in the NDB library.

@product-auto-label product-auto-label bot added the api: datastore Issues related to the googleapis/python-ndb API. label Nov 29, 2022
@aetherknight
Copy link
Author

When an entity with a structured property is fetched later/in a different context, NDB auto-adds a key to the structured property's model instance. However, it does not do so when initially creating the instance.

This issue is mainly a nuisance/annoyance when trying to write tests -- We often do test setup in one context, but then our API code creates a new context as a part of its WSGI middleware. We have mocks/spies that make assertions and want to ensure that a particular entity is passed in to some callable, but the objects differ due to the presence of the empty key n the structured properties.

(we have already updated our tests to work around this)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api: datastore Issues related to the googleapis/python-ndb API.
Projects
None yet
Development

No branches or pull requests

1 participant