|
24 | 24 |
|
25 | 25 | from bson.son import SON |
26 | 26 | from pymongo.cursor import _QUERY_OPTIONS |
| 27 | +from pymongo.master_slave_connection import MasterSlaveConnection |
| 28 | +from pymongo.mongo_client import MongoClient |
27 | 29 | from pymongo.mongo_replica_set_client import MongoReplicaSetClient |
28 | 30 | from pymongo.read_preferences import (ReadPreference, modes, MovingAverage, |
29 | 31 | secondary_ok_commands) |
@@ -278,6 +280,38 @@ def _test_fn(self, obedient, fn): |
278 | 280 | "Some members not used for NEAREST: %s" % ( |
279 | 281 | unused)) |
280 | 282 |
|
| 283 | + def test_command_read_pref_warning(self): |
| 284 | + ctx = catch_warnings() |
| 285 | + try: |
| 286 | + warnings.simplefilter("error", UserWarning) |
| 287 | + warnings.simplefilter("ignore", DeprecationWarning) |
| 288 | + self.assertRaises(UserWarning, self.c.pymongo_test.command, |
| 289 | + 'ping', read_preference=ReadPreference.SECONDARY) |
| 290 | + try: |
| 291 | + self.c.pymongo_test.command('dbStats', |
| 292 | + read_preference=ReadPreference.SECONDARY_PREFERRED) |
| 293 | + except UserWarning: |
| 294 | + self.fail("Shouldn't have raised UserWarning.") |
| 295 | + |
| 296 | + primary = MongoClient(host, port) |
| 297 | + try: |
| 298 | + primary.pymongo_test.command('ping', |
| 299 | + read_preference=ReadPreference.SECONDARY_PREFERRED) |
| 300 | + except UserWarning: |
| 301 | + self.fail("Shouldn't have raised UserWarning.") |
| 302 | + |
| 303 | + secondary = MongoClient(*next(iter(self.c.secondaries))) |
| 304 | + msclient = MasterSlaveConnection(primary, [secondary]) |
| 305 | + self.assertRaises(UserWarning, msclient.pymongo_test.command, |
| 306 | + 'ping', read_preference=ReadPreference.SECONDARY) |
| 307 | + try: |
| 308 | + msclient.pymongo_test.command('dbStats', |
| 309 | + read_preference=ReadPreference.SECONDARY_PREFERRED) |
| 310 | + except UserWarning: |
| 311 | + self.fail("Shouldn't have raised UserWarning.") |
| 312 | + finally: |
| 313 | + ctx.exit() |
| 314 | + |
281 | 315 | def test_command(self): |
282 | 316 | # Test generic 'command' method. Some commands obey read preference, |
283 | 317 | # most don't. |
|
0 commit comments