diff --git a/integration/conftest.py b/integration/conftest.py index 169e02d5b..71f53f612 100644 --- a/integration/conftest.py +++ b/integration/conftest.py @@ -15,6 +15,7 @@ """pytest configuration and global fixtures for integration tests.""" import json +import asyncio import pytest import firebase_admin @@ -70,3 +71,12 @@ def api_key(request): 'command-line option.') with open(path) as keyfile: return keyfile.read().strip() + +@pytest.fixture(scope="session") +def event_loop(): + """Create an instance of the default event loop for test session. + This avoids early eventloop closure. + """ + loop = asyncio.get_event_loop_policy().new_event_loop() + yield loop + loop.close() diff --git a/integration/test_firestore_async.py b/integration/test_firestore_async.py new file mode 100644 index 000000000..2a5b93217 --- /dev/null +++ b/integration/test_firestore_async.py @@ -0,0 +1,53 @@ +# Copyright 2022 Google Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +"""Integration tests for firebase_admin.firestore_async module.""" +import datetime +import pytest + +from firebase_admin import firestore_async + +@pytest.mark.asyncio +async def test_firestore_async(): + client = firestore_async.client() + expected = { + 'name': u'Mountain View', + 'country': u'USA', + 'population': 77846, + 'capital': False + } + doc = client.collection('cities').document() + await doc.set(expected) + + data = await doc.get() + assert data.to_dict() == expected + + await doc.delete() + data = await doc.get() + assert data.exists is False + +@pytest.mark.asyncio +async def test_server_timestamp(): + client = firestore_async.client() + expected = { + 'name': u'Mountain View', + 'timestamp': firestore_async.SERVER_TIMESTAMP # pylint: disable=no-member + } + doc = client.collection('cities').document() + await doc.set(expected) + + data = await doc.get() + data = data.to_dict() + assert isinstance(data['timestamp'], datetime.datetime) + await doc.delete() diff --git a/requirements.txt b/requirements.txt index 0dd529c04..87142fe93 100644 --- a/requirements.txt +++ b/requirements.txt @@ -3,6 +3,7 @@ pylint == 2.3.1 pytest >= 6.2.0 pytest-cov >= 2.4.0 pytest-localserver >= 0.4.1 +pytest-asyncio >= 0.16.0 cachecontrol >= 0.12.6 google-api-core[grpc] >= 1.22.1, < 3.0.0dev; platform.python_implementation != 'PyPy'