Ejemplo n.º 1
0
    def test(self):
        # Check base class can be sub-classed.
        class Event(EventWithTimestamp, EventWithOriginatorID):
            pass

        class Event2(EventWithTimestamp, EventWithOriginatorID):
            pass

        # Check subclass can be instantiated with 'originator_id' parameter.
        ID1 = uuid4()
        ID2 = uuid4()
        VALUE1 = 'a string'
        VALUE2 = 'another string'
        event1 = Event(
            originator_id=ID1,
        )
        self.assertEqual(event1.originator_id, ID1)

        # Check event has a domain event ID, and a timestamp.
        self.assertTrue(event1.timestamp)
        self.assertIsInstance(event1.timestamp, float)

        # Check subclass can be instantiated with 'timestamp' parameter.
        DOMAIN_EVENT_ID1 = create_timesequenced_event_id()
        event2 = Event(
            originator_id=ID1,
            timestamp=DOMAIN_EVENT_ID1,
        )
        self.assertEqual(event2.timestamp, DOMAIN_EVENT_ID1)

        # Check subclass can be instantiated with other parameters.
        event3 = Event(
            originator_id=ID1,
            an_attribute=VALUE1,
        )
        self.assertEqual(event3.an_attribute, VALUE1)

        # Check the attribute value cannot be changed.
        with self.assertRaises(AttributeError):
            event3.an_attribute = VALUE2
        self.assertEqual(event3.an_attribute, VALUE1)

        # Check an event is equal to itself.
        self.assertTrue(event3 == event3)
        self.assertFalse(event3 != event3)
        event4 = Event(
            originator_id=ID1,
            timestamp=event3.timestamp,
            an_attribute=VALUE1,
        )
        self.assertEqual(event3, event4)

        # Check domain events with same domain event ID have the same timestamp.
        event5 = Event(
            originator_id=event1.originator_id,
            timestamp=event1.timestamp,
            an_attribute=VALUE1,
        )
        self.assertEqual(event1.timestamp, event5.timestamp)

        # Check it's not equal to different type with same values.
        self.assertFalse(event3 == Event2(
            originator_id=event3.originator_id,
            an_attribute=event3.an_attribute,
        ))
        self.assertTrue(event3 != Event2(
            originator_id=ID1,
            an_attribute=VALUE1,
        ))

        # Check it's not equal to same type with different values.
        # - different originator_id
        self.assertNotEqual(event3.originator_id, ID2)
        self.assertEqual(event3.an_attribute, VALUE1)
        self.assertFalse(event2 == Event(
            originator_id=ID2,
            an_attribute=VALUE1,
        ))
        # - different attribute value
        self.assertTrue(event2 != Event(
            originator_id=ID1,
            an_attribute=VALUE2,
        ))

        # Check domain events with different domain event IDs have different timestamps.
        event4 = Event(
            originator_id=ID1,
            an_attribute=VALUE1,
        )
        self.assertNotEqual(event2.timestamp, event4.timestamp)
        self.assertNotEqual(event2.timestamp, event4.timestamp)
Ejemplo n.º 2
0
    def test_two_subclasses(self):
        # Check base class can be sub-classed.
        class Event(EventWithTimestamp, EventWithOriginatorID):
            pass

        class Event2(EventWithTimestamp, EventWithOriginatorID):
            pass

        # Check subclass can be instantiated with 'originator_id' parameter.
        ID1 = uuid4()
        ID2 = uuid4()
        VALUE1 = 'a string'
        VALUE2 = 'another string'
        event1 = Event(originator_id=ID1, )
        self.assertEqual(event1.originator_id, ID1)

        # Check event has a domain event ID, and a timestamp.
        self.assertTrue(event1.timestamp)
        self.assertIsInstance(event1.timestamp, float)

        # Check subclass can be instantiated with 'timestamp' parameter.
        DOMAIN_EVENT_ID1 = create_timesequenced_event_id()
        event2 = Event(
            originator_id=ID1,
            timestamp=DOMAIN_EVENT_ID1,
        )
        self.assertEqual(event2.timestamp, DOMAIN_EVENT_ID1)

        # Check subclass can be instantiated with other parameters.
        event3 = Event(
            originator_id=ID1,
            an_attribute=VALUE1,
        )
        self.assertEqual(event3.an_attribute, VALUE1)

        # Check the attribute value cannot be changed.
        with self.assertRaises(AttributeError):
            event3.an_attribute = VALUE2
        self.assertEqual(event3.an_attribute, VALUE1)

        # Check an event is equal to itself.
        self.assertTrue(event3 == event3)
        self.assertFalse(event3 != event3)
        event4 = Event(
            originator_id=ID1,
            timestamp=event3.timestamp,
            an_attribute=VALUE1,
        )
        self.assertEqual(event3, event4)

        # Check domain events with same domain event ID have the same timestamp.
        event5 = Event(
            originator_id=event1.originator_id,
            timestamp=event1.timestamp,
            an_attribute=VALUE1,
        )
        self.assertEqual(event1.timestamp, event5.timestamp)

        # Check it's not equal to different type with same values.
        self.assertFalse(event3 == Event2(
            originator_id=event3.originator_id,
            an_attribute=event3.an_attribute,
        ))
        self.assertTrue(event3 != Event2(
            originator_id=ID1,
            an_attribute=VALUE1,
        ))

        # Check it's not equal to same type with different values.
        # - different originator_id
        self.assertNotEqual(event3.originator_id, ID2)
        self.assertEqual(event3.an_attribute, VALUE1)
        self.assertFalse(event2 == Event(
            originator_id=ID2,
            an_attribute=VALUE1,
        ))
        # - different attribute value
        self.assertTrue(event2 != Event(
            originator_id=ID1,
            an_attribute=VALUE2,
        ))

        # Check domain events with different domain event IDs have different timestamps.
        event4 = Event(
            originator_id=ID1,
            an_attribute=VALUE1,
        )
        self.assertNotEqual(event2.timestamp, event4.timestamp)
        self.assertNotEqual(event2.timestamp, event4.timestamp)