Skip to content

Commit 5806fcd

Browse files
committed
converted collection, objectid and cursor
qcheck fixed (maybe, i'm understand it not very clearly)
1 parent a261f0a commit 5806fcd

File tree

5 files changed

+46
-38
lines changed

5 files changed

+46
-38
lines changed

pymongo/collection.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,12 +107,14 @@ def __getitem__(self, name):
107107
def __repr__(self):
108108
return "Collection(%r, %r)" % (self.__database, self.__name)
109109

110-
def __cmp__(self, other):
110+
def __ne__(self, other):
111111
if isinstance(other, Collection):
112-
return cmp((self.__database, self.__name),
113-
(other.__database, other.__name))
112+
return (self.__database, self.__name) != (other.__database, other.__name)
114113
return NotImplemented
115114

115+
def __eq__(self, other):
116+
return not self.__ne__(other)
117+
116118
def full_name(self):
117119
"""The full name of this :class:`Collection`.
118120

pymongo/objectid.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
1516
"""Tools for working with MongoDB `ObjectIds
1617
<http://www.mongodb.org/display/DOCS/Object+IDs>`_.
1718
"""
@@ -26,6 +27,8 @@
2627
import hashlib
2728
_md5func = hashlib.md5
2829

30+
from binascii import hexlify
31+
2932
from .errors import InvalidId
3033

3134

@@ -153,16 +156,21 @@ def generation_time(self):
153156
generation_time = property(generation_time)
154157

155158
def __str__(self):
156-
return self.__id.encode("hex")
159+
return hexlify(self.__id).decode()
160+
157161

158162
def __repr__(self):
159-
return "ObjectId('%s')" % self.__id.encode("hex")
163+
return "ObjectId(%s)" % (hexlify(self.__id).decode())
160164

161-
def __cmp__(self, other):
165+
def __ne__(self, other):
162166
if isinstance(other, ObjectId):
163-
return cmp(self.__id, other.__id)
167+
return self.__id != other.__id
164168
return NotImplemented
165169

170+
def __eq__(self, other):
171+
return not self.__ne__(other)
172+
173+
166174
def __hash__(self):
167175
"""Get a hash value for this :class:`ObjectId`.
168176

test/qcheck.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
import traceback
1717
import datetime
1818
import re
19-
import types
2019
import sys
2120
from functools import reduce
2221
sys.path[0:0] = [""]
@@ -107,10 +106,8 @@ def gen_datetime():
107106
def gen_dict(gen_key, gen_value, gen_length):
108107

109108
def a_dict(gen_key, gen_value, length):
110-
result = {}
111109
for _ in range(length):
112-
result[gen_key()] = gen_value()
113-
return result
110+
yield (gen_key(), gen_value())
114111
return lambda: a_dict(gen_key, gen_value, gen_length())
115112

116113

@@ -145,7 +142,7 @@ def gen_dbref():
145142
def gen_mongo_value(depth, ref):
146143
choices = [gen_unicode(gen_range(0, 50)),
147144
gen_printable_string(gen_range(0, 50)),
148-
list(map(gen_string(gen_range(0, 1000)), Binary)),
145+
map(gen_string(gen_range(0, 1000)), Binary),
149146
gen_int(),
150147
gen_float(),
151148
gen_boolean(),
@@ -165,9 +162,9 @@ def gen_mongo_list(depth, ref):
165162

166163

167164
def gen_mongo_dict(depth, ref=True):
168-
return list(map(gen_dict(gen_unicode(gen_range(0, 20)),
165+
return map(gen_dict(gen_unicode(gen_range(0, 20)),
169166
gen_mongo_value(depth - 1, ref),
170-
gen_range(0, 10)), SON))
167+
gen_range(0, 10)), SON)
171168

172169

173170
def simplify(case): # TODO this is a hack

test/test_collection.py

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@
2121
import sys
2222
sys.path[0:0] = [""]
2323

24-
from nose.plugins.skip import SkipTest
24+
#from nose.plugins.skip import SkipTest
2525

26-
from . import qcheck
27-
from .test_connection import get_connection
28-
from . import version
26+
import qcheck
27+
from test_connection import get_connection
28+
import version
2929
from pymongo.objectid import ObjectId
3030
from pymongo.code import Code
3131
from pymongo.binary import Binary
@@ -243,31 +243,31 @@ def test_field_selection(self):
243243

244244
doc = {"a": 1, "b": 5, "c": {"d": 5, "e": 10}}
245245
db.test.insert(doc)
246-
247-
self.assertEqual(list(db.test.find({}, ["_id"]).next().keys()), ["_id"])
248-
l = list(db.test.find({}, ["a"]).next().keys())
246+
247+
self.assertEqual(list(next(db.test.find({}, ["_id"])).keys()), ["_id"])
248+
l = list(next(db.test.find({}, ["a"])))
249249
l.sort()
250250
self.assertEqual(l, ["_id", "a"])
251-
l = list(db.test.find({}, ["b"]).next().keys())
251+
l = list(next(db.test.find({}, ["b"])))
252252
l.sort()
253253
self.assertEqual(l, ["_id", "b"])
254-
l = list(db.test.find({}, ["c"]).next().keys())
254+
l = list(next(db.test.find({}, ["c"])))
255255
l.sort()
256256
self.assertEqual(l, ["_id", "c"])
257-
self.assertEqual(db.test.find({}, ["a"]).next()["a"], 1)
258-
self.assertEqual(db.test.find({}, ["b"]).next()["b"], 5)
259-
self.assertEqual(db.test.find({}, ["c"]).next()["c"],
257+
self.assertEqual(next(db.test.find({}, ["a"]))["a"], 1)
258+
self.assertEqual(next(db.test.find({}, ["b"]))["b"], 5)
259+
self.assertEqual(next(db.test.find({}, ["c"]))["c"],
260260
{"d": 5, "e": 10})
261261

262-
self.assertEqual(db.test.find({}, ["c.d"]).next()["c"], {"d": 5})
263-
self.assertEqual(db.test.find({}, ["c.e"]).next()["c"], {"e": 10})
264-
self.assertEqual(db.test.find({}, ["b", "c.e"]).next()["c"],
262+
self.assertEqual(next(db.test.find({}, ["c.d"]))["c"], {"d": 5})
263+
self.assertEqual(next(db.test.find({}, ["c.e"]))["c"], {"e": 10})
264+
self.assertEqual(next(db.test.find({}, ["b", "c.e"]))["c"],
265265
{"e": 10})
266266

267-
l = list(db.test.find({}, ["b", "c.e"]).next().keys())
267+
l = list(next(db.test.find({}, ["b", "c.e"])))
268268
l.sort()
269269
self.assertEqual(l, ["_id", "b", "c"])
270-
self.assertEqual(db.test.find({}, ["b", "c.e"]).next()["b"], 5)
270+
self.assertEqual(next(db.test.find({}, ["b", "c.e"]))["b"], 5)
271271

272272
def test_options(self):
273273
db = self.db
@@ -297,6 +297,8 @@ def remove_insert_find_one(dict):
297297
db.test.remove({})
298298
db.test.insert(dict)
299299
return db.test.find_one() == dict
300+
301+
a = qcheck.gen_mongo_dict(3)
300302

301303
qcheck.check_unittest(self, remove_insert_find_one,
302304
qcheck.gen_mongo_dict(3))
@@ -452,7 +454,7 @@ def test_unique_index(self):
452454
db.test.save({"hello": "world"})
453455
db.test.save({"hello": "mike"})
454456
db.test.save({"hello": "world"})
455-
self.failIf(db.error())
457+
self.assertFalse(db.error())
456458

457459
db.drop_collection("test")
458460
db.test.create_index("hello", unique=True)
@@ -469,7 +471,7 @@ def test_index_on_subfield(self):
469471
db.test.insert({"hello": {"a": 4, "b": 5}})
470472
db.test.insert({"hello": {"a": 7, "b": 2}})
471473
db.test.insert({"hello": {"a": 4, "b": 10}})
472-
self.failIf(db.error())
474+
self.assertFalse(db.error())
473475

474476
db.drop_collection("test")
475477
db.test.create_index("hello.a", unique=True)

test/test_cursor.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,15 @@
2121
import itertools
2222
sys.path[0:0] = [""]
2323

24-
from nose.plugins.skip import SkipTest
24+
#from nose.plugins.skip import SkipTest
2525

2626
from pymongo.errors import InvalidOperation, OperationFailure
2727
from pymongo.cursor import Cursor
2828
from pymongo.database import Database
2929
from pymongo.code import Code
3030
from pymongo import ASCENDING, DESCENDING
31-
from .test_connection import get_connection
32-
from . import version
31+
from test_connection import get_connection
32+
import version
3333

3434

3535
class TestCursor(unittest.TestCase):
@@ -96,7 +96,7 @@ def test_slave_okay(self):
9696

9797
warnings.simplefilter("error")
9898

99-
self.assertEqual(1, db.test.find().next()["x"])
99+
self.assertEqual(1, next(db.test.find())["x"])
100100
self.assertRaises(DeprecationWarning, db.test.find, slave_okay=True)
101101
self.assertRaises(DeprecationWarning, db.test.find, slave_okay=False)
102102

@@ -467,7 +467,6 @@ def test_getitem_slice_index(self):
467467
for i in range(100):
468468
self.db.test.save({"i": i})
469469

470-
izip = itertools.izip
471470
count = itertools.count
472471

473472
self.assertRaises(IndexError, lambda: self.db.test.find()[-1:])

0 commit comments

Comments
 (0)