From 56b7e98d825482bb0455cea3ac37028329a4f73c Mon Sep 17 00:00:00 2001 From: Chris Wilcox Date: Mon, 30 Dec 2019 09:16:30 -0800 Subject: [PATCH 01/21] fix: harden the wait for consistency for firestore multiple listen snippet --- firestore/cloud-client/snippets.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/firestore/cloud-client/snippets.py b/firestore/cloud-client/snippets.py index abcb7157348..fa328e5f6d4 100644 --- a/firestore/cloud-client/snippets.py +++ b/firestore/cloud-client/snippets.py @@ -702,7 +702,13 @@ def on_snapshot(col_snapshot, changes, read_time): u'population': 860000 } db.collection(u'cities').document(u'SF').set(data) - sleep(1) + + # Wait for 'SF' to be in the doc_map for up to 60 seconds. + waited = 0 + while waited < 60 and not [x for x in query_watch.doc_map.keys() if x.endswith('/SF')]: + waited += 1 + sleep(1) + query_watch.unsubscribe() From 68c1cfed97d3931fed18670b24c43e03713b60af Mon Sep 17 00:00:00 2001 From: Chris Wilcox Date: Mon, 30 Dec 2019 09:41:23 -0800 Subject: [PATCH 02/21] fix: formatting --- firestore/cloud-client/snippets.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/firestore/cloud-client/snippets.py b/firestore/cloud-client/snippets.py index fa328e5f6d4..f2c088e4114 100644 --- a/firestore/cloud-client/snippets.py +++ b/firestore/cloud-client/snippets.py @@ -702,11 +702,12 @@ def on_snapshot(col_snapshot, changes, read_time): u'population': 860000 } db.collection(u'cities').document(u'SF').set(data) - + # Wait for 'SF' to be in the doc_map for up to 60 seconds. waited = 0 - while waited < 60 and not [x for x in query_watch.doc_map.keys() if x.endswith('/SF')]: - waited += 1 + while waited < 60 and not [ + x for x in query_watch.doc_map.keys() if x.endswith("/SF") + ]: waited += 1 sleep(1) query_watch.unsubscribe() From 2a876acdb8b740aab7e8df4f68fed713968ee789 Mon Sep 17 00:00:00 2001 From: Tianzi Cai Date: Mon, 30 Dec 2019 09:05:24 -0800 Subject: [PATCH 03/21] add a setup step in CLI (#2611) --- appengine/standard_python37/pubsub/README.md | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/appengine/standard_python37/pubsub/README.md b/appengine/standard_python37/pubsub/README.md index 029077281d6..4b0a1499dd3 100644 --- a/appengine/standard_python37/pubsub/README.md +++ b/appengine/standard_python37/pubsub/README.md @@ -13,7 +13,13 @@ Before you can run or deploy the sample, you will need to do the following: 1. Enable the Cloud Pub/Sub API in the [Google Developers Console](https://console.developers.google.com/project/_/apiui/apiview/pubsub/overview). -2. Create a topic and subscription. Your push auth service account must have Service Account Token Creator Role assigned, which can be done in the Cloud Console [IAM & admin](https://console.cloud.google.com/iam-admin/iam) UI. `--push-auth-token-audience` is optional. If set, remember to modify the audience field check in `main.py` (line 88). +1. Allow Cloud Pub/Sub to create authentication tokens in your project. + + $ gcloud projects add-iam-policy-binding [your-project-id] \ + --member=serviceAccount:service-[your-project-number]@gcp-sa-pubsub.iam.gserviceaccount.com \ + --role=roles/iam.serviceAccountTokenCreator + +1. Create a topic and subscription. The `--push-auth-service-account` flag activates the Pub/Sub push functionality for Authentication and Authorization. Pub/Sub messages pushed to your endpoint will carry the identity of this service account. You may use an existing service account or create a new one. The `--push-auth-token-audience` flag is optional; if set, remember to modify the audience field check in `main.py`. $ gcloud pubsub topics create [your-topic-name] $ gcloud beta pubsub subscriptions create [your-subscription-name] \ @@ -21,10 +27,10 @@ Before you can run or deploy the sample, you will need to do the following: --push-endpoint=\ https://[your-app-id].appspot.com/_ah/push-handlers/receive_messages?token=[your-token] \ --ack-deadline=30 \ - --push-auth-service-account=[your-service-account-email] \ + --push-auth-service-account=[your-service-account] \ --push-auth-token-audience=example.com -3. Update the environment variables in ``app.yaml``. +1. Update the environment variables in ``app.yaml``. ## Running locally From 30b72ecfc8e9b0b8f5a58264198be70351ff77b3 Mon Sep 17 00:00:00 2001 From: Chris Wilcox Date: Mon, 30 Dec 2019 09:42:41 -0800 Subject: [PATCH 04/21] fix: formatting --- firestore/cloud-client/snippets.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/firestore/cloud-client/snippets.py b/firestore/cloud-client/snippets.py index f2c088e4114..00334f30b5b 100644 --- a/firestore/cloud-client/snippets.py +++ b/firestore/cloud-client/snippets.py @@ -707,7 +707,8 @@ def on_snapshot(col_snapshot, changes, read_time): waited = 0 while waited < 60 and not [ x for x in query_watch.doc_map.keys() if x.endswith("/SF") - ]: waited += 1 + ]: + waited += 1 sleep(1) query_watch.unsubscribe() From 07d8516b776f2671b30f05f7d0e2944a13ca323a Mon Sep 17 00:00:00 2001 From: Chris Wilcox Date: Mon, 30 Dec 2019 09:49:57 -0800 Subject: [PATCH 05/21] fix: formatting --- firestore/cloud-client/snippets.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/firestore/cloud-client/snippets.py b/firestore/cloud-client/snippets.py index 00334f30b5b..097e5fa3b8e 100644 --- a/firestore/cloud-client/snippets.py +++ b/firestore/cloud-client/snippets.py @@ -707,7 +707,7 @@ def on_snapshot(col_snapshot, changes, read_time): waited = 0 while waited < 60 and not [ x for x in query_watch.doc_map.keys() if x.endswith("/SF") - ]: + ]: waited += 1 sleep(1) From dc5881a556fc71ed5a2bb9e347fad2849106d595 Mon Sep 17 00:00:00 2001 From: Chris Wilcox Date: Mon, 30 Dec 2019 11:06:13 -0800 Subject: [PATCH 06/21] fix: add setup to snippets for tests --- firestore/cloud-client/snippets.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/firestore/cloud-client/snippets.py b/firestore/cloud-client/snippets.py index 097e5fa3b8e..ef6d598c1bf 100644 --- a/firestore/cloud-client/snippets.py +++ b/firestore/cloud-client/snippets.py @@ -304,6 +304,9 @@ def structure_subcollection_ref(): def update_doc(): db = firestore.Client() + doc_ref = db.collection(u'cities').document(u'DC') + doc_ref.set({}) + # [START update_doc] city_ref = db.collection(u'cities').document(u'DC') @@ -314,6 +317,9 @@ def update_doc(): def update_doc_array(): db = firestore.Client() + doc_ref = db.collection(u'cities').document(u'DC') + doc_ref.set({}) + # [START fs_update_doc_array] city_ref = db.collection(u'cities').document(u'DC') @@ -329,6 +335,9 @@ def update_doc_array(): def update_multiple(): db = firestore.Client() + doc_ref = db.collection(u'cities').document(u'DC') + doc_ref.set({}) + # [START update_multiple] doc_ref = db.collection(u'cities').document(u'DC') @@ -754,6 +763,7 @@ def on_snapshot(col_snapshot, changes, read_time): u'capital': False, u'population': 90000 }) + sleep(1) # Delete document mtv_document.delete() From a113bee22d269a98ba13c7c6c5ac6e4fcdae3a39 Mon Sep 17 00:00:00 2001 From: Chris Wilcox Date: Mon, 30 Dec 2019 11:28:14 -0800 Subject: [PATCH 07/21] fix: rename sydney_query to denver_query as it refers to denver --- firestore/cloud-client/snippets.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/firestore/cloud-client/snippets.py b/firestore/cloud-client/snippets.py index ef6d598c1bf..3187a9bd28c 100644 --- a/firestore/cloud-client/snippets.py +++ b/firestore/cloud-client/snippets.py @@ -499,12 +499,12 @@ def compound_query_valid_multi_clause(): # [START compound_query_valid_multi_clause] cities_ref = db.collection(u'cities') - sydney_query = cities_ref.where( + denver_query = cities_ref.where( u'state', u'==', u'CO').where(u'name', u'==', u'Denver') large_us_cities_query = cities_ref.where( u'state', u'==', u'CA').where(u'population', u'>', 1000000) # [END compound_query_valid_multi_clause] - print(sydney_query) + print(denver_query) print(large_us_cities_query) From ddc9768b7bb59dc28c7fe216fbfe7abe4b5461ce Mon Sep 17 00:00:00 2001 From: Chris Wilcox Date: Mon, 30 Dec 2019 11:39:10 -0800 Subject: [PATCH 08/21] fix: Use stream instead of get in samples --- firestore/cloud-client/snippets.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/firestore/cloud-client/snippets.py b/firestore/cloud-client/snippets.py index 3187a9bd28c..492d00be131 100644 --- a/firestore/cloud-client/snippets.py +++ b/firestore/cloud-client/snippets.py @@ -819,7 +819,7 @@ def delete_full_collection(): # [START delete_full_collection] def delete_collection(coll_ref, batch_size): - docs = coll_ref.limit(batch_size).get() + docs = coll_ref.limit(batch_size).stream() deleted = 0 for doc in docs: From fda54c86a48c072ce819d981eb6b26b32c028c41 Mon Sep 17 00:00:00 2001 From: Chris Wilcox Date: Mon, 30 Dec 2019 11:55:00 -0800 Subject: [PATCH 09/21] fix: stop deleting LA, delete DEN instead --- firestore/cloud-client/snippets.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/firestore/cloud-client/snippets.py b/firestore/cloud-client/snippets.py index 492d00be131..0a4e9ddb639 100644 --- a/firestore/cloud-client/snippets.py +++ b/firestore/cloud-client/snippets.py @@ -450,9 +450,9 @@ def update_data_batch(): sf_ref = db.collection(u'cities').document(u'SF') batch.update(sf_ref, {u'population': 1000000}) - # Delete LA - la_ref = db.collection(u'cities').document(u'LA') - batch.delete(la_ref) + # Delete DEN + den_ref = db.collection(u'cities').document(u'DEN') + batch.delete(den_ref) # Commit the batch batch.commit() @@ -754,6 +754,7 @@ def on_snapshot(col_snapshot, changes, read_time): u'capital': False, u'population': 80000 }) + sleep(1) # Modifying document mtv_document.update({ From a48030df803a00e33f82989bdf7705901896733e Mon Sep 17 00:00:00 2001 From: Chris Wilcox Date: Mon, 30 Dec 2019 12:46:16 -0800 Subject: [PATCH 10/21] fix: move delete tests to end of file --- firestore/cloud-client/snippets_test.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/firestore/cloud-client/snippets_test.py b/firestore/cloud-client/snippets_test.py index 8a6a6d40dfb..0c4295650d4 100644 --- a/firestore/cloud-client/snippets_test.py +++ b/firestore/cloud-client/snippets_test.py @@ -250,15 +250,6 @@ def test_cursor_multiple_conditions(): snippets.cursor_multiple_conditions() -def test_delete_single_doc(): - snippets.delete_single_doc() - - -def test_delete_field(db): - db.collection('cities').document('Beijing').set({'capital': True}) - snippets.delete_field() - - def test_listen_document(capsys): snippets.listen_document() out, _ = capsys.readouterr() @@ -280,6 +271,15 @@ def test_listen_for_changes(capsys): assert 'Removed city: MTV' in out +def test_delete_single_doc(): + snippets.delete_single_doc() + + +def test_delete_field(db): + db.collection('cities').document('Beijing').set({'capital': True}) + snippets.delete_field() + + def test_delete_full_collection(): snippets.delete_full_collection() From 5c5dbe36d9843bb7c166e5fb62e78b425128a581 Mon Sep 17 00:00:00 2001 From: Chris Wilcox Date: Mon, 30 Dec 2019 13:54:25 -0800 Subject: [PATCH 11/21] fix: move BJ to top to work with snapshot cursors --- firestore/cloud-client/snippets.py | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/firestore/cloud-client/snippets.py b/firestore/cloud-client/snippets.py index 0a4e9ddb639..4a1ccabad89 100644 --- a/firestore/cloud-client/snippets.py +++ b/firestore/cloud-client/snippets.py @@ -159,6 +159,8 @@ def add_example_data(): db = firestore.Client() # [START add_example_data] cities_ref = db.collection(u'cities') + cities_ref.document(u'BJ').set( + City(u'Beijing', None, u'China', True, 21500000, [u'hebei']).to_dict()) cities_ref.document(u'SF').set( City(u'San Francisco', u'CA', u'USA', False, 860000, [u'west_coast', u'norcal']).to_dict()) @@ -171,8 +173,6 @@ def add_example_data(): cities_ref.document(u'TOK').set( City(u'Tokyo', None, u'Japan', True, 9000000, [u'kanto', u'honshu']).to_dict()) - cities_ref.document(u'BJ').set( - City(u'Beijing', None, u'China', True, 21500000, [u'hebei']).to_dict()) # [END add_example_data] @@ -304,8 +304,9 @@ def structure_subcollection_ref(): def update_doc(): db = firestore.Client() - doc_ref = db.collection(u'cities').document(u'DC') - doc_ref.set({}) + db.collection(u'cities').document(u'DC').set( + City(u'Washington D.C.', None, u'USA', True, 680000, + [u'east_coast']).to_dict()) # [START update_doc] city_ref = db.collection(u'cities').document(u'DC') @@ -317,8 +318,9 @@ def update_doc(): def update_doc_array(): db = firestore.Client() - doc_ref = db.collection(u'cities').document(u'DC') - doc_ref.set({}) + db.collection(u'cities').document(u'DC').set( + City(u'Washington D.C.', None, u'USA', True, 680000, + [u'east_coast']).to_dict()) # [START fs_update_doc_array] city_ref = db.collection(u'cities').document(u'DC') @@ -335,8 +337,9 @@ def update_doc_array(): def update_multiple(): db = firestore.Client() - doc_ref = db.collection(u'cities').document(u'DC') - doc_ref.set({}) + db.collection(u'cities').document(u'DC').set( + City(u'Washington D.C.', None, u'USA', True, 680000, + [u'east_coast']).to_dict()) # [START update_multiple] doc_ref = db.collection(u'cities').document(u'DC') From 102997c6011cfaf0c9a265aba287315539802adb Mon Sep 17 00:00:00 2001 From: Chris Wilcox Date: Mon, 30 Dec 2019 14:19:06 -0800 Subject: [PATCH 12/21] fix: Beijing -> BJ --- firestore/cloud-client/snippets_test.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/firestore/cloud-client/snippets_test.py b/firestore/cloud-client/snippets_test.py index 0c4295650d4..ec2c0c774e0 100644 --- a/firestore/cloud-client/snippets_test.py +++ b/firestore/cloud-client/snippets_test.py @@ -276,7 +276,7 @@ def test_delete_single_doc(): def test_delete_field(db): - db.collection('cities').document('Beijing').set({'capital': True}) + db.collection('cities').document('BJ').set({'capital': True}) snippets.delete_field() From 1acacb8b70fd8b0330afd0fd2b26197e8d9d54f3 Mon Sep 17 00:00:00 2001 From: Chris Wilcox Date: Mon, 30 Dec 2019 14:46:20 -0800 Subject: [PATCH 13/21] during delete collection delete all collections created for these tests --- firestore/cloud-client/snippets.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/firestore/cloud-client/snippets.py b/firestore/cloud-client/snippets.py index 4a1ccabad89..16c1927bbc2 100644 --- a/firestore/cloud-client/snippets.py +++ b/firestore/cloud-client/snippets.py @@ -836,6 +836,10 @@ def delete_collection(coll_ref, batch_size): # [END delete_full_collection] delete_collection(db.collection(u'cities'), 10) + delete_collection(db.collection(u'data'), 10) + delete_collection(db.collection(u'dc_samples'), 10) + delete_collection(db.collection(u'objects'), 10) + delete_collection(db.collection(u'users'), 10) def collection_group_query(db): From 0102d894fffe8e137820cf94f24d80878ebafd2f Mon Sep 17 00:00:00 2001 From: Chris Wilcox Date: Mon, 30 Dec 2019 15:37:10 -0800 Subject: [PATCH 14/21] fix: add cleanup test to distributed counters --- firestore/cloud-client/distributed_counters_test.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/firestore/cloud-client/distributed_counters_test.py b/firestore/cloud-client/distributed_counters_test.py index 7200a445f36..27a272c2b69 100644 --- a/firestore/cloud-client/distributed_counters_test.py +++ b/firestore/cloud-client/distributed_counters_test.py @@ -47,3 +47,15 @@ def test_distributed_counters(fs_client): counter.increment_counter(doc_ref) counter.increment_counter(doc_ref) assert counter.get_count(doc_ref) == 2 + + +def test_distributed_counters_cleanup(fs_client): + col = fs_client.collection("dc_samples") + doc_ref = col.document("distributed_counter") + + shards = doc_ref.collection("shards").list_documents() + shards_list = [shard for shard in shards] + for shard in shards_list: + shard.delete() + + doc_ref.delete() From 7c56405a1fbfed4b67efea21d8f37065c07887d2 Mon Sep 17 00:00:00 2001 From: Chris Wilcox Date: Mon, 30 Dec 2019 15:37:30 -0800 Subject: [PATCH 15/21] wrap db.collections calls to use unique names --- firestore/cloud-client/snippets.py | 115 +++++++++++++----------- firestore/cloud-client/snippets_test.py | 2 +- 2 files changed, 64 insertions(+), 53 deletions(-) diff --git a/firestore/cloud-client/snippets.py b/firestore/cloud-client/snippets.py index 16c1927bbc2..cadc90d2dd8 100644 --- a/firestore/cloud-client/snippets.py +++ b/firestore/cloud-client/snippets.py @@ -13,10 +13,12 @@ import datetime from time import sleep +import uuid from google.cloud import firestore import google.cloud.exceptions +UNIQUE_STRING = str(uuid.uuid4()).split("-")[0] def quickstart_new_instance(): # [START quickstart_new_instance] @@ -29,8 +31,18 @@ def quickstart_new_instance(): return db -def quickstart_add_data_one(): +def _make_one(): + # monkeypatch the collection method so the collection names can be unique for each test run. db = firestore.Client() + def modified_collection_creation(collection_name, *args, **kwargs): + unique_collection_name = "{}-{}".format(collection_name, UNIQUE_STRING) + return db._collection(unique_collection_name, *args, **kwargs) + db._collection = db.collection + db.collection = modified_collection_creation + return db + +def quickstart_add_data_one(): + db = _make_one() # [START quickstart_add_data_one] doc_ref = db.collection(u'users').document(u'alovelace') doc_ref.set({ @@ -42,7 +54,7 @@ def quickstart_add_data_one(): def quickstart_add_data_two(): - db = firestore.Client() + db = _make_one() # [START quickstart_add_data_two] doc_ref = db.collection(u'users').document(u'aturing') doc_ref.set({ @@ -55,7 +67,7 @@ def quickstart_add_data_two(): def quickstart_get_collection(): - db = firestore.Client() + db = _make_one() # [START quickstart_get_collection] users_ref = db.collection(u'users') docs = users_ref.stream() @@ -66,7 +78,7 @@ def quickstart_get_collection(): def add_from_dict(): - db = firestore.Client() + db = _make_one() # [START add_from_dict] data = { u'name': u'Los Angeles', @@ -80,7 +92,7 @@ def add_from_dict(): def add_data_types(): - db = firestore.Client() + db = _make_one() # [START add_data_types] data = { u'stringExample': u'Hello, World!', @@ -156,7 +168,7 @@ def __repr__(self): def add_example_data(): - db = firestore.Client() + db = _make_one() # [START add_example_data] cities_ref = db.collection(u'cities') cities_ref.document(u'BJ').set( @@ -177,7 +189,7 @@ def add_example_data(): def add_custom_class_with_id(): - db = firestore.Client() + db = _make_one() # [START add_custom_class_with_id] city = City(name=u'Los Angeles', state=u'CA', country=u'USA') db.collection(u'cities').document(u'LA').set(city.to_dict()) @@ -185,7 +197,7 @@ def add_custom_class_with_id(): def add_data_with_id(): - db = firestore.Client() + db = _make_one() data = {} # [START add_data_with_id] db.collection(u'cities').document(u'new-city-id').set(data) @@ -193,7 +205,7 @@ def add_data_with_id(): def add_custom_class_generated_id(): - db = firestore.Client() + db = _make_one() # [START add_custom_class_generated_id] city = City(name=u'Tokyo', state=None, country=u'Japan') db.collection(u'cities').add(city.to_dict()) @@ -201,7 +213,7 @@ def add_custom_class_generated_id(): def add_new_doc(): - db = firestore.Client() + db = _make_one() # [START add_new_doc] new_city_ref = db.collection(u'cities').document() @@ -213,7 +225,7 @@ def add_new_doc(): def get_check_exists(): - db = firestore.Client() + db = _make_one() # [START get_check_exists] doc_ref = db.collection(u'cities').document(u'SF') @@ -226,7 +238,7 @@ def get_check_exists(): def get_custom_class(): - db = firestore.Client() + db = _make_one() # [START get_custom_class] doc_ref = db.collection(u'cities').document(u'BJ') @@ -237,7 +249,7 @@ def get_custom_class(): def get_simple_query(): - db = firestore.Client() + db = _make_one() # [START get_simple_query] docs = db.collection(u'cities').where(u'capital', u'==', True).stream() @@ -247,7 +259,7 @@ def get_simple_query(): def array_contains_filter(): - db = firestore.Client() + db = _make_one() # [START fs_array_contains_filter] cities_ref = db.collection(u'cities') @@ -259,7 +271,7 @@ def array_contains_filter(): def get_full_collection(): - db = firestore.Client() + db = _make_one() # [START get_full_collection] docs = db.collection(u'cities').stream() @@ -269,7 +281,7 @@ def get_full_collection(): def structure_doc_ref(): - db = firestore.Client() + db = _make_one() # [START structure_doc_ref] a_lovelace_ref = db.collection(u'users').document(u'alovelace') # [END structure_doc_ref] @@ -277,7 +289,7 @@ def structure_doc_ref(): def structure_collection_ref(): - db = firestore.Client() + db = _make_one() # [START structure_collection_ref] users_ref = db.collection(u'users') # [END structure_collection_ref] @@ -285,7 +297,7 @@ def structure_collection_ref(): def structure_doc_ref_alternate(): - db = firestore.Client() + db = _make_one() # [START structure_doc_ref_alternate] a_lovelace_ref = db.document(u'users/alovelace') # [END structure_doc_ref_alternate] @@ -294,7 +306,7 @@ def structure_doc_ref_alternate(): def structure_subcollection_ref(): - db = firestore.Client() + db = _make_one() # [START structure_subcollection_ref] room_a_ref = db.collection(u'rooms').document(u'roomA') message_ref = room_a_ref.collection(u'messages').document(u'message1') @@ -303,7 +315,7 @@ def structure_subcollection_ref(): def update_doc(): - db = firestore.Client() + db = _make_one() db.collection(u'cities').document(u'DC').set( City(u'Washington D.C.', None, u'USA', True, 680000, [u'east_coast']).to_dict()) @@ -317,7 +329,7 @@ def update_doc(): def update_doc_array(): - db = firestore.Client() + db = _make_one() db.collection(u'cities').document(u'DC').set( City(u'Washington D.C.', None, u'USA', True, 680000, [u'east_coast']).to_dict()) @@ -336,7 +348,7 @@ def update_doc_array(): def update_multiple(): - db = firestore.Client() + db = _make_one() db.collection(u'cities').document(u'DC').set( City(u'Washington D.C.', None, u'USA', True, 680000, [u'east_coast']).to_dict()) @@ -353,7 +365,7 @@ def update_multiple(): def update_create_if_missing(): - db = firestore.Client() + db = _make_one() # [START update_create_if_missing] city_ref = db.collection(u'cities').document(u'BJ') @@ -364,7 +376,7 @@ def update_create_if_missing(): def update_nested(): - db = firestore.Client() + db = _make_one() # [START update_nested] # Create an initial document to update frank_ref = db.collection(u'users').document(u'frank') @@ -387,7 +399,7 @@ def update_nested(): def update_server_timestamp(): - db = firestore.Client() + db = _make_one() # [START update_server_timestamp] city_ref = db.collection(u'objects').document(u'some-id') city_ref.update({ @@ -397,7 +409,7 @@ def update_server_timestamp(): def update_data_transaction(): - db = firestore.Client() + db = _make_one() # [START update_data_transaction] transaction = db.transaction() city_ref = db.collection(u'cities').document(u'SF') @@ -414,7 +426,7 @@ def update_in_transaction(transaction, city_ref): def update_data_transaction_result(): - db = firestore.Client() + db = _make_one() # [START update_data_transaction_result] transaction = db.transaction() city_ref = db.collection(u'cities').document(u'SF') @@ -441,7 +453,7 @@ def update_in_transaction(transaction, city_ref): def update_data_batch(): - db = firestore.Client() + db = _make_one() # [START update_data_batch] batch = db.batch() @@ -463,7 +475,7 @@ def update_data_batch(): def compound_query_example(): - db = firestore.Client() + db = _make_one() # [START compound_query_example] # Create a reference to the cities collection cities_ref = db.collection(u'cities') @@ -476,7 +488,7 @@ def compound_query_example(): def compound_query_simple(): - db = firestore.Client() + db = _make_one() # [START compound_query_simple] cities_ref = db.collection(u'cities') @@ -487,7 +499,7 @@ def compound_query_simple(): def compound_query_single_clause(): - db = firestore.Client() + db = _make_one() # [START compound_query_single_clause] cities_ref = db.collection(u'cities') @@ -498,7 +510,7 @@ def compound_query_single_clause(): def compound_query_valid_multi_clause(): - db = firestore.Client() + db = _make_one() # [START compound_query_valid_multi_clause] cities_ref = db.collection(u'cities') @@ -512,7 +524,7 @@ def compound_query_valid_multi_clause(): def compound_query_valid_single_field(): - db = firestore.Client() + db = _make_one() # [START compound_query_valid_single_field] cities_ref = db.collection(u'cities') cities_ref.where(u'state', u'>=', u'CA').where(u'state', u'<=', u'IN') @@ -520,7 +532,7 @@ def compound_query_valid_single_field(): def compound_query_invalid_multi_field(): - db = firestore.Client() + db = _make_one() # [START compound_query_invalid_multi_field] cities_ref = db.collection(u'cities') cities_ref.where( @@ -529,14 +541,14 @@ def compound_query_invalid_multi_field(): def order_simple_limit(): - db = firestore.Client() + db = _make_one() # [START order_simple_limit] db.collection(u'cities').order_by(u'name').limit(3).stream() # [END order_simple_limit] def order_simple_limit_desc(): - db = firestore.Client() + db = _make_one() # [START order_simple_limit_desc] cities_ref = db.collection(u'cities') query = cities_ref.order_by( @@ -547,7 +559,7 @@ def order_simple_limit_desc(): def order_multiple(): - db = firestore.Client() + db = _make_one() # [START order_multiple] cities_ref = db.collection(u'cities') cities_ref.order_by(u'state').order_by( @@ -556,7 +568,7 @@ def order_multiple(): def order_where_limit(): - db = firestore.Client() + db = _make_one() # [START order_where_limit] cities_ref = db.collection(u'cities') query = cities_ref.where( @@ -567,7 +579,7 @@ def order_where_limit(): def order_where_valid(): - db = firestore.Client() + db = _make_one() # [START order_where_valid] cities_ref = db.collection(u'cities') query = cities_ref.where( @@ -578,7 +590,7 @@ def order_where_valid(): def order_where_invalid(): - db = firestore.Client() + db = _make_one() # [START order_where_invalid] cities_ref = db.collection(u'cities') query = cities_ref.where(u'population', u'>', 2500000).order_by(u'country') @@ -588,7 +600,7 @@ def order_where_invalid(): def cursor_simple_start_at(): - db = firestore.Client() + db = _make_one() # [START cursor_simple_start_at] cities_ref = db.collection(u'cities') query_start_at = cities_ref.order_by(u'population').start_at({ @@ -600,7 +612,7 @@ def cursor_simple_start_at(): def cursor_simple_end_at(): - db = firestore.Client() + db = _make_one() # [START cursor_simple_end_at] cities_ref = db.collection(u'cities') query_end_at = cities_ref.order_by(u'population').end_at({ @@ -612,7 +624,7 @@ def cursor_simple_end_at(): def snapshot_cursors(): - db = firestore.Client() + db = _make_one() # [START fs_start_at_snapshot_query_cursor] doc_ref = db.collection(u'cities').document(u'SF') @@ -628,7 +640,7 @@ def snapshot_cursors(): def cursor_paginate(): - db = firestore.Client() + db = _make_one() # [START cursor_paginate] cities_ref = db.collection(u'cities') first_query = cities_ref.order_by(u'population').limit(3) @@ -658,7 +670,7 @@ def cursor_paginate(): def listen_document(): - db = firestore.Client() + db = _make_one() # [START listen_document] # Create a callback on_snapshot function to capture changes @@ -689,7 +701,7 @@ def on_snapshot(doc_snapshot, changes, read_time): def listen_multiple(): - db = firestore.Client() + db = _make_one() # [START listen_multiple] # Create a callback on_snapshot function to capture changes @@ -727,7 +739,7 @@ def on_snapshot(col_snapshot, changes, read_time): def listen_for_changes(): - db = firestore.Client() + db = _make_one() # [START listen_for_changes] # Create a callback on_snapshot function to capture changes @@ -776,7 +788,7 @@ def on_snapshot(col_snapshot, changes, read_time): def cursor_multiple_conditions(): - db = firestore.Client() + db = _make_one() # [START cursor_multiple_conditions] start_at_name = ( db.collection(u'cities') @@ -802,14 +814,14 @@ def cursor_multiple_conditions(): def delete_single_doc(): - db = firestore.Client() + db = _make_one() # [START delete_single_doc] db.collection(u'cities').document(u'DC').delete() # [END delete_single_doc] def delete_field(): - db = firestore.Client() + db = _make_one() # [START delete_field] city_ref = db.collection(u'cities').document(u'BJ') city_ref.update({ @@ -819,7 +831,7 @@ def delete_field(): def delete_full_collection(): - db = firestore.Client() + db = _make_one() # [START delete_full_collection] def delete_collection(coll_ref, batch_size): @@ -837,7 +849,6 @@ def delete_collection(coll_ref, batch_size): delete_collection(db.collection(u'cities'), 10) delete_collection(db.collection(u'data'), 10) - delete_collection(db.collection(u'dc_samples'), 10) delete_collection(db.collection(u'objects'), 10) delete_collection(db.collection(u'users'), 10) diff --git a/firestore/cloud-client/snippets_test.py b/firestore/cloud-client/snippets_test.py index ec2c0c774e0..fca3ec96fdc 100644 --- a/firestore/cloud-client/snippets_test.py +++ b/firestore/cloud-client/snippets_test.py @@ -23,7 +23,7 @@ @pytest.fixture def db(): - yield firestore.Client() + yield snippets._make_one() def test_quickstart_new_instance(): From cbf00bb87cd752319e86fd43ff040f8520b00d10 Mon Sep 17 00:00:00 2001 From: Chris Wilcox Date: Mon, 30 Dec 2019 15:43:12 -0800 Subject: [PATCH 16/21] fix: remove unused import --- firestore/cloud-client/snippets_test.py | 1 - 1 file changed, 1 deletion(-) diff --git a/firestore/cloud-client/snippets_test.py b/firestore/cloud-client/snippets_test.py index fca3ec96fdc..2a4e430b2a8 100644 --- a/firestore/cloud-client/snippets_test.py +++ b/firestore/cloud-client/snippets_test.py @@ -13,7 +13,6 @@ import os -from google.cloud import firestore import pytest import snippets From 75fa62ec6ac1d0fa1583afc7f19fded2648c3408 Mon Sep 17 00:00:00 2001 From: Chris Wilcox Date: Mon, 30 Dec 2019 15:48:05 -0800 Subject: [PATCH 17/21] fix: remove more complicated fix for time now that we have separate collections --- firestore/cloud-client/snippets.py | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/firestore/cloud-client/snippets.py b/firestore/cloud-client/snippets.py index cadc90d2dd8..eb4a9b3f5ff 100644 --- a/firestore/cloud-client/snippets.py +++ b/firestore/cloud-client/snippets.py @@ -726,14 +726,7 @@ def on_snapshot(col_snapshot, changes, read_time): u'population': 860000 } db.collection(u'cities').document(u'SF').set(data) - - # Wait for 'SF' to be in the doc_map for up to 60 seconds. - waited = 0 - while waited < 60 and not [ - x for x in query_watch.doc_map.keys() if x.endswith("/SF") - ]: - waited += 1 - sleep(1) + sleep(1) query_watch.unsubscribe() From bb7afc72ab16136efc9227f7820b9c85cc269456 Mon Sep 17 00:00:00 2001 From: Chris Wilcox Date: Mon, 30 Dec 2019 15:52:51 -0800 Subject: [PATCH 18/21] fix: reorder tests --- firestore/cloud-client/snippets.py | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/firestore/cloud-client/snippets.py b/firestore/cloud-client/snippets.py index eb4a9b3f5ff..afa7423fd70 100644 --- a/firestore/cloud-client/snippets.py +++ b/firestore/cloud-client/snippets.py @@ -20,16 +20,6 @@ UNIQUE_STRING = str(uuid.uuid4()).split("-")[0] -def quickstart_new_instance(): - # [START quickstart_new_instance] - from google.cloud import firestore - - # Project ID is determined by the GCLOUD_PROJECT environment variable - db = firestore.Client() - # [END quickstart_new_instance] - - return db - def _make_one(): # monkeypatch the collection method so the collection names can be unique for each test run. @@ -41,6 +31,18 @@ def modified_collection_creation(collection_name, *args, **kwargs): db.collection = modified_collection_creation return db + +def quickstart_new_instance(): + # [START quickstart_new_instance] + from google.cloud import firestore + + # Project ID is determined by the GCLOUD_PROJECT environment variable + db = firestore.Client() + # [END quickstart_new_instance] + + return db + + def quickstart_add_data_one(): db = _make_one() # [START quickstart_add_data_one] From 93d92efec4fda4a468d1c9463e7f475da3dbb649 Mon Sep 17 00:00:00 2001 From: Chris Wilcox Date: Mon, 30 Dec 2019 16:27:41 -0800 Subject: [PATCH 19/21] fix: move test firestore client to test file --- firestore/cloud-client/snippets.py | 116 +++++++++++------------- firestore/cloud-client/snippets_test.py | 18 +++- 2 files changed, 67 insertions(+), 67 deletions(-) diff --git a/firestore/cloud-client/snippets.py b/firestore/cloud-client/snippets.py index afa7423fd70..ef06fbe1002 100644 --- a/firestore/cloud-client/snippets.py +++ b/firestore/cloud-client/snippets.py @@ -13,24 +13,10 @@ import datetime from time import sleep -import uuid from google.cloud import firestore import google.cloud.exceptions -UNIQUE_STRING = str(uuid.uuid4()).split("-")[0] - - -def _make_one(): - # monkeypatch the collection method so the collection names can be unique for each test run. - db = firestore.Client() - def modified_collection_creation(collection_name, *args, **kwargs): - unique_collection_name = "{}-{}".format(collection_name, UNIQUE_STRING) - return db._collection(unique_collection_name, *args, **kwargs) - db._collection = db.collection - db.collection = modified_collection_creation - return db - def quickstart_new_instance(): # [START quickstart_new_instance] @@ -44,7 +30,7 @@ def quickstart_new_instance(): def quickstart_add_data_one(): - db = _make_one() + db = firestore.Client() # [START quickstart_add_data_one] doc_ref = db.collection(u'users').document(u'alovelace') doc_ref.set({ @@ -56,7 +42,7 @@ def quickstart_add_data_one(): def quickstart_add_data_two(): - db = _make_one() + db = firestore.Client() # [START quickstart_add_data_two] doc_ref = db.collection(u'users').document(u'aturing') doc_ref.set({ @@ -69,7 +55,7 @@ def quickstart_add_data_two(): def quickstart_get_collection(): - db = _make_one() + db = firestore.Client() # [START quickstart_get_collection] users_ref = db.collection(u'users') docs = users_ref.stream() @@ -80,7 +66,7 @@ def quickstart_get_collection(): def add_from_dict(): - db = _make_one() + db = firestore.Client() # [START add_from_dict] data = { u'name': u'Los Angeles', @@ -94,7 +80,7 @@ def add_from_dict(): def add_data_types(): - db = _make_one() + db = firestore.Client() # [START add_data_types] data = { u'stringExample': u'Hello, World!', @@ -170,7 +156,7 @@ def __repr__(self): def add_example_data(): - db = _make_one() + db = firestore.Client() # [START add_example_data] cities_ref = db.collection(u'cities') cities_ref.document(u'BJ').set( @@ -191,7 +177,7 @@ def add_example_data(): def add_custom_class_with_id(): - db = _make_one() + db = firestore.Client() # [START add_custom_class_with_id] city = City(name=u'Los Angeles', state=u'CA', country=u'USA') db.collection(u'cities').document(u'LA').set(city.to_dict()) @@ -199,7 +185,7 @@ def add_custom_class_with_id(): def add_data_with_id(): - db = _make_one() + db = firestore.Client() data = {} # [START add_data_with_id] db.collection(u'cities').document(u'new-city-id').set(data) @@ -207,7 +193,7 @@ def add_data_with_id(): def add_custom_class_generated_id(): - db = _make_one() + db = firestore.Client() # [START add_custom_class_generated_id] city = City(name=u'Tokyo', state=None, country=u'Japan') db.collection(u'cities').add(city.to_dict()) @@ -215,7 +201,7 @@ def add_custom_class_generated_id(): def add_new_doc(): - db = _make_one() + db = firestore.Client() # [START add_new_doc] new_city_ref = db.collection(u'cities').document() @@ -227,7 +213,7 @@ def add_new_doc(): def get_check_exists(): - db = _make_one() + db = firestore.Client() # [START get_check_exists] doc_ref = db.collection(u'cities').document(u'SF') @@ -240,7 +226,7 @@ def get_check_exists(): def get_custom_class(): - db = _make_one() + db = firestore.Client() # [START get_custom_class] doc_ref = db.collection(u'cities').document(u'BJ') @@ -251,7 +237,7 @@ def get_custom_class(): def get_simple_query(): - db = _make_one() + db = firestore.Client() # [START get_simple_query] docs = db.collection(u'cities').where(u'capital', u'==', True).stream() @@ -261,7 +247,7 @@ def get_simple_query(): def array_contains_filter(): - db = _make_one() + db = firestore.Client() # [START fs_array_contains_filter] cities_ref = db.collection(u'cities') @@ -273,7 +259,7 @@ def array_contains_filter(): def get_full_collection(): - db = _make_one() + db = firestore.Client() # [START get_full_collection] docs = db.collection(u'cities').stream() @@ -283,7 +269,7 @@ def get_full_collection(): def structure_doc_ref(): - db = _make_one() + db = firestore.Client() # [START structure_doc_ref] a_lovelace_ref = db.collection(u'users').document(u'alovelace') # [END structure_doc_ref] @@ -291,7 +277,7 @@ def structure_doc_ref(): def structure_collection_ref(): - db = _make_one() + db = firestore.Client() # [START structure_collection_ref] users_ref = db.collection(u'users') # [END structure_collection_ref] @@ -299,7 +285,7 @@ def structure_collection_ref(): def structure_doc_ref_alternate(): - db = _make_one() + db = firestore.Client() # [START structure_doc_ref_alternate] a_lovelace_ref = db.document(u'users/alovelace') # [END structure_doc_ref_alternate] @@ -308,7 +294,7 @@ def structure_doc_ref_alternate(): def structure_subcollection_ref(): - db = _make_one() + db = firestore.Client() # [START structure_subcollection_ref] room_a_ref = db.collection(u'rooms').document(u'roomA') message_ref = room_a_ref.collection(u'messages').document(u'message1') @@ -317,7 +303,7 @@ def structure_subcollection_ref(): def update_doc(): - db = _make_one() + db = firestore.Client() db.collection(u'cities').document(u'DC').set( City(u'Washington D.C.', None, u'USA', True, 680000, [u'east_coast']).to_dict()) @@ -331,7 +317,7 @@ def update_doc(): def update_doc_array(): - db = _make_one() + db = firestore.Client() db.collection(u'cities').document(u'DC').set( City(u'Washington D.C.', None, u'USA', True, 680000, [u'east_coast']).to_dict()) @@ -350,7 +336,7 @@ def update_doc_array(): def update_multiple(): - db = _make_one() + db = firestore.Client() db.collection(u'cities').document(u'DC').set( City(u'Washington D.C.', None, u'USA', True, 680000, [u'east_coast']).to_dict()) @@ -367,7 +353,7 @@ def update_multiple(): def update_create_if_missing(): - db = _make_one() + db = firestore.Client() # [START update_create_if_missing] city_ref = db.collection(u'cities').document(u'BJ') @@ -378,7 +364,7 @@ def update_create_if_missing(): def update_nested(): - db = _make_one() + db = firestore.Client() # [START update_nested] # Create an initial document to update frank_ref = db.collection(u'users').document(u'frank') @@ -401,7 +387,7 @@ def update_nested(): def update_server_timestamp(): - db = _make_one() + db = firestore.Client() # [START update_server_timestamp] city_ref = db.collection(u'objects').document(u'some-id') city_ref.update({ @@ -411,7 +397,7 @@ def update_server_timestamp(): def update_data_transaction(): - db = _make_one() + db = firestore.Client() # [START update_data_transaction] transaction = db.transaction() city_ref = db.collection(u'cities').document(u'SF') @@ -428,7 +414,7 @@ def update_in_transaction(transaction, city_ref): def update_data_transaction_result(): - db = _make_one() + db = firestore.Client() # [START update_data_transaction_result] transaction = db.transaction() city_ref = db.collection(u'cities').document(u'SF') @@ -455,7 +441,7 @@ def update_in_transaction(transaction, city_ref): def update_data_batch(): - db = _make_one() + db = firestore.Client() # [START update_data_batch] batch = db.batch() @@ -477,7 +463,7 @@ def update_data_batch(): def compound_query_example(): - db = _make_one() + db = firestore.Client() # [START compound_query_example] # Create a reference to the cities collection cities_ref = db.collection(u'cities') @@ -490,7 +476,7 @@ def compound_query_example(): def compound_query_simple(): - db = _make_one() + db = firestore.Client() # [START compound_query_simple] cities_ref = db.collection(u'cities') @@ -501,7 +487,7 @@ def compound_query_simple(): def compound_query_single_clause(): - db = _make_one() + db = firestore.Client() # [START compound_query_single_clause] cities_ref = db.collection(u'cities') @@ -512,7 +498,7 @@ def compound_query_single_clause(): def compound_query_valid_multi_clause(): - db = _make_one() + db = firestore.Client() # [START compound_query_valid_multi_clause] cities_ref = db.collection(u'cities') @@ -526,7 +512,7 @@ def compound_query_valid_multi_clause(): def compound_query_valid_single_field(): - db = _make_one() + db = firestore.Client() # [START compound_query_valid_single_field] cities_ref = db.collection(u'cities') cities_ref.where(u'state', u'>=', u'CA').where(u'state', u'<=', u'IN') @@ -534,7 +520,7 @@ def compound_query_valid_single_field(): def compound_query_invalid_multi_field(): - db = _make_one() + db = firestore.Client() # [START compound_query_invalid_multi_field] cities_ref = db.collection(u'cities') cities_ref.where( @@ -543,14 +529,14 @@ def compound_query_invalid_multi_field(): def order_simple_limit(): - db = _make_one() + db = firestore.Client() # [START order_simple_limit] db.collection(u'cities').order_by(u'name').limit(3).stream() # [END order_simple_limit] def order_simple_limit_desc(): - db = _make_one() + db = firestore.Client() # [START order_simple_limit_desc] cities_ref = db.collection(u'cities') query = cities_ref.order_by( @@ -561,7 +547,7 @@ def order_simple_limit_desc(): def order_multiple(): - db = _make_one() + db = firestore.Client() # [START order_multiple] cities_ref = db.collection(u'cities') cities_ref.order_by(u'state').order_by( @@ -570,7 +556,7 @@ def order_multiple(): def order_where_limit(): - db = _make_one() + db = firestore.Client() # [START order_where_limit] cities_ref = db.collection(u'cities') query = cities_ref.where( @@ -581,7 +567,7 @@ def order_where_limit(): def order_where_valid(): - db = _make_one() + db = firestore.Client() # [START order_where_valid] cities_ref = db.collection(u'cities') query = cities_ref.where( @@ -592,7 +578,7 @@ def order_where_valid(): def order_where_invalid(): - db = _make_one() + db = firestore.Client() # [START order_where_invalid] cities_ref = db.collection(u'cities') query = cities_ref.where(u'population', u'>', 2500000).order_by(u'country') @@ -602,7 +588,7 @@ def order_where_invalid(): def cursor_simple_start_at(): - db = _make_one() + db = firestore.Client() # [START cursor_simple_start_at] cities_ref = db.collection(u'cities') query_start_at = cities_ref.order_by(u'population').start_at({ @@ -614,7 +600,7 @@ def cursor_simple_start_at(): def cursor_simple_end_at(): - db = _make_one() + db = firestore.Client() # [START cursor_simple_end_at] cities_ref = db.collection(u'cities') query_end_at = cities_ref.order_by(u'population').end_at({ @@ -626,7 +612,7 @@ def cursor_simple_end_at(): def snapshot_cursors(): - db = _make_one() + db = firestore.Client() # [START fs_start_at_snapshot_query_cursor] doc_ref = db.collection(u'cities').document(u'SF') @@ -642,7 +628,7 @@ def snapshot_cursors(): def cursor_paginate(): - db = _make_one() + db = firestore.Client() # [START cursor_paginate] cities_ref = db.collection(u'cities') first_query = cities_ref.order_by(u'population').limit(3) @@ -672,7 +658,7 @@ def cursor_paginate(): def listen_document(): - db = _make_one() + db = firestore.Client() # [START listen_document] # Create a callback on_snapshot function to capture changes @@ -703,7 +689,7 @@ def on_snapshot(doc_snapshot, changes, read_time): def listen_multiple(): - db = _make_one() + db = firestore.Client() # [START listen_multiple] # Create a callback on_snapshot function to capture changes @@ -734,7 +720,7 @@ def on_snapshot(col_snapshot, changes, read_time): def listen_for_changes(): - db = _make_one() + db = firestore.Client() # [START listen_for_changes] # Create a callback on_snapshot function to capture changes @@ -783,7 +769,7 @@ def on_snapshot(col_snapshot, changes, read_time): def cursor_multiple_conditions(): - db = _make_one() + db = firestore.Client() # [START cursor_multiple_conditions] start_at_name = ( db.collection(u'cities') @@ -809,14 +795,14 @@ def cursor_multiple_conditions(): def delete_single_doc(): - db = _make_one() + db = firestore.Client() # [START delete_single_doc] db.collection(u'cities').document(u'DC').delete() # [END delete_single_doc] def delete_field(): - db = _make_one() + db = firestore.Client() # [START delete_field] city_ref = db.collection(u'cities').document(u'BJ') city_ref.update({ @@ -826,7 +812,7 @@ def delete_field(): def delete_full_collection(): - db = _make_one() + db = firestore.Client() # [START delete_full_collection] def delete_collection(coll_ref, batch_size): diff --git a/firestore/cloud-client/snippets_test.py b/firestore/cloud-client/snippets_test.py index 2a4e430b2a8..3bc2e5922c7 100644 --- a/firestore/cloud-client/snippets_test.py +++ b/firestore/cloud-client/snippets_test.py @@ -12,18 +12,32 @@ # limitations under the License. import os +import uuid +from google.cloud import firestore import pytest import snippets os.environ['GOOGLE_CLOUD_PROJECT'] = os.environ['FIRESTORE_PROJECT'] +UNIQUE_STRING = str(uuid.uuid4()).split("-")[0] + +class TestFirestoreClient(firestore.Client): + def __init__(self, *args, **kwargs): + self._UNIQUE_STRING = UNIQUE_STRING + self._super = super() + self._super.__init__() + + def collection(self, collection_name, *args, **kwargs): + collection_name += '-{}'.format(self._UNIQUE_STRING) + return self._super.collection(collection_name, *args, **kwargs) + +snippets.firestore.Client = TestFirestoreClient @pytest.fixture def db(): - yield snippets._make_one() - + yield snippets.firestore.Client() def test_quickstart_new_instance(): snippets.quickstart_new_instance() From f7d346f78594b8e9802470a668c2083fc44287c4 Mon Sep 17 00:00:00 2001 From: Chris Wilcox Date: Mon, 30 Dec 2019 16:36:16 -0800 Subject: [PATCH 20/21] fix: lint --- firestore/cloud-client/snippets_test.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/firestore/cloud-client/snippets_test.py b/firestore/cloud-client/snippets_test.py index 3bc2e5922c7..5aa7cdd2e2b 100644 --- a/firestore/cloud-client/snippets_test.py +++ b/firestore/cloud-client/snippets_test.py @@ -23,6 +23,7 @@ UNIQUE_STRING = str(uuid.uuid4()).split("-")[0] + class TestFirestoreClient(firestore.Client): def __init__(self, *args, **kwargs): self._UNIQUE_STRING = UNIQUE_STRING @@ -33,12 +34,15 @@ def collection(self, collection_name, *args, **kwargs): collection_name += '-{}'.format(self._UNIQUE_STRING) return self._super.collection(collection_name, *args, **kwargs) + snippets.firestore.Client = TestFirestoreClient + @pytest.fixture def db(): yield snippets.firestore.Client() + def test_quickstart_new_instance(): snippets.quickstart_new_instance() From 945788453207057c202a4a8aaf84db6e4773456a Mon Sep 17 00:00:00 2001 From: Chris Wilcox Date: Mon, 30 Dec 2019 16:44:39 -0800 Subject: [PATCH 21/21] fix: use old-style classes for Python 2.7 --- firestore/cloud-client/snippets_test.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/firestore/cloud-client/snippets_test.py b/firestore/cloud-client/snippets_test.py index 5aa7cdd2e2b..36d19877e65 100644 --- a/firestore/cloud-client/snippets_test.py +++ b/firestore/cloud-client/snippets_test.py @@ -27,8 +27,8 @@ class TestFirestoreClient(firestore.Client): def __init__(self, *args, **kwargs): self._UNIQUE_STRING = UNIQUE_STRING - self._super = super() - self._super.__init__() + self._super = super(TestFirestoreClient, self) + self._super.__init__(*args, **kwargs) def collection(self, collection_name, *args, **kwargs): collection_name += '-{}'.format(self._UNIQUE_STRING)