|
| 1 | +# Copyright 2022 Google 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 | +"""Tests for firebase_admin.firestore_async.""" |
| 16 | + |
| 17 | +import platform |
| 18 | + |
| 19 | +import pytest |
| 20 | + |
| 21 | +import firebase_admin |
| 22 | +from firebase_admin import credentials |
| 23 | +try: |
| 24 | + from firebase_admin import firestore_async |
| 25 | +except ImportError: |
| 26 | + pass |
| 27 | +from tests import testutils |
| 28 | + |
| 29 | + |
| 30 | +@pytest.mark.skipif( |
| 31 | + platform.python_implementation() == 'PyPy', |
| 32 | + reason='Firestore is not supported on PyPy') |
| 33 | +class TestFirestoreAsync: |
| 34 | + """Test class Firestore Async APIs.""" |
| 35 | + |
| 36 | + def teardown_method(self, method): |
| 37 | + del method |
| 38 | + testutils.cleanup_apps() |
| 39 | + |
| 40 | + def test_no_project_id(self): |
| 41 | + def evaluate(): |
| 42 | + firebase_admin.initialize_app(testutils.MockCredential()) |
| 43 | + with pytest.raises(ValueError): |
| 44 | + firestore_async.client() |
| 45 | + testutils.run_without_project_id(evaluate) |
| 46 | + |
| 47 | + def test_project_id(self): |
| 48 | + cred = credentials.Certificate(testutils.resource_filename('service_account.json')) |
| 49 | + firebase_admin.initialize_app(cred, {'projectId': 'explicit-project-id'}) |
| 50 | + client = firestore_async.client() |
| 51 | + assert client is not None |
| 52 | + assert client.project == 'explicit-project-id' |
| 53 | + |
| 54 | + def test_project_id_with_explicit_app(self): |
| 55 | + cred = credentials.Certificate(testutils.resource_filename('service_account.json')) |
| 56 | + app = firebase_admin.initialize_app(cred, {'projectId': 'explicit-project-id'}) |
| 57 | + client = firestore_async.client(app=app) |
| 58 | + assert client is not None |
| 59 | + assert client.project == 'explicit-project-id' |
| 60 | + |
| 61 | + def test_service_account(self): |
| 62 | + cred = credentials.Certificate(testutils.resource_filename('service_account.json')) |
| 63 | + firebase_admin.initialize_app(cred) |
| 64 | + client = firestore_async.client() |
| 65 | + assert client is not None |
| 66 | + assert client.project == 'mock-project-id' |
| 67 | + |
| 68 | + def test_service_account_with_explicit_app(self): |
| 69 | + cred = credentials.Certificate(testutils.resource_filename('service_account.json')) |
| 70 | + app = firebase_admin.initialize_app(cred) |
| 71 | + client = firestore_async.client(app=app) |
| 72 | + assert client is not None |
| 73 | + assert client.project == 'mock-project-id' |
| 74 | + |
| 75 | + def test_geo_point(self): |
| 76 | + geo_point = firestore_async.GeoPoint(10, 20) # pylint: disable=no-member |
| 77 | + assert geo_point.latitude == 10 |
| 78 | + assert geo_point.longitude == 20 |
| 79 | + |
| 80 | + def test_server_timestamp(self): |
| 81 | + assert firestore_async.SERVER_TIMESTAMP is not None # pylint: disable=no-member |
0 commit comments