From 8f87e77ee96d2e8cc6c9850bac33db651f7abf55 Mon Sep 17 00:00:00 2001 From: Rehan Date: Fri, 5 Jan 2024 18:30:44 -0500 Subject: [PATCH] tests --- VisioNomicon/__init__.py | 1 - assets/test/1.jpg | Bin 0 -> 33333 bytes assets/test/2.jpg | Bin 0 -> 33331 bytes tests/conftest.py | 29 +++++++++++++++++++++++++ tests/test_mapping.py | 45 +++++++++++++++++++++++++++++++++++++++ 5 files changed, 74 insertions(+), 1 deletion(-) create mode 100644 assets/test/1.jpg create mode 100644 assets/test/2.jpg create mode 100644 tests/conftest.py create mode 100644 tests/test_mapping.py diff --git a/VisioNomicon/__init__.py b/VisioNomicon/__init__.py index e08203e..e9707f0 100644 --- a/VisioNomicon/__init__.py +++ b/VisioNomicon/__init__.py @@ -6,4 +6,3 @@ __version__ = "0.1.1" __author__ = 'Rehan Rana' - diff --git a/assets/test/1.jpg b/assets/test/1.jpg new file mode 100644 index 0000000000000000000000000000000000000000..ba240875710b7db813a89acd717fbe2262c834d7 GIT binary patch literal 33333 zcmeIwIZPB$9LMqZW@g!CwP3hB8iz~4sNqssj4^anxIqq!3y(BZ+s7#5En_-kxcsCT4 zR~z#Um&<8B6&cp3u}e{;v>vGp*XWz#AM(qD-`a-T$2`qzLU(8>_HC++6pAiYU7GD; ziWSl}G+X>Esi>MQ!0rfij9*jAgtUN=T(3Q{Bs8(6 zq0teQ-qIH4>FA42$}g=AbcRouIB9as)M>GC)01bI(_Esx$~_T zE?&BP<@$~Go40P?xqI*agNKhEKk4l1?s@*=<*V0kdf&e5fB)g*r_W!$4t(?Hl0qH1 zvY}vqfoFzFCznq3BZ8rP(ef zTOna*$Z+!i}RK&UABD1 z%2kC$t9@&VOUf%MtExBDY}~SS+x8thch&FNyKn!2gNF_uX*zcN#K}{q&zwDX{zA*e zOP8-)ZM)unJ;i zY%tg#xpXU+qG_sT_veyIwY61Uvqi)Q1gGWN^Gicq3AK*U^oGXf*1*U_Z(mqJS%)(` zDyb*B-yiKqvcC&f_fN@&gN@|s7GqUu{XA6{sbZkIx2e82b$45R%u$8S9bf5=8?D`I6k14zW literal 0 HcmV?d00001 diff --git a/tests/conftest.py b/tests/conftest.py new file mode 100644 index 0000000..31f6749 --- /dev/null +++ b/tests/conftest.py @@ -0,0 +1,29 @@ +import pytest, shutil, json +from pathlib import Path + +@pytest.fixture +def asset_copy(tmp_path): + # Create a subdirectory for our assets. + assets_dir = tmp_path / "assets" + assets_dir.mkdir() + + # Your original assets directory. + original_assets_dir = Path("assets/test") # Convert string to Path + + # Copy your assets into the temporary directory before each test. + for item in original_assets_dir.iterdir(): + if item.is_dir(): + shutil.copytree(item, assets_dir / item.name) + else: + shutil.copy(item, assets_dir / item.name) + + # Create mapping + mapping = { + str(assets_dir / "1.jpg"): str(assets_dir / "blue.jpg"), + str(assets_dir / "2.jpg"): str(assets_dir / "green.jpg") + } + mapping_file = assets_dir / 'mapping.json' + with open(mapping_file, 'w') as f: + json.dump(mapping, f, indent=4) + + return assets_dir diff --git a/tests/test_mapping.py b/tests/test_mapping.py new file mode 100644 index 0000000..41a65ab --- /dev/null +++ b/tests/test_mapping.py @@ -0,0 +1,45 @@ +import filecmp, hashlib, os.path, pytest, json +from types import SimpleNamespace +from VisioNomicon.main import save_mapping, rename_from_mapping + +def test_mapping_save(asset_copy): + og_filepaths = [str(asset_copy / '1.jpg'), str(asset_copy / '2.jpg')] + new_filepaths = [str(asset_copy / 'blue.jpg'), str(asset_copy / 'green.jpg')] + + args = SimpleNamespace() + args.files = og_filepaths + args.output = str(asset_copy / 'mapping-test.json') + + save_mapping(args, new_filepaths) + + assert filecmp.cmp(args.output, asset_copy / 'mapping.json') + +def test_rename_from_mapping(asset_copy): + # test execution + checksums = [sha256sum(asset_copy / "1.jpg"), sha256sum(asset_copy / "2.jpg")] + rename_from_mapping(str(asset_copy / 'mapping.json')) + + assert os.path.isfile(asset_copy / "blue.jpg") + assert os.path.isfile(asset_copy / "green.jpg") + + rename_sums = [sha256sum(asset_copy / "blue.jpg"), sha256sum(asset_copy / "green.jpg")] + assert(checksums == rename_sums) + + # test undo + rename_from_mapping(str(asset_copy / 'mapping.json'), True) + checksums = [sha256sum(asset_copy / "1.jpg"), sha256sum(asset_copy / "2.jpg")] + + assert os.path.isfile(asset_copy / "1.jpg") + assert os.path.isfile(asset_copy / "2.jpg") + + assert(checksums == rename_sums) + +def sha256sum(filename): + h = hashlib.sha256() + b = bytearray(128*1024) + mv = memoryview(b) + with open(filename, 'rb', buffering=0) as f: + while n := f.readinto(mv): + h.update(mv[:n]) + return h.hexdigest() +