Add config and client injection with test reorganization.
Introduce typed config objects, optional adapter injection, and .env loading to simplify testing while preserving env-based defaults for production usage. Co-authored-by: Cursor <[email protected]>
This commit is contained in:
co-authored by
Cursor
parent
366831ac52
commit
7991dabdbc
@@ -0,0 +1,71 @@
|
||||
"""Unit tests for RedisAdapter instantiation and injection."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from unittest.mock import MagicMock
|
||||
|
||||
import pytest
|
||||
import redis
|
||||
|
||||
from python_repositories.adapters.redis_adapter import RedisAdapter
|
||||
from python_repositories.interfaces import JsonRepositoryInterface
|
||||
from tests.conftest import TEST_REDIS_CONFIG
|
||||
|
||||
|
||||
def test_should_adhere_to_interface() -> None:
|
||||
assert issubclass(RedisAdapter, JsonRepositoryInterface)
|
||||
_ = RedisAdapter(config=TEST_REDIS_CONFIG)
|
||||
|
||||
|
||||
def test_should_have_logger_when_instantiated() -> None:
|
||||
adapter = RedisAdapter(config=TEST_REDIS_CONFIG)
|
||||
assert hasattr(adapter, "logger")
|
||||
assert adapter.logger is not None
|
||||
|
||||
|
||||
def test_should_not_be_connected_when_instantiated() -> None:
|
||||
adapter = RedisAdapter(config=TEST_REDIS_CONFIG)
|
||||
assert adapter._client is None
|
||||
assert not adapter.is_connected()
|
||||
|
||||
|
||||
def test_constructs_with_injected_config_without_env(
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
) -> None:
|
||||
monkeypatch.delenv("REDIS_URI", raising=False)
|
||||
adapter = RedisAdapter(config=TEST_REDIS_CONFIG)
|
||||
assert adapter._config == TEST_REDIS_CONFIG
|
||||
|
||||
|
||||
def test_constructs_with_injected_client_without_env(
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
) -> None:
|
||||
monkeypatch.delenv("REDIS_URI", raising=False)
|
||||
mock_client = MagicMock(spec=redis.Redis)
|
||||
adapter = RedisAdapter(config=TEST_REDIS_CONFIG, client=mock_client)
|
||||
assert adapter._client is mock_client
|
||||
assert adapter._client_injected is True
|
||||
|
||||
|
||||
def test_raises_when_client_provided_without_config() -> None:
|
||||
mock_client = MagicMock(spec=redis.Redis)
|
||||
with pytest.raises(ValueError, match="config is required"):
|
||||
RedisAdapter(client=mock_client)
|
||||
|
||||
|
||||
def test_disconnect_does_not_close_injected_client() -> None:
|
||||
mock_client = MagicMock(spec=redis.Redis)
|
||||
adapter = RedisAdapter(config=TEST_REDIS_CONFIG, client=mock_client)
|
||||
adapter.disconnect()
|
||||
mock_client.close.assert_not_called()
|
||||
assert adapter._client is None
|
||||
|
||||
|
||||
class CustomEnvRedisAdapter(RedisAdapter):
|
||||
uri_env_var_name = "CUSTOM_REDIS_URI"
|
||||
|
||||
|
||||
def test_subclass_custom_env_var_name(monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
monkeypatch.setenv("CUSTOM_REDIS_URI", "redis://custom:6379")
|
||||
adapter = CustomEnvRedisAdapter()
|
||||
assert adapter._config.uri == "redis://custom:6379"
|
||||
Reference in New Issue
Block a user