From 844408c6e328fab95d2b6c4f922c2b0de410be3d Mon Sep 17 00:00:00 2001 From: Ariel Sosnovsky Date: Wed, 23 Jan 2019 22:06:12 -0500 Subject: [PATCH] - ensured that all tests are working - added a main test script --- bin/run_tests.sh | 9 ++++++ pdfmajor/converters/html.py | 2 +- pdfmajor/converters/writers/json.py | 6 ++-- tests/test_conversions.py | 50 ++++++++++++++--------------- tests/test_image_writer.py | 9 +++--- tests/test_interpreter.py | 25 ++++++++------- tests/test_json_writer.py | 1 - tests/test_yml_writer.py | 19 ++++++++--- 8 files changed, 71 insertions(+), 50 deletions(-) create mode 100755 bin/run_tests.sh diff --git a/bin/run_tests.sh b/bin/run_tests.sh new file mode 100755 index 0000000..b7d45aa --- /dev/null +++ b/bin/run_tests.sh @@ -0,0 +1,9 @@ +#!/bin/bash +# Get working dir (this will make sure that the script will work the same anywhere you run it) +WK_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" +cd $WK_DIR +cd .. + +for filename in ./tests/test*.py; do + python $filename +done \ No newline at end of file diff --git a/pdfmajor/converters/html.py b/pdfmajor/converters/html.py index 72e16e8..a9f2381 100644 --- a/pdfmajor/converters/html.py +++ b/pdfmajor/converters/html.py @@ -166,7 +166,7 @@ def create_svg_path(item: LTCurve): if item.height > 0: dy = 1.0/item.height - dx = (item.width/item.height)/(item.x1-item.x0) + dx = (item.width/item.height)/max([(item.x1-item.x0),0.01]) svg_attr.update({ "viewBox": "0 0 %s %s" % ( item.width/item.height, 1 ) }) diff --git a/pdfmajor/converters/writers/json.py b/pdfmajor/converters/writers/json.py index 94a2bb7..4315800 100644 --- a/pdfmajor/converters/writers/json.py +++ b/pdfmajor/converters/writers/json.py @@ -50,7 +50,8 @@ class JSONMakerObject: self.write_prop(key, val) def string(self, key: str, val: str): - self.write_prop(key, f'"{val}"') + val_sant = str(val).replace('"', '\\"') + self.write_prop(key, f'"{val_sant}"') @contextmanager def object(self, key: str): @@ -109,7 +110,8 @@ class JSONMakerArray: self.write_prop(val) def string(self, val: str): - self.write_prop(f'"{val}"') + val_sant = str(val).replace('"', '\\"') + self.write_prop(f'"{val_sant}"') @contextmanager def object(self): diff --git a/tests/test_conversions.py b/tests/test_conversions.py index bfa1466..0fae8c9 100755 --- a/tests/test_conversions.py +++ b/tests/test_conversions.py @@ -22,30 +22,28 @@ if not os.path.exists(OUTPUT_FOLDER): files = os.listdir(INPUT_FOLDER) TOTAL = len(files)*len(EXTS) -waiter = tqdm(total=TOTAL) -for idx, file_name in enumerate(files): - file_name_no_ext = re.sub(r'\.pdf$',"", file_name) - input_file_path = os.path.join(INPUT_FOLDER, file_name) +with tqdm(total=TOTAL) as waiter: + for idx, file_name in enumerate(files): + file_name_no_ext = re.sub(r'\.pdf$',"", file_name) + input_file_path = os.path.join(INPUT_FOLDER, file_name) - for ext in EXTS: - if not os.path.exists(os.path.join(OUTPUT_FOLDER, ext)): - os.makedirs(os.path.join(OUTPUT_FOLDER, ext)) - output_file_path = os.path.join(OUTPUT_FOLDER, ext, file_name_no_ext + "." + ext) - if len(file_name) > len(file_name_no_ext): - waiter.set_description(f"Processing {file_name}") - convert_file( - input_file=input_file_path, - output_file=output_file_path, - out_type=ext, - debug_level=logging.WARN, - ) - if ext == 'xml': - waiter.set_description(f" > testing format {file_name}") - ElementTree.parse(output_file_path) - elif ext == 'json': - waiter.set_description(f" > testing format {file_name}") - json_load(open(output_file_path, 'r')) - waiter.refresh() - waiter.update() - waiter.set_description("Done") -waiter.close() \ No newline at end of file + for ext in EXTS: + if not os.path.exists(os.path.join(OUTPUT_FOLDER, ext)): + os.makedirs(os.path.join(OUTPUT_FOLDER, ext)) + output_file_path = os.path.join(OUTPUT_FOLDER, ext, file_name_no_ext + "." + ext) + if len(file_name) > len(file_name_no_ext): + waiter.set_description(f"Conversion test -- {file_name}") + convert_file( + input_file=input_file_path, + output_file=output_file_path, + out_type=ext, + debug_level=logging.WARN, + ) + if ext == 'xml': + waiter.set_description(f" > testing format {file_name}") + ElementTree.parse(output_file_path) + elif ext == 'json': + waiter.set_description(f" > testing format {file_name}") + json_load(open(output_file_path, 'r')) + waiter.update() + waiter.set_description("Done") \ No newline at end of file diff --git a/tests/test_image_writer.py b/tests/test_image_writer.py index 18b1de2..776ec50 100644 --- a/tests/test_image_writer.py +++ b/tests/test_image_writer.py @@ -1,4 +1,5 @@ import os +from tqdm import tqdm from pdfmajor.interpreter import LTImage, logging from pdfmajor.interpreter import PageInterpreter, PDFInterpreter @@ -18,16 +19,16 @@ OUTPUT_FOLDER = os.path.join( if not os.path.exists(OUTPUT_FOLDER): os.makedirs(OUTPUT_FOLDER) -for file_name in FILES: +for file_name in tqdm(FILES, desc='Image Writer Test'): for page in PDFInterpreter(os.path.join( CUR_PATH, - f"samples/pdf/{file_name}.pdf" + f"tests/samples/pdf/{file_name}.pdf" ), debug_level=logging.ERROR): assert isinstance(page, PageInterpreter) for item_idx, item in enumerate(page): assert isinstance(item, LTItem) if isinstance(item, LTImage): - print(item.save_image( + item.save_image( OUTPUT_FOLDER, file_name=f'{file_name}_p{page.page_num}i{item_idx}' - )) \ No newline at end of file + ) \ No newline at end of file diff --git a/tests/test_interpreter.py b/tests/test_interpreter.py index 9ae79c3..002a96c 100644 --- a/tests/test_interpreter.py +++ b/tests/test_interpreter.py @@ -1,21 +1,22 @@ import os - -# from pdfmajor.extractor import extract_items_from_pdf +from tqdm import tqdm from pdfmajor.interpreter import LTCharBlock, PDFInterpreter, logging from pdfmajor.interpreter import PageInterpreter from pdfmajor.interpreter.commands import LTItem from pdfmajor.interpreter.commands import LTCharBlock, LTChar CUR_PATH = os.path.dirname(os.path.dirname(__file__)) -FILE_NAME = os.path.join( - CUR_PATH, - "samples/pdf/tables.pdf" +INPUT_FOLDER = os.path.join( + CUR_PATH, "tests/samples/pdf" ) +FILES = os.listdir(INPUT_FOLDER) -for page in PDFInterpreter(FILE_NAME, debug_level=logging.INFO): - assert isinstance(page, PageInterpreter) - for item in page: - assert isinstance(item, LTItem) - if isinstance(item, LTCharBlock): - for char in item: - assert(char, LTChar) \ No newline at end of file +for file_name in tqdm(FILES, desc="Interpreter Test"): + file_path = os.path.join(INPUT_FOLDER, file_name) + for page in PDFInterpreter(file_path, debug_level=logging.ERROR): + assert isinstance(page, PageInterpreter) + for item in page: + assert isinstance(item, LTItem) + if isinstance(item, LTCharBlock): + for char in item: + assert isinstance(char, LTChar) \ No newline at end of file diff --git a/tests/test_json_writer.py b/tests/test_json_writer.py index c3642ca..93ac009 100644 --- a/tests/test_json_writer.py +++ b/tests/test_json_writer.py @@ -80,7 +80,6 @@ with JSONMaker(FILE_NAME) as obj: item.number("x1", 4.2) item.number("y0", 231.12) item.number("y1", 331.12) -print(open(FILE_NAME, 'r').read()) assert(open(FILE_NAME, 'r').read() == """{ "pages": [ { diff --git a/tests/test_yml_writer.py b/tests/test_yml_writer.py index cffa1a3..591ff53 100644 --- a/tests/test_yml_writer.py +++ b/tests/test_yml_writer.py @@ -20,7 +20,6 @@ with YAMLMaker(FILE_NAME) as yml: toys.write("bear") with yml.object("computer") as computer: computer.write("type", "awesome") -print(open(FILE_NAME, 'r').read()) assert(open(FILE_NAME, 'r').read() == """name: bob age: 20 wife: @@ -48,7 +47,6 @@ with YAMLMaker(FILE_NAME) as yml: "lion", "bear" ]) yml.place_object("computer", {"type": "awesome"}) -print(open(FILE_NAME, 'r').read()) assert(open(FILE_NAME, 'r').read() == """name: bob age: 20 wife: @@ -81,8 +79,21 @@ with YAMLMaker(FILE_NAME) as yml: curve.write("y0", 0) curve.write("y1", 1) -print(open(FILE_NAME, 'r').read()) -assert(open(FILE_NAME, 'r').read() == """""") +assert(open(FILE_NAME, 'r').read() == """pages: + - id: 0 + width: 100 + height: 200 + children: + - stroke: + type: DeviceRGB + value: ['1', '2', '3'] + x0: 0 + x1: 1 + fill: + type: DeviceRGB + value: ['1', '2', '3'] + y0: 0 + y1: 1""")