-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_faking_redis.py
59 lines (43 loc) · 1.48 KB
/
test_faking_redis.py
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
import json
from pathlib import Path
import pytest
from birdisle import redis
TEST_ROOT = Path(__file__).resolve().parent
rc = None
redis_key_format = "key:{i}"
def load_data_and_connect():
global rc
if not rc:
rc = redis.StrictRedis(decode_responses=True)
path = TEST_ROOT / "data"
files = Path.glob(path, "redis*.json")
tests = []
for file in files:
with open(str(file), "r") as infile:
data = json.load(infile)
tests.append(data)
return [(test_case['digits'], test_case['sum']) for test_case in tests]
def create_redis_data(rc, key_format, values):
for i, value in enumerate(values):
redis_key = key_format.format(i=i)
rc.set(redis_key, value)
def delete_redis_keys(rc, key_format):
for redis_key in rc.scan_iter(key_format.format(i='*')):
rc.delete(redis_key)
def sum_redis_key_values(rc, key_format):
total = 0
for redis_key in rc.scan_iter(key_format.format(i="*")):
total += int(rc.get(redis_key))
return total
@pytest.mark.parametrize('values, result', load_data_and_connect())
def test_sum_keys(values, result):
global rc, redis_key_format
create_redis_data(rc, redis_key_format, values)
redis_keys_sum = sum_redis_key_values(rc, redis_key_format)
delete_redis_keys(rc, redis_key_format)
assert redis_keys_sum == result
if __name__ == '__main__':
tests = load_data_and_connect()
for test in tests:
test_sum_keys(test[0], test[1])
pass