Skip to content

Commit 82a8a60

Browse files
authored
PYTHON-5077 - Convert test.test_data_lake to async (#2091)
1 parent c2e7fae commit 82a8a60

File tree

3 files changed

+123
-9
lines changed

3 files changed

+123
-9
lines changed

test/asynchronous/test_data_lake.py

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
# Copyright 2020-present MongoDB, 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+
"""Test Atlas Data Lake."""
16+
from __future__ import annotations
17+
18+
import os
19+
import sys
20+
from pathlib import Path
21+
22+
import pytest
23+
24+
sys.path[0:0] = [""]
25+
26+
from test.asynchronous import AsyncIntegrationTest, AsyncUnitTest, async_client_context, unittest
27+
from test.asynchronous.unified_format import generate_test_classes
28+
from test.utils import (
29+
OvertCommandListener,
30+
)
31+
32+
from pymongo.asynchronous.helpers import anext
33+
34+
_IS_SYNC = False
35+
36+
pytestmark = pytest.mark.data_lake
37+
38+
39+
class TestDataLakeMustConnect(AsyncUnitTest):
40+
async def test_connected_to_data_lake(self):
41+
data_lake = os.environ.get("TEST_DATA_LAKE")
42+
if not data_lake:
43+
self.skipTest("TEST_DATA_LAKE is not set")
44+
45+
self.assertTrue(
46+
async_client_context.is_data_lake and async_client_context.connected,
47+
"client context must be connected to data lake when DATA_LAKE is set. Failed attempts:\n{}".format(
48+
async_client_context.connection_attempt_info()
49+
),
50+
)
51+
52+
53+
class TestDataLakeProse(AsyncIntegrationTest):
54+
# Default test database and collection names.
55+
TEST_DB = "test"
56+
TEST_COLLECTION = "driverdata"
57+
58+
@async_client_context.require_data_lake
59+
async def asyncSetUp(self):
60+
await super().asyncSetUp()
61+
62+
# Test killCursors
63+
async def test_1(self):
64+
listener = OvertCommandListener()
65+
client = await self.async_rs_or_single_client(event_listeners=[listener])
66+
cursor = client[self.TEST_DB][self.TEST_COLLECTION].find({}, batch_size=2)
67+
await anext(cursor)
68+
69+
# find command assertions
70+
find_cmd = listener.succeeded_events[-1]
71+
self.assertEqual(find_cmd.command_name, "find")
72+
cursor_id = find_cmd.reply["cursor"]["id"]
73+
cursor_ns = find_cmd.reply["cursor"]["ns"]
74+
75+
# killCursors command assertions
76+
await cursor.close()
77+
started = listener.started_events[-1]
78+
self.assertEqual(started.command_name, "killCursors")
79+
succeeded = listener.succeeded_events[-1]
80+
self.assertEqual(succeeded.command_name, "killCursors")
81+
82+
self.assertIn(cursor_id, started.command["cursors"])
83+
target_ns = ".".join([started.command["$db"], started.command["killCursors"]])
84+
self.assertEqual(cursor_ns, target_ns)
85+
86+
self.assertIn(cursor_id, succeeded.reply["cursorsKilled"])
87+
88+
# Test no auth
89+
async def test_2(self):
90+
client = await self.async_rs_client_noauth()
91+
await client.admin.command("ping")
92+
93+
# Test with auth
94+
async def test_3(self):
95+
for mechanism in ["SCRAM-SHA-1", "SCRAM-SHA-256"]:
96+
client = await self.async_rs_or_single_client(authMechanism=mechanism)
97+
await client[self.TEST_DB][self.TEST_COLLECTION].find_one()
98+
99+
100+
# Location of JSON test specifications.
101+
if _IS_SYNC:
102+
TEST_PATH = Path(__file__).parent / "data_lake/unified"
103+
else:
104+
TEST_PATH = Path(__file__).parent.parent / "data_lake/unified"
105+
106+
# Generate unified tests.
107+
globals().update(generate_test_classes(TEST_PATH, module=__name__))
108+
109+
110+
if __name__ == "__main__":
111+
unittest.main()

test/test_data_lake.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,20 @@
2323

2424
sys.path[0:0] = [""]
2525

26-
from test import IntegrationTest, client_context, unittest
26+
from test import IntegrationTest, UnitTest, client_context, unittest
2727
from test.unified_format import generate_test_classes
2828
from test.utils import (
2929
OvertCommandListener,
3030
)
3131

32-
pytestmark = pytest.mark.data_lake
32+
from pymongo.synchronous.helpers import next
3333

34+
_IS_SYNC = True
3435

35-
# Location of JSON test specifications.
36-
_TEST_PATH = os.path.join(os.path.dirname(os.path.realpath(__file__)), "data_lake")
36+
pytestmark = pytest.mark.data_lake
3737

3838

39-
class TestDataLakeMustConnect(unittest.TestCase):
39+
class TestDataLakeMustConnect(UnitTest):
4040
def test_connected_to_data_lake(self):
4141
data_lake = os.environ.get("TEST_DATA_LAKE")
4242
if not data_lake:
@@ -55,10 +55,9 @@ class TestDataLakeProse(IntegrationTest):
5555
TEST_DB = "test"
5656
TEST_COLLECTION = "driverdata"
5757

58-
@classmethod
5958
@client_context.require_data_lake
60-
def setUpClass(cls):
61-
super().setUpClass()
59+
def setUp(self):
60+
super().setUp()
6261

6362
# Test killCursors
6463
def test_1(self):
@@ -99,7 +98,10 @@ def test_3(self):
9998

10099

101100
# Location of JSON test specifications.
102-
TEST_PATH = Path(__file__).parent / "data_lake/unified"
101+
if _IS_SYNC:
102+
TEST_PATH = Path(__file__).parent / "data_lake/unified"
103+
else:
104+
TEST_PATH = Path(__file__).parent.parent / "data_lake/unified"
103105

104106
# Generate unified tests.
105107
globals().update(generate_test_classes(TEST_PATH, module=__name__))

tools/synchro.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,7 @@ def async_only_test(f: str) -> bool:
203203
"test_crud_unified.py",
204204
"test_cursor.py",
205205
"test_database.py",
206+
"test_data_lake.py",
206207
"test_encryption.py",
207208
"test_grid_file.py",
208209
"test_logger.py",

0 commit comments

Comments
 (0)