Skip to content

Commit f83c363

Browse files
committed
chore: Add getters for attributes and test happy path
Signed-off-by: Tudor Plugaru <plugaru.tudor@protonmail.com>
1 parent 42b4fe1 commit f83c363

File tree

2 files changed

+96
-6
lines changed

2 files changed

+96
-6
lines changed

src/cloudevents/core/v1/event.py

Lines changed: 59 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -127,15 +127,69 @@ def _validate_attribute(attributes: dict) -> None:
127127
f"Extension attribute '{extension_attributes}' should only contain lowercase letters and numbers"
128128
)
129129

130-
def get_attribute(self, attribute: str) -> Optional[Any]:
130+
def get_id(self) -> str:
131131
"""
132-
Retrieve a value of an attribute of the event denoted by the given `attribute`.
132+
Retrieve the ID of the event.
133133
134-
:param attribute: The name of the event attribute to retrieve the value for.
134+
:return: The ID of the event.
135+
"""
136+
return self._attributes["id"]
137+
138+
def get_source(self) -> str:
139+
"""
140+
Retrieve the source of the event.
141+
142+
:return: The source of the event.
143+
"""
144+
return self._attributes["source"]
145+
146+
def get_type(self) -> str:
147+
"""
148+
Retrieve the type of the event.
149+
150+
:return: The type of the event.
151+
"""
152+
return self._attributes["type"]
153+
154+
def get_specversion(self) -> str:
155+
"""
156+
Retrieve the specversion of the event.
157+
158+
:return: The specversion of the event.
159+
"""
160+
return self._attributes["specversion"]
161+
162+
def get_datacontenttype(self) -> Optional[str]:
163+
"""
164+
Retrieve the datacontenttype of the event.
165+
166+
:return: The datacontenttype of the event.
167+
"""
168+
return self._attributes.get("datacontenttype")
169+
170+
def get_dataschema(self) -> Optional[str]:
171+
"""
172+
Retrieve the dataschema of the event.
173+
174+
:return: The dataschema of the event.
175+
"""
176+
return self._attributes.get("dataschema")
177+
178+
def get_subject(self) -> Optional[str]:
179+
"""
180+
Retrieve the subject of the event.
181+
182+
:return: The subject of the event.
183+
"""
184+
return self._attributes.get("subject")
185+
186+
def get_time(self) -> Optional[datetime]:
187+
"""
188+
Retrieve the time of the event.
135189
136-
:return: The event attribute value.
190+
:return: The time of the event.
137191
"""
138-
return self._attributes[attribute]
192+
return self._attributes.get("time")
139193

140194
def get_data(self) -> Optional[dict]:
141195
"""

tests/test_core/test_v1/test_event.py

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
from cloudevents.core.v1.event import CloudEvent
1616

1717
import pytest
18-
from datetime import datetime
18+
from datetime import datetime, timezone
1919
from typing import Any, Optional
2020

2121

@@ -207,3 +207,39 @@ def test_custom_extension(extension_name: str, error: str) -> None:
207207
)
208208

209209
assert str(e.value) == error
210+
211+
212+
def test_cloud_event_constructor() -> None:
213+
id = "1"
214+
source = "/source"
215+
type = "com.test.type"
216+
specversion = "1.0"
217+
datacontenttype = "application/json"
218+
dataschema = "http://example.com/schema"
219+
subject = "test_subject"
220+
time = datetime.now(tz=timezone.utc)
221+
data = {"key": "value"}
222+
223+
event = CloudEvent(
224+
attributes={
225+
"id": id,
226+
"source": source,
227+
"type": type,
228+
"specversion": specversion,
229+
"datacontenttype": datacontenttype,
230+
"dataschema": dataschema,
231+
"subject": subject,
232+
"time": time,
233+
},
234+
data=data,
235+
)
236+
237+
assert event.get_id() == id
238+
assert event.get_source() == source
239+
assert event.get_type() == type
240+
assert event.get_specversion() == specversion
241+
assert event.get_datacontenttype() == datacontenttype
242+
assert event.get_dataschema() == dataschema
243+
assert event.get_subject() == subject
244+
assert event.get_time() == time
245+
assert event.get_data() == data

0 commit comments

Comments
 (0)