Skip to content

Commit 9d9ac1c

Browse files
committed
Fix various tests for mongos and old mongo versions.
1 parent 594856d commit 9d9ac1c

File tree

3 files changed

+43
-30
lines changed

3 files changed

+43
-30
lines changed

test/test_collection.py

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -831,6 +831,9 @@ def test_insert_manipulate_false(self):
831831
# 1. The return value is None or [None] as appropriate.
832832
# 2. _id is not set on the passed-in document object.
833833
# 3. _id is not sent to server.
834+
if not version.at_least(self.db.connection, (2, 0)):
835+
raise SkipTest('Need at least MongoDB 2.0')
836+
834837
collection_name = 'test_insert_manipulate_false'
835838
try:
836839
self.db.drop_collection(collection_name)
@@ -1922,11 +1925,14 @@ def test_find_and_modify(self):
19221925
c.insert({'_id': 1, 'i': 1})
19231926

19241927
# Test that we raise DuplicateKeyError when appropriate.
1925-
c.ensure_index('i', unique=True)
1926-
self.assertRaises(DuplicateKeyError,
1927-
c.find_and_modify, query={'i': 1, 'j': 1},
1928-
update={'$set': {'k': 1}}, upsert=True)
1929-
c.drop_indexes()
1928+
# MongoDB doesn't have a code field for DuplicateKeyError
1929+
# from commands before 2.2.
1930+
if version.at_least(self.db.connection, (2, 2)):
1931+
c.ensure_index('i', unique=True)
1932+
self.assertRaises(DuplicateKeyError,
1933+
c.find_and_modify, query={'i': 1, 'j': 1},
1934+
update={'$set': {'k': 1}}, upsert=True)
1935+
c.drop_indexes()
19301936

19311937
# Test correct findAndModify
19321938
self.assertEqual({'_id': 1, 'i': 1},
@@ -1962,20 +1968,23 @@ def test_find_and_modify(self):
19621968
c.find_and_modify({'_id': 1}, {'$inc': {'i': 1}},
19631969
new=True, fields={'i': 1}))
19641970

1965-
# Test with full_response=True (version > 2.4.2)
1966-
result = c.find_and_modify({'_id': 1}, {'$inc': {'i': 1}},
1967-
new=True, upsert=True,
1968-
full_response=True,
1969-
fields={'i': 1})
1970-
self.assertEqual({'_id': 1, 'i': 5}, result["value"])
1971-
self.assertEqual(True, result["lastErrorObject"]["updatedExisting"])
1972-
1973-
result = c.find_and_modify({'_id': 2}, {'$inc': {'i': 1}},
1974-
new=True, upsert=True,
1975-
full_response=True,
1976-
fields={'i': 1})
1977-
self.assertEqual({'_id': 2, 'i': 1}, result["value"])
1978-
self.assertEqual(False, result["lastErrorObject"]["updatedExisting"])
1971+
# Test with full_response=True
1972+
# No lastErrorObject from mongos until 2.0
1973+
if (not is_mongos(self.db.connection) or
1974+
version.at_least(self.db.connection, (2, 0))):
1975+
result = c.find_and_modify({'_id': 1}, {'$inc': {'i': 1}},
1976+
new=True, upsert=True,
1977+
full_response=True,
1978+
fields={'i': 1})
1979+
self.assertEqual({'_id': 1, 'i': 5}, result["value"])
1980+
self.assertEqual(True, result["lastErrorObject"]["updatedExisting"])
1981+
1982+
result = c.find_and_modify({'_id': 2}, {'$inc': {'i': 1}},
1983+
new=True, upsert=True,
1984+
full_response=True,
1985+
fields={'i': 1})
1986+
self.assertEqual({'_id': 2, 'i': 1}, result["value"])
1987+
self.assertEqual(False, result["lastErrorObject"]["updatedExisting"])
19791988

19801989
class ExtendedDict(dict):
19811990
pass

test/test_cursor.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
OperationFailure)
3333
from test import version
3434
from test.test_client import get_client
35+
from test.utils import is_mongos
3536

3637

3738
class TestCursor(unittest.TestCase):
@@ -602,16 +603,17 @@ def test_add_remove_option(self):
602603
cursor.remove_option(32)
603604
self.assertEqual(2, cursor._Cursor__query_options())
604605

605-
# Exhaust
606-
cursor = self.db.test.find(exhaust=True)
607-
self.assertEqual(64, cursor._Cursor__query_options())
608-
cursor2 = self.db.test.find().add_option(64)
609-
self.assertEqual(cursor._Cursor__query_options(),
610-
cursor2._Cursor__query_options())
611-
self.assertTrue(cursor._Cursor__exhaust)
612-
cursor.remove_option(64)
613-
self.assertEqual(0, cursor._Cursor__query_options())
614-
self.assertFalse(cursor._Cursor__exhaust)
606+
# Exhaust - which mongos doesn't support
607+
if not is_mongos(self.db.connection):
608+
cursor = self.db.test.find(exhaust=True)
609+
self.assertEqual(64, cursor._Cursor__query_options())
610+
cursor2 = self.db.test.find().add_option(64)
611+
self.assertEqual(cursor._Cursor__query_options(),
612+
cursor2._Cursor__query_options())
613+
self.assertTrue(cursor._Cursor__exhaust)
614+
cursor.remove_option(64)
615+
self.assertEqual(0, cursor._Cursor__query_options())
616+
self.assertFalse(cursor._Cursor__exhaust)
615617

616618
# Partial
617619
cursor = self.db.test.find(partial=True)

test/test_database.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,9 @@ def test_command_ignores_network_timeout(self):
293293
{'$where': 'sleep(100); return true'}, network_timeout=0.001)
294294

295295
self.assertEqual(1, cursor.count())
296-
db.command('eval', 'sleep(100)', network_timeout=0.001)
296+
# mongos doesn't support the eval command
297+
if not is_mongos(self.client):
298+
db.command('eval', 'sleep(100)', network_timeout=0.001)
297299

298300
def test_last_status(self):
299301
db = self.client.pymongo_test

0 commit comments

Comments
 (0)