def test_timestamp_from_uuid(self): until = decimaltimestamp() uuid = uuid1() after = decimaltimestamp() uuid_timestamp = decimaltimestamp_from_uuid(uuid) self.assertLess(until, uuid_timestamp) self.assertGreater(after, uuid_timestamp) # Check timestamp_from_uuid() works with hex strings, as well as UUID objects. self.assertEqual(decimaltimestamp_from_uuid(uuid.hex), decimaltimestamp_from_uuid(uuid))
def test_decimaltimestamp_corresponds_with_decimaltimestamp_from_uuid( self): if os.getenv("TRAVIS_PYTHON_VERSION") in [ "3.6", "3.7", "3.7-dev", "pypy3.5" ]: self.skipTest("Somehow this fails on Travis dist:xenial.") # This is the weird error that happens in Python 3.7.1 on Travis Xenial dist. # Why does the second timestamp "happen" more than one second before the first? # Perhaps UUID1 doesn't actually use time.time() sometimes? Maybe it was a bug in v3.7.1? """ FAIL: test_decimaltimestamp_corresponds_with_decimaltimestamp_from_uuid ( eventsourcing.tests.core_tests.test_utils.TestUtils) ---------------------------------------------------------------------- Traceback (most recent call last): File "/home/travis/build/johnbywater/eventsourcing/eventsourcing/tests/core_tests/test_utils.py", line 61, in test_decimaltimestamp_corresponds_with_decimaltimestamp_from_uuid self.assertLess(timestamp1, timestamp2) AssertionError: Decimal('1561464862.322443') not less than Decimal('1561464861.100940') """ timestamp1 = decimaltimestamp() sleep(0.000001) uuid_1 = uuid1() sleep(0.000001) timestamp3 = decimaltimestamp() sleep(0.000001) timestamp2 = decimaltimestamp_from_uuid(uuid_1) self.assertLess(timestamp1, timestamp2) self.assertLess(timestamp2, timestamp3)
def test(self): # Check base class can be sub-classed. class Event(EventWithTimeuuid): pass # Check event has a UUID event_id. event = Event() self.assertIsInstance(event.event_id, UUID) # Check the event_id can't be reassigned. with self.assertRaises(AttributeError): # noinspection PyPropertyAccess event.event_id = decimaltimestamp() # Check event can be instantiated with a given UUID. event_id = uuid1() event = Event(event_id=event_id) self.assertEqual(event.event_id, event_id) # Generate a series of timestamps. events = [Event() for _ in range(100)] timestamps = [decimaltimestamp_from_uuid(e.event_id) for e in events] # Check series doesn't decrease at any point. last = timestamps[0] for timestamp in timestamps[1:]: self.assertLessEqual(last, timestamp) last = timestamp # Check last timestamp is greater than the first. self.assertGreater(timestamps[-1], timestamps[0])
def test_decimaltimestamp_from_uuid(self): # Check can convert a UUID to a Decimal. self.assertIsInstance(decimaltimestamp_from_uuid(uuid1()), Decimal) # Check can convert a UUID hex to a Decimal. self.assertIsInstance(decimaltimestamp_from_uuid(uuid1().hex), Decimal) # Generate a series of timestamps. timestamps = [decimaltimestamp_from_uuid(uuid1()) for _ in range(100)] # Check series doesn't decrease at any point. last = timestamps[0] for timestamp in timestamps[1:]: self.assertLessEqual(last, timestamp) last = timestamp # Check last timestamp is greater than the first. self.assertGreater(timestamps[-1], timestamps[0])
def test(self): # Check base class can be sub-classed. class Event(EventWithTimeuuid): pass # Check event can be instantiated with an event_id. event_id = uuid1() event = Event(event_id=event_id) self.assertEqual(event.event_id, event_id) # Check event can be instantiated without an event_id. time1 = decimaltimestamp() event = Event() self.assertGreater(decimaltimestamp_from_uuid(event.event_id), time1) self.assertLess(decimaltimestamp_from_uuid(event.event_id), decimaltimestamp()) # Check the event_id can't be reassigned. with self.assertRaises(AttributeError): # noinspection PyPropertyAccess event.event_id = decimaltimestamp()
def test(self): with ExampleApplicationWithTimeuuidSequencedItems() as app: # Create entity. entity1 = app.start_entity() self.assertIsInstance(entity1.___initial_event_id__, UUID) expected_timestamp = decimaltimestamp_from_uuid(entity1.___initial_event_id__) self.assertEqual(entity1.__created_on__, expected_timestamp) self.assertTrue(entity1.__last_modified__, expected_timestamp) # Read entity from repo. retrieved_obj = app.repository[entity1.id] self.assertEqual(retrieved_obj.id, entity1.id) retrieved_obj.finish() assert retrieved_obj.id not in app.repository
def __last_modified__(self): return decimaltimestamp_from_uuid(self.___last_event_id__)
def __created_on__(self): return decimaltimestamp_from_uuid(self.___initial_event_id__)