Finish up learnable tests

This commit is contained in:
lvrossem
2023-04-18 05:24:17 -06:00
parent 3968dfd4eb
commit 4235395579
4 changed files with 278 additions and 5 deletions

View File

@@ -77,11 +77,11 @@ def create_learnable(
db.commit() db.commit()
def patch_learnable(db: Session, user: User, learnable: SavedLearnableProgress): def patch_learnable(db: Session, user: User, learnable_name: str, learnable: SavedLearnableProgress):
"""Patch an existing learnable""" """Patch an existing learnable"""
db_learnable = ( db_learnable = (
db.query(LearnableProgress) db.query(LearnableProgress)
.filter(LearnableProgress.name == learnable.name) .filter(LearnableProgress.name == learnable_name)
.first() .first()
) )
@@ -90,6 +90,17 @@ def patch_learnable(db: Session, user: User, learnable: SavedLearnableProgress):
status_code=400, detail="Learnable with provided name not found" status_code=400, detail="Learnable with provided name not found"
) )
potential_duplicate = (
db.query(LearnableProgress)
.filter(LearnableProgress.name == learnable.name, LearnableProgress.learnable_progress_id != db_learnable.learnable_progress_id)
.first()
)
if potential_duplicate:
raise HTTPException(
status_code=400, detail="No duplicate learnable names allowed"
)
if learnable.index < -1: if learnable.index < -1:
raise HTTPException(status_code=400, detail="Invalid learnable index") raise HTTPException(status_code=400, detail="Invalid learnable index")
elif learnable.index > -1: elif learnable.index > -1:

View File

