|
15 | 15 |
|
16 | 16 | from datetime import datetime, timezone |
17 | 17 |
|
18 | | -import pytest |
19 | | - |
20 | 18 | from cloudevents.core.formats.json import JSONFormat |
21 | 19 | from cloudevents.core.v1.event import CloudEvent |
22 | 20 |
|
@@ -61,7 +59,7 @@ def test_write_cloud_event_to_json_with_data_as_json() -> None: |
61 | 59 |
|
62 | 60 | assert ( |
63 | 61 | result |
64 | | - == '{"id": "123", "source": "source", "type": "type", "specversion": "1.0", "time": "2023-10-25T17:09:19.736166Z", "datacontenttype": "application/json", "dataschema": "http://example.com/schema", "subject": "test_subject", "data": "{\'key\': \'value\'}"}'.encode( |
| 62 | + == '{"id": "123", "source": "source", "type": "type", "specversion": "1.0", "time": "2023-10-25T17:09:19.736166Z", "datacontenttype": "application/json", "dataschema": "http://example.com/schema", "subject": "test_subject", "data": {"key": "value"}}'.encode( |
65 | 63 | "utf-8" |
66 | 64 | ) |
67 | 65 | ) |
@@ -151,7 +149,7 @@ def test_write_cloud_event_to_json_with_no_content_type_set_and_data_as_json() - |
151 | 149 |
|
152 | 150 | assert ( |
153 | 151 | result |
154 | | - == '{"id": "123", "source": "source", "type": "type", "specversion": "1.0", "time": "2023-10-25T17:09:19.736166Z", "dataschema": "http://example.com/schema", "subject": "test_subject", "data": "{\'key\': \'value\'}"}'.encode( |
| 152 | + == '{"id": "123", "source": "source", "type": "type", "specversion": "1.0", "time": "2023-10-25T17:09:19.736166Z", "dataschema": "http://example.com/schema", "subject": "test_subject", "data": {"key": "value"}}'.encode( |
155 | 153 | "utf-8" |
156 | 154 | ) |
157 | 155 | ) |
@@ -215,3 +213,107 @@ def test_read_cloud_event_from_json_with_json_as_data() -> None: |
215 | 213 | assert result.get_dataschema() == "http://example.com/schema" |
216 | 214 | assert result.get_subject() == "test_subject" |
217 | 215 | assert result.get_data() == {"key": "value"} |
| 216 | + |
| 217 | + |
| 218 | +def test_write_cloud_event_with_extension_attributes() -> None: |
| 219 | + attributes = { |
| 220 | + "id": "123", |
| 221 | + "source": "source", |
| 222 | + "type": "type", |
| 223 | + "specversion": "1.0", |
| 224 | + "customext1": "value1", |
| 225 | + "customext2": 123, |
| 226 | + } |
| 227 | + event = CloudEvent(attributes=attributes, data=None) |
| 228 | + formatter = JSONFormat() |
| 229 | + result = formatter.write(event) |
| 230 | + |
| 231 | + assert b'"customext1": "value1"' in result |
| 232 | + assert b'"customext2": 123' in result |
| 233 | + |
| 234 | + |
| 235 | +def test_read_cloud_event_with_extension_attributes() -> None: |
| 236 | + data = '{"id": "123", "source": "source", "type": "type", "specversion": "1.0", "customext1": "value1", "customext2": 123}'.encode("utf-8") |
| 237 | + formatter = JSONFormat() |
| 238 | + result = formatter.read(CloudEvent, data) |
| 239 | + |
| 240 | + assert result.get_extension("customext1") == "value1" |
| 241 | + assert result.get_extension("customext2") == 123 |
| 242 | + |
| 243 | + |
| 244 | +def test_write_cloud_event_with_different_json_content_types() -> None: |
| 245 | + test_cases = [ |
| 246 | + ("application/vnd.api+json", {"key": "value"}), |
| 247 | + ("text/json", {"key": "value"}), |
| 248 | + ("application/json; charset=utf-8", {"key": "value"}), |
| 249 | + ] |
| 250 | + |
| 251 | + for content_type, data in test_cases: |
| 252 | + attributes = { |
| 253 | + "id": "123", |
| 254 | + "source": "source", |
| 255 | + "type": "type", |
| 256 | + "specversion": "1.0", |
| 257 | + "datacontenttype": content_type, |
| 258 | + } |
| 259 | + event = CloudEvent(attributes=attributes, data=data) |
| 260 | + formatter = JSONFormat() |
| 261 | + result = formatter.write(event) |
| 262 | + |
| 263 | + assert b'"data": {"key": "value"}' in result |
| 264 | + |
| 265 | + |
| 266 | +def test_read_cloud_event_with_string_data() -> None: |
| 267 | + data = '{"id": "123", "source": "source", "type": "type", "specversion": "1.0", "data": "plain string data"}'.encode("utf-8") |
| 268 | + formatter = JSONFormat() |
| 269 | + result = formatter.read(CloudEvent, data) |
| 270 | + |
| 271 | + assert result.get_data() == "plain string data" |
| 272 | + |
| 273 | + |
| 274 | +def test_write_cloud_event_with_utc_timezone_z_suffix() -> None: |
| 275 | + attributes = { |
| 276 | + "id": "123", |
| 277 | + "source": "source", |
| 278 | + "type": "type", |
| 279 | + "specversion": "1.0", |
| 280 | + "time": datetime(2023, 10, 25, 17, 9, 19, 736166, tzinfo=timezone.utc), |
| 281 | + } |
| 282 | + event = CloudEvent(attributes=attributes, data=None) |
| 283 | + formatter = JSONFormat() |
| 284 | + result = formatter.write(event) |
| 285 | + |
| 286 | + assert b'"time": "2023-10-25T17:09:19.736166Z"' in result |
| 287 | + |
| 288 | + |
| 289 | +def test_write_cloud_event_with_unicode_data() -> None: |
| 290 | + attributes = { |
| 291 | + "id": "123", |
| 292 | + "source": "source", |
| 293 | + "type": "type", |
| 294 | + "specversion": "1.0", |
| 295 | + } |
| 296 | + event = CloudEvent(attributes=attributes, data="Hello 世界 🌍") |
| 297 | + formatter = JSONFormat() |
| 298 | + result = formatter.write(event) |
| 299 | + |
| 300 | + decoded = result.decode("utf-8") |
| 301 | + assert '"data": "Hello' in decoded |
| 302 | + assert "Hello" in decoded |
| 303 | + |
| 304 | + |
| 305 | +def test_read_cloud_event_with_unicode_data() -> None: |
| 306 | + data = '{"id": "123", "source": "source", "type": "type", "specversion": "1.0", "data": "Hello 世界 🌍"}'.encode("utf-8") |
| 307 | + formatter = JSONFormat() |
| 308 | + result = formatter.read(CloudEvent, data) |
| 309 | + |
| 310 | + assert result.get_data() == "Hello 世界 🌍" |
| 311 | + |
| 312 | + |
| 313 | +def test_read_cloud_event_from_string_input() -> None: |
| 314 | + data = '{"id": "123", "source": "source", "type": "type", "specversion": "1.0"}' |
| 315 | + formatter = JSONFormat() |
| 316 | + result = formatter.read(CloudEvent, data) |
| 317 | + |
| 318 | + assert result.get_id() == "123" |
| 319 | + assert result.get_source() == "source" |
0 commit comments