From 00257a32511ae5243f4e5c5b40c86883ef572fbd Mon Sep 17 00:00:00 2001 From: Yarne Coppens Date: Fri, 2 Aug 2024 09:52:06 +0200 Subject: [PATCH] Reorganized modules. Created pytest config to make testing work --- classes/__init__.py | 0 .../{boardgame.py => boardgame_classes.py} | 0 main.py | 17 +++++----- modules/__init__.py | 0 auth_manager.py => modules/auth_manager.py | 0 .../bgg_connection.py | 32 ++++++++++--------- pytest.ini | 2 ++ tests/__init__.py | 0 test_main.py => tests/test_main.py | 11 ++++--- 9 files changed, 35 insertions(+), 27 deletions(-) create mode 100644 classes/__init__.py rename classes/{boardgame.py => boardgame_classes.py} (100%) create mode 100644 modules/__init__.py rename auth_manager.py => modules/auth_manager.py (100%) rename bgg_connection.py => modules/bgg_connection.py (84%) create mode 100644 pytest.ini create mode 100644 tests/__init__.py rename test_main.py => tests/test_main.py (71%) diff --git a/classes/__init__.py b/classes/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/classes/boardgame.py b/classes/boardgame_classes.py similarity index 100% rename from classes/boardgame.py rename to classes/boardgame_classes.py diff --git a/main.py b/main.py index 8129413..313787f 100644 --- a/main.py +++ b/main.py @@ -1,8 +1,9 @@ from typing import Union from fastapi import FastAPI -from classes.boardgame import BoardGame, BoardGameExpansion, CollectionBoardGame, CollectionBoardGameExpansion, WishlistBoardGame -from bgg_connection import get_boardgame, get_user_owned_collection, get_user_wishlist_collection +from classes import boardgame_classes +from modules import bgg_connection + app = FastAPI() @@ -11,18 +12,18 @@ app = FastAPI() def read_root(): 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): - requested_boardgame: BoardGame = get_boardgame(boardgame_id) + requested_boardgame: boardgame_classes.BoardGame = bgg_connection.get_boardgame(boardgame_id) return requested_boardgame -@app.get("/collection", response_model=list[CollectionBoardGame]) +@app.get("/collection", response_model=list[boardgame_classes.CollectionBoardGame]) 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 -@app.get("/wishlist", response_model=list[WishlistBoardGame]) +@app.get("/wishlist", response_model=list[boardgame_classes.WishlistBoardGame]) 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 \ No newline at end of file diff --git a/modules/__init__.py b/modules/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/auth_manager.py b/modules/auth_manager.py similarity index 100% rename from auth_manager.py rename to modules/auth_manager.py diff --git a/bgg_connection.py b/modules/bgg_connection.py similarity index 84% rename from bgg_connection.py rename to modules/bgg_connection.py index 80043df..be9924b 100644 --- a/bgg_connection.py +++ b/modules/bgg_connection.py @@ -4,8 +4,10 @@ from pydantic import HttpUrl import requests from datetime import datetime -from classes.boardgame import BoardGame, BoardGameExpansion, CollectionBoardGame, CollectionBoardGameExpansion, WishlistBoardGame, BoardgameType -import auth_manager +from classes import boardgame_classes +from modules import auth_manager + +#from classes.boardgame import BoardGame, BoardGameExpansion, CollectionBoardGame, CollectionBoardGameExpansion, WishlistBoardGame, BoardgameType authenticated_session: requests.Session = requests.Session() @@ -15,7 +17,7 @@ def url_to_xml_object(url: HttpUrl) -> ET.Element: root = ET.fromstring(r.content) 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) boardgame_xml_object : ET.Element = url_to_xml_object(url) @@ -23,14 +25,14 @@ def get_boardgame(boardgame_id: int) -> 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): for i in range(0, len(list_to_divide), 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 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 -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') @@ -77,15 +79,15 @@ def convert_xml_to_boardgame(boardgame_xml: ET.Element) -> BoardGame: match boardgame_type: case "boardgame": - boardgame = BoardGame(**boardgame_dict) + boardgame = boardgame_classes.BoardGame(**boardgame_dict) case "boardgameexpansion": - boardgame = BoardGameExpansion(**boardgame_dict) + boardgame = boardgame_classes.BoardGameExpansion(**boardgame_dict) 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') @@ -129,19 +131,19 @@ def convert_collection_xml_to_boardgame(boardgame_extra_info: BoardGame, collect match boardgame_type: case "boardgame": - boardgame = CollectionBoardGame(**boardgame_dict) + boardgame = boardgame_classes.CollectionBoardGame(**boardgame_dict) case "boardgameexpansion": - boardgame = CollectionBoardGameExpansion(**boardgame_dict) + boardgame = boardgame_classes.CollectionBoardGameExpansion(**boardgame_dict) return boardgame #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_list: list[BoardGame] = [] + collection_list: list[boardgame_classes.BoardGame] = [] collection_id_list: list[int] = [] @@ -164,7 +166,7 @@ def get_boardgames_from_collection_url(collection_url: str) -> list[BoardGame]: 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) owned_boardgames = get_boardgames_from_collection_url(url_no_expansions) @@ -175,7 +177,7 @@ def get_user_owned_collection() -> list[BoardGame]: 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) wishlisted_boardgames = get_boardgames_from_collection_url(url) diff --git a/pytest.ini b/pytest.ini new file mode 100644 index 0000000..03f586d --- /dev/null +++ b/pytest.ini @@ -0,0 +1,2 @@ +[pytest] +pythonpath = . \ No newline at end of file diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/test_main.py b/tests/test_main.py similarity index 71% rename from test_main.py rename to tests/test_main.py index 92e0e92..f76c793 100644 --- a/test_main.py +++ b/tests/test_main.py @@ -1,8 +1,11 @@ from fastapi.testclient import TestClient from datetime import date -from .main import app -from classes.boardgame import BoardGame, CollectionBoardGame +from ..main import app + +#from classes.boardgame_classes import BoardGame, CollectionBoardGame +#from classes import boardgame_classes +from ..classes import boardgame_classes client = TestClient(app) @@ -15,7 +18,7 @@ def test_retrieve_boardgame(): response = client.get("/boardgame/373167") 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.name) == str @@ -24,7 +27,7 @@ def test_retrieve_collection(): response = client.get("/collection") 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.name) == str