-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathtest_dynamic_typing.py
58 lines (50 loc) · 1.35 KB
/
test_dynamic_typing.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
from builtins import complex
import pytest
from json_to_models.dynamic_typing import DUnion, StringLiteral, get_hash_string
# *args | MetaData
test_dunion = [
pytest.param(
[int, int],
DUnion(int),
id="unique_types"
),
pytest.param(
[int, DUnion(int)],
DUnion(int),
id="nested_union_&_merge"
),
pytest.param(
[str, DUnion(int, DUnion(float, complex))],
DUnion(int, float, complex, str),
id="complex_merge"
),
pytest.param(
[str, StringLiteral({'a'})],
DUnion(str),
id="str_literal_to_string"
),
pytest.param(
[StringLiteral({'b'}), StringLiteral({'a'})],
DUnion(StringLiteral({'a', 'b'})),
id="str_literal_merge"
),
pytest.param(
[StringLiteral({str(i)}) for i in range(100)],
DUnion(str),
id="str_literal_too_much"
),
]
@pytest.mark.parametrize("value,expected", test_dunion)
def test_dunion_creation(value, expected):
result = DUnion(*value)
assert result == expected
def test_hash_string():
a = {'a': int}
b = {'b': int}
c = {'a': float}
assert len(set(map(get_hash_string, (a, b, c)))) == 3
union = DUnion(str, float)
h1 = union.to_hash_string()
union.replace(complex, index=0)
h2 = union.to_hash_string()
assert h1 != h2, f"{h1}, {h2}"