|
1
|
|
|
# coding=utf-8 |
|
2
|
|
|
from decision_engine.sources import DictSource, FixedValueSource, \ |
|
3
|
|
|
PercentageSource, RandomIntSource |
|
4
|
|
|
|
|
5
|
|
|
|
|
6
|
|
|
def test_dict_source(): |
|
7
|
|
|
test_key = 'test_key' |
|
8
|
|
|
test_value = 5000 |
|
9
|
|
|
data = {test_key: test_value} |
|
10
|
|
|
|
|
11
|
|
|
# Test source with normal data |
|
12
|
|
|
src = DictSource(test_key) |
|
13
|
|
|
assert src.get_value(data) == test_value |
|
14
|
|
|
|
|
15
|
|
|
# Test source with data set to None |
|
16
|
|
|
assert src.get_value() is None |
|
17
|
|
|
|
|
18
|
|
|
assert src.__repr__() == f"Name: '{src.name}' | key: '{src.key}'" |
|
19
|
|
|
|
|
20
|
|
|
|
|
21
|
|
|
def test_fixed_value_source(): |
|
22
|
|
|
test_value = 5000 |
|
23
|
|
|
src = FixedValueSource(test_value) |
|
24
|
|
|
|
|
25
|
|
|
assert src.get_value() == test_value |
|
26
|
|
|
assert src.__repr__() == f"Name: '{src.name}' | value: {src.value}" |
|
27
|
|
|
|
|
28
|
|
|
|
|
29
|
|
|
def test_percentage_value_source(): |
|
30
|
|
|
test_value = 75000 |
|
31
|
|
|
data = { |
|
32
|
|
|
'test_value': 100000 |
|
33
|
|
|
} |
|
34
|
|
|
src = PercentageSource(0.75, DictSource('test_value')) |
|
35
|
|
|
|
|
36
|
|
|
assert src.get_value(data) == test_value |
|
37
|
|
|
assert src.__repr__() == f"Name: '{src.name}' | " \ |
|
38
|
|
|
f"percentage: {src.percentage} | " \ |
|
39
|
|
|
f"source: '{src.source.name}'" |
|
40
|
|
|
|
|
41
|
|
|
|
|
42
|
|
|
def test_random_int_source(): |
|
43
|
|
|
max_test_value = 120 |
|
44
|
|
|
min_test_value = 12 |
|
45
|
|
|
|
|
46
|
|
|
# Test random source without explicit seed |
|
47
|
|
|
src = RandomIntSource(min_test_value, max_test_value) |
|
48
|
|
|
assert min_test_value <= src.get_value() <= max_test_value |
|
49
|
|
|
|
|
50
|
|
|
# Test random source with explicit seed |
|
51
|
|
|
src = RandomIntSource(min_test_value, max_test_value, 12345) |
|
52
|
|
|
assert min_test_value <= src.get_value() <= max_test_value |
|
53
|
|
|
|
|
54
|
|
|
# Check the name |
|
55
|
|
|
assert src.__repr__() == f"Name: '{src.name}' | " \ |
|
56
|
|
|
f"min_value: {src.min_value} | " \ |
|
57
|
|
|
f"max_value: {src.max_value} | " \ |
|
58
|
|
|
f"seed: {src.seed}" |
|
59
|
|
|
|