-
Notifications
You must be signed in to change notification settings - Fork 861
Expand file tree
/
Copy pathtest_json_codec.py
More file actions
202 lines (166 loc) · 5.05 KB
/
test_json_codec.py
File metadata and controls
202 lines (166 loc) · 5.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# Copyright The OpenTelemetry Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import math
import pytest # type: ignore
from opentelemetry.codegen.json.runtime.json_codec import (
decode_base64,
decode_float,
decode_hex,
decode_int64,
decode_repeated,
encode_base64,
encode_float,
encode_hex,
encode_int64,
encode_repeated,
validate_type,
)
@pytest.mark.parametrize(
"value, expected",
[
(b"\x01\x02\x03", "010203"),
(b"", ""),
(None, ""),
],
)
def test_encode_hex(value: bytes | None, expected: str) -> None:
assert encode_hex(value) == expected
@pytest.mark.parametrize(
"value, expected",
[
("010203", b"\x01\x02\x03"),
("", b""),
(None, b""),
],
)
def test_decode_hex(value: str | None, expected: bytes) -> None:
assert decode_hex(value, "field") == expected
def test_decode_hex_errors() -> None:
with pytest.raises(TypeError):
decode_hex(123, "field") # type: ignore
with pytest.raises(ValueError, match="Invalid hex string"):
decode_hex("not hex", "field")
@pytest.mark.parametrize(
"value, expected",
[
(b"hello", "aGVsbG8="),
(b"", ""),
(None, ""),
],
)
def test_encode_base64(value: bytes | None, expected: str) -> None:
assert encode_base64(value) == expected
@pytest.mark.parametrize(
"value, expected",
[
("aGVsbG8=", b"hello"),
("", b""),
(None, b""),
],
)
def test_decode_base64(value: str | None, expected: bytes) -> None:
assert decode_base64(value, "field") == expected
def test_decode_base64_errors() -> None:
with pytest.raises(TypeError):
decode_base64(123, "field") # type: ignore
@pytest.mark.parametrize(
"value, expected",
[
(123, "123"),
(0, "0"),
(-1, "-1"),
],
)
def test_encode_int64(value: int, expected: str) -> None:
assert encode_int64(value) == expected
@pytest.mark.parametrize(
"value, expected",
[
("123", 123),
(123, 123),
(None, 0),
],
)
def test_decode_int64(value: int | str | None, expected: int) -> None:
assert decode_int64(value, "field") == expected
def test_decode_int64_errors() -> None:
with pytest.raises(TypeError):
decode_int64([], "field") # type: ignore
with pytest.raises(ValueError, match="Invalid int64 value"):
decode_int64("abc", "field")
@pytest.mark.parametrize(
"value, expected",
[
(1.5, 1.5),
(float("nan"), "NaN"),
(float("inf"), "Infinity"),
(float("-inf"), "-Infinity"),
],
)
def test_encode_float(value: float, expected: float | str) -> None:
result = encode_float(value)
if isinstance(expected, float) and math.isnan(expected):
assert math.isnan(result) # type: ignore
else:
assert result == expected
@pytest.mark.parametrize(
"value, expected",
[
(1.5, 1.5),
("1.5", 1.5),
(1, 1.0),
("NaN", math.nan),
("Infinity", math.inf),
("-Infinity", -math.inf),
(None, 0.0),
],
)
def test_decode_float(
value: float | int | str | None, expected: float
) -> None:
result = decode_float(value, "field")
if math.isnan(expected):
assert math.isnan(result)
else:
assert result == expected
def test_decode_float_errors() -> None:
with pytest.raises(TypeError):
decode_float([], "field") # type: ignore
with pytest.raises(ValueError, match="Invalid float value"):
decode_float("abc", "field")
def test_repeated_fields() -> None:
values = [1, 2, 3]
assert encode_repeated(values, str) == ["1", "2", "3"]
assert not encode_repeated([], str)
assert not encode_repeated(None, str)
assert decode_repeated(["1", "2"], int, "field") == [1, 2]
assert not decode_repeated([], int, "field")
assert not decode_repeated(None, int, "field")
def test_decode_repeated_errors() -> None:
with pytest.raises(TypeError):
decode_repeated("not a list", lambda x: x, "field") # type: ignore
def test_validate_type() -> None:
validate_type("s", str, "field")
validate_type(1, int, "field")
validate_type(1, (int, str), "field")
validate_type("s", (int, str), "field")
with pytest.raises(
TypeError, match="Field 'field' expected <class 'int'>, got str"
):
validate_type("s", int, "field")
with pytest.raises(
TypeError,
match=r"Field 'field' expected \(<class 'int'>, <class 'float'>\), got str",
):
validate_type("s", (int, float), "field")