Skip to content

Unit testing with in memory db  #73

@farridav

Description

@farridav

Hi,

Im trying to get my test suite to run using an in memory sqlite db, as its very slow using one on the file system, but im finding failures, and not sure if i have things setup properly, or if there is an issue with the persistence of the connection, heres an excerpt from my setup:

# conftest.py

from typing import Any, AsyncGenerator
import pytest
from pydbantic import Database

from myapp.models import MyModel


@pytest.fixture
async def setup_db() -> AsyncGenerator[Database, Any]:
    db_url = "sqlite:///:memory:"
    db = await Database.create(
        db_url,
        tables=[MyModel],
        cache_enabled=False,
        testing=True,
    )
    yield db
    db.metadata.drop_all(db.engine)



async def test_thing(setup_db: Database) -> None:
    shop = await MyModel.create(name="test")

    assert shop is not None

And i seem to get ERROR tests/test_thing.py::test_thing - sqlite3.OperationalError: no such table: DatabaseInit

Which looks like the database session is not being reused, ive tried various things like sqlite:///:memory:?cache=shared to no avail.

Do you see anything immediately wrong in my approach ?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions