Skip to content

Commit

Permalink
Another attempt
Browse files Browse the repository at this point in the history
  • Loading branch information
muellerberndt committed Dec 7, 2024
1 parent 79c8c0b commit 17cf000
Showing 1 changed file with 57 additions and 11 deletions.
68 changes: 57 additions & 11 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,32 +1,66 @@
# isort: skip_file
# flake8: noqa: E402
import os
from unittest.mock import patch, Mock
import pytest
from unittest.mock import patch, Mock
import yaml
from src.config.config import Config, DEFAULT_CONFIG
from src.config.config import Config


TEST_CONFIG = {
"data_dir": "./test_data",
"database": {"host": "localhost", "port": 5432, "name": "test_db", "user": "test", "password": "test"},
"block_explorers": {
"etherscan": {"key": None},
"basescan": {"key": None},
"arbiscan": {"key": None},
"polygonscan": {"key": None},
"bscscan": {"key": None},
},
"llm": {"openai": {"key": "test-key", "model": "gpt-4"}},
"telegram": {"bot_token": "test-token", "chat_id": "test-chat"},
"watchers": {"active_watchers": []},
"extensions_dir": "./test_extensions",
"active_extensions": [],
}


@pytest.fixture(autouse=True)
def setup_test_config(tmp_path):
"""Set up test configuration."""
def setup_test_env(tmp_path):
"""Set up test environment with config file."""
# Create test config file
config_file = tmp_path / "config.yml"
with open(config_file, "w") as f:
yaml.dump(DEFAULT_CONFIG, f)
yaml.dump(TEST_CONFIG, f)

# Store original environment
original_env = {}
for key in list(os.environ.keys()):
if key.startswith("R4DAR_") or key == "DATABASE_URL":
original_env[key] = os.environ[key]
del os.environ[key]

# Set test environment
os.environ["R4DAR_CONFIG"] = str(config_file)
os.environ["DATABASE_URL"] = "postgresql://test:test@localhost:5432/test_db"

# Set test mode
Config.set_test_mode(True)

yield

# Restore original environment
for key in list(os.environ.keys()):
if key.startswith("R4DAR_") or key == "DATABASE_URL":
del os.environ[key]
for key, value in original_env.items():
os.environ[key] = value

# Reset test mode
Config.set_test_mode(False)
if "R4DAR_CONFIG" in os.environ:
del os.environ["R4DAR_CONFIG"]


@pytest.fixture(autouse=True)
def mock_database():
"""Mock database initialization for all tests."""
"""Mock database for all tests."""
mock_db = Mock()
mock_db._engine = Mock()
mock_db._async_engine = Mock()
Expand All @@ -52,5 +86,17 @@ def mock_database():
mock_db.async_session.return_value.__aenter__ = Mock(return_value=mock_async_session)
mock_db.async_session.return_value.__aexit__ = Mock(return_value=None)

with patch("src.backend.database.Database.__init__", return_value=None), patch("src.backend.database.db", mock_db):
# Create a patched Database class
def mock_init(self):
self._engine = mock_db._engine
self._async_engine = mock_db._async_engine
self._SessionLocal = mock_db._SessionLocal
self._AsyncSessionLocal = mock_db._AsyncSessionLocal
self.session = mock_db.session
self.async_session = mock_db.async_session
self.get_engine = mock_db.get_engine
self.get_async_engine = mock_db.get_async_engine
self.is_initialized = mock_db.is_initialized

with patch("src.backend.database.Database.__init__", mock_init), patch("src.backend.database.db", mock_db):
yield mock_db

0 comments on commit 17cf000

Please sign in to comment.