Skip to content

Commit 1801ba2

Browse files
authored
feat: use DatetimeWithNanoseconds throughout library (#116)
* chore: update minimum version of protoplus to ensure DatetimeWithNanoseconds availability * feat: Incorporate nanoseconds back into components, such as hashing * blacken * remove unused imports
1 parent 178fa2c commit 1801ba2

File tree

8 files changed

+18
-30
lines changed

8 files changed

+18
-30
lines changed

‎google/cloud/firestore_v1/base_document.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -243,12 +243,8 @@ def __eq__(self, other):
243243
return self._reference == other._reference and self._data == other._data
244244

245245
def __hash__(self):
246-
# TODO(microgen, https://github.com/googleapis/proto-plus-python/issues/38):
247-
# maybe add datetime_with_nanos to protoplus, revisit
248-
# seconds = self.update_time.seconds
249-
# nanos = self.update_time.nanos
250246
seconds = int(self.update_time.timestamp())
251-
nanos = 0
247+
nanos = self.update_time.nanosecond
252248
return hash(self._reference) + hash(seconds) + hash(nanos)
253249

254250
@property

‎google/cloud/firestore_v1/watch.py

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -565,13 +565,7 @@ def push(self, read_time, next_resume_token):
565565
key = functools.cmp_to_key(self._comparator)
566566
keys = sorted(updated_tree.keys(), key=key)
567567

568-
self._snapshot_callback(
569-
keys,
570-
appliedChanges,
571-
read_time
572-
# TODO(microgen): now a datetime
573-
# datetime.datetime.fromtimestamp(read_time.seconds, pytz.utc),
574-
)
568+
self._snapshot_callback(keys, appliedChanges, read_time)
575569
self.has_pushed = True
576570

577571
self.doc_tree = updated_tree

‎setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
"google-cloud-core >= 1.0.3, < 2.0dev",
3030
"pytz",
3131
"libcst >= 0.2.5",
32-
"proto-plus >= 0.4.0",
32+
"proto-plus >= 1.3.0",
3333
]
3434
extras = {}
3535

‎tests/system/test_system.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,6 @@ def test_update_document(client, cleanup):
340340
document.update({"bad": "time-past"}, option=option4)
341341

342342
# 6. Call ``update()`` with invalid (in future) "last timestamp" option.
343-
# TODO(microgen): start using custom datetime with nanos in protoplus?
344343
timestamp_pb = _datetime_to_pb_timestamp(snapshot4.update_time)
345344
timestamp_pb.seconds += 3600
346345

‎tests/unit/v1/test_async_batch.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,7 @@ async def test_commit(self):
6767
write_results = await batch.commit()
6868
self.assertEqual(write_results, list(commit_response.write_results))
6969
self.assertEqual(batch.write_results, write_results)
70-
# TODO(microgen): v2: commit time is already a datetime, though not with nano
71-
# self.assertEqual(batch.commit_time, timestamp)
70+
self.assertEqual(batch.commit_time.timestamp_pb(), timestamp)
7271
# Make sure batch has no more "changes".
7372
self.assertEqual(batch._write_pbs, [])
7473

@@ -108,8 +107,7 @@ async def test_as_context_mgr_wo_error(self):
108107
write_pbs = batch._write_pbs[::]
109108

110109
self.assertEqual(batch.write_results, list(commit_response.write_results))
111-
# TODO(microgen): v2: commit time is already a datetime, though not with nano
112-
# self.assertEqual(batch.commit_time, timestamp)
110+
self.assertEqual(batch.commit_time.timestamp_pb(), timestamp)
113111
# Make sure batch has no more "changes".
114112
self.assertEqual(batch._write_pbs, [])
115113

‎tests/unit/v1/test_base_client.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -300,10 +300,9 @@ def test_found(self):
300300
self.assertIs(snapshot._reference, mock.sentinel.reference)
301301
self.assertEqual(snapshot._data, {"foo": 1.5, "bar": u"skillz"})
302302
self.assertTrue(snapshot._exists)
303-
# TODO(microgen): v2: datetime with nanos implementation needed.
304-
# self.assertEqual(snapshot.read_time, read_time)
305-
# self.assertEqual(snapshot.create_time, create_time)
306-
# self.assertEqual(snapshot.update_time, update_time)
303+
self.assertEqual(snapshot.read_time.timestamp_pb(), read_time)
304+
self.assertEqual(snapshot.create_time.timestamp_pb(), create_time)
305+
self.assertEqual(snapshot.update_time.timestamp_pb(), update_time)
307306

308307
def test_missing(self):
309308
from google.cloud.firestore_v1.document import DocumentReference

‎tests/unit/v1/test_base_document.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
import unittest
1616

1717
import mock
18-
import datetime
19-
import pytz
18+
from proto.datetime_helpers import DatetimeWithNanoseconds
19+
from google.protobuf import timestamp_pb2
2020

2121

2222
class TestBaseDocumentReference(unittest.TestCase):
@@ -274,11 +274,15 @@ def test___hash__(self):
274274
client.__hash__.return_value = 234566789
275275
reference = self._make_reference("hi", "bye", client=client)
276276
data = {"zoop": 83}
277-
update_time = datetime.datetime.fromtimestamp(123456, pytz.utc)
277+
update_time = DatetimeWithNanoseconds.from_timestamp_pb(
278+
timestamp_pb2.Timestamp(seconds=123456, nanos=123456789)
279+
)
278280
snapshot = self._make_one(
279281
reference, data, True, None, mock.sentinel.create_time, update_time
280282
)
281-
self.assertEqual(hash(snapshot), hash(reference) + hash(123456) + hash(0))
283+
self.assertEqual(
284+
hash(snapshot), hash(reference) + hash(123456) + hash(123456789)
285+
)
282286

283287
def test__client_property(self):
284288
reference = self._make_reference(

‎tests/unit/v1/test_batch.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,7 @@ def test_commit(self):
6464
write_results = batch.commit()
6565
self.assertEqual(write_results, list(commit_response.write_results))
6666
self.assertEqual(batch.write_results, write_results)
67-
# TODO(microgen): v2: commit time is already a datetime, though not with nano
68-
# self.assertEqual(batch.commit_time, timestamp)
67+
self.assertEqual(batch.commit_time.timestamp_pb(), timestamp)
6968
# Make sure batch has no more "changes".
7069
self.assertEqual(batch._write_pbs, [])
7170

@@ -104,8 +103,7 @@ def test_as_context_mgr_wo_error(self):
104103
write_pbs = batch._write_pbs[::]
105104

106105
self.assertEqual(batch.write_results, list(commit_response.write_results))
107-
# TODO(microgen): v2: commit time is already a datetime, though not with nano
108-
# self.assertEqual(batch.commit_time, timestamp)
106+
self.assertEqual(batch.commit_time.timestamp_pb(), timestamp)
109107
# Make sure batch has no more "changes".
110108
self.assertEqual(batch._write_pbs, [])
111109

0 commit comments

Comments
 (0)