|
| 1 | +# Copyright 2010 New Relic, Inc. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +from testing_support.validators.validate_transaction_metrics import validate_transaction_metrics |
| 16 | +from newrelic.api.background_task import background_task |
| 17 | +from testing_support.validators.validate_database_duration import ( |
| 18 | + validate_database_duration, |
| 19 | +) |
| 20 | + |
| 21 | +# ===== WriteBatch ===== |
| 22 | + |
| 23 | +def _exercise_write_batch(client, collection): |
| 24 | + docs = [collection.document(str(x)) for x in range(1, 4)] |
| 25 | + batch = client.batch() |
| 26 | + for doc in docs: |
| 27 | + batch.set(doc, {}) |
| 28 | + |
| 29 | + batch.commit() |
| 30 | + |
| 31 | + |
| 32 | +def test_firestore_write_batch(client, collection): |
| 33 | + _test_scoped_metrics = [ |
| 34 | + ("Datastore/operation/Firestore/commit", 1), |
| 35 | + ] |
| 36 | + |
| 37 | + _test_rollup_metrics = [ |
| 38 | + ("Datastore/all", 1), |
| 39 | + ("Datastore/allOther", 1), |
| 40 | + ] |
| 41 | + @validate_database_duration() |
| 42 | + @validate_transaction_metrics( |
| 43 | + "test_firestore_write_batch", |
| 44 | + scoped_metrics=_test_scoped_metrics, |
| 45 | + rollup_metrics=_test_rollup_metrics, |
| 46 | + background_task=True, |
| 47 | + ) |
| 48 | + @background_task(name="test_firestore_write_batch") |
| 49 | + def _test(): |
| 50 | + _exercise_write_batch(client, collection) |
| 51 | + |
| 52 | + _test() |
| 53 | + |
| 54 | +# ===== BulkWriteBatch ===== |
| 55 | + |
| 56 | +def _exercise_bulk_write_batch(client, collection): |
| 57 | + from google.cloud.firestore_v1.bulk_batch import BulkWriteBatch |
| 58 | + |
| 59 | + docs = [collection.document(str(x)) for x in range(1, 4)] |
| 60 | + batch = BulkWriteBatch(client) |
| 61 | + for doc in docs: |
| 62 | + batch.set(doc, {}) |
| 63 | + |
| 64 | + batch.commit() |
| 65 | + |
| 66 | + |
| 67 | +def test_firestore_bulk_write_batch(client, collection): |
| 68 | + _test_scoped_metrics = [ |
| 69 | + ("Datastore/operation/Firestore/commit", 1), |
| 70 | + ] |
| 71 | + |
| 72 | + _test_rollup_metrics = [ |
| 73 | + ("Datastore/all", 1), |
| 74 | + ("Datastore/allOther", 1), |
| 75 | + ] |
| 76 | + @validate_database_duration() |
| 77 | + @validate_transaction_metrics( |
| 78 | + "test_firestore_bulk_write_batch", |
| 79 | + scoped_metrics=_test_scoped_metrics, |
| 80 | + rollup_metrics=_test_rollup_metrics, |
| 81 | + background_task=True, |
| 82 | + ) |
| 83 | + @background_task(name="test_firestore_bulk_write_batch") |
| 84 | + def _test(): |
| 85 | + _exercise_bulk_write_batch(client, collection) |
| 86 | + |
| 87 | + _test() |
0 commit comments