Feat: add e2e test for api
This commit is contained in:
@@ -1,10 +1,17 @@
|
||||
import sqlite3
|
||||
import time
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
import requests
|
||||
from sqlalchemy import create_engine
|
||||
from sqlalchemy.orm import clear_mappers, sessionmaker
|
||||
|
||||
from backend import config
|
||||
from backend.adapters.orm import metadata, start_mappers
|
||||
from backend.adapters.sqlite import create_db
|
||||
from tests.integration.test_repository_student_sqlite import populate_students
|
||||
from tests.integration.test_repository_tribe_sqlite import populate_tribes
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
@@ -23,6 +30,64 @@ def session(in_memory_db):
|
||||
|
||||
@pytest.fixture
|
||||
def sqlite_conn():
|
||||
conn = sqlite3.connect(":memory:")
|
||||
conn = sqlite3.connect("sqlite.db")
|
||||
create_db(conn)
|
||||
yield conn
|
||||
conn.close()
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def clean_db(sqlite_conn):
|
||||
sqlite_conn.execute("""DROP TABLE tribes""")
|
||||
sqlite_conn.execute("""DROP TABLE students""")
|
||||
sqlite_conn.commit()
|
||||
create_db(sqlite_conn)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def populate_db(sqlite_conn):
|
||||
_tribes = []
|
||||
_students = []
|
||||
|
||||
def _populate_db():
|
||||
tribes = populate_tribes(sqlite_conn)
|
||||
_tribes += tribes
|
||||
students = populate_students(sqlite_conn, tribes)
|
||||
_students += students
|
||||
return tribes, students
|
||||
|
||||
yield _populate_db
|
||||
|
||||
for student in _students:
|
||||
sqlite_conn.execute(
|
||||
"""
|
||||
DELETE FROM students WHERE id=:id
|
||||
""",
|
||||
{"id": student.id},
|
||||
)
|
||||
for tribe in _tribes:
|
||||
sqlite_conn.execute(
|
||||
"""
|
||||
DELETE FROM tribes WHERE name=:name
|
||||
""",
|
||||
{"name": tribe.name},
|
||||
)
|
||||
sqlite_conn.commit()
|
||||
|
||||
|
||||
def wait_for_webapp_to_come_up():
|
||||
deadline = time.time() + 10
|
||||
url = config.get_api_url()
|
||||
while time.time() < deadline:
|
||||
try:
|
||||
return requests.get(url)
|
||||
except ConnectionError:
|
||||
time.sleep(0.5)
|
||||
pytest.fail("API never came up")
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def restart_api():
|
||||
(Path(__file__).parent.parent / "backend" / "api" / "main.py").touch()
|
||||
time.sleep(0.5)
|
||||
wait_for_webapp_to_come_up()
|
||||
|
||||
59
tests/e2e/test_api.py
Normal file
59
tests/e2e/test_api.py
Normal file
@@ -0,0 +1,59 @@
|
||||
import pytest
|
||||
import requests
|
||||
|
||||
from backend import config
|
||||
from tests.model.fakes import build_tribes
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("restart_api")
|
||||
@pytest.mark.usefixtures("clean_db")
|
||||
def test_api_post_tribe():
|
||||
data = {"name": "tribe", "level": "2nd"}
|
||||
|
||||
url = config.get_api_url()
|
||||
r = requests.post(f"{url}/tribes", json=data)
|
||||
|
||||
assert r.status_code == 201
|
||||
assert r.json() == {
|
||||
"assessments": [],
|
||||
"level": "2nd",
|
||||
"name": "tribe",
|
||||
"students": [],
|
||||
}
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("restart_api")
|
||||
@pytest.mark.usefixtures("clean_db")
|
||||
def test_api_post_list_tribe():
|
||||
tribe = build_tribes(1)[0]
|
||||
|
||||
url = config.get_api_url()
|
||||
r = requests.post(f"{url}/tribes", json=tribe.to_dict())
|
||||
|
||||
assert r.status_code == 201
|
||||
|
||||
r = requests.get(f"{url}/tribes")
|
||||
assert r.json() == [
|
||||
{
|
||||
"assessments": [],
|
||||
"level": tribe.level,
|
||||
"name": tribe.name,
|
||||
"students": [],
|
||||
}
|
||||
]
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("restart_api")
|
||||
@pytest.mark.usefixtures("clean_db")
|
||||
def test_api_post_student():
|
||||
url = config.get_api_url()
|
||||
tribe = build_tribes(1)[0]
|
||||
requests.post(f"{url}/tribes", json=tribe.to_dict())
|
||||
|
||||
data = {"name": "zart", "tribe_name": tribe.name}
|
||||
r = requests.post(f"{url}/students", json=data)
|
||||
|
||||
assert r.status_code == 201
|
||||
assert r.json()["name"] == "zart"
|
||||
assert r.json()["tribe_name"] == tribe.name
|
||||
assert r.json()["id"]
|
||||
@@ -2,7 +2,6 @@ import sqlite3
|
||||
|
||||
import pytest
|
||||
|
||||
from backend.adapters.sqlite import create_db
|
||||
from backend.model.student import Student
|
||||
from backend.model.tribe import Tribe
|
||||
from backend.repository.student_sqlite_repository import StudentSQLiteRepository
|
||||
@@ -25,7 +24,6 @@ def populate_students(conn, tribes: list[Tribe]) -> list[Student]:
|
||||
|
||||
|
||||
def test_get_student(sqlite_conn):
|
||||
create_db(sqlite_conn)
|
||||
prebuild_tribes = populate_tribes(sqlite_conn)
|
||||
prebuild_students = populate_students(sqlite_conn, prebuild_tribes)
|
||||
|
||||
@@ -38,7 +36,6 @@ def test_get_student(sqlite_conn):
|
||||
|
||||
|
||||
def test_get_student_not_exists(sqlite_conn):
|
||||
create_db(sqlite_conn)
|
||||
prebuild_tribes = populate_tribes(sqlite_conn)
|
||||
prebuild_students = populate_students(sqlite_conn, prebuild_tribes)
|
||||
|
||||
@@ -48,7 +45,6 @@ def test_get_student_not_exists(sqlite_conn):
|
||||
|
||||
|
||||
def test_list_students(sqlite_conn):
|
||||
create_db(sqlite_conn)
|
||||
prebuild_tribes = populate_tribes(sqlite_conn)
|
||||
prebuild_students = populate_students(sqlite_conn, prebuild_tribes)
|
||||
|
||||
@@ -59,7 +55,6 @@ def test_list_students(sqlite_conn):
|
||||
|
||||
|
||||
def test_add_student(sqlite_conn):
|
||||
create_db(sqlite_conn)
|
||||
prebuild_tribes = populate_tribes(sqlite_conn)
|
||||
|
||||
student_repo = StudentSQLiteRepository(sqlite_conn)
|
||||
@@ -82,7 +77,6 @@ def test_add_student(sqlite_conn):
|
||||
|
||||
|
||||
def test_add_student_fail_exists(sqlite_conn):
|
||||
create_db(sqlite_conn)
|
||||
prebuild_tribes = populate_tribes(sqlite_conn)
|
||||
|
||||
student_repo = StudentSQLiteRepository(sqlite_conn)
|
||||
@@ -97,7 +91,6 @@ def test_add_student_fail_exists(sqlite_conn):
|
||||
|
||||
|
||||
def test_update_student(sqlite_conn):
|
||||
create_db(sqlite_conn)
|
||||
prebuild_tribes = populate_tribes(sqlite_conn)
|
||||
prebuild_students = populate_students(sqlite_conn, prebuild_tribes)
|
||||
|
||||
@@ -118,7 +111,6 @@ def test_update_student(sqlite_conn):
|
||||
|
||||
|
||||
def test_delete_student(sqlite_conn):
|
||||
create_db(sqlite_conn)
|
||||
prebuild_tribes = populate_tribes(sqlite_conn)
|
||||
prebuild_students = populate_students(sqlite_conn, prebuild_tribes)
|
||||
|
||||
|
||||
@@ -2,7 +2,6 @@ import sqlite3
|
||||
|
||||
import pytest
|
||||
|
||||
from backend.adapters.sqlite import create_db
|
||||
from backend.model.tribe import Tribe
|
||||
from backend.repository.tribe_sqlite_repository import TribeSQLiteRepository
|
||||
from tests.model.fakes import build_tribes
|
||||
@@ -23,7 +22,6 @@ def populate_tribes(conn) -> list[Tribe]:
|
||||
|
||||
|
||||
def test_get_tribe(sqlite_conn):
|
||||
create_db(sqlite_conn)
|
||||
prebuild_tribes = populate_tribes(sqlite_conn)
|
||||
|
||||
name = prebuild_tribes[0].name
|
||||
@@ -35,7 +33,6 @@ def test_get_tribe(sqlite_conn):
|
||||
|
||||
|
||||
def test_get_tribe_not_exists(sqlite_conn):
|
||||
create_db(sqlite_conn)
|
||||
prebuild_tribes = populate_tribes(sqlite_conn)
|
||||
|
||||
tribe_repo = TribeSQLiteRepository(sqlite_conn)
|
||||
@@ -44,7 +41,6 @@ def test_get_tribe_not_exists(sqlite_conn):
|
||||
|
||||
|
||||
def test_list_tribes(sqlite_conn):
|
||||
create_db(sqlite_conn)
|
||||
prebuild_tribes = populate_tribes(sqlite_conn)
|
||||
|
||||
tribe_repo = TribeSQLiteRepository(sqlite_conn)
|
||||
@@ -54,7 +50,6 @@ def test_list_tribes(sqlite_conn):
|
||||
|
||||
|
||||
def test_add_tribe(sqlite_conn):
|
||||
create_db(sqlite_conn)
|
||||
|
||||
tribe_repo = TribeSQLiteRepository(sqlite_conn)
|
||||
|
||||
@@ -76,7 +71,6 @@ def test_add_tribe(sqlite_conn):
|
||||
|
||||
|
||||
def test_add_tribe_fail_exists(sqlite_conn):
|
||||
create_db(sqlite_conn)
|
||||
prebuild_tribes = populate_tribes(sqlite_conn)
|
||||
|
||||
tribe_repo = TribeSQLiteRepository(sqlite_conn)
|
||||
@@ -87,7 +81,6 @@ def test_add_tribe_fail_exists(sqlite_conn):
|
||||
|
||||
|
||||
def test_update_tribe(sqlite_conn):
|
||||
create_db(sqlite_conn)
|
||||
prebuild_tribes = populate_tribes(sqlite_conn)
|
||||
|
||||
tribe_repo = TribeSQLiteRepository(sqlite_conn)
|
||||
@@ -102,7 +95,6 @@ def test_update_tribe(sqlite_conn):
|
||||
|
||||
|
||||
def test_delete_tribe(sqlite_conn):
|
||||
create_db(sqlite_conn)
|
||||
prebuild_tribes = populate_tribes(sqlite_conn)
|
||||
|
||||
tribe_repo = TribeSQLiteRepository(sqlite_conn)
|
||||
|
||||
Reference in New Issue
Block a user