Skip to content

Testing

AMSDAL provides testing utilities to simplify setting up test environments, overriding settings, and creating test data.

override_settings

A context manager that temporarily overrides AMSDAL settings for a test scope.

from amsdal.utils.tests.helpers import override_settings

def test_custom_config(tmp_path):
    with override_settings(APP_PATH=tmp_path, MEDIA_ROOT=tmp_path / 'media'):
        # Settings are overridden here
        from amsdal.configs.main import settings
        assert settings.APP_PATH == tmp_path
    # Settings are automatically restored

Use it in pytest fixtures:

import pytest
from amsdal.utils.tests.helpers import override_settings

@pytest.fixture
def custom_settings(tmp_path):
    with override_settings(APP_PATH=tmp_path):
        yield

init_manager_and_migrate

A context manager that sets up a fully configured AmsdalManager with database and migrations applied. This is the main entry point for integration tests.

from pathlib import Path
from amsdal.utils.tests.helpers import init_manager_and_migrate
from amsdal.utils.tests.enums import DbExecutionType, LakehouseOption, StateOption

def test_model_operations():
    with init_manager_and_migrate(
        src_dir_path=Path('src/my_app'),
        db_execution_type=DbExecutionType.lakehouse_only,
        lakehouse_option=LakehouseOption.sqlite,
        state_option=StateOption.sqlite,
    ) as manager:
        # Manager is ready with migrations applied
        # Run your test logic here
        pass

Parameters:

Parameter Type Description
src_dir_path Path Source directory of the application
db_execution_type DbExecutionType lakehouse_only or include_state_db
lakehouse_option LakehouseOption sqlite or postgres
state_option StateOption sqlite or postgres
app_models_path Path \| None Path to models (default: src_dir_path/models)
app_transactions_path Path \| None Path to transactions (default: src_dir_path/transactions)
app_fixtures_path Path \| None Path to fixtures (default: src_dir_path/fixtures)
**settings_options Additional settings to override

Async variant

from amsdal.utils.tests.helpers import async_init_manager_and_migrate

async def test_async_operations():
    async with async_init_manager_and_migrate(
        src_dir_path=Path('src/my_app'),
        db_execution_type=DbExecutionType.lakehouse_only,
        lakehouse_option=LakehouseOption.sqlite,
        state_option=StateOption.sqlite,
    ) as manager:
        # AsyncAmsdalManager is ready
        pass

Database Options

from amsdal.utils.tests.enums import DbExecutionType, LakehouseOption, StateOption
Enum Values Description
DbExecutionType lakehouse_only, include_state_db Whether to include a state database
LakehouseOption sqlite, postgres Backend for the lakehouse database
StateOption sqlite, postgres Backend for the state database

AmsdalFactory

A test factory for generating AMSDAL model instances using the polyfactory library.

pip install amsdal[factory]
from amsdal.utils.tests.factories import AmsdalFactory
from my_app.models import Post

class PostFactory(AmsdalFactory[Post]):
    __model__ = Post

# Create a single instance
post = PostFactory.build()

# Create multiple instances
posts = PostFactory.batch(size=10)

The factory handles AMSDAL-specific field types (references, metadata) automatically.


Helper Functions

init_manager

Set up a manager without running migrations:

from amsdal.utils.tests.helpers import init_manager

with init_manager(
    src_dir_path=Path('src/my_app'),
    db_execution_type=DbExecutionType.lakehouse_only,
    lakehouse_option=LakehouseOption.sqlite,
    state_option=StateOption.sqlite,
) as manager:
    pass

init_config

Set up just the database configuration:

from amsdal.utils.tests.helpers import init_config

with init_config(
    db_execution_type=DbExecutionType.lakehouse_only,
    lakehouse_option=LakehouseOption.sqlite,
    state_option=StateOption.sqlite,
) as config:
    # AmsdalConfig is ready
    pass

migrate / async_migrate

Run migrations independently:

from amsdal.utils.tests.migrations import migrate, async_migrate

# Sync
migrate()

# Async
await async_migrate()

Pytest Fixture Example

A complete pytest fixture setup for AMSDAL integration tests:

import pytest
from pathlib import Path
from amsdal.utils.tests.helpers import init_manager_and_migrate
from amsdal.utils.tests.enums import DbExecutionType, LakehouseOption, StateOption

SRC_DIR = Path(__file__).parent / 'data'

@pytest.fixture
def amsdal_manager():
    with init_manager_and_migrate(
        src_dir_path=SRC_DIR,
        db_execution_type=DbExecutionType.lakehouse_only,
        lakehouse_option=LakehouseOption.sqlite,
        state_option=StateOption.sqlite,
    ) as manager:
        yield manager

def test_create_and_query(amsdal_manager):
    from my_app.models import Person

    person = Person(first_name='John', last_name='Doe')
    person.save()

    result = Person.objects.filter(first_name='John').execute()
    assert len(result) == 1
    assert result[0].last_name == 'Doe'