-
Notifications
You must be signed in to change notification settings - Fork 43
Update README with test DB and fixture documentation #31
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
Open
leblancfg
wants to merge
4
commits into
mfreeborn:master
Choose a base branch
from
leblancfg:readme_test_fixture
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 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 hidden or 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 |
---|---|---|
|
@@ -95,7 +95,63 @@ Sometimes it is useful to be able to access the database outside the context of | |
|
||
return users | ||
|
||
Using a test database fixture with pytest | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
A suggested way of to override the database URL and yield a session fixture in your tests is to use environment variables. | ||
|
||
.. code-block:: python | ||
|
||
# Contents of app/configs.py | ||
import json | ||
import os | ||
|
||
DEV, PROD, TEST = ("development", "production", "test") | ||
CURRENT_ENV = os.environ.get("PYTHON_ENV", DEV) | ||
config = {DEV: "sqlite://dev.db", PROD: "postgresql://user:[email protected]/mydb", TEST: "sqlite://"} | ||
DATABASE_URL = config[CURRENT_ENV] | ||
|
||
|
||
# Contents of test_app.py | ||
import pytest | ||
from sqlalchemy import create_engine | ||
from fastapi.testclient import TestClient | ||
|
||
from app.configs import DATABASE_URL | ||
from app.db import Base # from sqlalchemy.ext.declarative import declarative_base | ||
from app.models import User | ||
from main import app, db | ||
|
||
|
||
@pytest.fixture(scope="function", name="session") | ||
def session_fixture(): | ||
engine = create_engine(DATABASE_URL) | ||
Base.metadata.drop_all(engine) | ||
Base.metadata.create_all(engine) | ||
with db(): | ||
yield db.session | ||
leblancfg marked this conversation as resolved.
Show resolved
Hide resolved
|
||
engine.dispose() | ||
|
||
|
||
@pytest.fixture(scope="function", name="client") | ||
def client_fixture(): | ||
return TestClient(app) | ||
|
||
|
||
def test_users_route(session, client): | ||
# Save a fake user | ||
NAME = 'Gontrand' | ||
user = User(name=NAME) | ||
session.add(user) | ||
session.commit() | ||
|
||
response = client.get('users') | ||
response_user = response.json()[0] | ||
assert response_user['name'] == NAME | ||
|
||
Run your tests with ``PYTHON_ENV=test pytest`` or use dotenv_ to manage these programmatically with an ``.env`` file. | ||
|
||
.. _FastAPI: https://github.com/tiangolo/fastapi | ||
.. _SQLAlchemy: https://github.com/pallets/flask-sqlalchemy | ||
.. _pip: https://pip.pypa.io/en/stable/quickstart/ | ||
.. _dotenv: https://github.com/theskumar/python-dotenv |
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.
Uh oh!
There was an error while loading. Please reload this page.