-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Setup Coverage Report & Test config.py (#31)
This is based on #29 Adds tests to reach above 80% coverage and workflow that fails if its below.
- Loading branch information
Showing
6 changed files
with
113 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,3 +8,4 @@ | |
__pycache__/ | ||
.pytest_cache/ | ||
.mypy_cache | ||
.coverage |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,3 +7,4 @@ pandas-stubs | |
sqlalchemy-stubs | ||
pre-commit | ||
pytest | ||
pytest-cov |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
import os | ||
import unittest | ||
from unittest.mock import patch, mock_open | ||
from src.config import Env, RuntimeConfig | ||
|
||
|
||
class TestEnv(unittest.TestCase): | ||
@patch( | ||
"src.config.load_dotenv" | ||
) # Mock load_dotenv to prevent loading the actual .env file | ||
@patch.dict( | ||
os.environ, {"DUNE_API_KEY": "test_key", "DB_URL": "postgres://localhost/test"} | ||
) | ||
def test_load_env_success(self, mock_load_dotenv): | ||
env = Env.load() | ||
self.assertEqual(env.dune_api_key, "test_key") | ||
self.assertEqual(env.db_url, "postgres://localhost/test") | ||
|
||
@patch( | ||
"src.config.load_dotenv" | ||
) # Mock load_dotenv to prevent loading the actual .env file | ||
@patch.dict(os.environ, {}, clear=True) | ||
def test_load_env_missing_dune_api_key(self, mock_load_dotenv): | ||
with self.assertRaises(RuntimeError) as context: | ||
Env.load() | ||
self.assertEqual( | ||
str(context.exception), "DUNE_API_KEY environment variable must be set!" | ||
) | ||
|
||
@patch( | ||
"src.config.load_dotenv" | ||
) # Mock load_dotenv to prevent loading the actual .env file | ||
@patch.dict(os.environ, {"DUNE_API_KEY": "test_key"}, clear=True) | ||
def test_load_env_missing_db_url(self, mock_load_dotenv): | ||
with self.assertRaises(RuntimeError) as context: | ||
Env.load() | ||
self.assertEqual( | ||
str(context.exception), "DB_URL environment variable must be set!" | ||
) | ||
|
||
|
||
class TestRuntimeConfig(unittest.TestCase): | ||
@patch( | ||
"builtins.open", | ||
new_callable=mock_open, | ||
read_data=b""" | ||
[[jobs]] | ||
query_id = 123 | ||
table_name = "test_table" | ||
poll_frequency = 5 | ||
query_engine = "medium" | ||
""", | ||
) | ||
def test_load_from_toml_success(self, mock_file): | ||
config = RuntimeConfig.load_from_toml("config.toml") | ||
self.assertEqual(len(config.jobs), 1) | ||
job = config.jobs[0] | ||
self.assertEqual(job.query_id, 123) | ||
self.assertEqual(job.table_name, "test_table") | ||
self.assertEqual(job.poll_frequency, 5) | ||
self.assertEqual(job.query_engine, "medium") | ||
|
||
@patch( | ||
"builtins.open", | ||
new_callable=mock_open, | ||
read_data=b""" | ||
[[jobs]] | ||
query_id = 123 | ||
table_name = "test_table" | ||
poll_frequency = 5 | ||
query_engine = "invalid" | ||
""", | ||
) | ||
def test_load_from_toml_invalid_query_engine(self, mock_file): | ||
with self.assertRaises(ValueError) as context: | ||
RuntimeConfig.load_from_toml("config.toml") | ||
self.assertEqual( | ||
str(context.exception), "query_engine must be either 'medium' or 'large'." | ||
) | ||
|
||
@patch( | ||
"builtins.open", | ||
new_callable=mock_open, | ||
read_data=b""" | ||
[[jobs]] | ||
query_id = 123 | ||
""", | ||
) | ||
def test_load_from_toml_missing_values(self, mock_file): | ||
config = RuntimeConfig.load_from_toml("config.toml") | ||
self.assertEqual(len(config.jobs), 1) | ||
job = config.jobs[0] | ||
self.assertEqual(job.query_id, 123) | ||
self.assertEqual(job.table_name, "dune_data_123") # Default table name | ||
self.assertEqual(job.poll_frequency, 1) # Default poll frequency | ||
self.assertEqual(job.query_engine, "medium") # Default query engine |