Skip to content

Commit

Permalink
working phash lib
Browse files Browse the repository at this point in the history
  • Loading branch information
bjlittle committed May 26, 2022
1 parent 2b48aff commit 7c35990
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 23 deletions.
2 changes: 1 addition & 1 deletion pytest_mpl/kernels.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from PIL import Image

#: The default hamming distance bit tolerance for "similar" imagehash hashes.
DEFAULT_HAMMING_TOLERANCE = 2
DEFAULT_HAMMING_TOLERANCE = 4

#: The default imagehash hash size (N), resulting in a hash of N**2 bits.
DEFAULT_HASH_SIZE = 16
Expand Down
4 changes: 4 additions & 0 deletions tests/baseline/hashes/test_phash.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"test.test_gen": "8fe8c01f7a45b01f6645ac1fe9572b2a807e2ae0d41a7ab085967aac87997eab",
"pytest-mpl-kernel": "phash"
}
7 changes: 6 additions & 1 deletion tests/test_kernels.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@
# --XX-----------X
HASH_4BIT = "0100456789abcdee"

#: baseline with 8-bit delta (64-bit)
# -X------------XX
HASH_8BIT = "0023456789abcd00"


#: Absolute path to test baseline image
baseline_image = Path(__file__).parent / "baseline" / "2.0.x" / "test_base_style.png"
Expand Down Expand Up @@ -97,7 +101,8 @@ def test_phash_option():
[
(HASH_BASE, True, 0),
(HASH_2BIT, True, 2),
(HASH_4BIT, False, 4),
(HASH_4BIT, True, 4),
(HASH_8BIT, False, 8),
(HASH_BASE_32, False, None),
],
)
Expand Down
26 changes: 5 additions & 21 deletions tests/test_pytest_mpl.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
fail_hash_library = Path(__file__).parent / "baseline" / "test_hash_lib.json"
baseline_dir_abs = Path(__file__).parent / "baseline" / baseline_subdir
hash_baseline_dir_abs = Path(__file__).parent / "baseline" / "hybrid"
phash_library = Path(__file__).parent / "baseline" / "hashes" / "test_phash.json"


WIN = sys.platform.startswith('win')
Expand Down Expand Up @@ -491,31 +492,14 @@ def test_results_always(tmpdir):
assert json_res[json_image_key] is None


def test_phash(tmpdir, capsys):
def test_phash(tmpdir):
test_file = tmpdir.join("test.py").strpath
with open(test_file, "w") as fo:
fo.write(TEST_GENERATE)

results_dir = tmpdir.mkdir("foo").strpath
gen_dir = tmpdir.mkdir("bar").strpath
hash_file = "test_phash.json"

code = call_pytest([f"--mpl-generate-path={gen_dir}", test_file])
assert code == 0
assert os.path.exists(os.path.join(gen_dir, "test_gen.png"))

command = [f"--mpl-generate-hash-library={hash_file}",
"--mpl-results-always",
f"--mpl-results-path={results_dir}",
command = ["--mpl",
"--mpl-kernel=phash",
f"--mpl-hash-library={phash_library}",
test_file]
code = call_pytest(command)
hash_file = os.path.join(results_dir, hash_file)
assert os.path.exists(os.path.join(hash_file))

with open(hash_file) as fi:
hash_lib = json.load(fi)

with capsys.disabled():
from pprint import pprint
pprint(hash_lib)
assert code == 0

0 comments on commit 7c35990

Please sign in to comment.