Reorganized modules. Created pytest config to make testing work

This commit is contained in:
Yarne Coppens 2024-08-02 09:52:06 +02:00
parent 724a40c0a6
commit 00257a3251
9 changed files with 35 additions and 27 deletions

0
classes/__init__.py Normal file
View file

17
main.py
View file

@ -1,8 +1,9 @@
from typing import Union from typing import Union
from fastapi import FastAPI from fastapi import FastAPI
from classes.boardgame import BoardGame, BoardGameExpansion, CollectionBoardGame, CollectionBoardGameExpansion, WishlistBoardGame from classes import boardgame_classes
from bgg_connection import get_boardgame, get_user_owned_collection, get_user_wishlist_collection from modules import bgg_connection
app = FastAPI() app = FastAPI()
@ -11,18 +12,18 @@ app = FastAPI()
def read_root(): def read_root():
return {"Hello": "World"} return {"Hello": "World"}
@app.get("/boardgame/{boardgame_id}", response_model=BoardGame) @app.get("/boardgame/{boardgame_id}", response_model=boardgame_classes.BoardGame)
def get_boardgame_by_id(boardgame_id: int): def get_boardgame_by_id(boardgame_id: int):
requested_boardgame: BoardGame = get_boardgame(boardgame_id) requested_boardgame: boardgame_classes.BoardGame = bgg_connection.get_boardgame(boardgame_id)
return requested_boardgame return requested_boardgame
@app.get("/collection", response_model=list[CollectionBoardGame]) @app.get("/collection", response_model=list[boardgame_classes.CollectionBoardGame])
def get_owned_collection(): def get_owned_collection():
requested_collection: list[CollectionBoardGame] = get_user_owned_collection() requested_collection: list[boardgame_classes.CollectionBoardGame] = bgg_connection.get_user_owned_collection()
return requested_collection return requested_collection
@app.get("/wishlist", response_model=list[WishlistBoardGame]) @app.get("/wishlist", response_model=list[boardgame_classes.WishlistBoardGame])
def get_wishlist_collection(): def get_wishlist_collection():
requested_collection: list[WishlistBoardGame] = get_user_wishlist_collection() requested_collection: list[boardgame_classes.WishlistBoardGame] = bgg_connection.get_user_wishlist_collection()
return requested_collection return requested_collection

0
modules/__init__.py Normal file
View file

View file

