Skip to content

Commit 60f1a43

Browse files
authored
PYTHON-4353 Make dns import optional but required for tests (#1588)
1 parent 9d2a1ce commit 60f1a43

File tree

5 files changed

+8
-21
lines changed

5 files changed

+8
-21
lines changed

pymongo/srv_resolver.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,15 @@
2525

2626
if TYPE_CHECKING:
2727
from dns import resolver
28+
29+
_HAVE_DNSPYTHON = True
2830
else:
29-
resolver = lazy_import("dns.resolver")
31+
try:
32+
resolver = lazy_import("dns.resolver")
3033

31-
_HAVE_DNSPYTHON = True
34+
_HAVE_DNSPYTHON = True
35+
except ImportError:
36+
_HAVE_DNSPYTHON = False
3237

3338

3439
# dnspython can return bytes or str from various parts

test/test_client.py

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,6 @@
110110
from pymongo.server_selectors import readable_server_selector, writable_server_selector
111111
from pymongo.server_type import SERVER_TYPE
112112
from pymongo.settings import TOPOLOGY_TYPE
113-
from pymongo.srv_resolver import _HAVE_DNSPYTHON
114113
from pymongo.topology import _ErrorContext
115114
from pymongo.topology_description import TopologyDescription
116115
from pymongo.write_concern import WriteConcern
@@ -455,7 +454,6 @@ def test_uri_option_precedence(self):
455454
self.assertEqual(clopts.replica_set_name, "newname")
456455
self.assertEqual(clopts.read_preference, ReadPreference.SECONDARY_PREFERRED)
457456

458-
@unittest.skipUnless(_HAVE_DNSPYTHON, "DNS-related tests need dnspython to be installed")
459457
def test_connection_timeout_ms_propagates_to_DNS_resolver(self):
460458
# Patch the resolver.
461459
from pymongo.srv_resolver import _resolve
@@ -1755,7 +1753,6 @@ def test_process_periodic_tasks(self):
17551753
with self.assertRaises(InvalidOperation):
17561754
coll.insert_many([{} for _ in range(5)])
17571755

1758-
@unittest.skipUnless(_HAVE_DNSPYTHON, "DNS-related tests need dnspython to be installed")
17591756
def test_service_name_from_kwargs(self):
17601757
client = MongoClient(
17611758
"mongodb+srv://user:[email protected]",
@@ -1776,7 +1773,6 @@ def test_service_name_from_kwargs(self):
17761773
)
17771774
self.assertEqual(client._topology_settings.srv_service_name, "customname")
17781775

1779-
@unittest.skipUnless(_HAVE_DNSPYTHON, "DNS-related tests need dnspython to be installed")
17801776
def test_srv_max_hosts_kwarg(self):
17811777
client = MongoClient("mongodb+srv://test1.test.build.10gen.cc/")
17821778
self.assertGreater(len(client.topology_description.server_descriptions()), 1)
@@ -1787,11 +1783,6 @@ def test_srv_max_hosts_kwarg(self):
17871783
)
17881784
self.assertEqual(len(client.topology_description.server_descriptions()), 2)
17891785

1790-
@unittest.skipIf(_HAVE_DNSPYTHON, "dnspython must not be installed")
1791-
def test_srv_no_dnspython_error(self):
1792-
with self.assertRaisesRegex(ConfigurationError, 'The "dnspython" module must be'):
1793-
MongoClient("mongodb+srv://test1.test.build.10gen.cc/")
1794-
17951786
@unittest.skipIf(
17961787
client_context.load_balancer or client_context.serverless,
17971788
"loadBalanced clients do not run SDAM",

test/test_dns.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
from pymongo.common import validate_read_preference_tags
2929
from pymongo.errors import ConfigurationError
3030
from pymongo.mongo_client import MongoClient
31-
from pymongo.srv_resolver import _HAVE_DNSPYTHON
3231
from pymongo.uri_parser import parse_uri, split_hosts
3332

3433

@@ -65,8 +64,6 @@ def setUp(self):
6564

6665
def create_test(test_case):
6766
def run_test(self):
68-
if not _HAVE_DNSPYTHON:
69-
raise unittest.SkipTest("DNS tests require the dnspython module")
7067
uri = test_case["uri"]
7168
seeds = test_case.get("seeds")
7269
num_seeds = test_case.get("numSeeds", len(seeds or []))
@@ -161,7 +158,6 @@ def create_tests(cls):
161158

162159

163160
class TestParsingErrors(unittest.TestCase):
164-
@unittest.skipUnless(_HAVE_DNSPYTHON, "DNS tests require the dnspython module")
165161
def test_invalid_host(self):
166162
self.assertRaisesRegex(
167163
ConfigurationError,
@@ -190,7 +186,6 @@ def test_invalid_host(self):
190186

191187

192188
class TestCaseInsensitive(IntegrationTest):
193-
@unittest.skipUnless(_HAVE_DNSPYTHON, "DNS tests require the dnspython module")
194189
def test_connect_case_insensitive(self):
195190
client = MongoClient("mongodb+srv://TEST1.TEST.BUILD.10GEN.cc/")
196191
self.addCleanup(client.close)

test/test_srv_polling.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,6 @@ class TestSrvPolling(unittest.TestCase):
9595
CONNECTION_STRING = "mongodb+srv://test1.test.build.10gen.cc"
9696

9797
def setUp(self):
98-
if not _HAVE_DNSPYTHON:
99-
raise unittest.SkipTest("SRV polling tests require the dnspython module")
10098
# Patch timeouts to ensure short rescan SRV interval.
10199
self.client_knobs = client_knobs(
102100
heartbeat_frequency=WAIT_TIME,
@@ -150,6 +148,7 @@ def predicate():
150148
return True
151149

152150
def run_scenario(self, dns_response, expect_change):
151+
self.assertEqual(_HAVE_DNSPYTHON, True)
153152
if callable(dns_response):
154153
dns_resolver_response = dns_response
155154
else:

test/test_uri_spec.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828

2929
from pymongo.common import INTERNAL_URI_OPTION_NAME_MAP, validate
3030
from pymongo.compression_support import _HAVE_SNAPPY
31-
from pymongo.srv_resolver import _HAVE_DNSPYTHON
3231
from pymongo.uri_parser import SRV_SCHEME, parse_uri
3332

3433
CONN_STRING_TEST_PATH = os.path.join(
@@ -98,8 +97,6 @@ def run_scenario(self):
9897
compressors = (test.get("options") or {}).get("compressors", [])
9998
if "snappy" in compressors and not _HAVE_SNAPPY:
10099
self.skipTest("This test needs the snappy module.")
101-
if test["uri"].startswith(SRV_SCHEME) and not _HAVE_DNSPYTHON:
102-
self.skipTest("This test needs dnspython package.")
103100
valid = True
104101
warning = False
105102
expected_warning = test.get("warning", False)

0 commit comments

Comments
 (0)