-
Notifications
You must be signed in to change notification settings - Fork 27
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
use FastAPI test client for testing #322
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,40 @@ | ||
import json | ||
import os | ||
|
||
import httpx | ||
import httpx_sse | ||
import pytest | ||
from fastapi.testclient import TestClient | ||
|
||
from ragna._utils import timeout_after | ||
from ragna.core._utils import default_user | ||
from ragna.deploy import Config | ||
from tests.utils import ragna_api | ||
from ragna.deploy._api import app | ||
|
||
|
||
@pytest.mark.parametrize("database", ["memory", "sqlite"]) | ||
@pytest.mark.parametrize("stream_answer", [True, False]) | ||
def test_e2e(tmp_local_root, database, stream_answer): | ||
if database == "memory": | ||
database_url = "memory" | ||
elif database == "sqlite": | ||
database_url = f"sqlite:///{tmp_local_root / 'ragna.db'}" | ||
|
||
def test_e2e(tmp_local_root, stream_answer): | ||
config = Config( | ||
local_cache_root=tmp_local_root, api=dict(database_url=database_url) | ||
local_cache_root=tmp_local_root, | ||
api=dict(database_url=f"sqlite:///{tmp_local_root / 'ragna.db'}"), | ||
) | ||
check_api(config, stream_answer=stream_answer) | ||
|
||
|
||
@timeout_after() | ||
def check_api(config, *, stream_answer): | ||
document_root = config.local_cache_root / "documents" | ||
document_root.mkdir() | ||
document_path = document_root / "test.txt" | ||
with open(document_path, "w") as file: | ||
file.write("!\n") | ||
|
||
with ragna_api(config), httpx.Client(base_url=config.api.url) as client: | ||
with TestClient(app(config)) as client: | ||
username = default_user() | ||
token = ( | ||
client.post( | ||
"/token", | ||
data={ | ||
"username": username, | ||
"password": os.environ.get( | ||
"AI_PROXY_DEMO_AUTHENTICATION_PASSWORD", username | ||
"RAGNA_DEMO_AUTHENTICATION_PASSWORD", username | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This was a driveby since I was looking at this function. After that I |
||
), | ||
}, | ||
) | ||
|
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removing the memory test here for two reasons:
TestClient
(Documented testing approach does not work for SQLite in-memory instances fastapi/fastapi#3906). The proposed solution, i.e. usingpoolclass=StaticPool
does indeed work, but will cause the streaming test to crash with RunTimeError: got Future <Future pending> attached to a different loop when using custom loop in sync fixtures when upgrading from 0.14.2 to 0.15.0 encode/starlette#1315. I figured it wasn't worth the effort to dive any deeper than that.