Skip to content

Commit d79eee5

Browse files
authored
PYTHON-4521 Example transition of an existing test to an async one (mongodb#1702)
1 parent a5c0db6 commit d79eee5

File tree

3 files changed

+108
-3
lines changed

3 files changed

+108
-3
lines changed

test/asynchronous/test_logger.py

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
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+
from __future__ import annotations
15+
16+
import os
17+
from test import unittest
18+
from test.asynchronous import AsyncIntegrationTest
19+
from test.utils import async_single_client
20+
from unittest.mock import patch
21+
22+
from bson import json_util
23+
from pymongo.errors import OperationFailure
24+
from pymongo.logger import _DEFAULT_DOCUMENT_LENGTH
25+
26+
_IS_SYNC = False
27+
28+
29+
# https://github.com/mongodb/specifications/tree/master/source/command-logging-and-monitoring/tests#prose-tests
30+
class TestLogger(AsyncIntegrationTest):
31+
async def test_default_truncation_limit(self):
32+
docs = [{"x": "y"} for _ in range(100)]
33+
db = self.db
34+
35+
with patch.dict("os.environ"):
36+
os.environ.pop("MONGOB_LOG_MAX_DOCUMENT_LENGTH", None)
37+
with self.assertLogs("pymongo.command", level="DEBUG") as cm:
38+
await db.test.insert_many(docs)
39+
40+
cmd_started_log = json_util.loads(cm.records[0].message)
41+
self.assertEqual(len(cmd_started_log["command"]), _DEFAULT_DOCUMENT_LENGTH + 3)
42+
43+
cmd_succeeded_log = json_util.loads(cm.records[1].message)
44+
self.assertLessEqual(len(cmd_succeeded_log["reply"]), _DEFAULT_DOCUMENT_LENGTH + 3)
45+
46+
with self.assertLogs("pymongo.command", level="DEBUG") as cm:
47+
await db.test.find({}).to_list()
48+
cmd_succeeded_log = json_util.loads(cm.records[1].message)
49+
self.assertEqual(len(cmd_succeeded_log["reply"]), _DEFAULT_DOCUMENT_LENGTH + 3)
50+
51+
async def test_configured_truncation_limit(self):
52+
cmd = {"hello": True}
53+
db = self.db
54+
with patch.dict("os.environ", {"MONGOB_LOG_MAX_DOCUMENT_LENGTH": "5"}):
55+
with self.assertLogs("pymongo.command", level="DEBUG") as cm:
56+
await db.command(cmd)
57+
58+
cmd_started_log = json_util.loads(cm.records[0].message)
59+
self.assertEqual(len(cmd_started_log["command"]), 5 + 3)
60+
61+
cmd_succeeded_log = json_util.loads(cm.records[1].message)
62+
self.assertLessEqual(len(cmd_succeeded_log["reply"]), 5 + 3)
63+
with self.assertRaises(OperationFailure):
64+
await db.command({"notARealCommand": True})
65+
cmd_failed_log = json_util.loads(cm.records[-1].message)
66+
self.assertEqual(len(cmd_failed_log["failure"]), 5 + 3)
67+
68+
async def test_truncation_multi_byte_codepoints(self):
69+
document_lengths = ["20000", "20001", "20002"]
70+
multi_byte_char_str_len = 50_000
71+
str_to_repeat = "界"
72+
73+
multi_byte_char_str = ""
74+
for i in range(multi_byte_char_str_len):
75+
multi_byte_char_str += str_to_repeat
76+
77+
for length in document_lengths:
78+
with patch.dict("os.environ", {"MONGOB_LOG_MAX_DOCUMENT_LENGTH": length}):
79+
with self.assertLogs("pymongo.command", level="DEBUG") as cm:
80+
await self.db.test.insert_one({"x": multi_byte_char_str})
81+
cmd_started_log = json_util.loads(cm.records[0].message)["command"]
82+
83+
cmd_started_log = cmd_started_log[:-3]
84+
last_3_bytes = cmd_started_log.encode()[-3:].decode()
85+
86+
self.assertEqual(last_3_bytes, str_to_repeat)
87+
88+
async def test_logging_without_listeners(self):
89+
c = await async_single_client()
90+
self.assertEqual(len(c._event_listeners.event_listeners()), 0)
91+
with self.assertLogs("pymongo.connection", level="DEBUG") as cm:
92+
await c.db.test.insert_one({"x": "1"})
93+
self.assertGreater(len(cm.records), 0)
94+
with self.assertLogs("pymongo.command", level="DEBUG") as cm:
95+
await c.db.test.insert_one({"x": "1"})
96+
self.assertGreater(len(cm.records), 0)
97+
with self.assertLogs("pymongo.serverSelection", level="DEBUG") as cm:
98+
await c.db.test.insert_one({"x": "1"})
99+
self.assertGreater(len(cm.records), 0)
100+
101+
102+
if __name__ == "__main__":
103+
unittest.main()

test/test_logger.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,16 @@
1414
from __future__ import annotations
1515

1616
import os
17-
from test import unittest
18-
from test.test_client import IntegrationTest
17+
from test import IntegrationTest, unittest
1918
from test.utils import single_client
2019
from unittest.mock import patch
2120

2221
from bson import json_util
2322
from pymongo.errors import OperationFailure
2423
from pymongo.logger import _DEFAULT_DOCUMENT_LENGTH
2524

25+
_IS_SYNC = True
26+
2627

2728
# https://github.com/mongodb/specifications/tree/master/source/command-logging-and-monitoring/tests#prose-tests
2829
class TestLogger(IntegrationTest):
@@ -42,7 +43,7 @@ def test_default_truncation_limit(self):
4243
self.assertLessEqual(len(cmd_succeeded_log["reply"]), _DEFAULT_DOCUMENT_LENGTH + 3)
4344

4445
with self.assertLogs("pymongo.command", level="DEBUG") as cm:
45-
list(db.test.find({}))
46+
db.test.find({}).to_list()
4647
cmd_succeeded_log = json_util.loads(cm.records[1].message)
4748
self.assertEqual(len(cmd_succeeded_log["reply"]), _DEFAULT_DOCUMENT_LENGTH + 3)
4849

tools/synchro.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@
154154
"test_collection.py",
155155
"test_cursor.py",
156156
"test_database.py",
157+
"test_logger.py",
157158
"test_session.py",
158159
"test_transactions.py",
159160
]

0 commit comments

Comments
 (0)