def test_events_are_equal_when_all_attributes_are_equal(): kwargs = build_event_kwargs() this = Event(**kwargs) that = Event(**kwargs) assert this.__eq__(that) is True assert that.__eq__(this) is True assert this == that assert not (this != that)
def test_fail_to_serialize_invalid_payload(): event = Event(name='shopping_cart.item.added') event.name = None payload = Payload(event=event) with pytest.raises(SchemaError) as e: serialize_payload(payload) assert str(e.value) == "Invalid MEGA payload. " \ "There is an error in the 'event' section: {'name': ['Missing data for required field.']}"
def test_events_are_not_equal_if_one_attribute_is_different( attribute_name, different_value): this_kwargs = build_event_kwargs() this = Event(**this_kwargs) that_kwargs = build_event_kwargs() that_kwargs[attribute_name] = different_value that = Event(**that_kwargs) assert this.__eq__(that) is False assert that.__eq__(this) is False assert this != that assert not (this == that)
def test_serialize_full_valid_payload(): event = Event(name='shopping_cart.item.added', version=2, timestamp=dateutil.parser.parse('2020-05-04T15:53:23.123'), domain='shopping_cart', subject='987650', publisher='shopping-cart-service', attributes=build_event_attributes()) _object = ObjectData(type='shopping_cart', id='18a3f92e-1fbf-45eb-8769-d836d0a1be55', version=3, current=build_current_object_data(), previous=build_previous_object_data()) extra = build_extra_data() payload = Payload(event=event, object=_object, extra=extra) data = serialize_payload(payload) assert data['protocol'] == 'mega' assert data['version'] == 1 assert data['event']['name'] == event.name assert data['event']['version'] == event.version assert dateutil.parser.parse(data['event']['timestamp']) == event.timestamp assert data['event']['domain'] == event.domain assert data['event']['subject'] == event.subject assert data['event']['publisher'] == event.publisher assert data['event']['attributes'] == event.attributes assert data['object']['type'] == _object.type assert data['object']['id'] == _object.id assert data['object']['version'] == _object.version assert data['object']['current'] == _object.current assert data['object']['previous'] == _object.previous assert data['extra'] == extra
def test_create_event_without_optional_attribute_should_use_default( attribute_name, expected_default): kwargs = build_event_kwargs() del kwargs[attribute_name] event = Event(**kwargs) assert getattr(event, attribute_name) == expected_default
def test_create_payload_with_additional_kwargs_should_be_added_to_the_extra_dictionary( ): payload = Payload(event=Event(**build_event_kwargs()), foo='bar', test=123) assert payload.extra == { 'foo': 'bar', 'test': 123, }
def test_create_event_without_timestamp_should_use_current_utc_datetime(): frozen_time = dateutil.parser.parse('2020-05-15T15:45:59.123') kwargs = build_event_kwargs() del kwargs['timestamp'] with freezegun.freeze_time(frozen_time): event = Event(**kwargs) assert event.timestamp == frozen_time
def test_create_event(): kwargs = build_event_kwargs() event = Event(**kwargs) assert event.name == kwargs['name'] assert event.timestamp == kwargs['timestamp'] assert event.version == kwargs['version'] assert event.domain == kwargs['domain'] assert event.subject == kwargs['subject'] assert event.publisher == kwargs['publisher'] assert event.attributes == kwargs['attributes']
def build_payload_kwargs(): return dict( event=Event(**build_event_kwargs()), object=ObjectData(**build_object_kwargs()), extra={ 'channel': 'web/desktop', 'user_agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_4) ' 'AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.1 Safari/605.1.15', 'user_ip_address': '177.182.205.103' })
def test_payloads_are_equal_when_all_attributes_are_equal(): event = Event(**build_event_kwargs()) mega_object = ObjectData(**build_object_kwargs()) extra = {'foo': 'bar', 'one': 1} this = Payload(event=event, object=mega_object, extra=extra) that = Payload(event=event, object=mega_object, extra=extra) assert this.__eq__(that) is True assert that.__eq__(this) is True assert this == that assert not (this != that)
def test_serialize_minimal_valid_payload(): timestamp = '2020-05-19T18:43:52.424566' with freezegun.freeze_time(timestamp): event = Event(name='shopping_cart.item.added') payload = Payload(event=event) data = serialize_payload(payload) assert data == { 'protocol': 'mega', 'version': 1, 'event': { 'name': 'shopping_cart.item.added', 'version': 1, 'timestamp': timestamp } }
def test_serialize_medium_valid_payload(): timestamp = '2020-05-19T18:43:52.424566' with freezegun.freeze_time(timestamp): event = Event(name='shopping_cart.item.removed', version=2, subject='235078', item_id='b23c5670-e4d9-40cd-94e0-b7776e08b104', all=True) _object = ObjectData(type='shopping_cart', current=build_current_object_data()) payload = Payload(event=event, object=_object, ip_address='172.217.162.174') data = serialize_payload(payload) assert data == { 'protocol': 'mega', 'version': 1, 'event': { 'name': 'shopping_cart.item.removed', 'timestamp': timestamp, 'version': 2, 'subject': '235078', 'attributes': { 'item_id': 'b23c5670-e4d9-40cd-94e0-b7776e08b104', 'all': True } }, 'object': { 'type': 'shopping_cart', 'version': 1, 'current': _object.current }, 'extra': { 'ip_address': '172.217.162.174' } }
def build_object(self, data, **kwargs): return Event(**data)
def test_payloads_are_equal_when_all_attributes_are_equal(): event = Event(**build_event_kwargs()) mega_object = ObjectData(**build_object_kwargs()) extra = {'foo': 'bar', 'one': 1} this = Payload(event=event, object=mega_object, extra=extra) that = Payload(event=event, object=mega_object, extra=extra) assert this.__eq__(that) is True assert that.__eq__(this) is True assert this == that assert not (this != that) @parameterized.expand([['event', Event(name='foo.bar')], ['object', ObjectData(current={'foo': 'bar'})], ['extra', { 'one': 1, 'two': [3, 4] }]]) def test_payloads_are_not_equal_if_one_attribute_is_different( attribute_name, different_value): this_kwargs = build_payload_kwargs() this = Payload(**this_kwargs) that_kwargs = build_payload_kwargs() that_kwargs[attribute_name] = different_value that = Payload(**that_kwargs) assert this.__eq__(that) is False
def test_an_event_is_not_equal_to(another_thing): event = Event(**build_event_kwargs()) assert event != another_thing
def test_fail_to_create_event_with_empty_name(bogus): with pytest.raises(AttributeError) as e: Event(name=bogus) assert str( e.value ) == 'Mega event attribute "name" has not been set, or set to an empty value'
def test_create_event_with_additional_kwargs_should_be_added_to_the_event_attributes( ): event = Event(name='test-event', foo='bar', test=123) assert event.name == 'test-event' assert event.attributes == {'foo': 'bar', 'test': 123}