-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathkey_value_mock.py
62 lines (45 loc) · 1.64 KB
/
key_value_mock.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
60
61
62
"""TcEx Framework Module"""
# standard library
from copy import deepcopy
from threading import Lock
from .key_value_abc import KeyValueABC
class KeyValueMock(KeyValueABC):
"""TcEx Key Value Mock Module.
Purely in-memory implementation of the KeyValueABC for local testing only.
"""
data = {}
def __init__(self):
"""Initialize the Class properties."""
self.lock = Lock()
# properties
self.kv_type = 'mock'
def create(self, context: str, key: str, value: bytes | str) -> int:
"""Create key/value pair.
Args:
context: A specific context for the create.
key (str): The field name (key) for the kv pair in Redis.
value (any): The value for the kv pair in Redis.
Returns:
str: The response from Redis.
"""
with self.lock:
self.data.setdefault(context, {})[key] = value
return 1
def read(self, context: str, key: str) -> bytes | str | None:
"""Read data for the provided key.
Args:
context: A specific context for the create.
key: The field name (key) for the kv pair in Redis.
Returns:
str: The response data from Redis.
"""
with self.lock:
return self.data.get(context, {}).get(key)
def get_all(self, context: str | None) -> dict[str, bytes | str | None]:
"""Return the contents for a given context.
Args:
context: the context to return
"""
if context is not None:
return deepcopy(self.data.get(context, {}))
return deepcopy(self.data)