Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Georges-Antoine Assi committed Sep 14, 2023
1 parent bce48c7 commit 42976e1
Show file tree
Hide file tree
Showing 9 changed files with 42 additions and 27 deletions.
31 changes: 19 additions & 12 deletions backend/endpoints/rom.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,22 @@
from stat import S_IFREG
from stream_zip import ZIP_64, stream_zip # type: ignore[import]

from config import LIBRARY_BASE_PATH
from logger.logger import log
from models import Rom
from handler import dbh
from utils import fs, get_file_name_with_no_tags
from utils.fs import _rom_exists, build_artwork_path, build_upload_roms_path
from exceptions.fs_exceptions import RomNotFoundError, RomAlreadyExistsException
from utils.oauth import protected_route
from models import Rom
from config import LIBRARY_BASE_PATH
from utils import get_file_name_with_no_tags
from utils.fs import (
_rom_exists,
build_artwork_path,
build_upload_roms_path,
rename_rom,
get_cover,
get_screenshots,
remove_rom,
)

from .utils import CustomStreamingResponse

Expand Down Expand Up @@ -180,12 +188,12 @@ def roms(
async def update_rom(
request: Request,
id: int,
rename_as_igdb: bool = False,
artwork: Optional[UploadFile] = File(None),
) -> dict:
"""Updates rom details"""

data = await request.form()
rename_as_igdb: bool = data["renameAsIGDB"]

cleaned_data = {}
cleaned_data["igdb_id"] = data["igdb_id"]
Expand All @@ -205,26 +213,25 @@ async def update_rom(

try:
if file_name != db_rom.file_name:
fs.rename_rom(db_rom.platform_slug, db_rom.file_name, file_name)
rename_rom(db_rom.platform_slug, db_rom.file_name, file_name)
except RomAlreadyExistsException as e:
log.error(str(e))
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail=str(e)
)

cleaned_data["file_name_no_tags"] = get_file_name_with_no_tags(
cleaned_data["file_name"]
)
cleaned_data["file_name"] = file_name
cleaned_data["file_name_no_tags"] = get_file_name_with_no_tags(file_name)
cleaned_data.update(
fs.get_cover(
get_cover(
overwrite=True,
fs_slug=db_rom.platform_slug,
rom_name=cleaned_data["file_name_no_tags"],
url_cover=cleaned_data.get("url_cover", ""),
)
)
cleaned_data.update(
fs.get_screenshots(
get_screenshots(
fs_slug=db_rom.platform_slug,
rom_name=cleaned_data["file_name_no_tags"],
url_screenshots=cleaned_data.get("url_screenshots", []),
Expand Down Expand Up @@ -267,7 +274,7 @@ def _delete_single_rom(rom_id: int, delete_from_fs: bool = False):
if delete_from_fs:
log.info(f"Deleting {rom.file_name} from filesystem")
try:
fs.remove_rom(rom.platform_slug, rom.file_name)
remove_rom(rom.platform_slug, rom.file_name)
except RomNotFoundError as e:
error = f"Couldn't delete from filesystem: {str(e)}"
log.error(error)
Expand Down
6 changes: 3 additions & 3 deletions backend/endpoints/tests/test_rom.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,16 @@ def test_get_all_roms(access_token, rom):
assert body["items"][0]["id"] == rom.id


@patch("utils.fs.rename_rom")
@patch("endpoints.rom.rename_rom")
def test_update_rom(rename_rom, access_token, rom):
response = client.patch(
f"/roms/{rom.id}",
headers={"Authorization": f"Bearer {access_token}"},
params={"rename_as_igdb": True},
data={
"igdb_id": "236663",
"name": "Metroid Prime Remastered",
"slug": "metroid-prime-remastered",
"file_name": "Metroid Prime Remastered.xci",
"summary": "summary test",
"url_cover": "https://images.igdb.com/igdb/image/upload/t_cover_big/co2l7z.jpg",
"url_screenshots": json.dumps(
Expand All @@ -53,7 +53,7 @@ def test_update_rom(rename_rom, access_token, rom):
assert response.status_code == 200

body = response.json()
assert body["rom"]["file_name"] == "Metroid Prime Remastered.xci"
assert body["rom"]["file_name"] == "Metroid Prime Remastered.zip"

assert rename_rom.called

Expand Down
2 changes: 1 addition & 1 deletion backend/handler/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def rom(platform):
name="test_rom",
slug="test_rom_slug",
platform_slug="test_platform_slug",
file_name="test_rom",
file_name="test_rom.zip",
file_name_no_tags="test_rom",
file_extension="zip",
file_path="test_platform_slug/roms",
Expand Down
2 changes: 1 addition & 1 deletion backend/handler/tests/test_db_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def test_roms(rom):
assert len(roms) == 2

rom = dbh.get_rom(roms[0].id)
assert rom.file_name == "test_rom"
assert rom.file_name == "test_rom.zip"

dbh.update_rom(roms[1].id, {"file_name": "test_rom_2_updated"})
rom_2 = dbh.get_rom(roms[1].id)
Expand Down
2 changes: 1 addition & 1 deletion backend/handler/tests/test_igdb_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def test_get_platform():
assert platform["name"] == "Nintendo 64"

platform = igdbh.get_platform("not_real")
assert platform == {"igdb_id": "", "name": "not_real", "slug": "not_real"}
assert platform == {"igdb_id": "", "name": "not_real"}


@pytest.mark.vcr()
Expand Down
2 changes: 1 addition & 1 deletion backend/models/tests/test_rom.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
def test_rom(rom):
assert rom.file_path == "test_platform_slug/roms"
assert rom.full_path == "test_platform_slug/roms/test_rom"
assert rom.full_path == "test_platform_slug/roms/test_rom.zip"
7 changes: 5 additions & 2 deletions backend/utils/fs.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,13 @@ def _get_cover_path(fs_slug: str, rom_name: str, size: str):
return f"{fs_slug}/{rom_name}/cover/{size}.png?timestamp={strtime}"


def get_cover(overwrite: bool, fs_slug: str, rom_name: str, url_cover: str = "") -> dict:
def get_cover(
overwrite: bool, fs_slug: str, rom_name: str, url_cover: str = ""
) -> dict:
# Cover small
if (overwrite or not _cover_exists(fs_slug, rom_name, "small")) and url_cover:
_store_cover(fs_slug, rom_name, url_cover, "small")

path_cover_s = (
_get_cover_path(fs_slug, rom_name, "small")
if _cover_exists(fs_slug, rom_name, "small")
Expand Down Expand Up @@ -300,7 +303,7 @@ def remove_rom(fs_slug: str, file_name: str):
shutil.rmtree(f"{LIBRARY_BASE_PATH}/{rom_path}/{file_name}")
except FileNotFoundError as exc:
raise RomNotFoundError(file_name, fs_slug) from exc


def build_upload_roms_path(fs_slug: str):
rom_path = get_roms_structure(fs_slug)
Expand Down
6 changes: 3 additions & 3 deletions backend/utils/tests/test_fs.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ def test_get_cover():
rom_name="Paper Mario",
)

assert "n64/Paper Mario/cover/small.png" in cover["path_cover_s"]
assert "n64/Paper Mario/cover/big.png" in cover["path_cover_l"]
assert cover["has_cover"] == 1
assert DEFAULT_PATH_COVER_S in cover["path_cover_s"]
assert DEFAULT_PATH_COVER_L in cover["path_cover_l"]
assert cover["has_cover"] == 0

# Game: Paper Mario (USA).z64
cover = get_cover(
Expand Down
11 changes: 8 additions & 3 deletions frontend/src/services/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,11 @@ async function updateRom({ rom, renameAsIGDB = false }) {
formData.append("url_cover", rom.url_cover);
formData.append("summary", rom.summary);
formData.append("url_screenshots", JSON.stringify(rom.url_screenshots));
formData.append("renameAsIGDB", renameAsIGDB);
if (artwork) formData.append("artwork", rom.artwork[0]);

return api.patch(`/roms/${rom.id}`, formData);
return api.patch(`/roms/${rom.id}`, formData, {
params: { rename_as_igdb: renameAsIGDB },
});
}

async function deleteRom({ rom, deleteFromFs = false }) {
Expand All @@ -114,7 +115,11 @@ async function deleteRoms({ roms, deleteFromFs = false }) {
}

async function searchIGDB({ romId, query, field }) {
return api.put("/search/roms/igdb", {}, { params: { rom_id: romId, query, field } });
return api.put(
"/search/roms/igdb",
{},
{ params: { rom_id: romId, query, field } }
);
}

async function fetchCurrentUser() {
Expand Down

0 comments on commit 42976e1

Please sign in to comment.