More refactoring

This commit is contained in:
lvrossem
2023-04-18 02:52:26 -06:00
parent d074074b03
commit 3968dfd4eb
10 changed files with 156 additions and 64 deletions

View File

@@ -3,17 +3,18 @@ import random
import pytest
from src.enums import MinigameEnum
from tests.base import avatar_index, client, password, register_user
from tests.base import (avatar_index, client, get_headers, password,
register_user)
from tests.config.database import clear_db
@pytest.mark.asyncio
async def test_put_highscore():
async def test_put_highscore_should_succeed():
"""Test whether putting a new high score succeeds"""
clear_db()
token = await register_user()
headers = {"Authorization": f"Bearer {token}", "Content-Type": "application/json"}
headers = get_headers(token)
for minigame in MinigameEnum:
score_value = random.random()
@@ -31,12 +32,12 @@ async def test_put_highscore():
@pytest.mark.asyncio
async def test_put_lower_highscore_does_not_change_old_value():
async def test_put_lower_highscore_should_not_change_old_value():
"""Test whether putting a new high score lower than the current one doesn't change the old one"""
clear_db()
token = await register_user()
headers = {"Authorization": f"Bearer {token}", "Content-Type": "application/json"}
headers = get_headers(token)
for minigame in MinigameEnum:
score_value = random.random()
@@ -74,7 +75,7 @@ async def test_put_highscore_for_nonexisting_minigame_should_fail():
fake_minigame = "FakeGame"
headers = {"Authorization": f"Bearer {token}", "Content-Type": "application/json"}
headers = get_headers(token)
response = client.put(
f"/highscores/{fake_minigame}",
@@ -90,7 +91,7 @@ async def test_put_highscores_without_auth_should_fail():
"""Test whether putting high scores without authentication fails"""
clear_db()
headers = {"Content-Type": "application/json"}
headers = get_headers()
for minigame in MinigameEnum:
response = client.put(
@@ -107,7 +108,7 @@ async def test_get_highscores_without_auth_should_fail():
"""Test whether fetching high scores without authentication fails"""
clear_db()
headers = {"Content-Type": "application/json"}
headers = get_headers()
for minigame in MinigameEnum:
response = client.get(
@@ -133,7 +134,7 @@ async def test_get_highscore_for_nonexisting_minigame_should_fail():
fake_minigame = "FakeGame"
headers = {"Authorization": f"Bearer {token}", "Content-Type": "application/json"}
headers = get_headers(token)
response = client.get(
f"/highscores/{fake_minigame}",
@@ -156,7 +157,7 @@ async def test_get_invalid_number_of_highscores_should_fail():
clear_db()
token = await register_user()
headers = {"Authorization": f"Bearer {token}", "Content-Type": "application/json"}
headers = get_headers(token)
for minigame in MinigameEnum:
response = client.get(
@@ -168,12 +169,12 @@ async def test_get_invalid_number_of_highscores_should_fail():
@pytest.mark.asyncio
async def test_get_highscores_should_work_with_default_value():
async def test_get_highscores_should_succeed_with_default_value():
"""Test whether fetching high scores without passing an explicit amount still succeeds"""
clear_db()
token = await register_user()
headers = {"Authorization": f"Bearer {token}", "Content-Type": "application/json"}
headers = get_headers(token)
for minigame in MinigameEnum:
response = client.get(
@@ -185,16 +186,16 @@ async def test_get_highscores_should_work_with_default_value():
@pytest.mark.asyncio
async def test_get_highscores_returns_sorted_list_with_correct_length():
async def test_get_highscores_should_return_sorted_list_with_correct_length():
"""Test whether getting a list of high scores gets a list in descending order and of the correct length"""
clear_db()
token = await register_user()
headers = {"Content-Type": "application/json"}
headers = get_headers()
for minigame in MinigameEnum:
clear_db()
nr_entries = random.randint(5, 50)
nr_entries = random.randint(5, 10)
token = ""
users_score_tuples = [
@@ -218,10 +219,7 @@ async def test_get_highscores_returns_sorted_list_with_correct_length():
response = client.put(
f"/highscores/{minigame}",
headers={
"Authorization": f"Bearer {token}",
"Content-Type": "application/json",
},
headers=get_headers(token),
json={"score_value": score},
)
@@ -229,10 +227,7 @@ async def test_get_highscores_returns_sorted_list_with_correct_length():
response = client.get(
f"/highscores/{minigame}?mine_only=false&amount={int(nr_entries)}",
headers={
"Authorization": f"Bearer {token}",
"Content-Type": "application/json",
},
headers=get_headers(token),
)
assert response.status_code == 200
@@ -243,14 +238,27 @@ async def test_get_highscores_returns_sorted_list_with_correct_length():
for i in range(1, len(response)):
assert response[i]["score_value"] <= response[i - 1]["score_value"]
response = client.get(
f"/highscores/{minigame}?most_recent=true&mine_only=false&amount={int(nr_entries)}",
headers=get_headers(token),
)
assert response.status_code == 200
response = response.json()
assert len(response) == nr_entries
for i in range(1, len(response)):
assert response[i]["time"] <= response[i - 1]["time"]
@pytest.mark.asyncio
async def test_get_own_existing_high_score_should_return_high_score():
async def test_get_own_existing_high_score_should_succeed():
"""Test whether fetching your own high score of a game succeeds"""
clear_db()
token = await register_user()
headers = {"Authorization": f"Bearer {token}", "Content-Type": "application/json"}
headers = get_headers(token)
for minigame in MinigameEnum:
response = client.put(
@@ -276,7 +284,7 @@ async def test_get_own_nonexisting_high_score_should_return_empty_list():
clear_db()
token = await register_user()
headers = {"Authorization": f"Bearer {token}", "Content-Type": "application/json"}
headers = get_headers(token)
for minigame in MinigameEnum:
response = client.get(
@@ -294,7 +302,7 @@ async def test_get_multiple_own_high_scores_of_same_game_should_fail():
clear_db()
token = await register_user()
headers = {"Authorization": f"Bearer {token}", "Content-Type": "application/json"}
headers = get_headers(token)
for minigame in MinigameEnum:
response = client.get(