forked from launchdarkly/python-server-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_feature_store.py
More file actions
108 lines (88 loc) · 3.47 KB
/
Copy pathtest_feature_store.py
File metadata and controls
108 lines (88 loc) · 3.47 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
import pytest
import redis
from ldclient.feature_store import InMemoryFeatureStore
from ldclient.redis_feature_store import RedisFeatureStore
class TestFeatureStore:
redis_host = 'localhost'
redis_port = 6379
def in_memory(self):
return InMemoryFeatureStore()
def redis_with_local_cache(self):
r = redis.StrictRedis(host=self.redis_host, port=self.redis_port, db=0)
r.delete("launchdarkly:features")
return RedisFeatureStore()
def redis_no_local_cache(self):
r = redis.StrictRedis(host=self.redis_host, port=self.redis_port, db=0)
r.delete("launchdarkly:features")
return RedisFeatureStore(expiration=0)
params = [in_memory, redis_with_local_cache, redis_no_local_cache]
@pytest.fixture(params=params)
def store(self, request):
return request.param(self)
@staticmethod
def make_feature(key, ver):
return {
u'key': key,
u'version': ver,
u'salt': u'abc',
u'on': True,
u'variations': [
{
u'value': True,
u'weight': 100,
u'targets': []
},
{
u'value': False,
u'weight': 0,
u'targets': []
}
]
}
def base_initialized_store(self, store):
store.init({
'foo': self.make_feature('foo', 10),
'bar': self.make_feature('bar', 10),
})
return store
def test_not_initially_initialized(self, store):
assert store.initialized is False
def test_initialized(self, store):
store = self.base_initialized_store(store)
assert store.initialized is True
def test_get_existing_feature(self, store):
store = self.base_initialized_store(store)
expected = self.make_feature('foo', 10)
assert store.get('foo', lambda x: x) == expected
def test_get_nonexisting_feature(self, store):
store = self.base_initialized_store(store)
assert store.get('biz', lambda x: x) is None
def test_upsert_with_newer_version(self, store):
store = self.base_initialized_store(store)
new_ver = self.make_feature('foo', 11)
store.upsert('foo', new_ver)
assert store.get('foo', lambda x: x) == new_ver
def test_upsert_with_older_version(self, store):
store = self.base_initialized_store(store)
new_ver = self.make_feature('foo', 9)
expected = self.make_feature('foo', 10)
store.upsert('foo', new_ver)
assert store.get('foo', lambda x: x) == expected
def test_upsert_with_new_feature(self, store):
store = self.base_initialized_store(store)
new_ver = self.make_feature('biz', 1)
store.upsert('biz', new_ver)
assert store.get('biz', lambda x: x) == new_ver
def test_delete_with_newer_version(self, store):
store = self.base_initialized_store(store)
store.delete('foo', 11)
assert store.get('foo', lambda x: x) is None
def test_delete_unknown_feature(self, store):
store = self.base_initialized_store(store)
store.delete('biz', 11)
assert store.get('biz', lambda x: x) is None
def test_delete_with_older_version(self, store):
store = self.base_initialized_store(store)
store.delete('foo', 9)
expected = self.make_feature('foo', 10)
assert store.get('foo', lambda x: x) == expected