Skip to content

Commit

Permalink
tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rehanzo committed Jan 5, 2024
1 parent 1ad8289 commit 8f87e77
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 1 deletion.
1 change: 0 additions & 1 deletion VisioNomicon/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,3 @@

__version__ = "0.1.1"
__author__ = 'Rehan Rana'

Binary file added assets/test/1.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/test/2.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
29 changes: 29 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -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
45 changes: 45 additions & 0 deletions tests/test_mapping.py
Original file line number Diff line number Diff line change
@@ -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()

0 comments on commit 8f87e77

Please sign in to comment.