Skip to content

Commit 1fda6a2

Browse files
authored
PYTHON-5110 - Convert test.test_unified_format to async (#2130)
1 parent 665eb9a commit 1fda6a2

File tree

5 files changed

+114
-7
lines changed

5 files changed

+114
-7
lines changed
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
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+
from __future__ import annotations
15+
16+
import os
17+
import sys
18+
from pathlib import Path
19+
from typing import Any
20+
21+
sys.path[0:0] = [""]
22+
23+
from test import UnitTest, unittest
24+
from test.asynchronous.unified_format import MatchEvaluatorUtil, generate_test_classes
25+
26+
from bson import ObjectId
27+
28+
_IS_SYNC = False
29+
30+
# Location of JSON test specifications.
31+
if _IS_SYNC:
32+
TEST_PATH = os.path.join(Path(__file__).resolve().parent, "unified-test-format")
33+
else:
34+
TEST_PATH = os.path.join(Path(__file__).resolve().parent.parent, "unified-test-format")
35+
36+
37+
globals().update(
38+
generate_test_classes(
39+
os.path.join(TEST_PATH, "valid-pass"),
40+
module=__name__,
41+
class_name_prefix="UnifiedTestFormat",
42+
expected_failures=[
43+
"Client side error in command starting transaction", # PYTHON-1894
44+
],
45+
RUN_ON_SERVERLESS=False,
46+
)
47+
)
48+
49+
50+
globals().update(
51+
generate_test_classes(
52+
os.path.join(TEST_PATH, "valid-fail"),
53+
module=__name__,
54+
class_name_prefix="UnifiedTestFormat",
55+
bypass_test_generation_errors=True,
56+
expected_failures=[
57+
".*", # All tests expected to fail
58+
],
59+
RUN_ON_SERVERLESS=False,
60+
)
61+
)
62+
63+
64+
class TestMatchEvaluatorUtil(UnitTest):
65+
def setUp(self):
66+
self.match_evaluator = MatchEvaluatorUtil(self)
67+
68+
def test_unsetOrMatches(self):
69+
spec: dict[str, Any] = {"$$unsetOrMatches": {"y": {"$$unsetOrMatches": 2}}}
70+
for actual in [{}, {"y": 2}, None]:
71+
self.match_evaluator.match_result(spec, actual)
72+
73+
spec = {"x": {"$$unsetOrMatches": {"y": {"$$unsetOrMatches": 2}}}}
74+
for actual in [{}, {"x": {}}, {"x": {"y": 2}}]:
75+
self.match_evaluator.match_result(spec, actual)
76+
77+
spec = {"y": {"$$unsetOrMatches": {"$$exists": True}}}
78+
self.match_evaluator.match_result(spec, {})
79+
self.match_evaluator.match_result(spec, {"y": 2})
80+
self.match_evaluator.match_result(spec, {"x": 1})
81+
self.match_evaluator.match_result(spec, {"y": {}})
82+
83+
def test_type(self):
84+
self.match_evaluator.match_result(
85+
{
86+
"operationType": "insert",
87+
"ns": {"db": "change-stream-tests", "coll": "test"},
88+
"fullDocument": {"_id": {"$$type": "objectId"}, "x": 1},
89+
},
90+
{
91+
"operationType": "insert",
92+
"fullDocument": {"_id": ObjectId("5fc93511ac93941052098f0c"), "x": 1},
93+
"ns": {"db": "change-stream-tests", "coll": "test"},
94+
},
95+
)
96+
97+
98+
if __name__ == "__main__":
99+
unittest.main()

test/asynchronous/unified_format.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -711,7 +711,7 @@ async def _databaseOperation_runCommand(self, target, **kwargs):
711711
return await target.command(**kwargs)
712712

713713
async def _databaseOperation_runCursorCommand(self, target, **kwargs):
714-
return list(await self._databaseOperation_createCommandCursor(target, **kwargs))
714+
return await (await self._databaseOperation_createCommandCursor(target, **kwargs)).to_list()
715715

716716
async def _databaseOperation_createCommandCursor(self, target, **kwargs):
717717
self.__raise_if_unsupported("createCommandCursor", target, AsyncDatabase)

test/test_unified_format.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,28 @@
1515

1616
import os
1717
import sys
18+
from pathlib import Path
1819
from typing import Any
1920

2021
sys.path[0:0] = [""]
2122

22-
from test import unittest
23+
from test import UnitTest, unittest
2324
from test.unified_format import MatchEvaluatorUtil, generate_test_classes
2425

2526
from bson import ObjectId
2627

27-
_TEST_PATH = os.path.join(os.path.dirname(os.path.realpath(__file__)), "unified-test-format")
28+
_IS_SYNC = True
29+
30+
# Location of JSON test specifications.
31+
if _IS_SYNC:
32+
TEST_PATH = os.path.join(Path(__file__).resolve().parent, "unified-test-format")
33+
else:
34+
TEST_PATH = os.path.join(Path(__file__).resolve().parent.parent, "unified-test-format")
2835

2936

3037
globals().update(
3138
generate_test_classes(
32-
os.path.join(_TEST_PATH, "valid-pass"),
39+
os.path.join(TEST_PATH, "valid-pass"),
3340
module=__name__,
3441
class_name_prefix="UnifiedTestFormat",
3542
expected_failures=[
@@ -42,7 +49,7 @@
4249

4350
globals().update(
4451
generate_test_classes(
45-
os.path.join(_TEST_PATH, "valid-fail"),
52+
os.path.join(TEST_PATH, "valid-fail"),
4653
module=__name__,
4754
class_name_prefix="UnifiedTestFormat",
4855
bypass_test_generation_errors=True,
@@ -54,7 +61,7 @@
5461
)
5562

5663

57-
class TestMatchEvaluatorUtil(unittest.TestCase):
64+
class TestMatchEvaluatorUtil(UnitTest):
5865
def setUp(self):
5966
self.match_evaluator = MatchEvaluatorUtil(self)
6067

test/unified_format.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -708,7 +708,7 @@ def _databaseOperation_runCommand(self, target, **kwargs):
708708
return target.command(**kwargs)
709709

710710
def _databaseOperation_runCursorCommand(self, target, **kwargs):
711-
return list(self._databaseOperation_createCommandCursor(target, **kwargs))
711+
return (self._databaseOperation_createCommandCursor(target, **kwargs)).to_list()
712712

713713
def _databaseOperation_createCommandCursor(self, target, **kwargs):
714714
self.__raise_if_unsupported("createCommandCursor", target, Database)

tools/synchro.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,7 @@ def async_only_test(f: str) -> bool:
228228
"test_session.py",
229229
"test_srv_polling.py",
230230
"test_transactions.py",
231+
"test_unified_format.py",
231232
"unified_format.py",
232233
]
233234

0 commit comments

Comments
 (0)