def test_add_document_with_different_read_times(self): bundle = FirestoreBundle("test") doc = _test_helpers.build_document_snapshot( client=_test_helpers.make_client(), data={"version": 1}, read_time=_test_helpers.build_test_timestamp(second=1), ) # Create another reference to the same document, but with new # data and a more recent `read_time` doc_refreshed = _test_helpers.build_document_snapshot( client=_test_helpers.make_client(), data={"version": 2}, read_time=_test_helpers.build_test_timestamp(second=2), ) bundle.add_document(doc) self.assertEqual( bundle.documents[self.doc_key].snapshot._data, {"version": 1}, ) bundle.add_document(doc_refreshed) self.assertEqual( bundle.documents[self.doc_key].snapshot._data, {"version": 2}, )
def test_add_document(self): from google.cloud.firestore_bundle import FirestoreBundle bundle = FirestoreBundle("test") doc = _test_helpers.build_document_snapshot( client=_test_helpers.make_client()) bundle.add_document(doc) assert bundle.documents[self.doc_key].snapshot == doc
def test_add_older_document(self): bundle = FirestoreBundle("test") new_doc = _test_helpers.build_document_snapshot( data={"version": 2}, client=_test_helpers.make_client(), read_time=Timestamp(seconds=1, nanos=2), ) bundle.add_document(new_doc) self.assertEqual( bundle.documents[self.doc_key].snapshot._data["version"], 2) # Builds the same ID by default old_doc = _test_helpers.build_document_snapshot( data={"version": 1}, client=_test_helpers.make_client(), read_time=Timestamp(seconds=1, nanos=1), ) bundle.add_document(old_doc) self.assertEqual( bundle.documents[self.doc_key].snapshot._data["version"], 2)
def test_add_older_document(self): from google.protobuf.timestamp_pb2 import Timestamp # type: ignore from google.cloud.firestore_bundle import FirestoreBundle bundle = FirestoreBundle("test") new_doc = _test_helpers.build_document_snapshot( data={"version": 2}, client=_test_helpers.make_client(), read_time=Timestamp(seconds=1, nanos=2), ) bundle.add_document(new_doc) assert bundle.documents[self.doc_key].snapshot._data["version"] == 2 # Builds the same ID by default old_doc = _test_helpers.build_document_snapshot( data={"version": 1}, client=_test_helpers.make_client(), read_time=Timestamp(seconds=1, nanos=1), ) bundle.add_document(old_doc) assert bundle.documents[self.doc_key].snapshot._data["version"] == 2
def create_and_build_bundle(): _setup_bundle() # [START firestore_create_and_build_bundle] from google.cloud import firestore from google.cloud.firestore_bundle import FirestoreBundle db = firestore.Client() bundle = FirestoreBundle("latest-stories") doc_snapshot = db.collection("stories").document("news-item").get() query = db.collection("stories")._query() # Build the bundle # Note how `query` is named "latest-stories-query" bundle_buffer: str = bundle.add_document(doc_snapshot).add_named_query( "latest-stories-query", query, ).build() # [END firestore_create_and_build_bundle] return bundle, bundle_buffer
def test_add_document(self): bundle = FirestoreBundle("test") doc = _test_helpers.build_document_snapshot( client=_test_helpers.make_client()) bundle.add_document(doc) self.assertEqual(bundle.documents[self.doc_key].snapshot, doc)