Skip to content
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
merged 2 commits into from
Feb 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion scripts/add_chats.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def main():
data={
"username": username,
"password": os.environ.get(
"AI_PROXY_DEMO_AUTHENTICATION_PASSWORD", username
"RAGNA_DEMO_AUTHENTICATION_PASSWORD", username
),
},
)
Expand Down
21 changes: 7 additions & 14 deletions tests/deploy/api/test_e2e.py
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"])
Copy link
Member Author

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:

  1. remove memory option for state db #320 will remove it from our special cases. Meaning, this is no longer part of our core API, but rather a special case.
  2. For some reason, in-memory databases don't play nice with the TestClient (Documented testing approach does not work for SQLite in-memory instances fastapi/fastapi#3906). The proposed solution, i.e. using poolclass=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.

@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
Copy link
Member Author

Choose a reason for hiding this comment

The 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 greped for the wrong string and found one more usage that I also fixed.

),
},
)
Expand Down
49 changes: 0 additions & 49 deletions tests/utils.py

This file was deleted.

Loading