Skip to content

Commit 2c432b5

Browse files
authored
PYTHON-4768 - Fix atlas connection tests and cleanup uses of raw MongoClients in tests (#1867)
1 parent 6d472a1 commit 2c432b5

15 files changed

+69
-71
lines changed

test/atlas/test_connection.py

+17-17
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import sys
2020
import unittest
2121
from collections import defaultdict
22+
from test import PyMongoTestCase
2223

2324
import pytest
2425

@@ -46,38 +47,37 @@
4647
}
4748

4849

49-
def connect(uri):
50-
if not uri:
51-
raise Exception("Must set env variable to test.")
52-
client = pymongo.MongoClient(uri)
53-
# No TLS error
54-
client.admin.command("ping")
55-
# No auth error
56-
client.test.test.count_documents({})
50+
class TestAtlasConnect(PyMongoTestCase):
51+
def connect(self, uri):
52+
if not uri:
53+
raise Exception("Must set env variable to test.")
54+
client = self.simple_client(uri)
55+
# No TLS error
56+
client.admin.command("ping")
57+
# No auth error
58+
client.test.test.count_documents({})
5759

58-
59-
class TestAtlasConnect(unittest.TestCase):
6060
@unittest.skipUnless(HAS_SNI, "Free tier requires SNI support")
6161
def test_free_tier(self):
62-
connect(URIS["ATLAS_FREE"])
62+
self.connect(URIS["ATLAS_FREE"])
6363

6464
def test_replica_set(self):
65-
connect(URIS["ATLAS_REPL"])
65+
self.connect(URIS["ATLAS_REPL"])
6666

6767
def test_sharded_cluster(self):
68-
connect(URIS["ATLAS_SHRD"])
68+
self.connect(URIS["ATLAS_SHRD"])
6969

7070
def test_tls_11(self):
71-
connect(URIS["ATLAS_TLS11"])
71+
self.connect(URIS["ATLAS_TLS11"])
7272

7373
def test_tls_12(self):
74-
connect(URIS["ATLAS_TLS12"])
74+
self.connect(URIS["ATLAS_TLS12"])
7575

7676
def test_serverless(self):
77-
connect(URIS["ATLAS_SERVERLESS"])
77+
self.connect(URIS["ATLAS_SERVERLESS"])
7878

7979
def connect_srv(self, uri):
80-
connect(uri)
80+
self.connect(uri)
8181
self.assertIn("mongodb+srv://", uri)
8282

8383
@unittest.skipUnless(HAS_SNI, "Free tier requires SNI support")

test/mockupdb/test_auth_recovering_member.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from __future__ import annotations
1515

1616
import unittest
17+
from test import PyMongoTestCase
1718

1819
import pytest
1920

@@ -30,7 +31,7 @@
3031
pytestmark = pytest.mark.mockupdb
3132

3233

33-
class TestAuthRecoveringMember(unittest.TestCase):
34+
class TestAuthRecoveringMember(PyMongoTestCase):
3435
def test_auth_recovering_member(self):
3536
# Test that we don't attempt auth against a recovering RS member.
3637
server = MockupDB()
@@ -48,12 +49,10 @@ def test_auth_recovering_member(self):
4849
server.run()
4950
self.addCleanup(server.stop)
5051

