|
| 1 | +from unittest.mock import Mock |
| 2 | + |
| 3 | +from cachetools import LRUCache |
| 4 | + |
| 5 | +from eppo_client.assignment_logger import AssignmentCacheLogger |
| 6 | +from eppo_client.client import _utcnow |
| 7 | +from eppo_client.version import __version__ |
| 8 | + |
| 9 | + |
| 10 | +def test_non_caching(): |
| 11 | + inner = Mock() |
| 12 | + logger = AssignmentCacheLogger(inner) |
| 13 | + |
| 14 | + logger.log_assignment(make_assignment_event()) |
| 15 | + logger.log_assignment(make_assignment_event()) |
| 16 | + logger.log_bandit_action(make_bandit_event()) |
| 17 | + logger.log_bandit_action(make_bandit_event()) |
| 18 | + |
| 19 | + assert inner.log_assignment.call_count == 2 |
| 20 | + assert inner.log_bandit_action.call_count == 2 |
| 21 | + |
| 22 | + |
| 23 | +def test_assignment_cache(): |
| 24 | + inner = Mock() |
| 25 | + logger = AssignmentCacheLogger(inner, assignment_cache=LRUCache(100)) |
| 26 | + |
| 27 | + logger.log_assignment(make_assignment_event()) |
| 28 | + logger.log_assignment(make_assignment_event()) |
| 29 | + |
| 30 | + assert inner.log_assignment.call_count == 1 |
| 31 | + |
| 32 | + |
| 33 | +def test_bandit_cache(): |
| 34 | + inner = Mock() |
| 35 | + logger = AssignmentCacheLogger(inner, bandit_cache=LRUCache(100)) |
| 36 | + |
| 37 | + logger.log_bandit_action(make_bandit_event()) |
| 38 | + logger.log_bandit_action(make_bandit_event()) |
| 39 | + |
| 40 | + assert inner.log_bandit_action.call_count == 1 |
| 41 | + |
| 42 | + |
| 43 | +def test_bandit_flip_flop(): |
| 44 | + inner = Mock() |
| 45 | + logger = AssignmentCacheLogger(inner, bandit_cache=LRUCache(100)) |
| 46 | + |
| 47 | + logger.log_bandit_action(make_bandit_event(action="action1")) |
| 48 | + logger.log_bandit_action(make_bandit_event(action="action1")) |
| 49 | + assert inner.log_bandit_action.call_count == 1 |
| 50 | + |
| 51 | + logger.log_bandit_action(make_bandit_event(action="action2")) |
| 52 | + assert inner.log_bandit_action.call_count == 2 |
| 53 | + |
| 54 | + logger.log_bandit_action(make_bandit_event(action="action1")) |
| 55 | + assert inner.log_bandit_action.call_count == 3 |
| 56 | + |
| 57 | + |
| 58 | +def make_assignment_event( |
| 59 | + *, |
| 60 | + allocation="allocation", |
| 61 | + experiment="experiment", |
| 62 | + featureFlag="featureFlag", |
| 63 | + variation="variation", |
| 64 | + subject="subject", |
| 65 | + timestamp=_utcnow().isoformat(), |
| 66 | + subjectAttributes={}, |
| 67 | + metaData={"sdkLanguage": "python", "sdkVersion": __version__}, |
| 68 | + extra_logging={}, |
| 69 | +): |
| 70 | + return { |
| 71 | + **extra_logging, |
| 72 | + "allocation": allocation, |
| 73 | + "experiment": experiment, |
| 74 | + "featureFlag": featureFlag, |
| 75 | + "variation": variation, |
| 76 | + "subject": subject, |
| 77 | + "timestamp": timestamp, |
| 78 | + "subjectAttributes": subjectAttributes, |
| 79 | + "metaData": metaData, |
| 80 | + } |
| 81 | + |
| 82 | + |
| 83 | +def make_bandit_event( |
| 84 | + *, |
| 85 | + flag_key="flagKey", |
| 86 | + bandit_key="banditKey", |
| 87 | + subject_key="subjectKey", |
| 88 | + action="action", |
| 89 | + action_probability=1.0, |
| 90 | + optimality_gap=None, |
| 91 | + evaluation=None, |
| 92 | + bandit_data=None, |
| 93 | + subject_context_attributes=None, |
| 94 | + timestamp=_utcnow().isoformat(), |
| 95 | + model_version="model_version", |
| 96 | + meta_data={"sdkLanguage": "python", "sdkVersion": __version__}, |
| 97 | +): |
| 98 | + return { |
| 99 | + "flagKey": flag_key, |
| 100 | + "banditKey": bandit_key, |
| 101 | + "subject": subject_key, |
| 102 | + "action": action, |
| 103 | + "actionProbability": action_probability, |
| 104 | + "optimalityGap": optimality_gap, |
| 105 | + "modelVersion": model_version, |
| 106 | + "timestamp": timestamp, |
| 107 | + "subjectNumericAttributes": {}, |
| 108 | + "subjectCategoricalAttributes": {}, |
| 109 | + "actionNumericAttributes": {}, |
| 110 | + "actionCategoricalAttributes": {}, |
| 111 | + "metaData": meta_data, |
| 112 | + } |
0 commit comments