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

Update README with test DB and fixture documentation #31

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
56 changes: 56 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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:password@sql.mydomain.com/mydb", TEST: "sqlite://"}
DATABASE_URL = config[CURRENT_ENV]


# Contents of test_app.py
import pytest
leblancfg marked this conversation as resolved.
Show resolved Hide resolved
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