51-
client = MongoClient(
52+
client = self.simple_client(
5253
server.uri, replicaSet="rs", serverSelectionTimeoutMS=100, socketTimeoutMS=100
5354
)
5455

55-
self.addCleanup(client.close)
56-
5756
# Should see there's no primary or secondary and raise selection timeout
5857
# error. If it raises AutoReconnect we know it actually tried the
5958
# server, and that's wrong.

test/mockupdb/test_cluster_time.py

+4-5
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from __future__ import annotations
1717

1818
import unittest
19+
from test import PyMongoTestCase
1920

2021
import pytest
2122

@@ -34,7 +35,7 @@
3435
pytestmark = pytest.mark.mockupdb
3536

3637

37-
class TestClusterTime(unittest.TestCase):
38+
class TestClusterTime(PyMongoTestCase):
3839
def cluster_time_conversation(self, callback, replies, max_wire_version=6):
3940
cluster_time = Timestamp(0, 0)
4041
server = MockupDB()
@@ -52,8 +53,7 @@ def cluster_time_conversation(self, callback, replies, max_wire_version=6):
5253
server.run()
5354
self.addCleanup(server.stop)
5455

55-
client = MongoClient(server.uri)
56-
self.addCleanup(client.close)
56+
client = self.simple_client(server.uri)
5757

5858
with going(callback, client):
5959
for reply in replies:
@@ -118,8 +118,7 @@ def test_monitor(self):
118118
server.run()
119119
self.addCleanup(server.stop)
120120

121-
client = MongoClient(server.uri, heartbeatFrequencyMS=500)
122-
self.addCleanup(client.close)
121+
client = self.simple_client(server.uri, heartbeatFrequencyMS=500)
123122

124123
request = server.receives("ismaster")
125124
# No $clusterTime in first ismaster, only in subsequent ones

test/mockupdb/test_cursor_namespace.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from __future__ import annotations
1717

1818
import unittest
19+
from test import PyMongoTestCase
1920

2021
import pytest
2122

@@ -32,15 +33,15 @@
3233
pytestmark = pytest.mark.mockupdb
3334

3435

35-
class TestCursorNamespace(unittest.TestCase):
36+
class TestCursorNamespace(PyMongoTestCase):
3637
server: MockupDB
3738
client: MongoClient
3839

3940
@classmethod
4041
def setUpClass(cls):
4142
cls.server = MockupDB(auto_ismaster={"maxWireVersion": 6})
4243
cls.server.run()
43-
cls.client = MongoClient(cls.server.uri)
44+
cls.client = cls.unmanaged_simple_client(cls.server.uri)
4445

4546
@classmethod
4647
def tearDownClass(cls):
@@ -88,15 +89,15 @@ def op():
8889
self._test_cursor_namespace(op, "listIndexes")
8990

9091

91-
class TestKillCursorsNamespace(unittest.TestCase):
92+
class TestKillCursorsNamespace(PyMongoTestCase):
9293
server: MockupDB
9394
client: MongoClient
9495

9596
@classmethod
9697
def setUpClass(cls):
9798
cls.server = MockupDB(auto_ismaster={"maxWireVersion": 6})
9899
cls.server.run()
99-
cls.client = MongoClient(cls.server.uri)
100+
cls.client = cls.unmanaged_simple_client(cls.server.uri)
100101

101102
@classmethod
102103
def tearDownClass(cls):

test/mockupdb/test_getmore_sharded.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import unittest
1919
from queue import Queue
20+
from test import PyMongoTestCase
2021

2122
import pytest
2223

@@ -33,7 +34,7 @@
3334
pytestmark = pytest.mark.mockupdb
3435

3536

36-
class TestGetmoreSharded(unittest.TestCase):
37+
class TestGetmoreSharded(PyMongoTestCase):
3738
def test_getmore_sharded(self):
3839
servers = [MockupDB(), MockupDB()]
3940

@@ -47,11 +48,10 @@ def test_getmore_sharded(self):
4748
server.run()
4849
self.addCleanup(server.stop)
4950

50-
client = MongoClient(
51+
client = self.simple_client(
5152
"mongodb://%s:%d,%s:%d"
5253
% (servers[0].host, servers[0].port, servers[1].host, servers[1].port)
5354
)
54-
self.addCleanup(client.close)
5555
collection = client.db.collection
5656
cursor = collection.find()
5757
with going(next, cursor):

test/mockupdb/test_initial_ismaster.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
import time
1717
import unittest
18+
from test import PyMongoTestCase
1819

1920
import pytest
2021

@@ -31,15 +32,14 @@
3132
pytestmark = pytest.mark.mockupdb
3233

3334

34-
class TestInitialIsMaster(unittest.TestCase):
35+
class TestInitialIsMaster(PyMongoTestCase):
3536
def test_initial_ismaster(self):
3637
server = MockupDB()
3738
server.run()
3839
self.addCleanup(server.stop)
3940

4041
start = time.time()
41-
client = MongoClient(server.uri)
42-
self.addCleanup(client.close)
42+
client = self.simple_client(server.uri)
4343

4444
# A single ismaster is enough for the client to be connected.
4545
self.assertFalse(client.nodes)

test/mockupdb/test_list_indexes.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from __future__ import annotations
1717

1818
import unittest
19+
from test import PyMongoTestCase
1920

2021
import pytest
2122

@@ -28,18 +29,16 @@
2829

2930

3031
from bson import SON
31-
from pymongo import MongoClient
3232

3333
pytestmark = pytest.mark.mockupdb
3434

3535

36-
class TestListIndexes(unittest.TestCase):
36+
class TestListIndexes(PyMongoTestCase):
3737
def test_list_indexes_command(self):
3838
server = MockupDB(auto_ismaster={"maxWireVersion": 6})
3939
server.run()
4040
self.addCleanup(server.stop)
41-
client = MongoClient(server.uri)
42-
self.addCleanup(client.close)
41+
client = self.simple_client(server.uri)
4342
with going(client.test.collection.list_indexes) as cursor:
4443
request = server.receives(listIndexes="collection", namespace="test")
4544
request.reply({"cursor": {"firstBatch": [{"name": "index_0"}], "id": 123}})

test/mockupdb/test_max_staleness.py

+4-5
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from __future__ import annotations
1515

1616
import unittest
17+
from test import PyMongoTestCase
1718

1819
import pytest
1920

@@ -30,7 +31,7 @@
3031
pytestmark = pytest.mark.mockupdb
3132

3233

33-
class TestMaxStalenessMongos(unittest.TestCase):
34+
class TestMaxStalenessMongos(PyMongoTestCase):
3435
def test_mongos(self):
3536
mongos = MockupDB()
3637
mongos.autoresponds("ismaster", maxWireVersion=6, ismaster=True, msg="isdbgrid")
@@ -40,8 +41,7 @@ def test_mongos(self):
4041
# No maxStalenessSeconds.
4142
uri = "mongodb://localhost:%d/?readPreference=secondary" % mongos.port
4243

43-
client = MongoClient(uri)
44-
self.addCleanup(client.close)
44+
client = self.simple_client(uri)
4545
with going(client.db.coll.find_one) as future:
4646
request = mongos.receives()
4747
self.assertNotIn("maxStalenessSeconds", request.doc["$readPreference"])
@@ -60,8 +60,7 @@ def test_mongos(self):
6060
"&maxStalenessSeconds=1" % mongos.port
6161
)
6262

63-
client = MongoClient(uri)
64-
self.addCleanup(client.close)
63+
client = self.simple_client(uri)
6564
with going(client.db.coll.find_one) as future:
6665
request = mongos.receives()
6766
self.assertEqual(1, request.doc["$readPreference"]["maxStalenessSeconds"])

test/mockupdb/test_mixed_version_sharded.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import time
1919
import unittest
2020
from queue import Queue
21+
from test import PyMongoTestCase
2122

2223
import pytest
2324

@@ -35,7 +36,7 @@
3536
pytestmark = pytest.mark.mockupdb
3637

3738

38-
class TestMixedVersionSharded(unittest.TestCase):
39+
class TestMixedVersionSharded(PyMongoTestCase):
3940
def setup_server(self, upgrade):
4041
self.mongos_old, self.mongos_new = MockupDB(), MockupDB()
4142

@@ -62,7 +63,7 @@ def setup_server(self, upgrade):
6263
self.mongos_new.address_string,
6364
)
6465

