Skip to content

Commit

Permalink
_prepare_for_put was not being called at entity level (#138)
Browse files Browse the repository at this point in the history
  • Loading branch information
cguardia authored Jul 9, 2019
1 parent 0d3697c commit 681a701
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/google/cloud/ndb/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -4855,12 +4855,18 @@ def put(self):

return self._key

self._prepare_for_put()
future = put(self)
future.add_done_callback(self._post_put_hook)
return future

put_async = _put_async

def _prepare_for_put(self):
if self._properties:
for prop in self._properties.values():
prop._prepare_for_put(self)

@classmethod
def _query(
cls,
Expand Down
19 changes: 19 additions & 0 deletions tests/system/test_crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"""
System tests for Create, Update, Delete. (CRUD)
"""
import datetime
import functools
import operator
import threading
Expand Down Expand Up @@ -581,3 +582,21 @@ class Cat(Feline):
assert isinstance(retrieved, Cat)

dispose_of(key._key)


@pytest.mark.usefixtures("client_context")
def test_insert_autonow_property(dispose_of):
class SomeKind(ndb.Model):
foo = ndb.StringProperty()
created_at = ndb.DateTimeProperty(indexed=True, auto_now_add=True)
updated_at = ndb.DateTimeProperty(indexed=True, auto_now=True)

entity = SomeKind(foo="bar")
key = entity.put()

retrieved = key.get()

assert isinstance(retrieved.created_at, datetime.datetime)
assert isinstance(retrieved.updated_at, datetime.datetime)

dispose_of(key._key)
13 changes: 13 additions & 0 deletions tests/unit/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -3684,6 +3684,19 @@ def test__put_async(_datastore_api):
entity_pb, _options.Options()
)

@staticmethod
@pytest.mark.usefixtures("in_context")
def test__prepare_for_put():
class Simple(model.Model):
foo = model.DateTimeProperty()

entity = Simple(foo=datetime.datetime.now())
with unittest.mock.patch.object(
entity._properties["foo"], "_prepare_for_put"
) as patched:
entity._prepare_for_put()
patched.assert_called_once()

@staticmethod
@pytest.mark.usefixtures("in_context")
@unittest.mock.patch("google.cloud.ndb.model._datastore_api")
Expand Down

0 comments on commit 681a701

Please sign in to comment.