-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
74 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,4 +6,3 @@ | |
|
||
__version__ = "0.1.1" | ||
__author__ = 'Rehan Rana' | ||
|
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
|