Skip to content

Commit dc35497

Browse files
authored
Merge pull request #175 from launchdarkly/cc/sc-130602/test-data-flag-rule-builder-op-bugfix
Bugfix for FlagRuleBuilder evaluation internals
2 parents 1f21ca5 + fb9494b commit dc35497

File tree

3 files changed

+39
-5
lines changed

3 files changed

+39
-5
lines changed

ldclient/impl/integrations/test_data/test_data_source.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@
55

66
class _TestDataSource():
77

8-
def __init__(self, feature_store, test_data):
8+
def __init__(self, feature_store, test_data, ready):
99
self._feature_store = feature_store
1010
self._test_data = test_data
11+
self._ready = ready
1112

1213
def start(self):
14+
self._ready.set()
1315
self._feature_store.init(self._test_data._make_init_data())
1416

1517
def stop(self):

ldclient/integrations/test_data.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ def __init__(self):
5151
self._instances = []
5252

5353
def __call__(self, config, store, ready):
54-
data_source = _TestDataSource(store, self)
54+
data_source = _TestDataSource(store, self, ready)
5555
try:
5656
self._lock.lock()
5757
self._instances.append(data_source)
@@ -485,7 +485,7 @@ def and_match(self, attribute: str, *values) -> 'FlagRuleBuilder':
485485
"""
486486
self._clauses.append({
487487
'attribute': attribute,
488-
'operator': 'in',
488+
'op': 'in',
489489
'values': list(values),
490490
'negate': False
491491
})
@@ -508,7 +508,7 @@ def and_not_match(self, attribute: str, *values) -> 'FlagRuleBuilder':
508508
"""
509509
self._clauses.append({
510510
'attribute': attribute,
511-
'operator': 'in',
511+
'op': 'in',
512512
'values': list(values),
513513
'negate': True
514514
})

testing/integrations/test_test_data_source.py

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ def test_flagbuilder_can_build():
285285
'clauses': [
286286
{'attribute': 'country',
287287
'negate': False,
288-
'operator': 'in',
288+
'op': 'in',
289289
'values': ['fr']
290290
}
291291
],
@@ -297,3 +297,35 @@ def test_flagbuilder_can_build():
297297
}
298298

299299
assert flag._build(1) == expected_result
300+
301+
def test_flag_can_evaluate_rules():
302+
td = TestData.data_source()
303+
store = InMemoryFeatureStore()
304+
305+
client = LDClient(config=Config('SDK_KEY',
306+
update_processor_class = td,
307+
send_events = False,
308+
feature_store = store))
309+
310+
td.update(td.flag(key='test-flag')
311+
.fallthrough_variation(False)
312+
.if_match('firstName', 'Mike')
313+
.and_not_match('country', 'gb')
314+
.then_return(True))
315+
316+
# user1 should satisfy the rule (matching firstname, not matching country)
317+
user1 = { 'key': 'user1', 'firstName': 'Mike', 'country': 'us' }
318+
eval1 = client.variation_detail('test-flag', user1, default='default')
319+
320+
assert eval1.value == True
321+
assert eval1.variation_index == 0
322+
assert eval1.reason['kind'] == 'RULE_MATCH'
323+
324+
# user2 should NOT satisfy the rule (not matching firstname despite not matching country)
325+
user2 = { 'key': 'user2', 'firstName': 'Joe', 'country': 'us' }
326+
eval2 = client.variation_detail('test-flag', user2, default='default')
327+
328+
assert eval2.value == False
329+
assert eval2.variation_index == 1
330+
assert eval2.reason['kind'] == 'FALLTHROUGH'
331+

0 commit comments

Comments
 (0)