Skip to content

Commit 28c6df3

Browse files
committed
test: add wanted check for testcase.
1 parent 977a3d8 commit 28c6df3

File tree

1 file changed

+26
-3
lines changed

1 file changed

+26
-3
lines changed

tests/test_regression.py

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1+
import hashlib
12
import sys
23

34
from path import Path
45

56
sys.path.insert(0, ".")
67

7-
88
from pdf_white_cut.logger import logger
99
from pdf_white_cut.worker import cut_pdf
1010

@@ -21,15 +21,21 @@ def __init__(self, input: str, ignore: int = 0) -> None:
2121
self.ignore = ignore
2222

2323
@property
24-
def input_path(self):
24+
def input_path(self) -> Path:
2525
return Path("./{}".format(self.input))
2626

2727
@property
28-
def output_path(self):
28+
def output_path(self) -> Path:
2929
output = Path("./output/{}".format(self.input))
3030
output.dirname().makedirs_p()
3131
return output
3232

33+
@property
34+
def wanted_path(self) -> Path:
35+
d, base = self.input_path.dirname(), self.input_path.basename()
36+
wanted = d / "wanted" / base
37+
return wanted
38+
3339

3440
def test_regression() -> None:
3541
cases = [
@@ -52,9 +58,26 @@ def test_bugfix_cut_again() -> None:
5258
cut_pdf(case.input_path, case.output_path, case.ignore)
5359

5460

61+
def check_case(case: Case):
62+
if case.wanted_path.exists():
63+
logger.info("assert for case {} {}", case.output_path, case.wanted_path)
64+
# TODO: hash compare is not a good solution
65+
return file_hash(case.output_path) == file_hash(case.wanted_path)
66+
return True
67+
68+
69+
def file_hash(filepath, hash_func=hashlib.md5):
70+
hasher = hash_func()
71+
with open(filepath, 'rb') as f:
72+
for chunk in iter(lambda: f.read(4096), b""): # 逐块读取
73+
hasher.update(chunk)
74+
return hasher.hexdigest()
75+
76+
5577
def test_bugfix_rotate() -> None:
5678
base = Path("cases/bugfix/rotate")
5779
cases = [Case(i) for i in base.files("*.pdf")]
5880

5981
for case in cases:
6082
cut_pdf(case.input_path, case.output_path, case.ignore)
83+
check_case(case)

0 commit comments

Comments
 (0)