fix: Enhance argument handling in seed data script and add unit tests
This commit is contained in:
46
tests/unit/test_seed_data.py
Normal file
46
tests/unit/test_seed_data.py
Normal file
@@ -0,0 +1,46 @@
|
||||
import argparse
|
||||
from unittest import mock
|
||||
|
||||
import scripts.seed_data as seed_data
|
||||
from scripts.seed_data import DatabaseConfig
|
||||
|
||||
|
||||
def test_run_with_namespace_handles_missing_theme_flag_without_actions() -> None:
|
||||
args = argparse.Namespace(currencies=False, units=False, defaults=False)
|
||||
config = mock.create_autospec(DatabaseConfig)
|
||||
config.application_dsn.return_value = "postgresql://example"
|
||||
|
||||
with (
|
||||
mock.patch("scripts.seed_data._configure_logging") as configure_logging,
|
||||
mock.patch("scripts.seed_data.psycopg2.connect") as connect_mock,
|
||||
mock.patch.object(seed_data.logger, "info") as info_mock,
|
||||
):
|
||||
seed_data.run_with_namespace(args, config=config)
|
||||
|
||||
configure_logging.assert_called_once()
|
||||
connect_mock.assert_not_called()
|
||||
info_mock.assert_called_with("No seeding options provided; exiting")
|
||||
|
||||
|
||||
def test_run_with_namespace_seeds_defaults_without_theme_flag() -> None:
|
||||
args = argparse.Namespace(
|
||||
currencies=False, units=False, defaults=True, dry_run=False)
|
||||
config = mock.create_autospec(DatabaseConfig)
|
||||
config.application_dsn.return_value = "postgresql://example"
|
||||
|
||||
connection_mock = mock.MagicMock()
|
||||
cursor_context = mock.MagicMock()
|
||||
cursor_mock = mock.MagicMock()
|
||||
connection_mock.__enter__.return_value = connection_mock
|
||||
connection_mock.cursor.return_value = cursor_context
|
||||
cursor_context.__enter__.return_value = cursor_mock
|
||||
|
||||
with (
|
||||
mock.patch("scripts.seed_data._configure_logging"),
|
||||
mock.patch("scripts.seed_data.psycopg2.connect", return_value=connection_mock) as connect_mock,
|
||||
mock.patch("scripts.seed_data._seed_defaults") as seed_defaults,
|
||||
):
|
||||
seed_data.run_with_namespace(args, config=config)
|
||||
|
||||
connect_mock.assert_called_once_with(config.application_dsn())
|
||||
seed_defaults.assert_called_once_with(cursor_mock, dry_run=False)
|
||||
Reference in New Issue
Block a user