2023-03-13 19:00:39 +00:00
|
|
|
import pytest
|
|
|
|
|
|
|
|
from pydantic import BaseModel
|
|
|
|
from datetime import datetime
|
|
|
|
from typing import Optional
|
|
|
|
|
|
|
|
from selfprivacy_api.utils.redis_model_storage import store_model_as_hash, hash_as_model
|
|
|
|
from selfprivacy_api.utils.redis_pool import RedisPool
|
|
|
|
|
|
|
|
TEST_KEY = "model_storage"
|
|
|
|
redis = RedisPool().get_connection()
|
|
|
|
|
2023-06-21 03:46:56 +00:00
|
|
|
|
2023-03-13 19:00:39 +00:00
|
|
|
@pytest.fixture()
|
|
|
|
def clean_redis():
|
|
|
|
redis.delete(TEST_KEY)
|
|
|
|
|
|
|
|
|
|
|
|
class DummyModel(BaseModel):
|
|
|
|
name: str
|
|
|
|
date: Optional[datetime]
|
|
|
|
|
2023-06-21 03:46:56 +00:00
|
|
|
|
2023-03-13 19:00:39 +00:00
|
|
|
def test_store_retrieve():
|
2023-06-21 03:46:56 +00:00
|
|
|
model = DummyModel(name="test", date=datetime.now())
|
2023-03-13 19:00:39 +00:00
|
|
|
store_model_as_hash(redis, TEST_KEY, model)
|
2023-06-21 03:46:56 +00:00
|
|
|
assert hash_as_model(redis, TEST_KEY, DummyModel) == model
|
|
|
|
|
2023-03-13 19:00:39 +00:00
|
|
|
|
|
|
|
def test_store_retrieve_none():
|
2023-06-21 03:46:56 +00:00
|
|
|
model = DummyModel(name="test", date=None)
|
2023-03-13 19:00:39 +00:00
|
|
|
store_model_as_hash(redis, TEST_KEY, model)
|
2023-06-21 03:46:56 +00:00
|
|
|
assert hash_as_model(redis, TEST_KEY, DummyModel) == model
|