@ -4,8 +4,10 @@ from pydantic import HttpUrl
import requests import requests
from datetime import datetime from datetime import datetime
from classes.boardgame import BoardGame, BoardGameExpansion, CollectionBoardGame, CollectionBoardGameExpansion, WishlistBoardGame, BoardgameType from classes import boardgame_classes
import auth_manager from modules import auth_manager
#from classes.boardgame import BoardGame, BoardGameExpansion, CollectionBoardGame, CollectionBoardGameExpansion, WishlistBoardGame, BoardgameType
authenticated_session: requests.Session = requests.Session() authenticated_session: requests.Session = requests.Session()
@ -15,7 +17,7 @@ def url_to_xml_object(url: HttpUrl) -> ET.Element:
root = ET.fromstring(r.content) root = ET.fromstring(r.content)
return root return root
def get_boardgame(boardgame_id: int) -> BoardGame: def get_boardgame(boardgame_id: int) -> boardgame_classes.BoardGame:
url : str = "https://boardgamegeek.com/xmlapi2/thing?id={}&stats=true".format(boardgame_id) url : str = "https://boardgamegeek.com/xmlapi2/thing?id={}&stats=true".format(boardgame_id)
boardgame_xml_object : ET.Element = url_to_xml_object(url) boardgame_xml_object : ET.Element = url_to_xml_object(url)
@ -23,14 +25,14 @@ def get_boardgame(boardgame_id: int) -> BoardGame:
return requested_boardgame return requested_boardgame
def get_multiple_boardgames(boardgame_ids: list[int]) -> list[BoardGame]: def get_multiple_boardgames(boardgame_ids: list[int]) -> list[boardgame_classes.BoardGame]:
def divide_list_in_chunks(list_to_divide: list[int], chunk_size: int = 20): def divide_list_in_chunks(list_to_divide: list[int], chunk_size: int = 20):
for i in range(0, len(list_to_divide), chunk_size): for i in range(0, len(list_to_divide), chunk_size):
yield list_to_divide[i:i + chunk_size] yield list_to_divide[i:i + chunk_size]
boardgame_list_to_return: list[BoardGame] = [] boardgame_list_to_return: list[boardgame_classes.BoardGame] = []
#Boardgamegeek only allows chunks of 20 boardgames at a time #Boardgamegeek only allows chunks of 20 boardgames at a time
boardgame_ids_divided = list(divide_list_in_chunks(boardgame_ids)) boardgame_ids_divided = list(divide_list_in_chunks(boardgame_ids))
@ -48,7 +50,7 @@ def get_multiple_boardgames(boardgame_ids: list[int]) -> list[BoardGame]:
#Requires single boardgame XML 'item' from bgg api on /thing #Requires single boardgame XML 'item' from bgg api on /thing
def convert_xml_to_boardgame(boardgame_xml: ET.Element) -> BoardGame: def convert_xml_to_boardgame(boardgame_xml: ET.Element) -> boardgame_classes.BoardGame:
boardgame_type = boardgame_xml.get('type') boardgame_type = boardgame_xml.get('type')
@ -77,15 +79,15 @@ def convert_xml_to_boardgame(boardgame_xml: ET.Element) -> BoardGame:
match boardgame_type: match boardgame_type:
case "boardgame": case "boardgame":
boardgame = BoardGame(**boardgame_dict) boardgame = boardgame_classes.BoardGame(**boardgame_dict)
case "boardgameexpansion": case "boardgameexpansion":
boardgame = BoardGameExpansion(**boardgame_dict) boardgame = boardgame_classes.BoardGameExpansion(**boardgame_dict)
return boardgame return boardgame
def convert_collection_xml_to_boardgame(boardgame_extra_info: BoardGame, collection_boardgame_xml: ET.Element) -> BoardGame: def convert_collection_xml_to_boardgame(boardgame_extra_info: boardgame_classes.BoardGame, collection_boardgame_xml: ET.Element) -> boardgame_classes.BoardGame:
boardgame_type = collection_boardgame_xml.get('subtype') boardgame_type = collection_boardgame_xml.get('subtype')
@ -129,19 +131,19 @@ def convert_collection_xml_to_boardgame(boardgame_extra_info: BoardGame, collect
match boardgame_type: match boardgame_type:
case "boardgame": case "boardgame":
boardgame = CollectionBoardGame(**boardgame_dict) boardgame = boardgame_classes.CollectionBoardGame(**boardgame_dict)
case "boardgameexpansion": case "boardgameexpansion":
boardgame = CollectionBoardGameExpansion(**boardgame_dict) boardgame = boardgame_classes.CollectionBoardGameExpansion(**boardgame_dict)
return boardgame return boardgame
#Creates list of board games from a collection '/collection' URL #Creates list of board games from a collection '/collection' URL
def get_boardgames_from_collection_url(collection_url: str) -> list[BoardGame]: def get_boardgames_from_collection_url(collection_url: str) -> list[boardgame_classes.BoardGame]:
collection_xml = url_to_xml_object(collection_url) collection_xml = url_to_xml_object(collection_url)
collection_list: list[BoardGame] = [] collection_list: list[boardgame_classes.BoardGame] = []
collection_id_list: list[int] = [] collection_id_list: list[int] = []
@ -164,7 +166,7 @@ def get_boardgames_from_collection_url(collection_url: str) -> list[BoardGame]:
return collection_list return collection_list
def get_user_owned_collection() -> list[BoardGame]: def get_user_owned_collection() -> list[boardgame_classes.BoardGame]:
url_no_expansions = 'https://boardgamegeek.com/xmlapi2/collection?username={}&own=1&stats=1&excludesubtype=boardgameexpansion&showprivate=1&version=1'.format(auth_manager.username) url_no_expansions = 'https://boardgamegeek.com/xmlapi2/collection?username={}&own=1&stats=1&excludesubtype=boardgameexpansion&showprivate=1&version=1'.format(auth_manager.username)
owned_boardgames = get_boardgames_from_collection_url(url_no_expansions) owned_boardgames = get_boardgames_from_collection_url(url_no_expansions)
@ -175,7 +177,7 @@ def get_user_owned_collection() -> list[BoardGame]:
return owned_boardgames return owned_boardgames
def get_user_wishlist_collection() -> list[BoardGame]: def get_user_wishlist_collection() -> list[boardgame_classes.BoardGame]:
url = 'https://boardgamegeek.com/xmlapi2/collection?username={}&wishlist=1&stats=1&showprivate=1&version=1'.format(auth_manager.username) url = 'https://boardgamegeek.com/xmlapi2/collection?username={}&wishlist=1&stats=1&showprivate=1&version=1'.format(auth_manager.username)
wishlisted_boardgames = get_boardgames_from_collection_url(url) wishlisted_boardgames = get_boardgames_from_collection_url(url)

2
pytest.ini Normal file
View file

@ -0,0 +1,2 @@
[pytest]
pythonpath = .

0
tests/__init__.py Normal file
View file

View file

@ -1,8 +1,11 @@
from fastapi.testclient import TestClient from fastapi.testclient import TestClient
from datetime import date from datetime import date
from .main import app from ..main import app
from classes.boardgame import BoardGame, CollectionBoardGame
#from classes.boardgame_classes import BoardGame, CollectionBoardGame
#from classes import boardgame_classes
from ..classes import boardgame_classes
client = TestClient(app) client = TestClient(app)
@ -15,7 +18,7 @@ def test_retrieve_boardgame():
response = client.get("/boardgame/373167") response = client.get("/boardgame/373167")
assert response.status_code == 200 assert response.status_code == 200
returned_boardgame = BoardGame(**response.json()) returned_boardgame = boardgame_classes.BoardGame(**response.json())
assert type(returned_boardgame.id) == int assert type(returned_boardgame.id) == int
assert type(returned_boardgame.name) == str assert type(returned_boardgame.name) == str
@ -24,7 +27,7 @@ def test_retrieve_collection():
response = client.get("/collection") response = client.get("/collection")
assert response.status_code == 200 assert response.status_code == 200
returned_boardgame = CollectionBoardGame(**response.json()[0]) returned_boardgame = boardgame_classes.CollectionBoardGame(**response.json()[0])
assert type(returned_boardgame.id) == int assert type(returned_boardgame.id) == int
assert type(returned_boardgame.name) == str assert type(returned_boardgame.name) == str