| 165 | self.assertEqual(ObjectId._inc, 0) |
| 166 | |
| 167 | def test_timestamp_values(self): |
| 168 | # Spec-test to check timestamp field is interpreted correctly. |
| 169 | TEST_DATA = { |
| 170 | 0x00000000: (1970, 1, 1, 0, 0, 0), |
| 171 | 0x7FFFFFFF: (2038, 1, 19, 3, 14, 7), |
| 172 | 0x80000000: (2038, 1, 19, 3, 14, 8), |
| 173 | 0xFFFFFFFF: (2106, 2, 7, 6, 28, 15), |
| 174 | } |
| 175 | |
| 176 | def generate_objectid_with_timestamp(timestamp): |
| 177 | oid = ObjectId() |
| 178 | _, trailing_bytes = struct.unpack(">IQ", oid.binary) |
| 179 | new_oid = struct.pack(">IQ", timestamp, trailing_bytes) |
| 180 | return ObjectId(new_oid) |
| 181 | |
| 182 | for tstamp, exp_datetime_args in TEST_DATA.items(): |
| 183 | oid = generate_objectid_with_timestamp(tstamp) |
| 184 | # 32-bit platforms may overflow in datetime.fromtimestamp. |
| 185 | if tstamp > 0x7FFFFFFF and sys.maxsize < 2**32: |
| 186 | try: |
| 187 | oid.generation_time |
| 188 | except (OverflowError, ValueError): |
| 189 | continue |
| 190 | self.assertEqual(oid.generation_time, datetime.datetime(*exp_datetime_args, tzinfo=utc)) |
| 191 | |
| 192 | def test_random_regenerated_on_pid_change(self): |
| 193 | # Test that change of pid triggers new random number generation. |