65-
self.client = MongoClient(self.mongoses_uri)
66+
self.client = self.simple_client(self.mongoses_uri)
6667

6768
def tearDown(self):
6869
if hasattr(self, "client") and self.client:

test/mockupdb/test_op_msg.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
import unittest
1717
from collections import namedtuple
18+
from test import PyMongoTestCase
1819

1920
import pytest
2021

@@ -273,15 +274,15 @@
273274
operations_312 = []
274275

275276

276-
class TestOpMsg(unittest.TestCase):
277+
class TestOpMsg(PyMongoTestCase):
277278
server: MockupDB
278279
client: MongoClient
279280

280281
@classmethod
281282
def setUpClass(cls):
282283
cls.server = MockupDB(auto_ismaster=True, max_wire_version=8)
283284
cls.server.run()
284-
cls.client = MongoClient(cls.server.uri)
285+
cls.client = cls.unmanaged_simple_client(cls.server.uri)
285286

286287
@classmethod
287288
def tearDownClass(cls):

test/mockupdb/test_op_msg_read_preference.py

+6-5
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import copy
1717
import itertools
1818
import unittest
19+
from test import PyMongoTestCase
1920
from typing import Any
2021

2122
import pytest
@@ -39,7 +40,7 @@
3940
pytestmark = pytest.mark.mockupdb
4041

4142

42-
class OpMsgReadPrefBase(unittest.TestCase):
43+
class OpMsgReadPrefBase(PyMongoTestCase):
4344
single_mongod = False
4445
primary: MockupDB
4546
secondary: MockupDB
@@ -53,8 +54,7 @@ def add_test(cls, mode, test_name, test):
5354
setattr(cls, test_name, test)
5455

5556
def setup_client(self, read_preference):
56-
client = MongoClient(self.primary.uri, read_preference=read_preference)
57-
self.addCleanup(client.close)
57+
client = self.simple_client(self.primary.uri, read_preference=read_preference)
5858
return client
5959

6060

@@ -115,12 +115,13 @@ def add_test(cls, mode, test_name, test):
115115
setattr(cls, test_name, test)
116116

117117
def setup_client(self, read_preference):
118-
client = MongoClient(self.primary.uri, replicaSet="rs", read_preference=read_preference)
118+
client = self.simple_client(
119+
self.primary.uri, replicaSet="rs", read_preference=read_preference
120+
)
119121

120122
# Run a command on a secondary to discover the topology. This ensures
121123
# that secondaryPreferred commands will select the secondary.
122124
client.admin.command("ismaster", read_preference=ReadPreference.SECONDARY)
123-
self.addCleanup(client.close)
124125
return client
125126

126127

0 commit comments

Comments
 (0)