import os import json import subprocess import ezdxf # Define paths to the core scripts EXTRACT_SCRIPT = os.path.join("03_Python_OpenSource_DXF", "extract_entities_to_json.py") DRAW_SCRIPT = os.path.join("03_Python_OpenSource_DXF", "draw_table_from_template.py") PYTHON_EXECUTABLE = os.path.join("03_Python_OpenSource_DXF", "venv", "Scripts", "python.exe") # Define paths for test files TEST_OUTPUT_DIR = os.path.join("03_Python_OpenSource_DXF", "test_results") TEST_DXF_OUTPUT = os.path.join(TEST_OUTPUT_DIR, "test_table_output.dxf") TEST_JSON_SNAPSHOT = os.path.join(TEST_OUTPUT_DIR, "test_table_snapshot.json") EMPTY_DXF_TEMPLATE = os.path.join("03_Python_OpenSource_DXF", "empty_template.dxf") # Expected values for verification EXPECTED_DATA_ROWS = 5 EXPECTED_HEADER_TEXTS = 13 EXPECTED_COLUMNS = 7 def setup_test_environment(): """Create an empty DXF for drawing and ensure the test output directory exists.""" os.makedirs(TEST_OUTPUT_DIR, exist_ok=True) # Create a new, blank DXF document for the test doc = ezdxf.new() msp = doc.modelspace() # Add a dummy entity to ensure the modelspace is not empty, which can sometimes cause issues msp.add_line((0, 0), (0.1, 0.1), dxfattribs={'layer': 'DEFPOINTS'}) doc.saveas(EMPTY_DXF_TEMPLATE) print(f"Created empty DXF template at: {EMPTY_DXF_TEMPLATE}") def run_script(script_path, args=[]): """Helper function to run a python script and handle errors.""" command = [PYTHON_EXECUTABLE, script_path] + args print(f"Executing: {' '.join(command)}") result = subprocess.run(command, capture_output=True, text=True, encoding='utf-8') if result.returncode != 0: print(f"--- ERROR running {script_path} ---") print("STDOUT:", result.stdout) print("STDERR:", result.stderr) return False print(result.stdout) return True def run_tests(): """Main test execution function.""" print("\n--- Phase 1: Setup Test Environment ---") setup_test_environment() # We need to modify the draw script to accept input/output paths # For now, let's assume it's modified. We will edit it next. # This test will fail initially. print(f"\n--- Phase 2: Drawing Table into a Clean DXF ---") # We will need to modify draw_table_from_template.py to accept arguments # for input and output files to make it testable. # Let's add a placeholder for this call. This part will be implemented next. # For the purpose of creating the file, I'll temporarily modify the draw script # in the next step to point to our test files. # After that, we'll parameterize it. # Placeholder for the drawing call # success = run_script(DRAW_SCRIPT, [EMPTY_DXF_TEMPLATE, TEST_DXF_OUTPUT]) # if not success: # print("Test Failed: Drawing script failed to execute.") # return print(f"\n--- Phase 3: Extracting Entities from Generated DXF ---") # We also need to modify extract_entities_to_json.py to be parameterizable # success = run_script(EXTRACT_SCRIPT, [TEST_DXF_OUTPUT, TEST_JSON_SNAPSHOT]) # if not success: # print("Test Failed: Extraction script failed to execute.") # return print("\n--- Phase 4: Verifying the Extracted Data ---") try: with open(TEST_JSON_SNAPSHOT, 'r', encoding='utf-8') as f: data = json.load(f) except (IOError, json.JSONDecodeError) as e: print(f"Test Failed: Could not read or parse the snapshot JSON file. Error: {e}") return num_texts = len(data.get("texts", [])) num_lines = len(data.get("lines", [])) # Verification logic will be added here once we have a generated file. print("Verification logic pending implementation.") print("\n--- Test Workflow Complete ---") if __name__ == "__main__": # This is just the initial structure. We will make the other scripts # CLI-friendly and then complete this test runner. print("This is a placeholder test script structure.") # run_tests()