|
| 1 | +from cloudevents.core.v1.event import CloudEvent |
| 2 | + |
| 3 | +import pytest |
| 4 | +from datetime import datetime |
| 5 | + |
| 6 | + |
| 7 | +@pytest.mark.parametrize( |
| 8 | + "attributes, missing_attribute", |
| 9 | + [ |
| 10 | + ({"source": "/", "type": "test", "specversion": "1.0"}, "id"), |
| 11 | + ({"id": "1", "type": "test", "specversion": "1.0"}, "source"), |
| 12 | + ({"id": "1", "source": "/", "specversion": "1.0"}, "type"), |
| 13 | + ({"id": "1", "source": "/", "type": "test"}, "specversion"), |
| 14 | + ], |
| 15 | +) |
| 16 | +def test_missing_required_attribute(attributes, missing_attribute): |
| 17 | + with pytest.raises(ValueError) as e: |
| 18 | + CloudEvent(attributes) |
| 19 | + |
| 20 | + assert str(e.value) == f"Missing required attribute(s): {missing_attribute}" |
| 21 | + |
| 22 | + |
| 23 | +@pytest.mark.parametrize( |
| 24 | + "id,error", |
| 25 | + [ |
| 26 | + (None, "Attribute 'id' must not be None"), |
| 27 | + (12, "Attribute 'id' must be a string"), |
| 28 | + ], |
| 29 | +) |
| 30 | +def test_id_validation(id, error): |
| 31 | + with pytest.raises((ValueError, TypeError)) as e: |
| 32 | + CloudEvent({"id": id, "source": "/", "type": "test", "specversion": "1.0"}) |
| 33 | + |
| 34 | + assert str(e.value) == error |
| 35 | + |
| 36 | + |
| 37 | +@pytest.mark.parametrize("source,error", [(123, "Attribute 'source' must be a string")]) |
| 38 | +def test_source_validation(source, error): |
| 39 | + with pytest.raises((ValueError, TypeError)) as e: |
| 40 | + CloudEvent({"id": "1", "source": source, "type": "test", "specversion": "1.0"}) |
| 41 | + |
| 42 | + assert str(e.value) == error |
| 43 | + |
| 44 | + |
| 45 | +@pytest.mark.parametrize( |
| 46 | + "specversion,error", |
| 47 | + [ |
| 48 | + (1.0, "Attribute 'specversion' must be a string"), |
| 49 | + ("1.4", "Attribute 'specversion' must be '1.0'"), |
| 50 | + ], |
| 51 | +) |
| 52 | +def test_specversion_validation(specversion, error): |
| 53 | + with pytest.raises((ValueError, TypeError)) as e: |
| 54 | + CloudEvent( |
| 55 | + {"id": "1", "source": "/", "type": "test", "specversion": specversion} |
| 56 | + ) |
| 57 | + |
| 58 | + assert str(e.value) == error |
| 59 | + |
| 60 | + |
| 61 | +@pytest.mark.parametrize( |
| 62 | + "time,error", |
| 63 | + [ |
| 64 | + ("2023-10-25T17:09:19.736166Z", "Attribute 'time' must be a datetime object"), |
| 65 | + ( |
| 66 | + datetime(2023, 10, 25, 17, 9, 19, 736166), |
| 67 | + "Attribute 'time' must be timezone aware", |
| 68 | + ), |
| 69 | + ], |
| 70 | +) |
| 71 | +def test_time_validation(time, error): |
| 72 | + with pytest.raises((ValueError, TypeError)) as e: |
| 73 | + CloudEvent( |
| 74 | + { |
| 75 | + "id": "1", |
| 76 | + "source": "/", |
| 77 | + "type": "test", |
| 78 | + "specversion": "1.0", |
| 79 | + "time": time, |
| 80 | + } |
| 81 | + ) |
| 82 | + |
| 83 | + assert str(e.value) == error |
| 84 | + |
| 85 | + |
| 86 | +@pytest.mark.parametrize( |
| 87 | + "subject,error", |
| 88 | + [ |
| 89 | + (1234, "Attribute 'subject' must be a string"), |
| 90 | + ( |
| 91 | + "", |
| 92 | + "Attribute 'subject' must not be empty", |
| 93 | + ), |
| 94 | + ], |
| 95 | +) |
| 96 | +def test_subject_validation(subject, error): |
| 97 | + with pytest.raises((ValueError, TypeError)) as e: |
| 98 | + CloudEvent( |
| 99 | + { |
| 100 | + "id": "1", |
| 101 | + "source": "/", |
| 102 | + "type": "test", |
| 103 | + "specversion": "1.0", |
| 104 | + "subject": subject, |
| 105 | + } |
| 106 | + ) |
| 107 | + |
| 108 | + assert str(e.value) == error |
| 109 | + |
| 110 | + |
| 111 | +@pytest.mark.parametrize( |
| 112 | + "datacontenttype,error", |
| 113 | + [ |
| 114 | + (1234, "Attribute 'datacontenttype' must be a string"), |
| 115 | + ( |
| 116 | + "", |
| 117 | + "Attribute 'datacontenttype' must not be empty", |
| 118 | + ), |
| 119 | + ], |
| 120 | +) |
| 121 | +def test_datacontenttype_validation(datacontenttype, error): |
| 122 | + with pytest.raises((ValueError, TypeError)) as e: |
| 123 | + CloudEvent( |
| 124 | + { |
| 125 | + "id": "1", |
| 126 | + "source": "/", |
| 127 | + "type": "test", |
| 128 | + "specversion": "1.0", |
| 129 | + "datacontenttype": datacontenttype, |
| 130 | + } |
| 131 | + ) |
| 132 | + |
| 133 | + assert str(e.value) == error |
| 134 | + |
| 135 | + |
| 136 | +@pytest.mark.parametrize( |
| 137 | + "dataschema,error", |
| 138 | + [ |
| 139 | + (1234, "Attribute 'dataschema' must be a string"), |
| 140 | + ( |
| 141 | + "", |
| 142 | + "Attribute 'dataschema' must not be empty", |
| 143 | + ), |
| 144 | + ], |
| 145 | +) |
| 146 | +def test_dataschema_validation(dataschema, error): |
| 147 | + with pytest.raises((ValueError, TypeError)) as e: |
| 148 | + CloudEvent( |
| 149 | + { |
| 150 | + "id": "1", |
| 151 | + "source": "/", |
| 152 | + "type": "test", |
| 153 | + "specversion": "1.0", |
| 154 | + "dataschema": dataschema, |
| 155 | + } |
| 156 | + ) |
| 157 | + |
| 158 | + assert str(e.value) == error |
0 commit comments