Skip to content

Instantly share code, notes, and snippets.

@paluchasz
Last active December 26, 2019 18:52
Show Gist options
  • Save paluchasz/0f25468360581e16e608330177f10f3b to your computer and use it in GitHub Desktop.
Save paluchasz/0f25468360581e16e608330177f10f3b to your computer and use it in GitHub Desktop.
Faking a Redis Connection
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):
# Function to be tested
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):
# Test function
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()
test_sum_keys(tests[0][0], tests[0][1])
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment