-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathtest_models.py
More file actions
153 lines (139 loc) · 4.01 KB
/
test_models.py
File metadata and controls
153 lines (139 loc) · 4.01 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
import typing
import pytest
from flagsmith.models import Flag, Flags
from flagsmith.types import SDKEvaluationResult, SDKFlagResult
def test_flag_from_evaluation_result() -> None:
# Given
flag_result: SDKFlagResult = {
"enabled": True,
"feature_key": "123",
"name": "test_feature",
"reason": "DEFAULT",
"value": "test-value",
"metadata": {"flagsmith_id": 123},
}
# When
flag: typing.Optional[Flag] = Flag.from_evaluation_result(flag_result)
# Then
assert flag
assert flag.enabled is True
assert flag.value == "test-value"
assert flag.feature_name == "test_feature"
assert flag.feature_id == 123
assert flag.is_default is False
@pytest.mark.parametrize(
"flags_result,expected_names",
[
({}, []),
(
{
"feature1": {
"enabled": True,
"feature_key": "1",
"name": "feature1",
"reason": "DEFAULT",
"value": "value1",
"metadata": {"flagsmith_id": 1},
}
},
["feature1"],
),
(
{
"feature1": {
"enabled": True,
"feature_key": "1",
"name": "feature1",
"reason": "DEFAULT",
"value": "value1",
"metadata": {"flagsmith_id": 1},
}
},
["feature1"],
),
(
{
"feature1": {
"enabled": True,
"feature_key": "1",
"name": "feature1",
"reason": "DEFAULT",
"value": "value1",
"metadata": {"flagsmith_id": 1},
},
"feature2": {
"enabled": True,
"feature_key": "2",
"name": "feature2",
"reason": "DEFAULT",
"value": "value2",
"metadata": {"flagsmith_id": 2},
},
"feature3": {
"enabled": True,
"feature_key": "3",
"name": "feature3",
"reason": "DEFAULT",
"value": 42,
"metadata": {"flagsmith_id": 3},
},
"feature4": {
"enabled": True,
"feature_key": "4",
"name": "feature4",
"reason": "DEFAULT",
"value": 42,
},
},
["feature1", "feature2", "feature3"],
),
],
)
def test_flags_from_evaluation_result(
flags_result: typing.Dict[str, SDKFlagResult],
expected_names: typing.List[str],
) -> None:
# Given
evaluation_result: SDKEvaluationResult = {
"flags": flags_result,
"segments": [],
}
# When
flags: Flags = Flags.from_evaluation_result(
evaluation_result=evaluation_result,
analytics_processor=None,
default_flag_handler=None,
)
# Then
assert set(flags.flags.keys()) == set(expected_names)
assert set(flag.feature_name for flag in flags.flags.values()) == set(
expected_names
)
@pytest.mark.parametrize(
"value,expected",
[
("string", "string"),
(42, 42),
(3.14, 3.14),
(True, True),
(False, False),
(None, None),
],
)
def test_flag_from_evaluation_result_value_types(
value: typing.Any, expected: typing.Any
) -> None:
# Given
flag_result: SDKFlagResult = {
"enabled": True,
"feature_key": "123",
"name": "test_feature",
"reason": "DEFAULT",
"value": value,
"metadata": {"flagsmith_id": 123},
}
# When
flag: typing.Optional[Flag] = Flag.from_evaluation_result(flag_result)
# Then
assert flag
assert flag.value == expected