|
| 1 | +# Copyright 2017 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.""" |
| 16 | + |
| 17 | +import os |
| 18 | + |
| 19 | +import pytest |
| 20 | + |
| 21 | +import firebase_admin |
| 22 | +from firebase_admin import credentials |
| 23 | +from firebase_admin import firestore |
| 24 | +from tests import testutils |
| 25 | + |
| 26 | + |
| 27 | +def teardown_function(): |
| 28 | + testutils.cleanup_apps() |
| 29 | + |
| 30 | +def test_no_project_id(): |
| 31 | + env_var = 'GCLOUD_PROJECT' |
| 32 | + gcloud_project = os.environ.get(env_var) |
| 33 | + if gcloud_project: |
| 34 | + del os.environ[env_var] |
| 35 | + try: |
| 36 | + firebase_admin.initialize_app(testutils.MockCredential()) |
| 37 | + with pytest.raises(ValueError): |
| 38 | + firestore.client() |
| 39 | + finally: |
| 40 | + if gcloud_project: |
| 41 | + os.environ[env_var] = gcloud_project |
| 42 | + |
| 43 | +def test_project_id(): |
| 44 | + cred = credentials.Certificate(testutils.resource_filename('service_account.json')) |
| 45 | + firebase_admin.initialize_app(cred, {'projectId': 'explicit-project-id'}) |
| 46 | + client = firestore.client() |
| 47 | + assert client is not None |
| 48 | + assert client.project == 'explicit-project-id' |
| 49 | + |
| 50 | +def test_project_id_with_explicit_app(): |
| 51 | + cred = credentials.Certificate(testutils.resource_filename('service_account.json')) |
| 52 | + app = firebase_admin.initialize_app(cred, {'projectId': 'explicit-project-id'}) |
| 53 | + client = firestore.client(app=app) |
| 54 | + assert client is not None |
| 55 | + assert client.project == 'explicit-project-id' |
| 56 | + |
| 57 | +def test_service_account(): |
| 58 | + cred = credentials.Certificate(testutils.resource_filename('service_account.json')) |
| 59 | + firebase_admin.initialize_app(cred) |
| 60 | + client = firestore.client() |
| 61 | + assert client is not None |
| 62 | + assert client.project == 'mock-project-id' |
| 63 | + |
| 64 | +def test_service_account_with_explicit_app(): |
| 65 | + cred = credentials.Certificate(testutils.resource_filename('service_account.json')) |
| 66 | + app = firebase_admin.initialize_app(cred) |
| 67 | + client = firestore.client(app=app) |
| 68 | + assert client is not None |
| 69 | + assert client.project == 'mock-project-id' |
| 70 | + |
| 71 | +def test_geo_point(): |
| 72 | + geo_point = firestore.GeoPoint(10, 20) # pylint: disable=no-member |
| 73 | + assert geo_point.latitude == 10 |
| 74 | + assert geo_point.longitude == 20 |
| 75 | + |
| 76 | +def test_server_timestamp(): |
| 77 | + assert firestore.SERVER_TIMESTAMP is not None # pylint: disable=no-member |
0 commit comments