def compare_event(self, event: CloudEvent, *, type: str, source: str, id: str, specversion="1.0", subject=None, data=None, datacontenttype=None, dataschema=None, time=None, **attributes): self.assertEqual(type, event.type) self.assertEqual(source, event.source) self.assertEqual(id, event.id) self.assertEqual(specversion, event.specversion) self.assertEqual(subject, event.subject) self.assertEqual(data, event.data) self.assertEqual(datacontenttype, event.datacontenttype) self.assertEqual(dataschema, event.dataschema) self.assertEqual(time, event.time) for name, value in attributes.items(): self.assertEqual(value, event.attribute(name))
def test_eq_distinct_instance(self): event = CloudEvent( type="OximeterMeasured", source="oximeter/123", id="1000", ) self.assertNotEqual(None, event)
def test_repr(self): event = CloudEvent( type="OximeterMeasured", source="oximeter/123", id="1000", ) event_str = repr(event) target = '''{'type': 'OximeterMeasured', 'source': 'oximeter/123', 'id': '1000', 'specversion': '1.0'}''' self.assertEqual(target, event_str)
def test_set_extension_attribute(self): event = CloudEvent(type="OximeterMeasured", source="oximeter/123", id="1000", external1="foo/bar") self.compare_event(event, type="OximeterMeasured", source="oximeter/123", id="1000", external1="foo/bar")
def test_set_subject(self): event = CloudEvent(type="OximeterMeasured", source="oximeter/123", id="1000", subject="patient/123") self.compare_event(event, type="OximeterMeasured", source="oximeter/123", id="1000", subject="patient/123")
def test_set_datacontenttype(self): event = CloudEvent(type="OximeterMeasured", source="oximeter/123", id="1000", datacontenttype="application/json") self.compare_event(event, type="OximeterMeasured", source="oximeter/123", id="1000", datacontenttype="application/json")
def test_encode_extension_attribute(self): event = CloudEvent(type="OximeterMeasured", source="oximeter/123", id="1000", external1="foo/bar") encoded = Avro.encode(event) target = \ (b'\n\x08type\x06 OximeterMeasured\x0csource\x06\x18oximeter/123\x04id' b'\x06\x081000\x16specversion\x06\x061.0\x12external1\x06\x0efoo/bar\x00\x02') self.assertEqual(target, encoded)
def test_decode_required(self): encoded_event =\ (b'\x08\x08type\x06 OximeterMeasured\x0csource\x06\x18oximeter/123\x04id' b'\x06\x081000\x16specversion\x06\x061.0\x00\x02') target = CloudEvent( type="OximeterMeasured", source="oximeter/123", id="1000", ) event = Avro.decode(encoded_event) self.assertEqual(target, event)
def test_create_event(self): event = CloudEvent( type="OximeterMeasured", source="oximeter/123", id="1000", ) self.compare_event(event, type="OximeterMeasured", source="oximeter/123", id="1000")
def test_set_dataschema(self): event = CloudEvent( type="OximeterMeasured", source="oximeter/123", id="1000", dataschema="http://particlemetrics.com/schemas/oximeter#") self.compare_event( event, type="OximeterMeasured", source="oximeter/123", id="1000", dataschema="http://particlemetrics.com/schemas/oximeter#")
def test_encode_string_data(self): event = CloudEvent(type="OximeterMeasured", source="oximeter/123", id="1000", data=json.dumps({"spo2": 99}), datacontenttype="application/json") encoded = Avro.encode(event) target = \ (b'\n\x08type\x06 OximeterMeasured\x0csource\x06\x18oximeter/123\x04id' b'\x06\x081000\x16specversion\x06\x061.0\x1edatacontenttype\x06 application' b'/json\x00\x0c\x18{"spo2": 99}') self.assertEqual(target, encoded)
def test_encode_binary_data(self): event = CloudEvent(type="OximeterMeasured", source="oximeter/123", id="1000", data=b'\x01\x02\x03\x04', datacontenttype="application/octet-stream") encoded = Avro.encode(event) target = \ (b'\n\x08type\x06 OximeterMeasured\x0csource\x06\x18oximeter/123\x04id' b'\x06\x081000\x16specversion\x06\x061.0\x1edatacontenttype\x060application' b'/octet-stream\x00\x00\x08\x01\x02\x03\x04') self.assertEqual(target, encoded)
def test_encode_batch_2_items(self): event_batch = [ CloudEvent( type="OximeterMeasured", source="oximeter/123", id="1000", datacontenttype="application/json", data=json.dumps({"spo2": 99}), ), CloudEvent( type="OximeterMeasured", source="oximeter/123", id="1001", datacontenttype="application/json", data=b'\x01binarydata\x02', ), ] encoded_batch = Json.encode(event_batch) target = r''' [ { "type":"OximeterMeasured", "source":"oximeter/123", "id":"1000", "specversion":"1.0", "datacontenttype": "application/json", "data": "{\"spo2\": 99}" }, { "type":"OximeterMeasured", "source":"oximeter/123", "id":"1001", "specversion":"1.0", "datacontenttype": "application/json", "data_base64": "AWJpbmFyeWRhdGEC" } ] ''' self.assertEqual(json.loads(target), json.loads(encoded_batch))
def test_decode_optional(self): encoded_event = \ (b'\x0e\x08type\x06 OximeterMeasured\x0csource\x06\x18oximeter/123\x04id' b'\x06\x081000\x16specversion\x06\x061.0\x0esubject\x06\x10subject1\x14datasc' b'hema\x06Dhttps://particlemetrics.com/schema\x08time\x06(2020-09-28T21:33:' b'21Z\x00\x02') target = CloudEvent(type="OximeterMeasured", source="oximeter/123", id="1000", subject="subject1", dataschema="https://particlemetrics.com/schema", time="2020-09-28T21:33:21Z") event = Avro.decode(encoded_event) self.assertEqual(target, event)
def test_set_time(self): from datetime import datetime, timezone, timedelta event = CloudEvent(type="OximeterMeasured", source="oximeter/123", id="1000", time="2020-09-28T21:33:21Z") self.compare_event(event, type="OximeterMeasured", source="oximeter/123", id="1000", time="2020-09-28T21:33:21Z") # datetime object without timezone dt = datetime(2020, 9, 25, 13, 32, 56) event = CloudEvent(type="OximeterMeasured", source="oximeter/123", id="1000", time=dt) self.compare_event(event, type="OximeterMeasured", source="oximeter/123", id="1000", time="2020-09-25T13:32:56Z") istanbul = timezone(timedelta(0, 10800), "+03") # datetime object with timezone dt = datetime(2020, 9, 25, 13, 32, 56, tzinfo=istanbul) event = CloudEvent(type="OximeterMeasured", source="oximeter/123", id="1000", time=dt) self.compare_event(event, type="OximeterMeasured", source="oximeter/123", id="1000", time="2020-09-25T13:32:56+03:00")
def test_set_data(self): # set string data event = CloudEvent(type="OximeterMeasured", source="oximeter/123", id="1000", data='{"spo2": 99}') self.assertFalse(event._has_binary_data) self.compare_event(event, type="OximeterMeasured", source="oximeter/123", id="1000", data='{"spo2": 99}') # set binary data event = CloudEvent(type="OximeterMeasured", source="oximeter/123", id="1000", data=b'{"spo2": 99}') self.assertTrue(event._has_binary_data) self.compare_event(event, type="OximeterMeasured", source="oximeter/123", id="1000", data=b'{"spo2": 99}')
def test_decode_required(self): encoded_event = ''' { "type":"OximeterMeasured", "source":"oximeter/123", "id":"1000", "specversion":"1.0" } ''' target = CloudEvent( type="OximeterMeasured", source="oximeter/123", id="1000", ) event = Json.decode(encoded_event) self.assertEqual(target, event)
def test_encode_required(self): event = CloudEvent( type="OximeterMeasured", source="oximeter/123", id="1000", ) encoded = Json.encode(event) target = ''' { "type":"OximeterMeasured", "source":"oximeter/123", "id":"1000", "specversion":"1.0" } ''' self.assertEqual(json.loads(target), json.loads(encoded))
def test_encode_extension_attribute(self): event = CloudEvent( type="OximeterMeasured", source="oximeter/123", id="1000", external1="foo/bar" ) encoded = Json.encode(event) target = ''' { "type":"OximeterMeasured", "source":"oximeter/123", "id":"1000", "specversion":"1.0", "external1": "foo/bar" } ''' self.assertEqual(json.loads(target), json.loads(encoded))
def test_decode_extension_attribute(self): encoded_event = ''' { "type":"OximeterMeasured", "source":"oximeter/123", "id":"1000", "specversion":"1.0", "external1": "foo/bar" } ''' target = CloudEvent( type="OximeterMeasured", source="oximeter/123", id="1000", external1="foo/bar" ) event = Json.decode(encoded_event) self.assertEqual(target, event)
def test_encode_string_data(self): event = CloudEvent( type="OximeterMeasured", source="oximeter/123", id="1000", data=json.dumps({"spo2": 99}), datacontenttype="application/json" ) encoded = Json.encode(event) target = r''' { "type": "OximeterMeasured", "source": "oximeter/123", "id": "1000", "specversion": "1.0", "datacontenttype": "application/json", "data": "{\"spo2\": 99}" } ''' self.assertEqual(json.loads(target), json.loads(encoded))
def test_encode_binary_data(self): event = CloudEvent( type="OximeterMeasured", source="oximeter/123", id="1000", data=b'\x01\x02\x03\x04', datacontenttype="application/octet-stream" ) encoded = Json.encode(event) target = r''' { "type": "OximeterMeasured", "source": "oximeter/123", "id": "1000", "specversion": "1.0", "datacontenttype": "application/octet-stream", "data_base64": "AQIDBA==" } ''' self.assertEqual(json.loads(target), json.loads(encoded))
def test_decode_batch_1_item(self): encoded_batch = r''' [{ "type":"OximeterMeasured", "source":"oximeter/123", "id":"1000", "specversion":"1.0", "datacontenttype": "application/json", "data": "{\"spo2\": 99}" }] ''' target = [ CloudEvent( type="OximeterMeasured", source="oximeter/123", id="1000", datacontenttype="application/json", data=json.dumps({"spo2": 99}), ) ] self.assertEqual(target, Json.decode(encoded_batch))
def test_encode_optional(self): event = CloudEvent( type="OximeterMeasured", source="oximeter/123", id="1000", subject="subject1", dataschema="https://particlemetrics.com/schema", time="2020-09-28T21:33:21Z" ) encoded = Json.encode(event) target = ''' {"dataschema": "https://particlemetrics.com/schema", "id": "1000", "source": "oximeter/123", "specversion": "1.0", "subject": "subject1", "time": "2020-09-28T21:33:21Z", "type": "OximeterMeasured" } ''' self.assertEqual(json.loads(target), json.loads(encoded))