Skip to content

Commit f0ea4f9

Browse files
authored
Merge branch 'master' into PYTHON-4842-2
2 parents 2092ee2 + 33163ec commit f0ea4f9

11 files changed

+386
-47
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# Copyright 2023-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+
"""Run the command monitoring unified format spec tests."""
16+
from __future__ import annotations
17+
18+
import os
19+
import pathlib
20+
import sys
21+
22+
sys.path[0:0] = [""]
23+
24+
from test import unittest
25+
from test.asynchronous.unified_format import generate_test_classes
26+
27+
_IS_SYNC = False
28+
29+
# Location of JSON test specifications.
30+
if _IS_SYNC:
31+
_TEST_PATH = os.path.join(pathlib.Path(__file__).resolve().parent, "command_logging")
32+
else:
33+
_TEST_PATH = os.path.join(pathlib.Path(__file__).resolve().parent.parent, "command_logging")
34+
35+
36+
globals().update(
37+
generate_test_classes(
38+
_TEST_PATH,
39+
module=__name__,
40+
)
41+
)
42+
43+
if __name__ == "__main__":
44+
unittest.main()
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# Copyright 2015-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+
"""Run the command monitoring unified format spec tests."""
16+
from __future__ import annotations
17+
18+
import os
19+
import pathlib
20+
import sys
21+
22+
sys.path[0:0] = [""]
23+
24+
from test import unittest
25+
from test.asynchronous.unified_format import generate_test_classes
26+
27+
_IS_SYNC = False
28+
29+
# Location of JSON test specifications.
30+
if _IS_SYNC:
31+
_TEST_PATH = os.path.join(pathlib.Path(__file__).resolve().parent, "command_monitoring")
32+
else:
33+
_TEST_PATH = os.path.join(pathlib.Path(__file__).resolve().parent.parent, "command_monitoring")
34+
35+
36+
globals().update(
37+
generate_test_classes(
38+
_TEST_PATH,
39+
module=__name__,
40+
)
41+
)
42+
43+
44+
if __name__ == "__main__":
45+
unittest.main()

