~ubuntu-branches/ubuntu/wily/pymongo/wily

« back to all changes in this revision

Viewing changes to test/test_objectid.py

  • Committer: Package Import Robot
  • Author(s): Federico Ceratto
  • Date: 2012-05-10 21:21:40 UTC
  • mfrom: (1.1.11)
  • Revision ID: package-import@ubuntu.com-20120510212140-9c66c00zz850h6l9
Tags: 2.2-1
* New upstream release.
* Dependencies added (Closes: #670268)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright 2009-2010 10gen, Inc.
 
1
# Copyright 2009-2012 10gen, Inc.
2
2
#
3
3
# Licensed under the Apache License, Version 2.0 (the "License");
4
4
# you may not use this file except in compliance with the License.
26
26
 
27
27
from bson.errors import InvalidId
28
28
from bson.objectid import ObjectId
 
29
from bson.py3compat import b, binary_type
29
30
from bson.tz_util import (FixedOffset,
30
31
                          utc)
31
32
 
 
33
PY3 = sys.version_info[0] == 3
 
34
 
32
35
 
33
36
def oid(x):
34
37
    return ObjectId()
47
50
        self.assertRaises(InvalidId, ObjectId, "")
48
51
        self.assertRaises(InvalidId, ObjectId, "12345678901")
49
52
        self.assertRaises(InvalidId, ObjectId, "1234567890123")
50
 
        self.assert_(ObjectId())
51
 
        self.assert_(ObjectId("123456789012"))
 
53
        self.assertTrue(ObjectId())
 
54
        self.assertTrue(ObjectId(b("123456789012")))
52
55
        a = ObjectId()
53
 
        self.assert_(ObjectId(a))
 
56
        self.assertTrue(ObjectId(a))
54
57
 
55
58
    def test_unicode(self):
56
59
        a = ObjectId()
69
72
                         "ObjectId('1234567890abcdef12345678')")
70
73
        self.assertEqual(str(ObjectId("1234567890abcdef12345678")),
71
74
                         "1234567890abcdef12345678")
72
 
        self.assertEqual(str(ObjectId("123456789012")),
 
75
        self.assertEqual(str(ObjectId(b("123456789012"))),
73
76
                         "313233343536373839303132")
74
77
        self.assertEqual(ObjectId("1234567890abcdef12345678").binary,
75
 
                         '\x124Vx\x90\xab\xcd\xef\x124Vx')
76
 
        self.assertEqual(str(ObjectId('\x124Vx\x90\xab\xcd\xef\x124Vx')),
 
78
                         b('\x124Vx\x90\xab\xcd\xef\x124Vx'))
 
79
        self.assertEqual(str(ObjectId(b('\x124Vx\x90\xab\xcd\xef\x124Vx'))),
77
80
                         "1234567890abcdef12345678")
78
81
 
79
82
    def test_cmp(self):
80
83
        a = ObjectId()
81
84
        self.assertEqual(a, ObjectId(a))
82
 
        self.assertEqual(ObjectId("123456789012"), ObjectId("123456789012"))
 
85
        self.assertEqual(ObjectId(b("123456789012")), ObjectId(b("123456789012")))
83
86
        self.assertNotEqual(ObjectId(), ObjectId())
84
 
        self.assertNotEqual(ObjectId("123456789012"), "123456789012")
 
87
        self.assertNotEqual(ObjectId(b("123456789012")), b("123456789012"))
85
88
 
86
89
    def test_binary_str_equivalence(self):
87
90
        a = ObjectId()
107
110
        map = {}
108
111
 
109
112
        for id in ids:
110
 
            self.assert_(id not in map)
 
113
            self.assertTrue(id not in map)
111
114
            map[id] = True
112
115
 
113
116
    def test_generation_time(self):
116
119
 
117
120
        self.assertEqual(utc, d2.tzinfo)
118
121
        d2 = d2.replace(tzinfo=None)
119
 
        self.assert_(d2 - d1 < datetime.timedelta(seconds=2))
 
122
        self.assertTrue(d2 - d1 < datetime.timedelta(seconds=2))
120
123
 
121
124
    def test_from_datetime(self):
 
125
        if 'PyPy 1.8.0' in sys.version:
 
126
            # See https://bugs.pypy.org/issue1092
 
127
            raise SkipTest("datetime.timedelta is broken in pypy 1.8.0")
122
128
        d = datetime.datetime.utcnow()
123
129
        d = d - datetime.timedelta(microseconds=d.microsecond)
124
130
        oid = ObjectId.from_datetime(d)
133
139
 
134
140
    def test_pickling(self):
135
141
        orig = ObjectId()
136
 
        self.assertEqual(orig, pickle.loads(pickle.dumps(orig)))
 
142
        for protocol in [0, 1, 2, -1]:
 
143
            pkl = pickle.dumps(orig, protocol=protocol)
 
144
            self.assertEqual(orig, pickle.loads(pkl))
137
145
 
138
146
    def test_pickle_backwards_compatability(self):
 
147
        # For a full discussion see http://bugs.python.org/issue6137
 
148
        if sys.version.startswith('3.0'):
 
149
            raise SkipTest("Python 3.0.x can't unpickle "
 
150
                           "objects pickled in Python 2.x.")
139
151
 
140
152
        # This string was generated by pickling an ObjectId in pymongo
141
153
        # version 1.9
142
 
        pickled_with_1_9 = (
 
154
        pickled_with_1_9 = b(
143
155
            "ccopy_reg\n_reconstructor\np0\n"
144
156
            "(cbson.objectid\nObjectId\np1\nc__builtin__\n"
145
157
            "object\np2\nNtp3\nRp4\n"
149
161
        # We also test against a hardcoded "New" pickle format so that we
150
162
        # make sure we're backward compatible with the current version in
151
163
        # the future as well.
152
 
        pickled_with_1_10 = (
 
164
        pickled_with_1_10 = b(
153
165
            "ccopy_reg\n_reconstructor\np0\n"
154
166
            "(cbson.objectid\nObjectId\np1\nc__builtin__\n"
155
167
            "object\np2\nNtp3\nRp4\n"
156
168
            "S'M\\x9afV\\x13v\\xc0\\x0b\\x88\\x00\\x00\\x00'\np5\nb."
157
169
            )
158
170
 
159
 
        oid_1_9 = pickle.loads(pickled_with_1_9)
160
 
        oid_1_10 = pickle.loads(pickled_with_1_10)
 
171
        if PY3:
 
172
            # Have to load using 'latin-1' since these were pickled in python2.x.
 
173
            oid_1_9 = pickle.loads(pickled_with_1_9, encoding='latin-1')
 
174
            oid_1_10 = pickle.loads(pickled_with_1_10, encoding='latin-1')
 
175
        else:
 
176
            oid_1_9 = pickle.loads(pickled_with_1_9)
 
177
            oid_1_10 = pickle.loads(pickled_with_1_10)
161
178
 
162
179
        self.assertEqual(oid_1_9, ObjectId("4d9a66561376c00b88000000"))
163
180
        self.assertEqual(oid_1_9, oid_1_10)