Files
back-end/tests/test_courseprogress.py
2023-04-10 14:07:30 -06:00

175 lines
4.6 KiB
Python

import random
import sys
import pytest
from fastapi.testclient import TestClient
sys.path.append("..")
from src.enums import CourseEnum
from src.main import app, get_db
from tests.config.database import clear_db, override_get_db
app.dependency_overrides[get_db] = override_get_db
client = TestClient(app)
username = "user1"
password = "password"
avatar = "lion"
async def register_user():
response = client.post(
"/register",
headers={"Content-Type": "application/json"},
json={"username": username, "password": password, "avatar": avatar},
)
assert response.status_code == 200
return response.json()["access_token"]
@pytest.mark.asyncio
async def test_register_creates_progress_of_zero():
"""Test whether registering a new user initializes all progress values to 0.0"""
clear_db()
token = await register_user()
headers = {"Authorization": f"Bearer {token}", "Content-Type": "application/json"}
for course in CourseEnum:
if course != CourseEnum.All:
response = client.get(f"/courseprogress/{course}", headers=headers)
assert response.status_code == 200
response = response.json()[0]
assert response["progress_value"] == 0.0
assert response["course"] == course
@pytest.mark.asyncio
async def test_get_all_returns_all():
clear_db()
token = await register_user()
headers = {"Authorization": f"Bearer {token}", "Content-Type": "application/json"}
response = client.get("/courseprogress/All", headers=headers)
assert response.status_code == 200
response = response.json()
for course in CourseEnum:
if course != CourseEnum.All:
assert {"progress_value": 0.0, "course": course} in response
@pytest.mark.asyncio
async def test_get_nonexisting_course_should_fail():
clear_db()
token = await register_user()
fake_course = "FakeCourse"
headers = {"Authorization": f"Bearer {token}", "Content-Type": "application/json"}
response = client.get(f"/courseprogress/{fake_course}", headers=headers)
assert response.status_code == 422
@pytest.mark.asyncio
async def test_patch_course_progress():
clear_db()
token = await register_user()
headers = {"Authorization": f"Bearer {token}", "Content-Type": "application/json"}
for course in CourseEnum:
if course != CourseEnum.All:
progress_value = random.uniform(0, 1)
response = client.patch(
f"/courseprogress",
headers=headers,
json={"progress_value": progress_value, "course": course},
)
assert response.status_code == 200
assert response.json()[0]["progress_value"] == progress_value
@pytest.mark.asyncio
async def test_patch_all_should_patch_all_courses():
clear_db()
token = await register_user()
headers = {"Authorization": f"Bearer {token}", "Content-Type": "application/json"}
progress_value = random.uniform(0, 1)
response = client.patch(
f"/courseprogress",
headers=headers,
json={"progress_value": progress_value, "course": "All"},
)
assert response.status_code == 200
response = client.get("/courseprogress/All", headers=headers)
assert response.status_code == 200
response = response.json()
for course in CourseEnum:
if course != CourseEnum.All:
assert {"progress_value": progress_value, "course": course} in response
@pytest.mark.asyncio
async def test_patch_nonexisting_course_should_fail():
clear_db()
token = await register_user()
fake_course = "FakeCourse"
headers = {"Authorization": f"Bearer {token}", "Content-Type": "application/json"}
progress_value = random.uniform(0, 1)
response = client.patch(
f"/courseprogress",
headers=headers,
json={"progress_value": progress_value, "course": fake_course},
)
assert response.status_code == 422
@pytest.mark.asyncio
async def test_patch_course_with_invalid_value_should_fail():
clear_db()
token = await register_user()
headers = {"Authorization": f"Bearer {token}", "Content-Type": "application/json"}
too_high_progress_value = random.uniform(0, 1) + 2
too_low_progress_value = random.uniform(0, 1) - 2
response = client.patch(
f"/courseprogress",
headers=headers,
json={"progress_value": too_high_progress_value, "course": "All"},
)
assert response.status_code == 400
response = client.patch(
f"/courseprogress",
headers=headers,
json={"progress_value": too_low_progress_value, "course": "All"},
)
assert response.status_code == 400