Skip to content

Commit f94bb78

Browse files
crwilcoxanguillanneuf
authored andcommitted
fix: Improve Firestore tests by separating test runs, improving cleanup (#2667)
* fix: harden the wait for consistency for firestore multiple listen snippet * fix: formatting * add a setup step in CLI (#2611) * fix: formatting * fix: formatting * fix: add setup to snippets for tests * fix: rename sydney_query to denver_query as it refers to denver * fix: Use stream instead of get in samples * fix: stop deleting LA, delete DEN instead * fix: move delete tests to end of file * fix: move BJ to top to work with snapshot cursors * fix: Beijing -> BJ * during delete collection delete all collections created for these tests * fix: add cleanup test to distributed counters * wrap db.collections calls to use unique names * fix: remove unused import * fix: remove more complicated fix for time now that we have separate collections * fix: reorder tests * fix: move test firestore client to test file * fix: lint * fix: use old-style classes for Python 2.7 Co-authored-by: Tianzi Cai <[email protected]>
1 parent ebe1500 commit f94bb78

File tree

3 files changed

+65
-18
lines changed

3 files changed

+65
-18
lines changed

firestore/cloud-client/distributed_counters_test.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,15 @@ def test_distributed_counters(fs_client):
4747
counter.increment_counter(doc_ref)
4848
counter.increment_counter(doc_ref)
4949
assert counter.get_count(doc_ref) == 2
50+
51+
52+
def test_distributed_counters_cleanup(fs_client):
53+
col = fs_client.collection("dc_samples")
54+
doc_ref = col.document("distributed_counter")
55+
56+
shards = doc_ref.collection("shards").list_documents()
57+
shards_list = [shard for shard in shards]
58+
for shard in shards_list:
59+
shard.delete()
60+
61+
doc_ref.delete()

firestore/cloud-client/snippets.py

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,8 @@ def add_example_data():
159159
db = firestore.Client()
160160
# [START add_example_data]
161161
cities_ref = db.collection(u'cities')
162+
cities_ref.document(u'BJ').set(
163+
City(u'Beijing', None, u'China', True, 21500000, [u'hebei']).to_dict())
162164
cities_ref.document(u'SF').set(
163165
City(u'San Francisco', u'CA', u'USA', False, 860000,
164166
[u'west_coast', u'norcal']).to_dict())
@@ -171,8 +173,6 @@ def add_example_data():
171173
cities_ref.document(u'TOK').set(
172174
City(u'Tokyo', None, u'Japan', True, 9000000,
173175
[u'kanto', u'honshu']).to_dict())
174-
cities_ref.document(u'BJ').set(
175-
City(u'Beijing', None, u'China', True, 21500000, [u'hebei']).to_dict())
176176
# [END add_example_data]
177177

178178

@@ -304,6 +304,10 @@ def structure_subcollection_ref():
304304

305305
def update_doc():
306306
db = firestore.Client()
307+
db.collection(u'cities').document(u'DC').set(
308+
City(u'Washington D.C.', None, u'USA', True, 680000,
309+
[u'east_coast']).to_dict())
310+
307311
# [START update_doc]
308312
city_ref = db.collection(u'cities').document(u'DC')
309313

@@ -314,6 +318,10 @@ def update_doc():
314318

315319
def update_doc_array():
316320
db = firestore.Client()
321+
db.collection(u'cities').document(u'DC').set(
322+
City(u'Washington D.C.', None, u'USA', True, 680000,
323+
[u'east_coast']).to_dict())
324+
317325
# [START fs_update_doc_array]
318326
city_ref = db.collection(u'cities').document(u'DC')
319327

@@ -329,6 +337,10 @@ def update_doc_array():
329337

330338
def update_multiple():
331339
db = firestore.Client()
340+
db.collection(u'cities').document(u'DC').set(
341+
City(u'Washington D.C.', None, u'USA', True, 680000,
342+
[u'east_coast']).to_dict())
343+
332344
# [START update_multiple]
333345
doc_ref = db.collection(u'cities').document(u'DC')
334346

@@ -441,9 +453,9 @@ def update_data_batch():
441453
sf_ref = db.collection(u'cities').document(u'SF')
442454
batch.update(sf_ref, {u'population': 1000000})
443455

444-
# Delete LA
445-
la_ref = db.collection(u'cities').document(u'LA')
446-
batch.delete(la_ref)
456+
# Delete DEN
457+
den_ref = db.collection(u'cities').document(u'DEN')
458+
batch.delete(den_ref)
447459

448460
# Commit the batch
449461
batch.commit()
@@ -490,12 +502,12 @@ def compound_query_valid_multi_clause():
490502
# [START compound_query_valid_multi_clause]
491503
cities_ref = db.collection(u'cities')
492504

493-
sydney_query = cities_ref.where(
505+
denver_query = cities_ref.where(
494506
u'state', u'==', u'CO').where(u'name', u'==', u'Denver')
495507
large_us_cities_query = cities_ref.where(
496508
u'state', u'==', u'CA').where(u'population', u'>', 1000000)
497509
# [END compound_query_valid_multi_clause]
498-
print(sydney_query)
510+
print(denver_query)
499511
print(large_us_cities_query)
500512

501513

@@ -703,6 +715,7 @@ def on_snapshot(col_snapshot, changes, read_time):
703715
}
704716
db.collection(u'cities').document(u'SF').set(data)
705717
sleep(1)
718+
706719
query_watch.unsubscribe()
707720

708721

@@ -737,6 +750,7 @@ def on_snapshot(col_snapshot, changes, read_time):
737750
u'capital': False,
738751
u'population': 80000
739752
})
753+
sleep(1)
740754

