Skip to content

fix: Improve Firestore tests by separating test runs, improving cleanup #2667

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 25 commits into from
Jan 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
56b7e98
fix: harden the wait for consistency for firestore multiple listen sn…
crwilcox Dec 30, 2019
68c1cfe
fix: formatting
crwilcox Dec 30, 2019
2a876ac
add a setup step in CLI (#2611)
anguillanneuf Dec 30, 2019
30b72ec
fix: formatting
crwilcox Dec 30, 2019
a99c2ab
Merge branch 'master' into fix-2529
crwilcox Dec 30, 2019
07d8516
fix: formatting
crwilcox Dec 30, 2019
2422828
Merge branch 'fix-2529' of github.com:GoogleCloudPlatform/python-docs…
crwilcox Dec 30, 2019
dc5881a
fix: add setup to snippets for tests
crwilcox Dec 30, 2019
a113bee
fix: rename sydney_query to denver_query as it refers to denver
crwilcox Dec 30, 2019
ddc9768
fix: Use stream instead of get in samples
crwilcox Dec 30, 2019
fda54c8
fix: stop deleting LA, delete DEN instead
crwilcox Dec 30, 2019
a48030d
fix: move delete tests to end of file
crwilcox Dec 30, 2019
5c5dbe3
fix: move BJ to top to work with snapshot cursors
crwilcox Dec 30, 2019
102997c
fix: Beijing -> BJ
crwilcox Dec 30, 2019
968ef21
Merge branch 'master' into fix-2529
crwilcox Dec 30, 2019
1acacb8
during delete collection delete all collections created for these tests
crwilcox Dec 30, 2019
f718d62
Merge branch 'fix-2529' of github.com:GoogleCloudPlatform/python-docs…
crwilcox Dec 30, 2019
0102d89
fix: add cleanup test to distributed counters
crwilcox Dec 30, 2019
7c56405
wrap db.collections calls to use unique names
crwilcox Dec 30, 2019
cbf00bb
fix: remove unused import
crwilcox Dec 30, 2019
75fa62e
fix: remove more complicated fix for time now that we have separate c…
crwilcox Dec 30, 2019
bb7afc7
fix: reorder tests
crwilcox Dec 30, 2019
93d92ef
fix: move test firestore client to test file
crwilcox Dec 31, 2019
f7d346f
fix: lint
crwilcox Dec 31, 2019
9457884
fix: use old-style classes for Python 2.7
crwilcox Dec 31, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions firestore/cloud-client/distributed_counters_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
34 changes: 26 additions & 8 deletions firestore/cloud-client/snippets.py
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand All @@ -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]


Expand Down Expand Up @@ -304,6 +304,10 @@ def structure_subcollection_ref():

def update_doc():
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())

# [START update_doc]
city_ref = db.collection(u'cities').document(u'DC')

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

def update_doc_array():
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())

# [START fs_update_doc_array]
city_ref = db.collection(u'cities').document(u'DC')

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

def update_multiple():
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())

# [START update_multiple]
doc_ref = db.collection(u'cities').document(u'DC')

Expand Down Expand Up @@ -441,9 +453,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()
Expand Down Expand Up @@ -490,12 +502,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)


Expand Down Expand Up @@ -703,6 +715,7 @@ def on_snapshot(col_snapshot, changes, read_time):
}
db.collection(u'cities').document(u'SF').set(data)
sleep(1)

query_watch.unsubscribe()


Expand Down Expand Up @@ -737,6 +750,7 @@ def on_snapshot(col_snapshot, changes, read_time):
u'capital': False,
u'population': 80000
})
sleep(1)

# Modifying document
mtv_document.update({
Expand All @@ -746,6 +760,7 @@ def on_snapshot(col_snapshot, changes, read_time):
u'capital': False,
u'population': 90000
})
sleep(1)

# Delete document
mtv_document.delete()
Expand Down Expand Up @@ -801,7 +816,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:
Expand All @@ -814,6 +829,9 @@ 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'objects'), 10)
delete_collection(db.collection(u'users'), 10)


def collection_group_query(db):
Expand Down
37 changes: 27 additions & 10 deletions firestore/cloud-client/snippets_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
# limitations under the License.

import os
import uuid

from google.cloud import firestore
import pytest
Expand All @@ -20,10 +21,26 @@

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(TestFirestoreClient, self)
self._super.__init__(*args, **kwargs)

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 firestore.Client()
yield snippets.firestore.Client()


def test_quickstart_new_instance():
Expand Down Expand Up @@ -250,15 +267,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()
Expand All @@ -280,6 +288,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('BJ').set({'capital': True})
snippets.delete_field()


def test_delete_full_collection():
snippets.delete_full_collection()

Expand Down