|
| 1 | +# Copyright 2018-Present The CloudEvents Authors |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 4 | +# not use this file except in compliance with the License. You may obtain |
| 5 | +# a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 11 | +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 12 | +# License for the specific language governing permissions and limitations |
| 13 | +# under the License. |
| 14 | + |
| 15 | + |
| 16 | +from datetime import datetime, timezone |
| 17 | + |
| 18 | +import pytest |
| 19 | + |
| 20 | +from cloudevents.core.formats.json import JSONFormat |
| 21 | +from cloudevents.core.v1.event import CloudEvent |
| 22 | + |
| 23 | + |
| 24 | +def test_write_cloud_event_to_json_with_attributes_only() -> None: |
| 25 | + attributes = { |
| 26 | + "id": "123", |
| 27 | + "source": "source", |
| 28 | + "type": "type", |
| 29 | + "specversion": "1.0", |
| 30 | + "time": datetime(2023, 10, 25, 17, 9, 19, 736166, tzinfo=timezone.utc), |
| 31 | + "datacontenttype": "application/json", |
| 32 | + "dataschema": "http://example.com/schema", |
| 33 | + "subject": "test_subject", |
| 34 | + } |
| 35 | + event = CloudEvent(attributes=attributes, data=None) |
| 36 | + formatter = JSONFormat() |
| 37 | + result = formatter.write(event) |
| 38 | + |
| 39 | + assert ( |
| 40 | + result |
| 41 | + == '{"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"}'.encode( |
| 42 | + "utf-8" |
| 43 | + ) |
| 44 | + ) |
| 45 | + |
| 46 | + |
| 47 | +def test_write_cloud_event_to_json_with_data_as_json() -> None: |
| 48 | + attributes = { |
| 49 | + "id": "123", |
| 50 | + "source": "source", |
| 51 | + "type": "type", |
| 52 | + "specversion": "1.0", |
| 53 | + "time": datetime(2023, 10, 25, 17, 9, 19, 736166, tzinfo=timezone.utc), |
| 54 | + "datacontenttype": "application/json", |
| 55 | + "dataschema": "http://example.com/schema", |
| 56 | + "subject": "test_subject", |
| 57 | + } |
| 58 | + event = CloudEvent(attributes=attributes, data={"key": "value"}) |
| 59 | + formatter = JSONFormat() |
| 60 | + result = formatter.write(event) |
| 61 | + |
| 62 | + assert ( |
| 63 | + 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( |
| 65 | + "utf-8" |
| 66 | + ) |
| 67 | + ) |
| 68 | + |
| 69 | + |
| 70 | +def test_write_cloud_event_to_json_with_data_as_bytes() -> None: |
| 71 | + attributes = { |
| 72 | + "id": "123", |
| 73 | + "source": "source", |
| 74 | + "type": "type", |
| 75 | + "specversion": "1.0", |
| 76 | + "time": datetime(2023, 10, 25, 17, 9, 19, 736166, tzinfo=timezone.utc), |
| 77 | + "datacontenttype": "application/json", |
| 78 | + "dataschema": "http://example.com/schema", |
| 79 | + "subject": "test_subject", |
| 80 | + } |
| 81 | + event = CloudEvent(attributes=attributes, data=b"test") |
| 82 | + formatter = JSONFormat() |
| 83 | + result = formatter.write(event) |
| 84 | + |
| 85 | + assert ( |
| 86 | + result |
| 87 | + == '{"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_base64": "dGVzdA=="}'.encode( |
| 88 | + "utf-8" |
| 89 | + ) |
| 90 | + ) |
| 91 | + |
| 92 | + |
| 93 | +def test_write_cloud_event_to_json_with_data_as_str_and_content_type_not_json() -> None: |
| 94 | + attributes = { |
| 95 | + "id": "123", |
| 96 | + "source": "source", |
| 97 | + "type": "type", |
| 98 | + "specversion": "1.0", |
| 99 | + "time": datetime(2023, 10, 25, 17, 9, 19, 736166, tzinfo=timezone.utc), |
| 100 | + "datacontenttype": "text/plain", |
| 101 | + "dataschema": "http://example.com/schema", |
| 102 | + "subject": "test_subject", |
| 103 | + } |
| 104 | + event = CloudEvent(attributes=attributes, data="test") |
| 105 | + formatter = JSONFormat() |
| 106 | + result = formatter.write(event) |
| 107 | + |
| 108 | + assert ( |
| 109 | + result |
| 110 | + == '{"id": "123", "source": "source", "type": "type", "specversion": "1.0", "time": "2023-10-25T17:09:19.736166Z", "datacontenttype": "text/plain", "dataschema": "http://example.com/schema", "subject": "test_subject", "data": "test"}'.encode( |
| 111 | + "utf-8" |
| 112 | + ) |
| 113 | + ) |
| 114 | + |
| 115 | + |
| 116 | +def test_write_cloud_event_to_json_with_no_content_type_set_and_data_as_str() -> None: |
| 117 | + attributes = { |
| 118 | + "id": "123", |
| 119 | + "source": "source", |
| 120 | + "type": "type", |
| 121 | + "specversion": "1.0", |
| 122 | + "time": datetime(2023, 10, 25, 17, 9, 19, 736166, tzinfo=timezone.utc), |
| 123 | + "dataschema": "http://example.com/schema", |
| 124 | + "subject": "test_subject", |
| 125 | + } |
| 126 | + event = CloudEvent(attributes=attributes, data="I'm just a string") |
| 127 | + formatter = JSONFormat() |
| 128 | + result = formatter.write(event) |
| 129 | + |
| 130 | + assert ( |
| 131 | + result |
| 132 | + == '{"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": "I\'m just a string"}'.encode( |
| 133 | + "utf-8" |
| 134 | + ) |
| 135 | + ) |
| 136 | + |
| 137 | + |
| 138 | +def test_write_cloud_event_to_json_with_no_content_type_set_and_data_as_json() -> None: |
| 139 | + attributes = { |
| 140 | + "id": "123", |
| 141 | + "source": "source", |
| 142 | + "type": "type", |
| 143 | + "specversion": "1.0", |
| 144 | + "time": datetime(2023, 10, 25, 17, 9, 19, 736166, tzinfo=timezone.utc), |
| 145 | + "dataschema": "http://example.com/schema", |
| 146 | + "subject": "test_subject", |
| 147 | + } |
| 148 | + event = CloudEvent(attributes=attributes, data={"key": "value"}) |
| 149 | + formatter = JSONFormat() |
| 150 | + result = formatter.write(event) |
| 151 | + |
| 152 | + assert ( |
| 153 | + 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( |
| 155 | + "utf-8" |
| 156 | + ) |
| 157 | + ) |
0 commit comments