Add tests for fs

This commit is contained in:
Georges-Antoine Assi
2023-08-01 15:27:59 -04:00
parent ace2ba448f
commit a965b39bcc
17 changed files with 85622 additions and 82229 deletions

View File

@@ -1,6 +1,7 @@
from handler.igdb_handler import IGDBHandler, TwitchAuth
import pytest
from handler.igdb_handler import IGDBHandler
igdbh = IGDBHandler()
@@ -14,6 +15,7 @@ def test_get_platform():
platform = igdbh.get_platform("not_real")
assert platform == {}
@pytest.mark.vcr()
def test_get_rom():
rom = igdbh.get_rom("Paper Mario (USA).n64", 4)
@@ -27,6 +29,7 @@ def test_get_rom():
rom = igdbh.get_rom("Not a real game title", 4)
assert rom == {}
@pytest.mark.vcr()
def test_get_rom_by_id():
rom = igdbh.get_rom_by_id(3340)
@@ -40,6 +43,7 @@ def test_get_rom_by_id():
rom = igdbh.get_rom_by_id(-1)
assert rom == {}
@pytest.mark.vcr()
def test_get_matched_roms_by_id():
roms = igdbh.get_matched_roms_by_id(3340)
@@ -49,6 +53,7 @@ def test_get_matched_roms_by_id():
assert roms[0]["r_slug"] == "paper-mario"
assert "t_cover_big" in roms[0]["url_cover"]
@pytest.mark.vcr()
def test_get_matched_roms_by_name():
roms = igdbh.get_matched_roms_by_name("Mario", 4)
@@ -65,6 +70,7 @@ def test_get_matched_roms_by_name():
roms = igdbh.get_matched_roms_by_name("Notarealgametitle", 4)
assert roms == []
@pytest.mark.vcr()
def test_get_matched_roms():
roms = igdbh.get_matched_roms("Paper Mario (USA).n64", 4)

Binary file not shown.

Before

Width:  |  Height:  |  Size: 26 KiB

After

Width:  |  Height:  |  Size: 66 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 4.8 KiB

After

Width:  |  Height:  |  Size: 10 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.0 MiB

After

Width:  |  Height:  |  Size: 1.5 MiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.5 MiB

After

Width:  |  Height:  |  Size: 2.0 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 51 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.8 KiB

View File

