Skip to content

Migration Testing

Here's the reference information for the migration testing classes, with all their parameters, attributes and methods.

You can import these classes directly from pyrmute:

from pyrmute import (
    MigrationTestCase,
    MigrationTestResult,
    MigrationTestResults,
)

pyrmute.MigrationTestCase dataclass

MigrationTestCase(source, target=None, description='')

Test case for migration validation.

Defines input data and expected output for testing a migration function. If target is None, the test only verifies the migration doesn't crash.

ATTRIBUTE DESCRIPTION
source

Input data to migrate.

TYPE: ModelData

target

Expected output after migration. If None, only validates that migration completes without errors.

TYPE: ModelData | None

description

Optional description of what this test case validates.

TYPE: str

Example
test_case = MigrationTestCase(
    source={"name": "Alice"},
    target={"name": "Alice", "email": "alice@example.com"},
    description="Adds default email field"
)

source instance-attribute

source

target class-attribute instance-attribute

target = None

description class-attribute instance-attribute

description = ''

pyrmute.MigrationTestResult dataclass

MigrationTestResult(test_case, actual, passed, error=None)

Result of a single migration test case.

Contains the test case, actual output, pass/fail status, and any error message.

ATTRIBUTE DESCRIPTION
test_case

Original test case that was executed.

TYPE: MigrationTestCase

actual

Actual output produced by the migration.

TYPE: ModelData

passed

Whether the test passed (output matched expected or no errors).

TYPE: bool

error

Error message if test failed, None if passed.

TYPE: str | None

Example
result = MigrationTestResult(
    test_case=test_case,
    actual={"name": "Alice", "email": "alice@example.com"},
    passed=True,
    error=None
)

test_case instance-attribute

test_case

actual instance-attribute

actual

passed instance-attribute

passed

error class-attribute instance-attribute

error = None

pyrmute.MigrationTestResults

MigrationTestResults(results)

Collection of migration test results.

Provides convenient methods for checking overall test status and accessing failed tests.

ATTRIBUTE DESCRIPTION
results

List of individual test results.

Example
results = MigrationTestResults([result1, result2, result3])
if results.all_passed:
    print("All tests passed!")
else:
    print(f"{len(results.failures)} test(s) failed")
    for failure in results.failures:
        print(failure)

Initialize test results collection.

PARAMETER DESCRIPTION
results

List of individual test results.

TYPE: list[MigrationTestResult]

Source code in src/pyrmute/migration_testing.py
def __init__(self: Self, results: list[MigrationTestResult]) -> None:
    """Initialize test results collection.

    Args:
        results: List of individual test results.
    """
    self.results = results

results instance-attribute

results = results

all_passed property

all_passed

Check if all tests passed.

RETURNS DESCRIPTION
bool

True if all tests passed, False if any failed.

failures property

failures

Get list of failed tests.

RETURNS DESCRIPTION
list[MigrationTestResult]

List of test results that failed.

assert_all_passed

assert_all_passed()

Assert all tests passed, raising detailed error if any failed.

RAISES DESCRIPTION
AssertionError

If any tests failed, with details about failures.

Example
# Use in pytest
def test_user_migration():
    res = manager.test_migration("User", "1.0.0", "2.0.0", test_cases)
    res.assert_all_passed()
Source code in src/pyrmute/migration_testing.py
def assert_all_passed(self: Self) -> None:
    """Assert all tests passed, raising detailed error if any failed.

    Raises:
        AssertionError: If any tests failed, with details about failures.

    Example:
        ```python
        # Use in pytest
        def test_user_migration():
            res = manager.test_migration("User", "1.0.0", "2.0.0", test_cases)
            res.assert_all_passed()
        ```
    """
    if not self.all_passed:
        messages = [str(f) for f in self.failures]
        raise AssertionError(
            f"\n{len(self.failures)} migration test(s) failed:\n"
            + "\n\n".join(messages)
        )