Skip to content

Testing

cattle_grid.testing

mocked_config

mocked_config(config: Dynaconf | dict)

overrides the configuration stored in global_container.config with the value in dict, afterwards resets the original value.

Source code in cattle_grid/testing/__init__.py
@contextmanager
def mocked_config(config: Dynaconf | dict):
    """overrides the configuration stored in `global_container.config`
    with the value in dict, afterwards resets the original value."""
    if isinstance(config, dict):
        config = DynaconfDict(config)
    old_config = global_container.config

    global_container._config = config

    yield

    global_container._config = old_config

cattle_grid.testing.fixtures

account_for_test async

account_for_test()

Fixture to create an account

Source code in cattle_grid/testing/fixtures.py
@pytest.fixture
async def account_for_test():
    """Fixture to create an account"""
    return await create_account("alice", "alice", permissions=["admin"])

actor_for_test async

actor_for_test()

Fixture to create an actor

Source code in cattle_grid/testing/fixtures.py
@pytest.fixture
async def actor_for_test():
    """Fixture to create an actor"""
    return await create_actor("http://localhost/ap")

actor_with_account async

actor_with_account(account_for_test)

Fixture to create an actor with an account

Source code in cattle_grid/testing/fixtures.py
@pytest.fixture
async def actor_with_account(account_for_test):
    """Fixture to create an actor with an account"""
    actor = await create_actor("http://localhost/ap")
    await add_actor_to_account(account_for_test, actor, name="test_fixture")

    return actor

database_for_tests async

database_for_tests()

Fixture so that the database is initialized

Source code in cattle_grid/testing/fixtures.py
@pytest.fixture(autouse=True)
async def database_for_tests():
    """Fixture so that the database is initialized"""
    async with with_database(db_uri="sqlite://:memory:", generate_schemas=True):
        yield

loaded_config

loaded_config()

Ensures the configuration variables are loaded

Source code in cattle_grid/testing/fixtures.py
@pytest.fixture(autouse=True, scope="session")
def loaded_config():
    """Ensures the configuration variables are loaded"""
    global_container.load_config()

sql_engine_for_tests async

sql_engine_for_tests()

Provides the sql engine (as in memory sqlite) for tests

Source code in cattle_grid/testing/fixtures.py
@pytest.fixture()
async def sql_engine_for_tests():
    """Provides the sql engine (as in memory sqlite) for tests"""
    async with global_container.alchemy_database(
        "sqlite+aiosqlite:///:memory:", echo=False
    ) as engine:  # type: ignore
        yield engine