@@ -47,16 +47,24 @@ def scan_rom(
rom_attrs.update(
fs.get_cover(
overwrite, platform.slug, rom_attrs["file_name"], rom_attrs["url_cover"]
overwrite=overwrite,
p_slug=platform.slug,
file_name=rom_attrs["file_name"],
url_cover=rom_attrs["url_cover"],
)
)
rom_attrs.update(
fs.get_screenshots(
platform.slug, rom_attrs["file_name"], rom_attrs["url_screenshots"]
p_slug=platform.slug,
file_name=rom_attrs["file_name"],
url_screenshots=rom_attrs["url_screenshots"],
)
)
file_size, file_size_units = fs.get_rom_size(
rom_attrs["multi"], rom_attrs["file_name"], rom_attrs["files"], roms_path
multi=rom_attrs["multi"],
file_name=rom_attrs["file_name"],
multi_files=rom_attrs["files"],
roms_path=roms_path,
)
reg, rev, other_tags = parse_tags(rom_attrs["file_name"])
rom_attrs.update(

View File

@@ -47,8 +47,8 @@ def _store_cover(p_slug: str, file_name: str, url_cover: str, size: str):
url_cover: url to get the cover
size: size of the cover -> big | small
"""
cover_file: str = f"{size}.png"
cover_path: str = f"{RESOURCES_BASE_PATH}/{p_slug}/{file_name}/cover"
cover_file = f"{size}.png"
cover_path = f"{RESOURCES_BASE_PATH}/{p_slug}/{file_name}/cover"
res = requests.get(
url_cover.replace("t_thumb", f"t_cover_{size}"), stream=True, timeout=120
)
@@ -70,7 +70,7 @@ def _get_cover_path(p_slug: str, file_name: str, size: str):
return f"{p_slug}/{file_name}/cover/{size}.png?timestamp={strtime}"
def get_cover(overwrite: bool, p_slug: str, file_name: str, url_cover: str):
def get_cover(overwrite: bool, p_slug: str, file_name: str, url_cover: str = None):
# Cover small
if (overwrite or not _cover_exists(p_slug, file_name, "small")) and url_cover:
_store_cover(p_slug, file_name, url_cover, "small")
@@ -79,6 +79,7 @@ def get_cover(overwrite: bool, p_slug: str, file_name: str, url_cover: str):
if _cover_exists(p_slug, file_name, "small")
else DEFAULT_PATH_COVER_S
)
# Cover big
if (overwrite or not _cover_exists(p_slug, file_name, "big")) and url_cover:
_store_cover(p_slug, file_name, url_cover, "big")
@@ -87,6 +88,7 @@ def get_cover(overwrite: bool, p_slug: str, file_name: str, url_cover: str):
if _cover_exists(p_slug, file_name, "big")
else (DEFAULT_PATH_COVER_L, 0)
)
return {
"path_cover_s": path_cover_s,
"path_cover_l": path_cover_l,
@@ -246,11 +248,14 @@ def get_roms(p_slug: str):
]
def get_rom_size(multi: bool, rom: str, files: list, roms_path: str):
files: list = (
[f"{LIBRARY_BASE_PATH}/{roms_path}/{rom}"]
def get_rom_size(roms_path: str, file_name: str, multi: bool, multi_files: list = []):
files = (
[f"{LIBRARY_BASE_PATH}/{roms_path}/{file_name}"]
if not multi
else [f"{LIBRARY_BASE_PATH}/{roms_path}/{rom}/{file}" for file in files]
else [
f"{LIBRARY_BASE_PATH}/{roms_path}/{file_name}/{file}"
for file in multi_files
]
)
total_size: float = 0.0
for file in files:

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@@ -14,7 +14,7 @@ def test_scan_platform():
assert platform.slug == "n64"
assert platform.name == "Nintendo 64"
assert platform.igdb_id == 4
assert platform.n_roms == 1
assert platform.n_roms == 2
try:
platform = scan_platform("")
@@ -38,8 +38,8 @@ def test_scan_rom():
assert rom.file_name == "Paper Mario (USA).z64"
assert rom.r_name == "Paper Mario"
assert rom.r_igdb_id == 3340
assert rom.file_size == 0.0
assert rom.file_size_units == "B"
assert rom.file_size == 1.0
assert rom.file_size_units == "KB"
assert rom.files == ["Paper Mario (USA).z64"]
assert rom.tags == []
assert not rom.multi

View File

@@ -0,0 +1,108 @@
import pytest
from utils.fs import (
get_cover,
get_platforms,
get_roms_structure,
get_rom_files,
get_roms,
get_rom_size,
rename_rom,
remove_rom,
)
from config import (
DEFAULT_PATH_COVER_L,
DEFAULT_PATH_COVER_S,
)
@pytest.mark.vcr
def test_get_cover():
cover = get_cover(
overwrite=False,
p_slug="n64",
file_name="Paper Mario (USA).z64",
)
assert "n64/Paper Mario (USA).z64/cover/small.png" in cover["path_cover_s"]
assert "n64/Paper Mario (USA).z64/cover/big.png" in cover["path_cover_l"]
assert cover["has_cover"] == 1
cover = get_cover(
overwrite=True,
p_slug="n64",
file_name="Paper Mario (USA).z64",
url_cover="https://images.igdb.com/igdb/image/upload/t_thumb/co1qda.png",
)
assert "n64/Paper Mario (USA).z64/cover/small.png" in cover["path_cover_s"]
assert "n64/Paper Mario (USA).z64/cover/big.png" in cover["path_cover_l"]
assert cover["has_cover"] == 1
cover = get_cover(
overwrite=False,
p_slug="n64",
file_name="Super Mario 64 (USA).z64",
url_cover="https://images.igdb.com/igdb/image/upload/t_thumb/co6cl1.png",
)
assert "n64/Super Mario 64 (USA).z64/cover/small.png" in cover["path_cover_s"]
assert "n64/Super Mario 64 (USA).z64/cover/big.png" in cover["path_cover_l"]
assert cover["has_cover"] == 1
cover = get_cover(
overwrite=False,
p_slug="n64",
file_name="Fake Game.xyz",
)
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
def test_get_platforms():
platforms = get_platforms()
assert "n64" in platforms
assert "psx" in platforms
def test_get_roms_structure():
roms_structure = get_roms_structure(p_slug="n64")
assert roms_structure == "n64/roms"
def test_get_roms():
roms = get_roms(p_slug="n64")
assert len(roms) == 2
assert roms[0]["file_name"] == "Paper Mario (USA).z64"
assert not roms[0]["multi"]
assert roms[1]["file_name"] == "Super Mario 64 (J) (Rev A)"
assert roms[1]["multi"]
def test_rom_size():
rom_size = get_rom_size(
roms_path=get_roms_structure(p_slug="n64"),
file_name="Paper Mario (USA).z64",
multi=False,
)
assert rom_size == (1.0, "KB")
rom_size = get_rom_size(
roms_path=get_roms_structure(p_slug="n64"),
file_name="Super Mario 64 (J) (Rev A)",
multi=True,
multi_files=[
"Super Mario 64 (J) (Rev A) [Part 1].z64",
"Super Mario 64 (J) (Rev A) [Part 2].z64",
],
)
assert rom_size == (2.0, "KB")