|
| 1 | +"""Unit tests for Domain implementation.""" |
| 2 | + |
| 3 | +from unittest.mock import AsyncMock, Mock |
| 4 | + |
| 5 | +import pytest |
| 6 | +from sqlalchemy import MetaData |
| 7 | +from sqlalchemy.ext.asyncio import AsyncEngine |
| 8 | + |
| 9 | +from datajam import IConfigureDomainMappings |
| 10 | +from datajam_sqlalchemy import DataContext, Domain, Repository |
| 11 | + |
| 12 | + |
| 13 | +class MockMappingConfigurator(IConfigureDomainMappings[MetaData]): |
| 14 | + """Mock mapping configurator for testing.""" |
| 15 | + |
| 16 | + def __init__(self): |
| 17 | + self.configured = False |
| 18 | + |
| 19 | + def configure(self, metadata: MetaData) -> None: |
| 20 | + self.configured = True |
| 21 | + |
| 22 | + |
| 23 | +class TestDomain: |
| 24 | + """Test Domain implementation.""" |
| 25 | + |
| 26 | + @pytest.fixture |
| 27 | + def mock_engine(self): |
| 28 | + """Create a mock async engine.""" |
| 29 | + engine = Mock(spec=AsyncEngine) |
| 30 | + |
| 31 | + # Create a mock connection |
| 32 | + mock_conn = Mock() |
| 33 | + mock_conn.run_sync = AsyncMock() |
| 34 | + |
| 35 | + # Create an async context manager that returns the connection |
| 36 | + async_context = AsyncMock() |
| 37 | + async_context.__aenter__ = AsyncMock(return_value=mock_conn) |
| 38 | + async_context.__aexit__ = AsyncMock(return_value=None) |
| 39 | + |
| 40 | + # Make engine.begin() return the async context manager |
| 41 | + engine.begin.return_value = async_context |
| 42 | + |
| 43 | + # Store the connection for test assertions |
| 44 | + engine._test_connection = mock_conn |
| 45 | + |
| 46 | + return engine |
| 47 | + |
| 48 | + @pytest.fixture |
| 49 | + def metadata(self): |
| 50 | + """Create a metadata instance.""" |
| 51 | + return MetaData() |
| 52 | + |
| 53 | + @pytest.fixture |
| 54 | + def mapping_configurator(self): |
| 55 | + """Create a mock mapping configurator.""" |
| 56 | + return MockMappingConfigurator() |
| 57 | + |
| 58 | + @pytest.fixture |
| 59 | + def domain(self, mock_engine, metadata, mapping_configurator): |
| 60 | + """Create a Domain instance.""" |
| 61 | + return Domain( |
| 62 | + connection_string="test://connection", |
| 63 | + engine=mock_engine, |
| 64 | + mapping_configurator=mapping_configurator, |
| 65 | + metadata=metadata, |
| 66 | + ) |
| 67 | + |
| 68 | + def test_configuration_options_returns_connection_string(self, domain): |
| 69 | + """Test that configuration_options returns the connection string.""" |
| 70 | + assert domain.configuration_options == "test://connection" |
| 71 | + |
| 72 | + def test_mapping_configurator_is_called_during_init(self, domain, mapping_configurator): |
| 73 | + """Test that mapping configurator is called during initialization.""" |
| 74 | + assert mapping_configurator.configured |
| 75 | + |
| 76 | + def test_metadata_property_returns_metadata(self, domain, metadata): |
| 77 | + """Test that metadata property returns the metadata.""" |
| 78 | + assert domain.metadata == metadata |
| 79 | + |
| 80 | + def test_engine_property_returns_engine(self, domain, mock_engine): |
| 81 | + """Test that engine property returns the engine.""" |
| 82 | + assert domain.engine == mock_engine |
| 83 | + |
| 84 | + async def test_create_data_context_returns_data_context(self, domain): |
| 85 | + """Test that create_data_context returns a DataContext instance.""" |
| 86 | + data_context = await domain.create_data_context() |
| 87 | + assert isinstance(data_context, DataContext) |
| 88 | + |
| 89 | + async def test_create_repository_returns_repository(self, domain): |
| 90 | + """Test that create_repository returns a Repository instance.""" |
| 91 | + repository = await domain.create_repository() |
| 92 | + assert isinstance(repository, Repository) |
| 93 | + |
| 94 | + async def test_create_tables_calls_metadata_create_all(self, domain, mock_engine): |
| 95 | + """Test that create_tables calls metadata.create_all.""" |
| 96 | + await domain.create_tables() |
| 97 | + |
| 98 | + # Verify that run_sync was called on the connection |
| 99 | + mock_engine._test_connection.run_sync.assert_called_once() |
| 100 | + |
| 101 | + async def test_drop_tables_calls_metadata_drop_all(self, domain, mock_engine): |
| 102 | + """Test that drop_tables calls metadata.drop_all.""" |
| 103 | + await domain.drop_tables() |
| 104 | + |
| 105 | + # Verify that run_sync was called on the connection |
| 106 | + mock_engine._test_connection.run_sync.assert_called_once() |
| 107 | + |
| 108 | + def test_domain_without_mapping_configurator(self, mock_engine, metadata): |
| 109 | + """Test that Domain can be created without mapping configurator.""" |
| 110 | + domain = Domain(connection_string="test://connection", engine=mock_engine, metadata=metadata) |
| 111 | + assert domain.mapping_configurator is None |
| 112 | + |
| 113 | + def test_domain_without_metadata_creates_default(self, mock_engine): |
| 114 | + """Test that Domain creates default metadata if none provided.""" |
| 115 | + domain = Domain(connection_string="test://connection", engine=mock_engine) |
| 116 | + assert isinstance(domain.metadata, MetaData) |
0 commit comments