Skip to content

Commit 7070e51

Browse files
authored
Adding web app tests (cloudevents#13)
* Adding web app tests Signed-off-by: Denis Makogon <denys.makogon@oracle.com> * addressing review comments Signed-off-by: Denis Makogon <denys.makogon@oracle.com>
1 parent 5cd67e4 commit 7070e51

16 files changed

+278
-160
lines changed

cloudevents/sdk/converters/base.py

+12-4
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,13 @@ class Converter(object):
2121

2222
TYPE = None
2323

24-
def read(self, event, headers: dict, body: typing.IO,
25-
data_unmarshaller: typing.Callable) -> base.BaseEvent:
24+
def read(
25+
self,
26+
event,
27+
headers: dict,
28+
body: typing.IO,
29+
data_unmarshaller: typing.Callable
30+
) -> base.BaseEvent:
2631
raise Exception("not implemented")
2732

2833
def event_supported(self, event: object) -> bool:
@@ -31,6 +36,9 @@ def event_supported(self, event: object) -> bool:
3136
def can_read(self, content_type: str) -> bool:
3237
raise Exception("not implemented")
3338

34-
def write(self, event: base.BaseEvent,
35-
data_marshaller: typing.Callable) -> (dict, typing.IO):
39+
def write(
40+
self,
41+
event: base.BaseEvent,
42+
data_marshaller: typing.Callable
43+
) -> (dict, object):
3644
raise Exception("not implemented")

cloudevents/sdk/converters/binary.py

+11-7
Original file line numberDiff line numberDiff line change
@@ -23,25 +23,29 @@
2323
class BinaryHTTPCloudEventConverter(base.Converter):
2424

2525
TYPE = "binary"
26-
SUPPORTED_VERSIONS = [v02.Event, ]
26+
SUPPORTED_VERSIONS = [v02.Event]
2727

2828
def can_read(self, content_type: str) -> bool:
2929
return True
3030

3131
def event_supported(self, event: object) -> bool:
3232
return type(event) in self.SUPPORTED_VERSIONS
3333

34-
def read(self,
35-
event: event_base.BaseEvent,
36-
headers: dict, body: typing.IO,
37-
data_unmarshaller: typing.Callable) -> event_base.BaseEvent:
34+
def read(
35+
self,
36+
event: event_base.BaseEvent,
37+
headers: dict,
38+
body: typing.IO,
39+
data_unmarshaller: typing.Callable,
40+
) -> event_base.BaseEvent:
3841
if type(event) not in self.SUPPORTED_VERSIONS:
3942
raise exceptions.UnsupportedEvent(type(event))
4043
event.UnmarshalBinary(headers, body, data_unmarshaller)
4144
return event
4245

43-
def write(self, event: event_base.BaseEvent,
44-
data_marshaller: typing.Callable) -> (dict, typing.IO):
46+
def write(
47+
self, event: event_base.BaseEvent, data_marshaller: typing.Callable
48+
) -> (dict, typing.IO):
4549
return event.MarshalBinary(data_marshaller)
4650

4751

cloudevents/sdk/converters/structured.py

+11-8
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,20 @@ def event_supported(self, event: object) -> bool:
3030
# structured format supported by both spec 0.1 and 0.2
3131
return True
3232

33-
def read(self, event: event_base.BaseEvent,
34-
headers: dict,
35-
body: typing.IO,
36-
data_unmarshaller: typing.Callable) -> event_base.BaseEvent:
33+
def read(
34+
self,
35+
event: event_base.BaseEvent,
36+
headers: dict,
37+
body: typing.IO,
38+
data_unmarshaller: typing.Callable,
39+
) -> event_base.BaseEvent:
3740
event.UnmarshalJSON(body, data_unmarshaller)
3841
return event
3942

40-
def write(self,
41-
event: event_base.BaseEvent,
42-
data_marshaller: typing.Callable) -> (dict, typing.IO):
43-
http_headers = {'content-type': self.MIME_TYPE}
43+
def write(
44+
self, event: event_base.BaseEvent, data_marshaller: typing.Callable
45+
) -> (dict, typing.IO):
46+
http_headers = {"content-type": self.MIME_TYPE}
4447
return http_headers, event.MarshalJSON(data_marshaller)
4548

4649

cloudevents/sdk/event/base.py

+16-18
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919

2020
class EventGetterSetter(object):
21-
2221
def CloudEventVersion(self) -> str:
2322
raise Exception("not implemented")
2423

@@ -76,18 +75,13 @@ def SetContentType(self, contentType: str) -> object:
7675

7776

7877
class BaseEvent(EventGetterSetter):
79-
8078
def Properties(self, with_nullable=False) -> dict:
8179
props = dict()
8280
for name, value in self.__dict__.items():
8381
if str(name).startswith("ce__"):
8482
v = value.get()
8583
if v is not None or with_nullable:
86-
props.update(
87-
{
88-
str(name).replace("ce__", ""): value.get()
89-
}
90-
)
84+
props.update({str(name).replace("ce__", ""): value.get()})
9185

9286
return props
9387

@@ -119,33 +113,38 @@ def MarshalJSON(self, data_marshaller: typing.Callable) -> typing.IO:
119113
props["data"] = data_marshaller(props.get("data"))
120114
return io.BytesIO(json.dumps(props).encode("utf-8"))
121115

122-
def UnmarshalJSON(self, b: typing.IO,
123-
data_unmarshaller: typing.Callable):
116+
def UnmarshalJSON(self, b: typing.IO, data_unmarshaller: typing.Callable):
124117
raw_ce = json.load(b)
125118
for name, value in raw_ce.items():
126119
if name == "data":
127120
value = data_unmarshaller(value)
128121
self.Set(name, value)
129122

130-
def UnmarshalBinary(self, headers: dict, body: typing.IO,
131-
data_unmarshaller: typing.Callable):
132-
BINARY_MAPPING = {
133-
'content-type': 'contenttype',
123+
def UnmarshalBinary(
124+
self,
125+
headers: dict,
126+
body: typing.IO,
127+
data_unmarshaller: typing.Callable
128+
):
129+
binary_mapping = {
130+
"content-type": "contenttype",
134131
# TODO(someone): add Distributed Tracing. It's not clear
135132
# if this is one extension or two.
136133
# https://github.com/cloudevents/spec/blob/master/extensions/distributed-tracing.md
137134
}
138135
for header, value in headers.items():
139136
header = header.lower()
140-
if header in BINARY_MAPPING:
141-
self.Set(BINARY_MAPPING[header], value)
137+
if header in binary_mapping:
138+
self.Set(binary_mapping[header], value)
142139
elif header.startswith("ce-"):
143140
self.Set(header[3:], value)
144141

145142
self.Set("data", data_unmarshaller(body))
146143

147144
def MarshalBinary(
148-
self, data_marshaller: typing.Callable) -> (dict, object):
145+
self,
146+
data_marshaller: typing.Callable
147+
) -> (dict, object):
149148
headers = {}
150149
if self.ContentType():
151150
headers["content-type"] = self.ContentType()
@@ -159,5 +158,4 @@ def MarshalBinary(
159158
headers["ce-{0}".format(key)] = value
160159

161160
data, _ = self.Get("data")
162-
return headers, io.BytesIO(
163-
str(data_marshaller(data)).encode("utf-8"))
161+
return headers, data_marshaller(data)

cloudevents/sdk/event/opt.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414

1515

1616
class Option(object):
17-
1817
def __init__(self, name, value, is_required):
1918
self.name = name
2019
self.value = value
@@ -25,7 +24,9 @@ def set(self, new_value):
2524
if self.is_required and is_none:
2625
raise ValueError(
2726
"Attribute value error: '{0}', "
28-
"invalid new value.".format(self.name))
27+
"" "invalid new value."
28+
.format(self.name)
29+
)
2930

3031
self.value = new_value
3132

cloudevents/sdk/event/v01.py

+49-12
Original file line numberDiff line numberDiff line change
@@ -12,25 +12,62 @@
1212
# License for the specific language governing permissions and limitations
1313
# under the License.
1414

15-
from cloudevents.sdk.event import opt
1615
from cloudevents.sdk.event import base
16+
from cloudevents.sdk.event import opt
1717

1818

1919
class Event(base.BaseEvent):
20-
2120
def __init__(self):
2221
self.ce__cloudEventsVersion = opt.Option(
23-
"cloudEventsVersion", "0.1", True)
24-
self.ce__eventType = opt.Option("eventType", None, True)
22+
"cloudEventsVersion",
23+
"0.1",
24+
True
25+
)
26+
self.ce__eventType = opt.Option(
27+
"eventType",
28+
None,
29+
True
30+
)
2531
self.ce__eventTypeVersion = opt.Option(
26-
"eventTypeVersion", None, False)
27-
self.ce__source = opt.Option("source", None, True)
28-
self.ce__eventID = opt.Option("eventID", None, True)
29-
self.ce__eventTime = opt.Option("eventTime", None, True)
30-
self.ce__schemaURL = opt.Option("schemaURL", None, False)
31-
self.ce__contentType = opt.Option("contentType", None, False)
32-
self.ce__data = opt.Option("data", None, False)
33-
self.ce__extensions = opt.Option("extensions", dict(), False)
32+
"eventTypeVersion",
33+
None,
34+
False
35+
)
36+
self.ce__source = opt.Option(
37+
"source",
38+
None,
39+
True
40+
)
41+
self.ce__eventID = opt.Option(
42+
"eventID",
43+
None,
44+
True
45+
)
46+
self.ce__eventTime = opt.Option(
47+
"eventTime",
48+
None,
49+
True
50+
)
51+
self.ce__schemaURL = opt.Option(
52+
"schemaURL",
53+
None,
54+
False
55+
)
56+
self.ce__contentType = opt.Option(
57+
"contentType",
58+
None,
59+
False
60+
)
61+
self.ce__data = opt.Option(
62+
"data",
63+
None,
64+
False
65+
)
66+
self.ce__extensions = opt.Option(
67+
"extensions",
68+
dict(),
69+
False
70+
)
3471

