-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Cleanup storage integration and write pending batches on shutdown (#76)
* Promote close() method to the _Storage class definition * Switch to asynccontextmanager method for handling startup/shutdown events in FastAPI Add storage.close() to the shutdown phase of the lifespan method Use fully-qualified names for create_storage() and create_translator() methods so they can be mocked for unit tests * Add unit test for startup/shutdown handling method
- Loading branch information
Showing
4 changed files
with
53 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
from fastapi.testclient import TestClient | ||
from fastapi import FastAPI | ||
from anyvar.restapi.main import app_lifespan | ||
from anyvar.storage import _Storage | ||
|
||
|
||
def test_lifespan(mocker): | ||
"""Test the app_lifespan method in anyvar.restapi.main""" | ||
create_storage_mock = mocker.patch("anyvar.anyvar.create_storage") | ||
storage_mock = mocker.Mock(spec=_Storage) | ||
create_storage_mock.return_value = storage_mock | ||
create_translator_mock = mocker.patch("anyvar.anyvar.create_translator") | ||
create_translator_mock.return_value = {} | ||
app = FastAPI( | ||
title="AnyVarTest", | ||
docs_url="/", | ||
openapi_url="/openapi.json", | ||
description="Test app", | ||
lifespan=app_lifespan, | ||
) | ||
with TestClient(app) as client: | ||
create_storage_mock.assert_called_once() | ||
create_translator_mock.assert_called_once() | ||
assert app.state.anyvar is not None | ||
|
||
storage_mock.close.assert_called_once() |