-
Notifications
You must be signed in to change notification settings - Fork 3
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
140 additions
and
181 deletions.
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
This file was deleted.
Oops, something went wrong.
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
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,76 @@ | ||
import unittest | ||
import shutil | ||
|
||
from pathlib import Path | ||
|
||
from gouda.engines import ZbarEngine | ||
from gouda.scripts.decode_barcodes import main | ||
|
||
from utils import temp_directory_with_files | ||
|
||
|
||
TESTDATA = Path(__file__).parent.joinpath('test_data') | ||
|
||
|
||
@unittest.skipUnless(ZbarEngine.available(), 'ZbarEngine unavailable') | ||
class TestRename(unittest.TestCase): | ||
def test_rename(self): | ||
"File is renamed with value of barcode" | ||
with temp_directory_with_files(TESTDATA.joinpath('code128.png')) as tempdir: | ||
main(['zbar', '--action=rename', unicode(tempdir)]) | ||
self.assertEqual( | ||
['Stegosaurus.png'], | ||
[path.name for path in sorted(tempdir.iterdir())] | ||
) | ||
|
||
def test_rename_multiple(self): | ||
"File with multiple barcodes results in renamed / copied to three files" | ||
with temp_directory_with_files(TESTDATA.joinpath('BM001128287.jpg')) as tempdir: | ||
main(['zbar', '--action=rename', unicode(tempdir)]) | ||
self.assertEqual( | ||
['BM001128286.jpg', 'BM001128287.jpg', 'BM001128288.jpg'], | ||
[path.name for path in sorted(tempdir.iterdir())] | ||
) | ||
|
||
def test_rename_with_collisions(self): | ||
"Files with same barcode values results in just a single rename" | ||
with temp_directory_with_files(TESTDATA.joinpath('code128.png')) as tempdir: | ||
shutil.copy( | ||
unicode(TESTDATA.joinpath('code128.png')), | ||
unicode(tempdir.joinpath('first copy.png')) | ||
) | ||
|
||
shutil.copy( | ||
unicode(TESTDATA.joinpath('code128.png')), | ||
unicode(tempdir.joinpath('second copy.png')) | ||
) | ||
|
||
main(['zbar', '--action=rename', unicode(tempdir)]) | ||
self.assertEqual( | ||
['Stegosaurus.png', 'first copy.png', 'second copy.png'], | ||
[path.name for path in sorted(tempdir.iterdir())] | ||
) | ||
|
||
def test_rename_avoid_collisions(self): | ||
"Files with same barcode values results in new files with suffixes" | ||
with temp_directory_with_files(TESTDATA.joinpath('code128.png')) as tempdir: | ||
shutil.copy( | ||
unicode(TESTDATA.joinpath('code128.png')), | ||
unicode(tempdir.joinpath('first copy.png')) | ||
) | ||
|
||
shutil.copy( | ||
unicode(TESTDATA.joinpath('code128.png')), | ||
unicode(tempdir.joinpath('second copy.png')) | ||
) | ||
|
||
main(['zbar', '--action=rename', unicode(tempdir), '--avoid-collisions']) | ||
print([path.name for path in sorted(tempdir.iterdir())]) | ||
self.assertEqual( | ||
['Stegosaurus-1.png', 'Stegosaurus-2.png', 'Stegosaurus.png'], | ||
[path.name for path in sorted(tempdir.iterdir())] | ||
) | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |
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,20 @@ | ||
import shutil | ||
import tempfile | ||
|
||
from contextlib import contextmanager | ||
from pathlib import Path | ||
|
||
|
||
@contextmanager | ||
def temp_directory_with_files(*paths): | ||
"""A context manager that creates a temporary directory and copies all | ||
paths to it. The temporary directory is unlinked when the context is exited. | ||
""" | ||
temp = tempfile.mkdtemp() | ||
try: | ||
temp = Path(temp) | ||
for p in paths: | ||
shutil.copy(str(p), str(temp / Path(p).name)) | ||
yield temp | ||
finally: | ||
shutil.rmtree(str(temp)) |