Skip to content

Commit a16b3f6

Browse files
committed
Fix async
1 parent 0e48ce5 commit a16b3f6

File tree

2 files changed

+53
-55
lines changed

2 files changed

+53
-55
lines changed

test/asynchronous/test_dns.py

Lines changed: 30 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -40,26 +40,6 @@
4040
_IS_SYNC = False
4141

4242

43-
def run_initial_dns_seedlist_discovery_prose_tests(self, test_cases):
44-
for case in test_cases:
45-
with patch("dns.resolver.resolve") as mock_resolver:
46-
47-
def mock_resolve(query, record_type, *args, **kwargs):
48-
mock_srv = MagicMock()
49-
mock_srv.target.to_text.return_value = case["mock_target"]
50-
return [mock_srv]
51-
52-
mock_resolver.side_effect = mock_resolve
53-
domain = case["query"].split("._tcp.")[1]
54-
connection_string = f"mongodb+srv://{domain}"
55-
try:
56-
parse_uri(connection_string)
57-
except ConfigurationError as e:
58-
self.assertIn(case["expected_error"], str(e))
59-
else:
60-
self.fail(f"ConfigurationError was not raised for query: {case['query']}")
61-
62-
6343
class TestDNSRepl(AsyncPyMongoTestCase):
6444
if _IS_SYNC:
6545
TEST_PATH = os.path.join(
@@ -228,12 +208,31 @@ class TestInitialDnsSeedlistDiscovery(AsyncPyMongoTestCase):
228208
https://github.com/mongodb/specifications/blob/0a7a8b5/source/initial-dns-seedlist-discovery/tests/README.md#prose-tests
229209
"""
230210

231-
def test_1_allow_srv_hosts_with_fewer_than_three_dot_separated_parts(self):
232-
with patch("dns.resolver.resolve"):
233-
parse_uri("mongodb+srv://localhost/")
234-
parse_uri("mongodb+srv://mongo.local/")
235-
236-
def test_2_throw_when_return_address_does_not_end_with_srv_domain(self):
211+
async def run_initial_dns_seedlist_discovery_prose_tests(self, test_cases):
212+
for case in test_cases:
213+
with patch("dns.asyncresolver.resolve") as mock_resolver:
214+
215+
async def mock_resolve(query, record_type, *args, **kwargs):
216+
mock_srv = MagicMock()
217+
mock_srv.target.to_text.return_value = case["mock_target"]
218+
return [mock_srv]
219+
220+
mock_resolver.side_effect = mock_resolve
221+
domain = case["query"].split("._tcp.")[1]
222+
connection_string = f"mongodb+srv://{domain}"
223+
try:
224+
await parse_uri(connection_string)
225+
except ConfigurationError as e:
226+
self.assertIn(case["expected_error"], str(e))
227+
else:
228+
self.fail(f"ConfigurationError was not raised for query: {case['query']}")
229+
230+
async def test_1_allow_srv_hosts_with_fewer_than_three_dot_separated_parts(self):
231+
with patch("dns.asyncresolver.resolve"):
232+
await parse_uri("mongodb+srv://localhost/")
233+
await parse_uri("mongodb+srv://mongo.local/")
234+
235+
async def test_2_throw_when_return_address_does_not_end_with_srv_domain(self):
237236
test_cases = [
238237
{
239238
"query": "_mongodb._tcp.localhost",
@@ -251,9 +250,9 @@ def test_2_throw_when_return_address_does_not_end_with_srv_domain(self):
251250
"expected_error": "Invalid SRV host",
252251
},
253252
]
254-
run_initial_dns_seedlist_discovery_prose_tests(self, test_cases)
253+
await self.run_initial_dns_seedlist_discovery_prose_tests(test_cases)
255254

256-
def test_3_throw_when_return_address_is_identical_to_srv_hostname(self):
255+
async def test_3_throw_when_return_address_is_identical_to_srv_hostname(self):
257256
test_cases = [
258257
{
259258
"query": "_mongodb._tcp.localhost",
@@ -266,9 +265,9 @@ def test_3_throw_when_return_address_is_identical_to_srv_hostname(self):
266265
"expected_error": "Invalid SRV host",
267266
},
268267
]
269-
run_initial_dns_seedlist_discovery_prose_tests(self, test_cases)
268+
await self.run_initial_dns_seedlist_discovery_prose_tests(test_cases)
270269

271-
def test_4_throw_when_return_address_does_not_contain_dot_separating_shared_part_of_domain(
270+
async def test_4_throw_when_return_address_does_not_contain_dot_separating_shared_part_of_domain(
272271
self
273272
):
274273
test_cases = [
@@ -288,7 +287,7 @@ def test_4_throw_when_return_address_does_not_contain_dot_separating_shared_part
288287
"expected_error": "Invalid SRV host",
289288
},
290289
]
291-
run_initial_dns_seedlist_discovery_prose_tests(self, test_cases)
290+
await self.run_initial_dns_seedlist_discovery_prose_tests(test_cases)
292291

293292

294293
if __name__ == "__main__":

test/test_dns.py

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -40,26 +40,6 @@
4040
_IS_SYNC = True
4141

4242

43-
def run_initial_dns_seedlist_discovery_prose_tests(self, test_cases):
44-
for case in test_cases:
45-
with patch("dns.resolver.resolve") as mock_resolver:
46-
47-
def mock_resolve(query, record_type, *args, **kwargs):
48-
mock_srv = MagicMock()
49-
mock_srv.target.to_text.return_value = case["mock_target"]
50-
return [mock_srv]
51-
52-
mock_resolver.side_effect = mock_resolve
53-
domain = case["query"].split("._tcp.")[1]
54-
connection_string = f"mongodb+srv://{domain}"
55-
try:
56-
parse_uri(connection_string)
57-
except ConfigurationError as e:
58-
self.assertIn(case["expected_error"], str(e))
59-
else:
60-
self.fail(f"ConfigurationError was not raised for query: {case['query']}")
61-
62-
6343
class TestDNSRepl(PyMongoTestCase):
6444
if _IS_SYNC:
6545
TEST_PATH = os.path.join(
@@ -226,8 +206,27 @@ class TestInitialDnsSeedlistDiscovery(PyMongoTestCase):
226206
https://github.com/mongodb/specifications/blob/0a7a8b5/source/initial-dns-seedlist-discovery/tests/README.md#prose-tests
227207
"""
228208

209+
def run_initial_dns_seedlist_discovery_prose_tests(self, test_cases):
210+
for case in test_cases:
211+
with patch("dns.asyncresolver.resolve") as mock_resolver:
212+
213+
def mock_resolve(query, record_type, *args, **kwargs):
214+
mock_srv = MagicMock()
215+
mock_srv.target.to_text.return_value = case["mock_target"]
216+
return [mock_srv]
217+
218+
mock_resolver.side_effect = mock_resolve
219+
domain = case["query"].split("._tcp.")[1]
220+
connection_string = f"mongodb+srv://{domain}"
221+
try:
222+
parse_uri(connection_string)
223+
except ConfigurationError as e:
224+
self.assertIn(case["expected_error"], str(e))
225+
else:
226+
self.fail(f"ConfigurationError was not raised for query: {case['query']}")
227+
229228
def test_1_allow_srv_hosts_with_fewer_than_three_dot_separated_parts(self):
230-
with patch("dns.resolver.resolve"):
229+
with patch("dns.asyncresolver.resolve"):
231230
parse_uri("mongodb+srv://localhost/")
232231
parse_uri("mongodb+srv://mongo.local/")
233232

@@ -249,7 +248,7 @@ def test_2_throw_when_return_address_does_not_end_with_srv_domain(self):
249248
"expected_error": "Invalid SRV host",
250249
},
251250
]
252-
run_initial_dns_seedlist_discovery_prose_tests(self, test_cases)
251+
self.run_initial_dns_seedlist_discovery_prose_tests(test_cases)
253252

254253
def test_3_throw_when_return_address_is_identical_to_srv_hostname(self):
255254
test_cases = [
@@ -264,7 +263,7 @@ def test_3_throw_when_return_address_is_identical_to_srv_hostname(self):
264263
"expected_error": "Invalid SRV host",
265264
},
266265
]
267-
run_initial_dns_seedlist_discovery_prose_tests(self, test_cases)
266+
self.run_initial_dns_seedlist_discovery_prose_tests(test_cases)
268267

269268
def test_4_throw_when_return_address_does_not_contain_dot_separating_shared_part_of_domain(
270269
self
@@ -286,7 +285,7 @@ def test_4_throw_when_return_address_does_not_contain_dot_separating_shared_part
286285
"expected_error": "Invalid SRV host",
287286
},
288287
]
289-
run_initial_dns_seedlist_discovery_prose_tests(self, test_cases)
288+
self.run_initial_dns_seedlist_discovery_prose_tests(test_cases)
290289

291290

292291
if __name__ == "__main__":

0 commit comments

Comments
 (0)