-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathtest_feature_manager_async.py
More file actions
268 lines (240 loc) · 10.6 KB
/
test_feature_manager_async.py
File metadata and controls
268 lines (240 loc) · 10.6 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
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
# -------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for
# license information.
# --------------------------------------------------------------------------
import unittest
import pytest
from featuremanagement.aio import FeatureManager, FeatureFilter
class TestFeatureManager(unittest.IsolatedAsyncioTestCase):
def __init__(self, methodName="runTest"):
super().__init__(methodName=methodName)
self.called_telemetry = False
# method: feature_manager_creation
@pytest.mark.asyncio
async def test_empty_feature_manager_creation(self):
feature_manager = FeatureManager({})
assert feature_manager is not None
assert not await feature_manager.is_enabled("Alpha")
# method: feature_manager_creation
@pytest.mark.asyncio
async def test_basic_feature_manager_creation(self):
feature_flags = {
"feature_management": {
"feature_flags": [
{"id": "Alpha", "description": "", "enabled": "true", "conditions": {"client_filters": []}},
{"id": "Beta", "description": "", "enabled": "false", "conditions": {"client_filters": []}},
]
}
}
feature_manager = FeatureManager(feature_flags)
assert feature_manager is not None
assert await feature_manager.is_enabled("Alpha")
assert not await feature_manager.is_enabled("Beta")
# method: feature_manager_creation
@pytest.mark.asyncio
def test_feature_manager_creation_invalid_feature_filter(self):
feature_flags = {"feature_management": {"feature_flags": []}}
with self.assertRaises(ValueError):
FeatureManager(feature_flags, feature_filters=["invalid_filter"])
# method: feature_manager_creation
@pytest.mark.asyncio
async def test_feature_manager_creation_with_filters(self):
feature_flags = {
"feature_management": {
"feature_flags": [
{
"id": "Alpha",
"description": "",
"enabled": "true",
"conditions": {"client_filters": [{"name": "AlwaysOn", "parameters": {}}]},
},
{
"id": "Beta",
"description": "",
"enabled": "false",
"conditions": {"client_filters": [{"name": "AlwaysOn", "parameters": {}}]},
},
{
"id": "Gamma",
"description": "",
"enabled": "True",
"conditions": {"client_filters": [{"name": "AlwaysOff", "parameters": {}}]},
},
{
"id": "Delta",
"description": "",
"enabled": "False",
"conditions": {"client_filters": [{"name": "AlwaysOff", "parameters": {}}]},
},
]
}
}
feature_manager = FeatureManager(feature_flags, feature_filters=[AlwaysOn(), AlwaysOff()])
assert feature_manager is not None
assert len(feature_manager._filters) == 4 # pylint: disable=protected-access
assert await feature_manager.is_enabled("Alpha")
assert not await feature_manager.is_enabled("Beta")
assert not await feature_manager.is_enabled("Gamma")
assert not await feature_manager.is_enabled("Delta")
assert not await feature_manager.is_enabled("Epsilon")
# method: feature_manager_creation
@pytest.mark.asyncio
async def test_feature_manager_creation_with_override_default(self):
feature_manager = FeatureManager({}, feature_filters=[AlwaysOn(), AlwaysOff(), FakeTimeWindowFilter()])
assert feature_manager is not None
# The fake time window should override the default one
assert len(feature_manager._filters) == 4 # pylint: disable=protected-access
# method: list_feature_flags
@pytest.mark.asyncio
async def test_list_feature_flags(self):
feature_manager = FeatureManager({})
assert feature_manager is not None
assert len(feature_manager.list_feature_flag_names()) == 0
feature_flags = {
"feature_management": {
"feature_flags": [
{"id": "Alpha", "description": "", "enabled": "true", "conditions": {"client_filters": []}},
{"id": "Beta", "description": "", "enabled": "false", "conditions": {"client_filters": []}},
]
}
}
feature_manager = FeatureManager(feature_flags)
assert feature_manager is not None
assert await feature_manager.is_enabled("Alpha")
assert not await feature_manager.is_enabled("Beta")
assert len(feature_manager.list_feature_flag_names()) == 2
# method: is_enabled
@pytest.mark.asyncio
async def test_unknown_feature_filter(self):
feature_flags = {
"feature_management": {
"feature_flags": [
{
"id": "Alpha",
"description": "",
"enabled": "true",
"conditions": {"client_filters": [{"name": "UnknownFilter", "parameters": {}}]},
},
]
}
}
feature_manager = FeatureManager(feature_flags, feature_filters=[AlwaysOn(), AlwaysOff()])
assert feature_manager is not None
with pytest.raises(ValueError) as e_info:
await feature_manager.is_enabled("Alpha")
assert e_info.type == ValueError
assert e_info.value.args[0] == "Feature flag Alpha has unknown filter UnknownFilter"
# method: feature_manager_creation
@pytest.mark.asyncio
async def test_feature_with_telemetry(self):
self.called_telemetry = False
feature_flags = {
"feature_management": {
"feature_flags": [
{"id": "Alpha", "description": "", "enabled": "true", "telemetry": {"enabled": "true"}},
]
}
}
feature_manager = FeatureManager(feature_flags, on_feature_evaluated=self.fake_telemetry_callback)
assert feature_manager is not None
assert await feature_manager.is_enabled("Alpha")
assert self.called_telemetry
# method: feature_manager_creation
@pytest.mark.asyncio
async def test_feature_with_telemetry_async(self):
self.called_telemetry = False
feature_flags = {
"feature_management": {
"feature_flags": [
{"id": "Alpha", "description": "", "enabled": "true", "telemetry": {"enabled": "true"}},
]
}
}
feature_manager = FeatureManager(feature_flags, on_feature_evaluated=self.fake_telemetry_callback_async)
assert feature_manager is not None
assert await feature_manager.is_enabled("Alpha")
assert self.called_telemetry
def fake_telemetry_callback(self, evaluation_event):
assert evaluation_event
self.called_telemetry = True
async def fake_telemetry_callback_async(self, evaluation_event):
assert evaluation_event
self.called_telemetry = True
# method: duplicate_feature_flag_handling
@pytest.mark.asyncio
async def test_duplicate_feature_flags_last_wins_async(self):
"""Test that when multiple feature flags have the same ID, the last one wins."""
feature_flags = {
"feature_management": {
"feature_flags": [
{
"id": "DuplicateFlag",
"description": "First",
"enabled": "true",
"conditions": {"client_filters": []},
},
{
"id": "DuplicateFlag",
"description": "Second",
"enabled": "false",
"conditions": {"client_filters": []},
},
{
"id": "DuplicateFlag",
"description": "Third",
"enabled": "true",
"conditions": {"client_filters": []},
},
]
}
}
feature_manager = FeatureManager(feature_flags)
# The last flag should win (enabled: true)
assert await feature_manager.is_enabled("DuplicateFlag") == True
# Should only list unique names
flag_names = feature_manager.list_feature_flag_names()
assert "DuplicateFlag" in flag_names
# Count how many times DuplicateFlag appears in the list
duplicate_count = flag_names.count("DuplicateFlag")
assert duplicate_count == 1, f"Expected DuplicateFlag to appear once, but appeared {duplicate_count} times"
@pytest.mark.asyncio
async def test_duplicate_feature_flags_last_wins_disabled_async(self):
"""Test that when multiple feature flags have the same ID, the last one wins even if disabled."""
feature_flags = {
"feature_management": {
"feature_flags": [
{
"id": "DuplicateFlag",
"description": "First",
"enabled": "true",
"conditions": {"client_filters": []},
},
{
"id": "DuplicateFlag",
"description": "Second",
"enabled": "true",
"conditions": {"client_filters": []},
},
{
"id": "DuplicateFlag",
"description": "Third",
"enabled": "false",
"conditions": {"client_filters": []},
},
]
}
}
feature_manager = FeatureManager(feature_flags)
# The last flag should win (enabled: false)
assert await feature_manager.is_enabled("DuplicateFlag") == False
class AlwaysOn(FeatureFilter):
async def evaluate(self, context, **kwargs):
return True
class AlwaysOff(FeatureFilter):
async def evaluate(self, context, **kwargs):
return False
@FeatureFilter.alias("Microsoft.TimeWindow")
class FakeTimeWindowFilter(FeatureFilter):
async def evaluate(self, context, **kwargs):
return True