bgg_api/tests/test_main.py
2024-10-17 11:53:55 +02:00

129 lines
No EOL
4.7 KiB
Python

import validators
from fastapi.testclient import TestClient
from datetime import date
from typing import Union, Dict
from src.main import app
from src.classes import boardgame_classes, play_classes, statistic_classes
client = TestClient(app)
def default_boardgame_test(to_test_boardgame: boardgame_classes.BoardGame):
assert type(to_test_boardgame.id) == int
assert type(to_test_boardgame.name) == str
assert type(to_test_boardgame.weight) == float
assert type(to_test_boardgame.description) == str
assert validators.url(str(to_test_boardgame.image_url))
assert validators.url(str(to_test_boardgame.thumbnail_url))
assert type(to_test_boardgame.year_published) == int
assert type(to_test_boardgame.min_players) == int
assert type(to_test_boardgame.max_players) == int
assert type(to_test_boardgame.min_playing_time) == int
assert type(to_test_boardgame.max_playing_time) == int
assert type(to_test_boardgame.min_age) == int
def default_statistic_test(to_test_statistic: statistic_classes.StatisticBase):
assert type(to_test_statistic.name) == str, to_test_statistic
def test_read_main():
response = client.get("/")
assert response.status_code == 200
assert response.json() == {"Hello": "World"}
def test_retrieve_boardgame():
response = client.get("/boardgame?id=373167")
assert response.status_code == 200
returned_boardgame = boardgame_classes.BoardGamePublic.model_validate(response.json())
default_boardgame_test(returned_boardgame)
def test_retrieve_owned():
response = client.get("/owned")
assert response.status_code == 200
returned_boardgame = boardgame_classes.BoardGamePublicNoPlays.model_validate(response.json()[0])
default_boardgame_test(returned_boardgame)
assert type(returned_boardgame.owned_info.price_paid) == float
assert type(returned_boardgame.owned_info.acquisition_date) == date
assert type(returned_boardgame.owned_info.acquired_from) == str
def test_retrieve_wishlist():
response = client.get("/wishlist")
assert response.status_code == 200
returned_boardgame = boardgame_classes.BoardGamePublicNoPlays.model_validate(response.json()[0])
default_boardgame_test(returned_boardgame)
assert type(returned_boardgame.wishlist_info.wishlist_priority) == int
assert returned_boardgame.wishlist_info.wishlist_priority > 0
def test_retrieve_plays():
response = client.get("/plays")
assert response.status_code == 200
returned_play = play_classes.PlayPublic.model_validate(response.json()[0])
assert type(returned_play.boardgame_id) == int
assert type(returned_play.play_date) == date
assert type(returned_play.duration) == int
assert type(returned_play.ignore_for_stats) == bool
assert type(returned_play.location) == str
def test_retrieve_players():
response = client.get("/players_from_play?play_id=1")
assert response.status_code == 200
returned_player = play_classes.PlayPlayerPublic.model_validate(response.json()[0])
assert type(returned_player.name) == str
assert type(returned_player.username) == str
assert type(returned_player.score) == float or returned_player.score == None
assert type(returned_player.first_play) == bool
assert type(returned_player.has_won) == bool
assert type(returned_player.play_id) == int
def test_retrieve_basic_statistic():
response = client.get("/statistics/amount_of_games")
assert response.status_code == 200
returned_statistic = statistic_classes.NumberStatistic.model_validate(response.json())
default_statistic_test(returned_statistic)
assert type(returned_statistic.result) == float
def test_retrieve_timeline_statistic():
response = client.get("/statistics/amount_of_games_over_time")
assert response.status_code == 200
returned_statistic = statistic_classes.TimeLineStatistic.model_validate(response.json())
default_statistic_test(returned_statistic)
assert type(returned_statistic.result) == dict
response = client.get("/statistics/games_played_per_year")
assert response.status_code == 200
returned_statistic = statistic_classes.TimeLineStatistic.model_validate(response.json())
default_statistic_test(returned_statistic)
assert type(returned_statistic.result) == dict
def test_retrieve_game_order_statistic():
response = client.get("/statistics/most_expensive_games")
assert response.status_code == 200
returned_statistic = statistic_classes.GamesStatistic.model_validate(response.json())
default_statistic_test(returned_statistic)
assert type(returned_statistic.result) == dict
assert type(returned_statistic.games) == list
assert type(returned_statistic.games[0]) == boardgame_classes.BoardGamePublicNoPlays