Commit 4654785a authored by Alexander Gehrke's avatar Alexander Gehrke

Add test for files.py and fix corresponding bugs

Also: 100% test coverage 馃帀
parent 5ac9ebd8
......@@ -35,7 +35,7 @@ def imread_bin(path, white_is_fg=False):
return np.logical_not(bin)
def match_filenames(base_files: List[str], *file_lists: str) -> Tuple[bool, Optional[str]]:
def match_filenames(base_files: List[str], *file_lists: List[str]) -> Tuple[bool, Optional[str]]:
"""
Compares filenames in given lists to check if they match up. This requires all filenames in file_lists to start with
the basename of the corresponding element in base_file. If the file names do not match, or the lists have different
......@@ -63,10 +63,10 @@ def glob_all(filenames):
def split_filename(image) -> Tuple[str, str, str]:
image = os.path.basename(image)
dir = os.path.dirname(image)
base, ext = image.split(".", 1)
return dir, base, ext
base = os.path.basename(image)
root, ext = base.split(".", 1)
return dir, root, ext
def random_indices(lst) -> List[int]:
......
from ocr4all.files import imread, imread_bin, match_filenames, glob_all, split_filename, random_indices, chunks
from tests.samples import *
from tests.utils import same_elements
def test_imread():
with temp_image(SAMPLE_A_IMAGE) as image:
assert np.array_equal(imread(image), SAMPLE_A_IMAGE)
assert np.array_equal(imread(image, as_gray=True), SAMPLE_A_IMAGE_GRAY)
def test_imread_bin():
with temp_image(SAMPLE_BINARY_MASK, mode='1') as binimage:
assert np.array_equal(imread_bin(binimage, white_is_fg=True), SAMPLE_BINARY_MASK)
assert np.array_equal(imread_bin(binimage), np.logical_not(SAMPLE_BINARY_MASK))
def test_match_filenames():
err1, wrong_length = match_filenames(['a'], ['a', 'b'], ['a', 'b', 'c'])
assert not err1
assert "length" in wrong_length
err2, mismatch = match_filenames(
['foo.png', 'bar.png', 'baz.png'],
['foo.png', 'qux.png', 'baz.png']
)
assert not err2
assert "mismatch" in mismatch
assert 'bar.png' in mismatch and 'qux.png' in mismatch
success, no_error = match_filenames(
['foo.png', 'bar.png', 'baz.png'],
['foo.bin.png', 'bar.bin.png', 'baz.bin.png']
)
assert success and no_error is None
success, no_error = match_filenames(
['foo.bin.png', 'bar.bin.png', 'baz.bin.png'],
['foo.bin.norm', 'bar.bin.norm', 'baz.bin.norm']
)
assert success and no_error is None
success, no_error = match_filenames(
['foo.png', 'bar.png', 'baz.png'],
['foo.bin.norm', 'bar.bin.norm', 'baz.bin.norm']
)
assert success and no_error is None
def test_glob_all():
with TempDirectory() as d:
def in_tempdir(files):
return [os.path.join(d.path, pat) for pat in files]
pngs = ['a.png', 'b.png', 'c.png', 'd.png', 'e.png']
txts = ['1.txt', '2.txt', '3.txt']
miscs = ['foo.iso', 'bar.json']
for f in pngs + txts + miscs:
d.write(f, b'')
assert same_elements(in_tempdir(pngs + txts), glob_all(in_tempdir(['*.png', '*.txt'])))
assert same_elements(in_tempdir(pngs + txts + miscs + txts), glob_all(in_tempdir(['*.*', '*.txt'])))
assert len(glob_all(['*.nonexistent'])) == 0
def test_split_filename():
assert split_filename("foo/bar/baz.bin.png") == ("foo/bar", "baz", "bin.png")
def test_random_indices():
input = ['a', 'b', 'c', 'd']
indices = list(range(4))
for _ in range(5):
assert same_elements(indices, random_indices(input))
def test_chunks():
input = list(range(10))
assert list(chunks(input, 1)) == [[i] for i in input]
assert list(chunks(input, 2)) == [[0, 1], [2, 3], [4, 5], [6, 7], [8, 9]]
assert list(chunks(input, 3)) == [[0, 1, 2], [3, 4, 5], [6, 7, 8], [9]]
assert list(chunks(input, 10)) == [input]
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment