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 0000000..ba24087 Binary files /dev/null and b/assets/test/1.jpg differ diff --git a/assets/test/2.jpg b/assets/test/2.jpg new file mode 100644 index 0000000..af5d48e Binary files /dev/null and b/assets/test/2.jpg differ 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() +