741755
# Modifying document
742756
mtv_document.update({
@@ -746,6 +760,7 @@ def on_snapshot(col_snapshot, changes, read_time):
746760
u'capital': False,
747761
u'population': 90000
748762
})
763+
sleep(1)
749764

750765
# Delete document
751766
mtv_document.delete()
@@ -801,7 +816,7 @@ def delete_full_collection():
801816

802817
# [START delete_full_collection]
803818
def delete_collection(coll_ref, batch_size):
804-
docs = coll_ref.limit(batch_size).get()
819+
docs = coll_ref.limit(batch_size).stream()
805820
deleted = 0
806821

807822
for doc in docs:
@@ -814,6 +829,9 @@ def delete_collection(coll_ref, batch_size):
814829
# [END delete_full_collection]
815830

816831
delete_collection(db.collection(u'cities'), 10)
832+
delete_collection(db.collection(u'data'), 10)
833+
delete_collection(db.collection(u'objects'), 10)
834+
delete_collection(db.collection(u'users'), 10)
817835

818836

819837
def collection_group_query(db):

firestore/cloud-client/snippets_test.py

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
# limitations under the License.
1313

1414
import os
15+
import uuid
1516

1617
from google.cloud import firestore
1718
import pytest
@@ -20,10 +21,26 @@
2021

2122
os.environ['GOOGLE_CLOUD_PROJECT'] = os.environ['FIRESTORE_PROJECT']
2223

24+
UNIQUE_STRING = str(uuid.uuid4()).split("-")[0]
25+
26+
27+
class TestFirestoreClient(firestore.Client):
28+
def __init__(self, *args, **kwargs):
29+
self._UNIQUE_STRING = UNIQUE_STRING
30+
self._super = super(TestFirestoreClient, self)
31+
self._super.__init__(*args, **kwargs)
32+
33+
def collection(self, collection_name, *args, **kwargs):
34+
collection_name += '-{}'.format(self._UNIQUE_STRING)
35+
return self._super.collection(collection_name, *args, **kwargs)
36+
37+
38+
snippets.firestore.Client = TestFirestoreClient
39+
2340

2441
@pytest.fixture
2542
def db():
26-
yield firestore.Client()
43+
yield snippets.firestore.Client()
2744

2845

2946
def test_quickstart_new_instance():
@@ -250,15 +267,6 @@ def test_cursor_multiple_conditions():
250267
snippets.cursor_multiple_conditions()
251268

252269

253-
def test_delete_single_doc():
254-
snippets.delete_single_doc()
255-
256-
257-
def test_delete_field(db):
258-
db.collection('cities').document('Beijing').set({'capital': True})
259-
snippets.delete_field()
260-
261-
262270
def test_listen_document(capsys):
263271
snippets.listen_document()
264272
out, _ = capsys.readouterr()
@@ -280,6 +288,15 @@ def test_listen_for_changes(capsys):
280288
assert 'Removed city: MTV' in out
281289

282290

291+
def test_delete_single_doc():
292+
snippets.delete_single_doc()
293+
294+
295+
def test_delete_field(db):
296+
db.collection('cities').document('BJ').set({'capital': True})
297+
snippets.delete_field()
298+
299+
283300
def test_delete_full_collection():
284301
snippets.delete_full_collection()
285302

0 commit comments

Comments
 (0)