|
| 1 | +from typing import Optional, Type, Union |
| 2 | + |
1 | 3 | import pytest |
2 | 4 | from marshmallow import Schema, ValidationError |
3 | 5 |
|
4 | | -from starknet_py.net.client_models import BlockStatus, TransactionStatus |
5 | | -from starknet_py.net.schemas.common import NonPrefixedHex |
6 | | -from starknet_py.net.schemas.rpc import BlockStatusField, Felt, StatusField |
| 6 | +from starknet_py.net.client_models import BlockStatus, DAMode, Hash, TransactionStatus |
| 7 | +from starknet_py.net.schemas.common import ( |
| 8 | + BlockStatusField, |
| 9 | + DAModeField, |
| 10 | + Felt, |
| 11 | + NonPrefixedHex, |
| 12 | + StatusField, |
| 13 | + Uint64, |
| 14 | + Uint128, |
| 15 | +) |
7 | 16 |
|
8 | 17 |
|
9 | | -def test_serialize_felt(): |
10 | | - class SchemaWithFelt(Schema): |
11 | | - value1 = Felt(data_key="value1") |
| 18 | +class SchemaWithUint64(Schema): |
| 19 | + value = Uint64(data_key="value") |
12 | 20 |
|
13 | | - data = {"value1": 2137} |
14 | 21 |
|
15 | | - serialized = SchemaWithFelt().dumps(data) |
16 | | - assert '"value1": "0x859"' in serialized |
| 22 | +class SchemaWithUint128(Schema): |
| 23 | + value = Uint128(data_key="value") |
| 24 | + |
| 25 | + |
| 26 | +class SchemaWithFelt(Schema): |
| 27 | + value = Felt(data_key="value") |
17 | 28 |
|
18 | 29 |
|
19 | | -def test_serialize_felt_throws_on_none(): |
20 | | - class SchemaWithFelt(Schema): |
21 | | - value1 = Felt(data_key="value1") |
| 30 | +class SchemaWithDAModeField(Schema): |
| 31 | + value = DAModeField(data_key="value") |
22 | 32 |
|
23 | | - data = {"value1": None} |
24 | | - with pytest.raises(TypeError): |
| 33 | + |
| 34 | +def test_serialize_felt(): |
| 35 | + data = {"value": 2137} |
| 36 | + |
| 37 | + serialized = SchemaWithFelt().dumps(data) |
| 38 | + assert '"value": "0x859"' in serialized |
| 39 | + |
| 40 | + |
| 41 | +@pytest.mark.parametrize( |
| 42 | + "data", |
| 43 | + [ |
| 44 | + {"value": None}, |
| 45 | + {"value": 2**252}, |
| 46 | + ], |
| 47 | +) |
| 48 | +def test_serialize_felt_throws_on_invalid_data(data): |
| 49 | + with pytest.raises(ValidationError, match="Invalid value provided for Felt"): |
25 | 50 | SchemaWithFelt().dumps(data) |
26 | 51 |
|
27 | 52 |
|
28 | 53 | def test_deserialize_felt(): |
29 | | - class SchemaWithFelt(Schema): |
30 | | - value1 = Felt(data_key="value1") |
31 | | - |
32 | | - data = {"value1": "0x859"} |
| 54 | + data = {"value": "0x859"} |
33 | 55 |
|
34 | 56 | deserialized = SchemaWithFelt().load(data) |
35 | 57 | assert isinstance(deserialized, dict) |
36 | | - assert deserialized["value1"] == 2137 |
| 58 | + assert deserialized["value"] == 2137 |
37 | 59 |
|
38 | 60 |
|
39 | 61 | def test_deserialize_felt_throws_on_invalid_data(): |
40 | | - class SchemaWithFelt(Schema): |
41 | | - value1 = Felt(data_key="value1") |
42 | | - |
43 | | - data = {"value1": "2137"} |
| 62 | + data = {"value": "2137"} |
44 | 63 |
|
45 | | - with pytest.raises(ValidationError, match="Invalid value provided for felt"): |
| 64 | + with pytest.raises(ValidationError, match="Invalid value provided for Felt"): |
46 | 65 | SchemaWithFelt().load(data) |
47 | 66 |
|
48 | | - data = {"value1": "0xwww"} |
49 | | - with pytest.raises(ValidationError, match="Invalid felt."): |
| 67 | + data = {"value": "0xwww"} |
| 68 | + with pytest.raises(ValidationError, match="Invalid value provided for Felt"): |
50 | 69 | SchemaWithFelt().load(data) |
51 | 70 |
|
52 | 71 |
|
| 72 | +@pytest.mark.parametrize( |
| 73 | + "data, expected_serialized", |
| 74 | + ( |
| 75 | + ({"value": 0}, "0x0"), |
| 76 | + ({"value": "0x100"}, "0x100"), |
| 77 | + ({"value": 2**32}, "0x100000000"), |
| 78 | + ), |
| 79 | +) |
| 80 | +def test_serialize_uint64(data, expected_serialized): |
| 81 | + serialized = SchemaWithUint64().dumps(data) |
| 82 | + assert f'"value": "{expected_serialized}"' in serialized |
| 83 | + |
| 84 | + |
| 85 | +@pytest.mark.parametrize( |
| 86 | + "data", |
| 87 | + [{"value": -1}, {"value": 2**64}, {"value": None}], |
| 88 | +) |
| 89 | +def test_serialize_uint64_throws_on_invalid_data(data): |
| 90 | + with pytest.raises( |
| 91 | + ValidationError, |
| 92 | + match=get_uint_error_message(Uint64, data["value"]), |
| 93 | + ): |
| 94 | + SchemaWithUint64().dumps(data) |
| 95 | + |
| 96 | + |
| 97 | +@pytest.mark.parametrize( |
| 98 | + "data", |
| 99 | + [{"value": "0x100000000"}, {"value": 2**32}], |
| 100 | +) |
| 101 | +def test_deserialize_uint64(data): |
| 102 | + deserialized = SchemaWithUint64().load(data) |
| 103 | + assert isinstance(deserialized, dict) |
| 104 | + assert deserialized["value"] == 2**32 |
| 105 | + |
| 106 | + |
| 107 | +@pytest.mark.parametrize( |
| 108 | + "data", |
| 109 | + [ |
| 110 | + {"value": -1}, |
| 111 | + {"value": "1000"}, |
| 112 | + {"value": 2**64}, |
| 113 | + {"value": "0xwrong"}, |
| 114 | + {"value": ""}, |
| 115 | + ], |
| 116 | +) |
| 117 | +def test_deserialize_uint64_throws_on_invalid_data(data): |
| 118 | + with pytest.raises( |
| 119 | + ValidationError, |
| 120 | + match=get_uint_error_message(Uint64, data["value"]), |
| 121 | + ): |
| 122 | + SchemaWithUint64().load(data) |
| 123 | + |
| 124 | + |
| 125 | +def test_serialize_uint128(): |
| 126 | + data = {"value": 2**64} |
| 127 | + serialized = SchemaWithUint128().dumps(data) |
| 128 | + assert '"value": "0x10000000000000000"' in serialized |
| 129 | + |
| 130 | + |
| 131 | +def test_serialize_uint128_throws_on_invalid_data(): |
| 132 | + data = {"value": 2**128} |
| 133 | + with pytest.raises( |
| 134 | + ValidationError, |
| 135 | + match=get_uint_error_message(Uint128, data["value"]), |
| 136 | + ): |
| 137 | + SchemaWithUint128().dumps(data) |
| 138 | + |
| 139 | + |
| 140 | +@pytest.mark.parametrize( |
| 141 | + "data", |
| 142 | + [{"value": "0x10000000000000000"}, {"value": 2**64}], |
| 143 | +) |
| 144 | +def test_deserialize_uint128(data): |
| 145 | + deserialized = SchemaWithUint128().load(data) |
| 146 | + assert isinstance(deserialized, dict) |
| 147 | + assert deserialized["value"] == 2**64 |
| 148 | + |
| 149 | + |
| 150 | +@pytest.mark.parametrize( |
| 151 | + "data", |
| 152 | + [ |
| 153 | + {"value": -1}, |
| 154 | + {"value": "1000"}, |
| 155 | + {"value": 2**128}, |
| 156 | + {"value": "0xwrong"}, |
| 157 | + {"value": ""}, |
| 158 | + ], |
| 159 | +) |
| 160 | +def test_deserialize_uint128_throws_on_invalid_data(data): |
| 161 | + with pytest.raises( |
| 162 | + ValidationError, |
| 163 | + match=get_uint_error_message(Uint128, data["value"]), |
| 164 | + ): |
| 165 | + SchemaWithUint128().load(data) |
| 166 | + |
| 167 | + |
53 | 168 | def test_serialize_hex(): |
54 | 169 | class SchemaWithHex(Schema): |
55 | 170 | value1 = NonPrefixedHex(data_key="value1") |
@@ -134,3 +249,31 @@ class SchemaWithBlockStatusField(Schema): |
134 | 249 |
|
135 | 250 | with pytest.raises(ValidationError, match="Invalid value for BlockStatus provided"): |
136 | 251 | SchemaWithBlockStatusField().load(data) |
| 252 | + |
| 253 | + |
| 254 | +@pytest.mark.parametrize( |
| 255 | + "data", |
| 256 | + [{"value": DAMode.L1}, {"value": DAMode.L2}], |
| 257 | +) |
| 258 | +def test_serialize_damode_field(data): |
| 259 | + serialized = SchemaWithDAModeField().dumps(data) |
| 260 | + assert f'"value": "{data["value"].name}"' in serialized |
| 261 | + |
| 262 | + |
| 263 | +@pytest.mark.parametrize( |
| 264 | + "data, expected_deserialized", |
| 265 | + ( |
| 266 | + ({"value": DAMode.L1.name}, DAMode.L1), |
| 267 | + ({"value": DAMode.L2.name}, DAMode.L2), |
| 268 | + ), |
| 269 | +) |
| 270 | +def test_deserialize_damode_field(data, expected_deserialized): |
| 271 | + deserialized = SchemaWithDAModeField().load(data) |
| 272 | + assert isinstance(deserialized, dict) |
| 273 | + assert deserialized["value"] == expected_deserialized |
| 274 | + |
| 275 | + |
| 276 | +def get_uint_error_message( |
| 277 | + class_type: Union[Type[Uint64], Type[Uint128]], value: Optional[Hash] |
| 278 | +) -> str: |
| 279 | + return f"Invalid value provided for {class_type.__name__}: {str(value)}" |
0 commit comments