test/asynchronous/test_comment.py

Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
# Copyright 2022-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 the keyword argument 'comment' in various helpers."""
16+
17+
from __future__ import annotations
18+
19+
import inspect
20+
import sys
21+
22+
sys.path[0:0] = [""]
23+
from asyncio import iscoroutinefunction
24+
from test.asynchronous import AsyncIntegrationTest, async_client_context, unittest
25+
from test.utils import OvertCommandListener
26+
27+
from bson.dbref import DBRef
28+
from pymongo.asynchronous.command_cursor import AsyncCommandCursor
29+
from pymongo.operations import IndexModel
30+
31+
_IS_SYNC = False
32+
33+
34+
class AsyncTestComment(AsyncIntegrationTest):
35+
async def _test_ops(
36+
self,
37+
helpers,
38+
already_supported,
39+
listener,
40+
):
41+
for h, args in helpers:
42+
c = "testing comment with " + h.__name__
43+
with self.subTest("collection-" + h.__name__ + "-comment"):
44+
for cc in [c, {"key": c}, ["any", 1]]:
45+
listener.reset()
46+
kwargs = {"comment": cc}
47+
try:
48+
maybe_cursor = await h(*args, **kwargs)
49+
except Exception:
50+
maybe_cursor = None
51+
self.assertIn(
52+
"comment",
53+
inspect.signature(h).parameters,
54+
msg="Could not find 'comment' in the "
55+
"signature of function %s" % (h.__name__),
56+
)
57+
self.assertEqual(
58+
inspect.signature(h).parameters["comment"].annotation, "Optional[Any]"
59+
)
60+
if isinstance(maybe_cursor, AsyncCommandCursor):
61+
await maybe_cursor.close()
62+
63+
cmd = listener.started_events[0]
64+
self.assertEqual(cc, cmd.command.get("comment"), msg=cmd)
65+
66+
if h.__name__ != "aggregate_raw_batches":
67+
self.assertIn(
68+
":param comment:",
69+
h.__doc__,
70+
)
71+
if h not in already_supported:
72+
self.assertIn(
73+
"Added ``comment`` parameter",
74+
h.__doc__,
75+
)
76+
else:
77+
self.assertNotIn(
78+
"Added ``comment`` parameter",
79+
h.__doc__,
80+
)
81+
82+
listener.reset()
83+
84+
@async_client_context.require_version_min(4, 7, -1)
85+
@async_client_context.require_replica_set
86+
async def test_database_helpers(self):
87+
listener = OvertCommandListener()
88+
db = (await self.async_rs_or_single_client(event_listeners=[listener])).db
89+
helpers = [
90+
(db.watch, []),
91+
(db.command, ["hello"]),
92+
(db.list_collections, []),
93+
(db.list_collection_names, []),
94+
(db.drop_collection, ["hello"]),
95+
(db.validate_collection, ["test"]),
96+
(db.dereference, [DBRef("collection", 1)]),
97+
]
98+
already_supported = [db.command, db.list_collections, db.list_collection_names]
99+
await self._test_ops(helpers, already_supported, listener)
100+
101+
@async_client_context.require_version_min(4, 7, -1)
102+
@async_client_context.require_replica_set
103+
async def test_client_helpers(self):
104+
listener = OvertCommandListener()
105+
cli = await self.async_rs_or_single_client(event_listeners=[listener])
106+
helpers = [
107+
(cli.watch, []),
108+
(cli.list_databases, []),
109+
(cli.list_database_names, []),
110+
(cli.drop_database, ["test"]),
111+
]
112+
already_supported = [
113+
cli.list_databases,
114+
]
115+
await self._test_ops(helpers, already_supported, listener)
116+
117+
@async_client_context.require_version_min(4, 7, -1)
118+
async def test_collection_helpers(self):
119+
listener = OvertCommandListener()
120+
db = (await self.async_rs_or_single_client(event_listeners=[listener]))[self.db.name]
121+
coll = db.get_collection("test")
122+
123+
helpers = [
124+
(coll.list_indexes, []),
125+
(coll.drop, []),
126+
(coll.index_information, []),
127+
(coll.options, []),
128+
(coll.aggregate, [[{"$set": {"x": 1}}]]),
129+
(coll.aggregate_raw_batches, [[{"$set": {"x": 1}}]]),
130+
(coll.rename, ["temp_temp_temp"]),
131+
(coll.distinct, ["_id"]),
132+
(coll.find_one_and_delete, [{}]),
133+
(coll.find_one_and_replace, [{}, {}]),
134+
(coll.find_one_and_update, [{}, {"$set": {"a": 1}}]),
135+
(coll.estimated_document_count, []),
136+
(coll.count_documents, [{}]),
137+
(coll.create_indexes, [[IndexModel("a")]]),
138+
(coll.create_index, ["a"]),
139+
(coll.drop_index, [[("a", 1)]]),
140+
(coll.drop_indexes, []),
141+
]
142+
already_supported = [
143+
coll.estimated_document_count,
144+
coll.count_documents,
145+
coll.create_indexes,
146+
coll.drop_indexes,
147+
coll.options,
148+
coll.find_one_and_replace,
149+
coll.drop_index,
150+
coll.rename,
151+
coll.distinct,
152+
coll.find_one_and_delete,
153+
coll.find_one_and_update,
154+
]
155+
await self._test_ops(helpers, already_supported, listener)
156+
157+
158+
if __name__ == "__main__":
159+
unittest.main()
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# Copyright 2023-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+
"""Run the connection logging unified format spec tests."""
16+
from __future__ import annotations
17+
18+
import os
19+
import pathlib
20+
import sys
21+
22+
sys.path[0:0] = [""]
23+
24+
from test import unittest
25+
from test.unified_format import generate_test_classes
26+
27+
_IS_SYNC = False
28+
29+
# Location of JSON test specifications.
30+
if _IS_SYNC:
31+
_TEST_PATH = os.path.join(pathlib.Path(__file__).resolve().parent, "connection_logging")
32+
else:
33+
_TEST_PATH = os.path.join(pathlib.Path(__file__).resolve().parent.parent, "connection_logging")
34+
35+
36+
globals().update(
37+
generate_test_classes(
38+
_TEST_PATH,
39+
module=__name__,
40+
)
41+
)
42+
43+
44+
if __name__ == "__main__":
45+
unittest.main()
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Copyright 2021-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 the CRUD unified spec tests."""
16+
from __future__ import annotations
17+
18+
import os
19+
import pathlib
20+
import sys
21+
22+
sys.path[0:0] = [""]
23+
24+
from test import unittest
25+
from test.unified_format import generate_test_classes
26+
27+
_IS_SYNC = False
28+
29+
# Location of JSON test specifications.
30+
if _IS_SYNC:
31+
_TEST_PATH = os.path.join(pathlib.Path(__file__).resolve().parent, "crud", "unified")
32+
else:
33+
_TEST_PATH = os.path.join(pathlib.Path(__file__).resolve().parent.parent, "crud", "unified")
34+
35+
# Generate unified tests.
36+
globals().update(generate_test_classes(_TEST_PATH, module=__name__, RUN_ON_SERVERLESS=True))
37+
38+
if __name__ == "__main__":
39+
unittest.main()

test/test_command_logging.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,22 @@
1616
from __future__ import annotations
1717

1818
import os
19+
import pathlib
1920
import sys
2021

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

2324
from test import unittest
2425
from test.unified_format import generate_test_classes
2526

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

2936
globals().update(
3037
generate_test_classes(

test/test_command_monitoring.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,21 @@
1616
from __future__ import annotations
1717

1818
import os
19+
import pathlib
1920
import sys
2021

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

2324
from test import unittest
2425
from test.unified_format import generate_test_classes
2526

27+
_IS_SYNC = True
28+
2629
# Location of JSON test specifications.
27-
_TEST_PATH = os.path.join(os.path.dirname(os.path.realpath(__file__)), "command_monitoring")
30+
if _IS_SYNC:
31+
_TEST_PATH = os.path.join(pathlib.Path(__file__).resolve().parent, "command_monitoring")
32+
else:
33+
_TEST_PATH = os.path.join(pathlib.Path(__file__).resolve().parent.parent, "command_monitoring")
2834

2935

3036
globals().update(

0 commit comments

Comments
 (0)