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_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_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_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_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_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_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))
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_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_batch_0_items(self): self.assertEqual([], Json.decode("[]"))
def test_encode_batch_0_items(self): self.assertEqual("[]", Json.encode([]))