-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_simple.py
71 lines (60 loc) · 2.18 KB
/
test_simple.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import base64
import json
import logging
from cloudevents import conversion
from cloudevents.http.event import CloudEvent
def test_json_serialize_string() -> None:
event = CloudEvent.create(
{
"specversion": "1.0",
"type": "example.type",
"source": "example/uri",
"datacontenttype": "application/json",
},
data='{"Hello": "there"}',
)
logging.debug("event = %s", event)
event_json = conversion.to_json(event)
logging.debug("event_json = %s", event_json)
parsed = json.loads(event_json)
logging.debug("pretty event_json = \n%s", json.dumps(parsed, indent=2))
logging.debug("parsed['data'] = %r", parsed["data"])
assert parsed["data"] == '{"Hello": "there"}'
def test_json_serialize_bytes() -> None:
event = CloudEvent.create(
{
"specversion": "1.0",
"type": "example.type",
"source": "example/uri",
"datacontenttype": "application/json",
},
data=b'{"Hello": "there"}',
)
logging.debug("event = %s", event)
event_json = conversion.to_json(event)
logging.debug("event_json = %s", event_json)
parsed = json.loads(event_json)
logging.debug("pretty event_json = \n%s", json.dumps(parsed, indent=2))
logging.debug("parsed['data_base64'] = %s", parsed["data_base64"])
logging.debug(
"decoded parsed['data_base64'] = %r",
base64.b64decode(parsed["data_base64"]).decode(),
)
assert parsed["data_base64"] == base64.b64encode(b'{"Hello": "there"}').decode()
def test_json_serialize_dict() -> None:
event = CloudEvent.create(
{
"specversion": "1.0",
"type": "example.type",
"source": "example/uri",
"datacontenttype": "application/json",
},
data={"Hello": "there"},
)
logging.debug("event = %s", event)
event_json = conversion.to_json(event)
logging.debug("event_json = %s", event_json)
parsed = json.loads(event_json)
logging.debug("pretty event_json = \n%s", json.dumps(parsed, indent=2))
logging.debug("parsed['data'] = %r", parsed["data"])
assert parsed["data"] == {"Hello": "there"}