From 48c355fa94cc60e6298e49ed714dca5bfbdfa02e Mon Sep 17 00:00:00 2001 From: Georges-Antoine Assi Date: Sat, 5 Jul 2025 23:10:47 -0400 Subject: [PATCH] complete tests for tasks folder --- backend/tasks/tests/conftest.py | 93 +----- backend/tasks/tests/fixtures/Platforms.xml | 11 + .../tasks/tests/fixtures/sample_metadata.zip | Bin 0 -> 2597 bytes .../tests/fixtures/sample_metadata/Files.xml | 13 + .../tests/fixtures/sample_metadata/Mame.xml | 13 + .../fixtures/sample_metadata/Metadata.xml | 31 ++ .../sample_metadata_with_empty_elements.zip | Bin 0 -> 599 bytes backend/tasks/tests/test_scan_library.py | 50 +++ backend/tasks/tests/test_tasks.py | 300 ++++++++++++++++++ .../tests/test_update_launchbox_metadata.py | 44 +-- 10 files changed, 437 insertions(+), 118 deletions(-) create mode 100644 backend/tasks/tests/fixtures/Platforms.xml create mode 100644 backend/tasks/tests/fixtures/sample_metadata.zip create mode 100644 backend/tasks/tests/fixtures/sample_metadata/Files.xml create mode 100644 backend/tasks/tests/fixtures/sample_metadata/Mame.xml create mode 100644 backend/tasks/tests/fixtures/sample_metadata/Metadata.xml create mode 100644 backend/tasks/tests/fixtures/sample_metadata_with_empty_elements.zip create mode 100644 backend/tasks/tests/test_scan_library.py create mode 100644 backend/tasks/tests/test_tasks.py diff --git a/backend/tasks/tests/conftest.py b/backend/tasks/tests/conftest.py index a0dd61e9b..f0edd04cb 100644 --- a/backend/tasks/tests/conftest.py +++ b/backend/tasks/tests/conftest.py @@ -1,5 +1,4 @@ -import zipfile -from io import BytesIO +import os import pytest from tasks.update_launchbox_metadata import UpdateLaunchboxMetadataTask @@ -13,93 +12,11 @@ def task(): @pytest.fixture def sample_zip_content(): - """Create sample ZIP content with XML files""" - zip_buffer = BytesIO() + test_dir = os.path.dirname(__file__) + sample_path = os.path.join(test_dir, "fixtures", "sample_metadata.zip") - with zipfile.ZipFile(zip_buffer, "w") as zip_file: - # Add Platforms.xml - platforms_xml = """ - - - Nintendo 64 - Console - 1996-06-23 - - - PlayStation - Console - 1994-12-03 - -""" - zip_file.writestr("Platforms.xml", platforms_xml) - - # Add Metadata.xml - metadata_xml = """ - - - 67890 - Crash Bandicoot - PlayStation - 1996-09-09 - Platformer - - - 12345 - Super Mario 64 - Nintendo 64 - 1996-06-23 - Platformer - - - Super Mario 64 (USA) - 12345 - - - 12345 - super_mario_64.jpg - Cover - - - 12345 - super_mario_64_screenshot.jpg - Screenshot - -""" - zip_file.writestr("Metadata.xml", metadata_xml) - - # Add Mame.xml - mame_xml = """ - - - mario.zip - mario - Super Mario Bros. - - - pacman.zip - pacman - Pac-Man - -""" - zip_file.writestr("Mame.xml", mame_xml) - - # Add Files.xml - files_xml = """ - - - super_mario_64.z64 - ROM - 8388608 - - - crash_bandicoot.bin - ROM - 524288000 - -""" - zip_file.writestr("Files.xml", files_xml) - - return zip_buffer.getvalue() + with open(sample_path, "rb") as f: + return f.read() @pytest.fixture diff --git a/backend/tasks/tests/fixtures/Platforms.xml b/backend/tasks/tests/fixtures/Platforms.xml new file mode 100644 index 000000000..434defa64 --- /dev/null +++ b/backend/tasks/tests/fixtures/Platforms.xml @@ -0,0 +1,11 @@ + + + + + Console + + + Valid Platform + + + diff --git a/backend/tasks/tests/fixtures/sample_metadata.zip b/backend/tasks/tests/fixtures/sample_metadata.zip new file mode 100644 index 0000000000000000000000000000000000000000..a4fb8c6898bd9f88c33bf2f44cc20264e885e443 GIT binary patch literal 2597 zcmWIWW@Zs#-~d9w?N6f^kN_ux0)ty-PHM4UMQ%=M1vdjD%U4DQ2B2yNhR_gR2KL8K zGc!7#XJ)hj@#c-|Pn-?Z@YdBjcjo-&P@^lx0YM&Tz0didJmbARl#93U^45S&+aGWG z=&1t~EjYEFVbfis;-I%XL>d^*eDOZxyH#6H*F(4KysyrwGyUeq7grk@Z@k#R(G-s~J=Q$MII0ycq0>)D*MWbwN*)60Y&ycw|@)iYz(yJ|p3! ztK|n&Tb5gRG_Ad4a5V9S=)bb%ukXe_?l))k_~N1}eB%47kIKhPKTI|K)A4@y+;{gs zw$Efc>AQ-lXl1O>;@Pe?dsKJmeg7+YV`F>~dt0R6_5BvdIZx+AO-(tyMU! z!ZrUg_I|ESeAjbh+Wz_#{SwC~{BsBrU%v7iU3z6qt}A$uFpxi-f!{ed;a( z71jri=^L6~J*RKHUd2>jHU8k4BX06KOcPsH@~+hJ*jjw-D~jk~I0 zWnctVw*lU$#vxY>A}GfF1R6(FC5FXhP#KF{`f{L{yb%~BIEz`JX`pHb*_|NMkliVa zV%jn$6nBzhFen=%XK)@AgJXeN9&ZT?bSkKXMRqF4L}aImqnP-L1=XqeiU}-+gAy-t zvgSoGd?_n1q2tZSKqEmJ8QHxcBaz)JfnwxhHWc>~X*w%V7X!FKxB(P3>>wThp+{j# literal 0 HcmV?d00001 diff --git a/backend/tasks/tests/fixtures/sample_metadata/Files.xml b/backend/tasks/tests/fixtures/sample_metadata/Files.xml new file mode 100644 index 000000000..90de95749 --- /dev/null +++ b/backend/tasks/tests/fixtures/sample_metadata/Files.xml @@ -0,0 +1,13 @@ + + + + super_mario_64.z64 + ROM + 8388608 + + + crash_bandicoot.bin + ROM + 524288000 + + diff --git a/backend/tasks/tests/fixtures/sample_metadata/Mame.xml b/backend/tasks/tests/fixtures/sample_metadata/Mame.xml new file mode 100644 index 000000000..7cf2f7eec --- /dev/null +++ b/backend/tasks/tests/fixtures/sample_metadata/Mame.xml @@ -0,0 +1,13 @@ + + + + mario.zip + mario + Super Mario Bros. + + + pacman.zip + pacman + Pac-Man + + diff --git a/backend/tasks/tests/fixtures/sample_metadata/Metadata.xml b/backend/tasks/tests/fixtures/sample_metadata/Metadata.xml new file mode 100644 index 000000000..ba7b54ad2 --- /dev/null +++ b/backend/tasks/tests/fixtures/sample_metadata/Metadata.xml @@ -0,0 +1,31 @@ + + + + 67890 + Crash Bandicoot + PlayStation + 1996-09-09 + Platformer + + + 12345 + Super Mario 64 + Nintendo 64 + 1996-06-23 + Platformer + + + Super Mario 64 (USA) + 12345 + + + 12345 + super_mario_64.jpg + Cover + + + 12345 + super_mario_64_screenshot.jpg + Screenshot + + diff --git a/backend/tasks/tests/fixtures/sample_metadata_with_empty_elements.zip b/backend/tasks/tests/fixtures/sample_metadata_with_empty_elements.zip new file mode 100644 index 0000000000000000000000000000000000000000..571a760002ad4e479f617b48c86e5ebd1e443fa3 GIT binary patch literal 599 zcmWIWW@Zs#-~ht4JDx@{AOT(m1%`l}#FDi9qTFJ=irk#i3T_5QmamKq3_u+W451;s z4D8>ZXJ-5aVi3J~5rCh=FN#!<-|Nw)|NX!y&HoWcrNRS?P=UmkBH~XjPoby0Yxt&&s1e zEmxZT(pV%Q{57!mNTzc{l3!XElaJW`+7_Kn7u`RJGX!|EbL`RH*;@&86*tg%0XY36 z0rXRRysx9Pe{h7pUOds>`wR45a)M}bdO}jd2VbAC5By;r0S!zOB$(9&q!|kp1)6_! zOLj2zDJnV!sCgXcQF!JSe5gY}-1pr1tKK>qCp@oeYn;;eKI?tP+b=aCCF#WUiiAaF zUm758U*xstTNu#oi-9=6n~_O`84)AM{^Dhj0IGn1C5<2&zXw - - - - Console - - - Valid Platform - - -""" - zip_file.writestr("Platforms.xml", platforms_xml) + test_dir = os.path.dirname(__file__) + sample_path = os.path.join( + test_dir, "fixtures", "sample_metadata_with_empty_elements.zip" + ) - mock_super_run.return_value = zip_buffer.getvalue() + async with await anyio.open_file(sample_path, "rb") as f: + mock_super_run.return_value = await f.read() # Create a mock pipeline with async context manager support mock_pipe = AsyncMock() @@ -214,19 +204,13 @@ class TestUpdateLaunchboxMetadataTask: task, ): """Test handling when some XML files are missing from the ZIP""" - # Create ZIP with only Platforms.xml - zip_buffer = BytesIO() - with zipfile.ZipFile(zip_buffer, "w") as zip_file: - platforms_xml = """ - - - Test Platform - Console - -""" - zip_file.writestr("Platforms.xml", platforms_xml) + test_dir = os.path.dirname(__file__) + sample_path = os.path.join( + test_dir, "fixtures", "sample_metadata_with_empty_elements.zip" + ) - mock_super_run.return_value = zip_buffer.getvalue() + async with await anyio.open_file(sample_path, "rb") as f: + mock_super_run.return_value = await f.read() # Create a mock pipeline with async context manager support mock_pipe = AsyncMock()