@@ -152,11 +152,12 @@ async def create_learnable(
crud_learnables.create_learnable(db, current_user, course, learnable) crud_learnables.create_learnable(db, current_user, course, learnable)
@app.patch("/learnables") @app.patch("/learnables/{name}")
async def create_learnable( async def create_learnable(
name: str,
learnable: learnableprogress.SavedLearnableProgress, learnable: learnableprogress.SavedLearnableProgress,
current_user_name: str = Depends(crud_authentication.get_current_user_name), current_user_name: str = Depends(crud_authentication.get_current_user_name),
db: Session = Depends(get_db), db: Session = Depends(get_db),
): ):
current_user = crud_users.get_user_by_username(db, current_user_name) current_user = crud_users.get_user_by_username(db, current_user_name)
crud_learnables.patch_learnable(db, current_user, learnable) crud_learnables.patch_learnable(db, current_user, name, learnable)

View File

@@ -9,6 +9,7 @@ from tests.config.database import clear_db
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_create_learnables_should_succeed(): async def test_create_learnables_should_succeed():
"""Test whether creating a new learnable succeeds"""
clear_db() clear_db()
token = await register_user() token = await register_user()
@@ -37,3 +38,263 @@ async def test_create_learnables_should_succeed():
response = response.json() response = response.json()
assert len(response) == nr_learnables assert len(response) == nr_learnables
@pytest.mark.asyncio
async def test_patch_learnables_should_succeed():
"""Test whether patching learnables succeeds"""
clear_db()
token = await register_user()
headers = get_headers(token)
for course in CourseEnum:
if course != CourseEnum.All:
response = client.post(
f"/learnables/{course}",
json={
"index": random.randint(0, 100),
"in_use": bool(random.randint(0, 1)),
"name": f"{course}",
},
headers=headers,
)
assert response.status_code == 200
new_index = random.randint(0, 100)
new_in_use = bool(random.randint(0, 1))
new_name = "New" + course
response = client.patch(f"/learnables/{course}", json={"index": new_index, "in_use": new_in_use, "name": new_name}, headers=headers)
assert response.status_code == 200
response = client.get(f"/learnables/{course}", headers=headers)
assert response.status_code == 200
response = response.json()[0]
assert response["index"] == new_index
assert response["in_use"] == new_in_use
assert response["name"] == new_name
@pytest.mark.asyncio
async def test_create_learnables_without_name_should_fail():
"""Test whether creating a new learnable without name fails"""
clear_db()
token = await register_user()
headers = get_headers(token)
for course in CourseEnum:
if course != CourseEnum.All:
response = client.post(
f"/learnables/{course}",
json={
"index": random.randint(0, 100),
"in_use": bool(random.randint(0, 1)),
},
headers=headers,
)
assert response.status_code == 400
@pytest.mark.asyncio
async def test_create_learnables_without_index_should_fail():
"""Test whether creating a new learnable without index fails"""
clear_db()
token = await register_user()
headers = get_headers(token)
for course in CourseEnum:
if course != CourseEnum.All:
response = client.post(
f"/learnables/{course}",
json={
"name": course,
"in_use": bool(random.randint(0, 1)),
},
headers=headers,
)
assert response.status_code == 400
@pytest.mark.asyncio
async def test_create_learnables_without_in_use_should_fail():
"""Test whether creating a new learnable without in_use fails"""
clear_db()
token = await register_user()
headers = get_headers(token)
for course in CourseEnum:
if course != CourseEnum.All:
response = client.post(
f"/learnables/{course}",
json={
"index": random.randint(0, 100),
"name": course,
},
headers=headers,
)
assert response.status_code == 400
@pytest.mark.asyncio
async def test_create_learnable_without_auth_should_fail():
"""Test whether creating learnables without authentication fails"""
clear_db()
for course in CourseEnum:
if course != CourseEnum.All:
response = client.post(
f"/learnables/{course}",
json={
"index": 0,
"in_use": bool(random.randint(0, 1)),
"name": f"{course}",
},
headers=get_headers(),
)
assert response.status_code == 403
@pytest.mark.asyncio
async def test_get_learnables_of_nonexisting_course_should_fail():
"""Test whether learnables of a nonexisting course fails"""
clear_db()
token = await register_user()
course = "FakeCourse"
response = client.get(f"/learnables/{course}", headers=get_headers(token))
assert response.status_code == 422
@pytest.mark.asyncio
async def test_post_learnable_to_nonexisting_course_should_fail():
"""Test whether creating a learnable for a nonexisting course fails fails"""
clear_db()
token = await register_user()
course = "FakeCourse"
response = client.post(
f"/learnables/{course}",
json={
"index": 0,
"in_use": bool(random.randint(0, 1)),
"name": f"{course}",
},
headers=get_headers(token),
)
assert response.status_code == 422
@pytest.mark.asyncio
async def test_get_learnables_without_auth_should_fail():
"""Test whether fetching learnables without authentication fails"""
clear_db()
for course in CourseEnum:
if course != CourseEnum.All:
response = client.get(f"/learnables/{course}", headers=get_headers())
assert response.status_code == 403
@pytest.mark.asyncio
async def test_patch_learnable_without_auth_should_fail():
"""Test whether patching learnables without authentication fails"""
clear_db()
token = await register_user()
for course in CourseEnum:
if course != CourseEnum.All:
response = client.post(
f"/learnables/{course}",
json={
"index": 0,
"in_use": bool(random.randint(0, 1)),
"name": f"{course}",
},
headers=get_headers(token),
)
assert response.status_code == 200
response = client.patch(
f"/learnables/{course}",
json={
"index": 0,
"in_use": bool(random.randint(0, 1)),
"name": f"{course}",
},
headers=get_headers(),
)
assert response.status_code == 403
@pytest.mark.asyncio
async def test_create_learnable_with_existing_name_should_fail():
"""Test whether putting high scores without authentication fails"""
clear_db()
token = await register_user()
for course in CourseEnum:
if course != CourseEnum.All:
response = client.post(
f"/learnables/{course}",
json={
"index": 0,
"in_use": bool(random.randint(0, 1)),
"name": f"{course}",
},
headers=get_headers(token),
)
assert response.status_code == 200
response = client.post(
f"/learnables/{course}",
json={
"index": 1,
"in_use": bool(random.randint(0, 1)),
"name": f"{course}",
},
headers=get_headers(token),
)
assert response.status_code == 400
@pytest.mark.asyncio
async def test_patch_nonexisting_learnable_should_fail():
"""Test whether patching nonexisting learnables fails"""
clear_db()
token = await register_user()
for course in CourseEnum:
if course != CourseEnum.All:
response = client.patch(
f"/learnables/{course}",
json={
"index": 0,
"in_use": bool(random.randint(0, 1)),
"name": f"{course}",
},
headers=get_headers(token),
)
assert response.status_code == 400

View File

@@ -1,6 +1,6 @@
import pytest import pytest
from tests.base import (avatar_index, client, get_headers, register_user, from tests.base import (client, get_headers, register_user,
username) username)
from tests.config.database import clear_db from tests.config.database import clear_db