2019-10-31 15:34:16 +00:00
|
|
|
import storage.common
|
2023-06-28 10:58:54 +00:00
|
|
|
from mock import patch
|
|
|
|
|
2023-06-28 10:46:29 +00:00
|
|
|
|
2019-10-02 14:07:43 +00:00
|
|
|
class MockStorage:
|
|
|
|
PATCH_METHODS = ("get", "set", "delete")
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
self.namespace = {}
|
|
|
|
self.patches = [
|
2019-10-31 15:34:16 +00:00
|
|
|
patch(storage.common, method, getattr(self, method))
|
2019-10-02 14:07:43 +00:00
|
|
|
for method in self.PATCH_METHODS
|
|
|
|
]
|
|
|
|
|
|
|
|
def set(self, app: int, key: int, data: bytes, public: bool = False) -> None:
|
|
|
|
self.namespace.setdefault(app, {})
|
|
|
|
self.namespace[app][key] = data
|
|
|
|
|
2021-12-08 09:10:58 +00:00
|
|
|
def get(self, app: int, key: int, public: bool = False) -> bytes | None:
|
2019-10-02 14:07:43 +00:00
|
|
|
self.namespace.setdefault(app, {})
|
|
|
|
return self.namespace[app].get(key)
|
|
|
|
|
|
|
|
def delete(self, app: int, key: int, public: bool = False) -> None:
|
|
|
|
self.namespace.setdefault(app, {})
|
|
|
|
self.namespace[app].pop(key, None)
|
|
|
|
|
|
|
|
def __enter__(self):
|
2023-06-28 10:48:01 +00:00
|
|
|
for self_patch in self.patches:
|
|
|
|
self_patch.__enter__()
|
2019-10-02 14:07:43 +00:00
|
|
|
return self
|
|
|
|
|
|
|
|
def __exit__(self, exc_type, exc_value, tb):
|
2023-06-28 10:48:01 +00:00
|
|
|
for self_patch in self.patches:
|
|
|
|
self_patch.__exit__(exc_type, exc_value, tb)
|
2019-10-02 14:07:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
def mock_storage(func):
|
|
|
|
def inner(*args, **kwargs):
|
|
|
|
with MockStorage():
|
|
|
|
return func(*args, **kwargs)
|
|
|
|
|
|
|
|
return inner
|