3572
def CloudEventVersion(self) -> str:
3673
return self.ce__cloudEventsVersion.get()

cloudevents/sdk/event/v02.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,11 @@
1212
# License for the specific language governing permissions and limitations
1313
# under the License.
1414

15-
from cloudevents.sdk.event import opt
1615
from cloudevents.sdk.event import base
16+
from cloudevents.sdk.event import opt
1717

1818

1919
class Event(base.BaseEvent):
20-
2120
def __init__(self):
2221
self.ce__specversion = opt.Option("specversion", "0.2", True)
2322
self.ce__type = opt.Option("type", None, True)

cloudevents/sdk/exceptions.py

+10-10
Original file line numberDiff line numberDiff line change
@@ -14,34 +14,34 @@
1414

1515

1616
class UnsupportedEvent(Exception):
17-
1817
def __init__(self, event_class):
19-
super().__init__("Invalid CloudEvent class: "
20-
"'{0}'".format(event_class))
18+
super().__init__(
19+
"Invalid CloudEvent class: '{0}'".format(event_class)
20+
)
2121

2222

2323
class InvalidDataUnmarshaller(Exception):
24-
2524
def __init__(self):
26-
super().__init__(
27-
"Invalid data unmarshaller, is not a callable")
25+
super().__init__("Invalid data unmarshaller, is not a callable")
2826

2927

3028
class InvalidDataMarshaller(Exception):
31-
3229
def __init__(self):
3330
super().__init__(
34-
"Invalid data marshaller, is not a callable")
31+
"Invalid data marshaller, is not a callable"
32+
)
3533

3634

3735
class NoSuchConverter(Exception):
3836
def __init__(self, converter_type):
3937
super().__init__(
40-
"No such converter {0}".format(converter_type))
38+
"No such converter {0}".format(converter_type)
39+
)
4140

4241

4342
class UnsupportedEventConverter(Exception):
4443
def __init__(self, content_type):
4544
super().__init__(
4645
"Unable to identify valid event converter "
47-
"for content-type: '{0}'".format(content_type))
46+
"for content-type: '{0}'".format(content_type)
47+
)

0 commit comments

Comments
 (0)