|
1 | 1 | """Evaluator tests module.""" |
| 2 | +import json |
2 | 3 | import logging |
| 4 | +import os |
3 | 5 | import pytest |
4 | 6 | import copy |
5 | 7 |
|
6 | 8 | from splitio.models.splits import Split, Status |
| 9 | +from splitio.models import segments |
7 | 10 | from splitio.models.grammar.condition import Condition, ConditionType |
8 | 11 | from splitio.models.impressions import Label |
9 | 12 | from splitio.models.grammar import condition |
@@ -245,6 +248,74 @@ def test_evaluate_treatment_with_rule_based_segment(self, mocker): |
245 | 248 | result = e. eval_with_context( '[email protected]', '[email protected]', 'some', { 'email': '[email protected]'}, ctx) |
246 | 249 | assert result['treatment'] == 'off' |
247 | 250 |
|
| 251 | + def test_evaluate_treatment_with_rbs_in_condition(self): |
| 252 | + e = evaluator.Evaluator(splitters.Splitter()) |
| 253 | + splits_storage = InMemorySplitStorage() |
| 254 | + rbs_storage = InMemoryRuleBasedSegmentStorage() |
| 255 | + segment_storage = InMemorySegmentStorage() |
| 256 | + evaluation_facctory = EvaluationDataFactory(splits_storage, segment_storage, rbs_storage) |
| 257 | + |
| 258 | + rbs_segments = os.path.join(os.path.dirname(__file__), 'files', 'rule_base_segments.json') |
| 259 | + with open(rbs_segments, 'r') as flo: |
| 260 | + data = json.loads(flo.read()) |
| 261 | + |
| 262 | + mocked_split = Split('some', 12345, False, 'off', 'user', Status.ACTIVE, 12, split_conditions, 1.2, 100, 1234, {}, None, False) |
| 263 | + rbs = rule_based_segments.from_raw(data["rbs"]["d"][0]) |
| 264 | + rbs2 = rule_based_segments.from_raw(data["rbs"]["d"][1]) |
| 265 | + rbs_storage.update([rbs, rbs2], [], 12) |
| 266 | + splits_storage.update([mocked_split], [], 12) |
| 267 | + |
| 268 | + ctx = evaluation_facctory. context_for( '[email protected]', [ 'some']) |
| 269 | + assert e. eval_with_context( '[email protected]', '[email protected]', 'some', { 'email': '[email protected]'}, ctx)[ 'treatment'] == "on" |
| 270 | + ctx = evaluation_facctory. context_for( '[email protected]', [ 'some']) |
| 271 | + assert e. eval_with_context( '[email protected]', '[email protected]', 'some', { 'email': '[email protected]'}, ctx)[ 'treatment'] == "off" |
| 272 | + |
| 273 | + |
| 274 | + def test_using_segment_in_excluded(self): |
| 275 | + rbs_segments = os.path.join(os.path.dirname(__file__), 'files', 'rule_base_segments3.json') |
| 276 | + with open(rbs_segments, 'r') as flo: |
| 277 | + data = json.loads(flo.read()) |
| 278 | + e = evaluator.Evaluator(splitters.Splitter()) |
| 279 | + splits_storage = InMemorySplitStorage() |
| 280 | + rbs_storage = InMemoryRuleBasedSegmentStorage() |
| 281 | + segment_storage = InMemorySegmentStorage() |
| 282 | + evaluation_facctory = EvaluationDataFactory(splits_storage, segment_storage, rbs_storage) |
| 283 | + |
| 284 | + mocked_split = Split('some', 12345, False, 'off', 'user', Status.ACTIVE, 12, split_conditions, 1.2, 100, 1234, {}, None, False) |
| 285 | + rbs = rule_based_segments.from_raw(data["rbs"]["d"][0]) |
| 286 | + rbs_storage.update([rbs], [], 12) |
| 287 | + splits_storage.update([mocked_split], [], 12) |
| 288 | + segment = segments. from_raw({ 'name': 'segment1', 'added': [ '[email protected]'], 'removed': [], 'till': 123}) |
| 289 | + segment_storage.put(segment) |
| 290 | + |
| 291 | + ctx = evaluation_facctory. context_for( '[email protected]', [ 'some']) |
| 292 | + assert e. eval_with_context( '[email protected]', '[email protected]', 'some', { 'email': '[email protected]'}, ctx)[ 'treatment'] == "on" |
| 293 | + ctx = evaluation_facctory. context_for( '[email protected]', [ 'some']) |
| 294 | + assert e. eval_with_context( '[email protected]', '[email protected]', 'some', { 'email': '[email protected]'}, ctx)[ 'treatment'] == "off" |
| 295 | + ctx = evaluation_facctory. context_for( '[email protected]', [ 'some']) |
| 296 | + assert e. eval_with_context( '[email protected]', '[email protected]', 'some', { 'email': '[email protected]'}, ctx)[ 'treatment'] == "off" |
| 297 | + |
| 298 | + def test_using_rbs_in_excluded(self): |
| 299 | + rbs_segments = os.path.join(os.path.dirname(__file__), 'files', 'rule_base_segments2.json') |
| 300 | + with open(rbs_segments, 'r') as flo: |
| 301 | + data = json.loads(flo.read()) |
| 302 | + e = evaluator.Evaluator(splitters.Splitter()) |
| 303 | + splits_storage = InMemorySplitStorage() |
| 304 | + rbs_storage = InMemoryRuleBasedSegmentStorage() |
| 305 | + segment_storage = InMemorySegmentStorage() |
| 306 | + evaluation_facctory = EvaluationDataFactory(splits_storage, segment_storage, rbs_storage) |
| 307 | + |
| 308 | + mocked_split = Split('some', 12345, False, 'off', 'user', Status.ACTIVE, 12, split_conditions, 1.2, 100, 1234, {}, None, False) |
| 309 | + rbs = rule_based_segments.from_raw(data["rbs"]["d"][0]) |
| 310 | + rbs2 = rule_based_segments.from_raw(data["rbs"]["d"][1]) |
| 311 | + rbs_storage.update([rbs, rbs2], [], 12) |
| 312 | + splits_storage.update([mocked_split], [], 12) |
| 313 | + |
| 314 | + ctx = evaluation_facctory. context_for( '[email protected]', [ 'some']) |
| 315 | + assert e. eval_with_context( '[email protected]', '[email protected]', 'some', { 'email': '[email protected]'}, ctx)[ 'treatment'] == "on" |
| 316 | + ctx = evaluation_facctory. context_for( '[email protected]', [ 'some']) |
| 317 | + assert e. eval_with_context( '[email protected]', '[email protected]', 'some', { 'email': '[email protected]'}, ctx)[ 'treatment'] == "on" |
| 318 | + |
248 | 319 | class EvaluationDataFactoryTests(object): |
249 | 320 | """Test evaluation factory class.""" |
250 | 321 |
|
|
0 commit comments