-
Hi, I have question how to start run databases when doing testing ? # conftest.py
SQLALCHEMY_DATABASE_URI = "sqlite:///./test.db"
engine = create_engine(
SQLALCHEMY_DATABASE_URI, connect_args={"check_same_thread": False}
)
TestingSessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
class YOLOConnection(aiosqlite.Connection):
def _get_reset_query(self):
return None
database = databases.Database(
SQLALCHEMY_DATABASE_URI, min_size=3, max_size=10, connection_class=YOLOConnection
)
Base.metadata.create_all(bind=engine)
@pytest.fixture(scope="function")
async def async_client() -> Generator:
async with AsyncClient(
app=app, base_url=f"{settings.SERVER_HOST}{settings.API_V1_STR}"
) as ac:
yield ac # test_device.py
@pytest.mark.anyio
async def test_get_multi(async_client: AsyncClient) -> None:
devices = await crud_device.device.get_multi()
assert devices # models/device.py
class CRUDDevice(CRUDBase[Device, DeviceCreate, None]):
...
...
...
async def get_multi(self, *, skip: int = 0, limit: int = 100) -> List[Device]:
query = select(self.model).offset(skip).limit(limit)
return await database.fetch_all(query=query)
device = CRUDDevice(Device) When I run the test, error shown : async def acquire(self) -> None:
assert self._connection is None, "Connection is already acquired"
> assert self._database._pool is not None, "DatabaseBackend is not running"
E AssertionError: DatabaseBackend is not running Thanks in advance! |
Beta Was this translation helpful? Give feedback.
Answered by
aminalaee
Oct 27, 2021
Replies: 1 comment 11 replies
-
I haven't run that locally (yet), but I guess you're missing a |
Beta Was this translation helpful? Give feedback.
11 replies
Answer selected by
dvrg
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I haven't run that locally (yet), but I guess you're missing a
await database.connect()
? You create aDatabase
instance but it's not awaited.