generated from ministryofjustice/template-repository
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added unit testing with in-memory database and TestClient.
- Loading branch information
1 parent
db163c7
commit bfb4148
Showing
16 changed files
with
93 additions
and
74 deletions.
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
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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from app.main import create_app | ||
|
||
case_api = create_app() |
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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from app.db.database import engine | ||
|
||
db_engine = engine |
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
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 was deleted.
Oops, something went wrong.
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 +1 @@ | ||
|
||
from .cases import Case |
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
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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
[tool.pytest.ini_options] | ||
filterwarnings = [ | ||
"ignore", | ||
"default:::app", | ||
] |
Empty file.
Empty file.
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 |
---|---|---|
@@ -0,0 +1,19 @@ | ||
from fastapi.testclient import TestClient | ||
from sqlmodel import Session | ||
|
||
|
||
def test_create_case(client: TestClient, session: Session): | ||
response = client.post( | ||
"/cases/", json={"category": "Housing", "name": "John Doe"}) | ||
case = response.json() | ||
|
||
assert response.status_code == 200 | ||
assert case["category"] == "Housing" | ||
assert case["name"] == "John Doe" | ||
assert case["id"] is not None | ||
|
||
|
||
def test_read_case(client: TestClient, session: Session): | ||
response = client.post( | ||
"/cases/", json={"category": "Housing", "name": "John Doe"}) | ||
case = response.json() |
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 |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import pytest | ||
from sqlmodel import SQLModel, create_engine, Session, StaticPool | ||
from app import case_api | ||
from app.db.database import get_session | ||
from fastapi.testclient import TestClient | ||
|
||
|
||
@pytest.fixture(name="session") | ||
def session_fixture(): | ||
engine = create_engine( | ||
"sqlite://", connect_args={"check_same_thread": False}, poolclass=StaticPool | ||
) | ||
SQLModel.metadata.create_all(engine) | ||
with Session(engine) as session: | ||
yield session | ||
|
||
|
||
@pytest.fixture(name="client") | ||
def client_fixture(session: Session): | ||
def get_session_override(): | ||
return session | ||
|
||
case_api.dependency_overrides[get_session] = get_session_override | ||
|
||
client = TestClient(case_api) | ||
yield client | ||
case_api.dependency_overrides.clear() |