/
test_encoding.py
275 lines (255 loc) · 8.92 KB
/
test_encoding.py
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
# Copyright (c) 2017-present, Facebook, Inc.
# All rights reserved.
#
# This source code is licensed under the BSD-style license found in the
# LICENSE file in the root directory of this source tree.
from typing import (
Any,
Dict,
List,
Optional,
Set,
Tuple,
Type,
Union,
)
import pytest
from monkeytype.encoding import (
CallTraceRow,
maybe_decode_type,
maybe_encode_type,
type_from_dict,
type_from_json,
type_to_dict,
type_to_json,
serialize_traces,
)
from mypy_extensions import TypedDict
from monkeytype.exceptions import InvalidTypeError
from monkeytype.tracing import CallTrace
from monkeytype.typing import DUMMY_TYPED_DICT_NAME, NoneType, NotImplementedType, mappingproxy
from .util import Outer
from unittest.mock import Mock
def dummy_func(a, b):
return a + b
class TestTypeConversion:
@pytest.mark.parametrize(
'typ',
[
# Non-generics
NoneType,
NotImplementedType,
mappingproxy,
int,
Outer,
Outer.Inner,
Any,
# Simple generics
Dict[Any, Any],
Dict[int, str],
List[str],
Optional[str],
Set[int],
Tuple[int, str, str],
Tuple, # unparameterized tuple
Tuple[()], # empty tuple
Type[Outer],
Union[Outer.Inner, str, None],
# Nested generics
Dict[str, Union[str, int]],
List[Optional[str]],
# Let's get craaaazy
Dict[
str,
Union[
Dict[str, int],
Set[Outer.Inner],
Optional[Dict[str, int]]
]
],
],
)
def test_type_round_trip(self, typ):
assert type_from_dict(type_to_dict(typ)) == typ
assert type_from_json(type_to_json(typ)) == typ
@pytest.mark.parametrize(
'typ, expected',
[
(
Dict[str, int],
{
'elem_types': [
{'module': 'builtins', 'qualname': 'str'},
{'module': 'builtins', 'qualname': 'int'},
],
'module': 'typing',
'qualname': 'Dict',
},
),
(
TypedDict(DUMMY_TYPED_DICT_NAME, {'a': int, 'b': str}),
{
'elem_types': {
'a': {'module': 'builtins', 'qualname': 'int'},
'b': {'module': 'builtins', 'qualname': 'str'},
},
'is_typed_dict': True,
'module': 'tests.test_encoding',
'qualname': DUMMY_TYPED_DICT_NAME,
},
),
(
TypedDict(DUMMY_TYPED_DICT_NAME, {'a': TypedDict(DUMMY_TYPED_DICT_NAME, {'a': int, 'b': str})}),
{
'elem_types': {
'a': {
'elem_types': {
'a': {'module': 'builtins', 'qualname': 'int'},
'b': {'module': 'builtins', 'qualname': 'str'},
},
'is_typed_dict': True,
'module': 'tests.test_encoding',
'qualname': DUMMY_TYPED_DICT_NAME,
},
},
'is_typed_dict': True,
'module': 'tests.test_encoding',
'qualname': DUMMY_TYPED_DICT_NAME,
},
),
],
)
def test_type_to_dict(self, typ, expected):
assert type_to_dict(typ) == expected
@pytest.mark.parametrize(
'type_dict, expected',
[
(
{
'elem_types': {
'a': {'module': 'builtins', 'qualname': 'int'},
'b': {'module': 'builtins', 'qualname': 'str'},
},
'is_typed_dict': True,
'module': 'tests.test_encoding',
'qualname': DUMMY_TYPED_DICT_NAME,
},
TypedDict(DUMMY_TYPED_DICT_NAME, {'a': int, 'b': str}),
),
],
)
def test_type_from_dict(self, type_dict, expected):
assert type_from_dict(type_dict) == expected
@pytest.mark.parametrize(
'type_dict, expected',
[
(
{
'elem_types': {
'a': {
'elem_types': {
'a': {'module': 'builtins', 'qualname': 'int'},
'b': {'module': 'builtins', 'qualname': 'str'},
},
'is_typed_dict': True,
'module': 'tests.test_encoding',
'qualname': DUMMY_TYPED_DICT_NAME,
},
},
'is_typed_dict': True,
'module': 'tests.test_encoding',
'qualname': DUMMY_TYPED_DICT_NAME,
},
TypedDict(DUMMY_TYPED_DICT_NAME, {'a': TypedDict(DUMMY_TYPED_DICT_NAME, {'a': int, 'b': str})}),
),
],
)
def test_type_from_dict_nested(self, type_dict, expected):
assert type_from_dict(type_dict) == expected
@pytest.mark.parametrize(
'type_dict, expected',
[
(
TypedDict(DUMMY_TYPED_DICT_NAME, {'a': int, 'b': str}),
'{"elem_types": {"a": {"module": "builtins", "qualname": "int"},'
+ ' "b": {"module": "builtins", "qualname": "str"}},'
+ ' "is_typed_dict": true, "module": "tests.test_encoding", "qualname": "DUMMY_NAME"}',
),
],
)
def test_type_to_json(self, type_dict, expected):
assert type_to_json(type_dict) == expected
@pytest.mark.parametrize(
'type_dict_string, expected',
[
(
'{"elem_types": {"a": {"module": "builtins", "qualname": "int"},'
+ ' "b": {"module": "builtins", "qualname": "str"}},'
+ ' "is_typed_dict": true, "module": "tests.test_encoding", "qualname": "DUMMY_NAME"}',
TypedDict(DUMMY_TYPED_DICT_NAME, {'a': int, 'b': str}),
),
],
)
def test_type_from_json(self, type_dict_string, expected):
assert type_from_json(type_dict_string) == expected
@pytest.mark.parametrize(
'type_dict',
[
(TypedDict(DUMMY_TYPED_DICT_NAME, {'a': int, 'b': str})),
],
)
def test_type_round_trip_typed_dict(self, type_dict):
assert type_from_dict(type_to_dict(type_dict)) == type_dict
assert type_from_json(type_to_json(type_dict)) == type_dict
def test_trace_round_trip(self):
trace = CallTrace(dummy_func, {'a': int, 'b': int}, int)
assert CallTraceRow.from_trace(trace).to_trace() == trace
def test_convert_non_type(self):
with pytest.raises(InvalidTypeError):
type_from_dict({
'module': Outer.Inner.f.__module__,
'qualname': Outer.Inner.f.__qualname__,
})
@pytest.mark.parametrize(
'encoder, typ, expected, should_call_encoder',
[
(Mock(), None, None, False),
(Mock(return_value='foo'), str, 'foo', True),
]
)
def test_maybe_encode_type(self, encoder, typ, expected, should_call_encoder):
ret = maybe_encode_type(encoder, typ)
if should_call_encoder:
encoder.assert_called_with(typ)
else:
encoder.assert_not_called()
assert ret == expected
@pytest.mark.parametrize(
'encoder, typ, expected, should_call_encoder',
[
(Mock(), None, None, False),
(Mock(), 'null', None, False),
(Mock(return_value='foo'), 'str', 'foo', True),
]
)
def test_maybe_decode_type(self, encoder, typ, expected, should_call_encoder):
ret = maybe_decode_type(encoder, typ)
if should_call_encoder:
encoder.assert_called_with(typ)
else:
encoder.assert_not_called()
assert ret == expected
class TestSerializeTraces:
def test_log_failure_and_continue(self, caplog):
traces = [
CallTrace(dummy_func, {'a': int, 'b': int}, int),
CallTrace(object(), {}), # object() will fail to serialize
CallTrace(dummy_func, {'a': str, 'b': str}, str),
]
rows = list(serialize_traces(traces))
expected = [
CallTraceRow.from_trace(traces[0]),
CallTraceRow.from_trace(traces[2]),
]
assert rows == expected
assert [r.msg for r in caplog.records] == ["Failed to serialize trace"]