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

use FastAPI test client for testing #322

merged 2 commits into from Feb 19, 2024

Conversation

pmeier
Copy link
Member

@pmeier pmeier commented Feb 14, 2024

Per title. Documentation is here. This has at least three upsides:

  1. We don't need to maintain code to actually starting and shutting down the API.
  2. Since the test client doesn't actually start the API, this is quite a bit faster.
  3. This allows the API code to be tracked in coverage without any shenanigans regarding coverage in subprocesses.

@pmeier pmeier added type: maintenance 🛠️ Day-to-day maintenance tasks area: testing ✅ labels Feb 14, 2024
@pmeier pmeier requested a review from nenb February 14, 2024 22:19
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.



@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 tiangolo/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.

Copy link
Contributor

@nenb nenb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Makes sense.

@pmeier pmeier merged commit 6f09d47 into main Feb 19, 2024
10 checks passed
@pmeier pmeier deleted the api-test branch February 19, 2024 07:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants