diff --git a/tests/test_cli.py b/tests/test_cli.py new file mode 100644 index 0000000..49eca56 --- /dev/null +++ b/tests/test_cli.py @@ -0,0 +1,74 @@ +import unittest +import os +import json +from datetime import date +import shutil +import subprocess +import sys + +from rendercv import rendering, data_model + + +class TestCLI(unittest.TestCase): + def test_render(self): + # Change the working directory to the root of the project: + workspace_path = os.path.dirname(os.path.dirname(__file__)) + + test_input_file_path = os.path.join( + workspace_path, "tests", "reference_files", "John_Doe_CV_test.yaml" + ) + subprocess.run( + [sys.executable, "-m", "rendercv", "render", test_input_file_path], + check=True, + ) + + # Read the necessary information and remove the output directory: + output_file_path = os.path.join(workspace_path, "output", "John_Doe_CV.pdf") + pdf_file_size = os.path.getsize(output_file_path) + file_exists = os.path.exists(output_file_path) + shutil.rmtree(os.path.join(workspace_path, "output")) + + # Check if the output file exists: + self.assertTrue(file_exists, msg="PDF file couldn't be generated.") + + # Compare the pdf file with the reference pdf file: + reference_pdf_file = os.path.join( + workspace_path, "tests", "reference_files", "John_Doe_CV_reference.pdf" + ) + reference_pdf_file_size = os.path.getsize(reference_pdf_file) + ratio = min(reference_pdf_file_size, pdf_file_size) / max( + reference_pdf_file_size, pdf_file_size + ) + self.assertTrue(ratio > 0.98, msg="PDF file didn't match the reference.") + + # Wrong input: + with self.subTest(msg="Wrong input"): + with self.assertRaises(subprocess.CalledProcessError): + subprocess.run( + [ + sys.executable, + "-m", + "rendercv", + "wrong_input.yaml", + ], + check=True, + ) + + def test_new(self): + # Change the working directory to the root of the project: + workspace_path = os.path.dirname(os.path.dirname(__file__)) + + subprocess.run( + [sys.executable, "-m", "rendercv", "new", "John Doe"], + check=True, + ) + output_file_path = os.path.join(workspace_path, "John_Doe_CV.yaml") + + model: data_model.RenderCVDataModel = rendering.read_input_file( + output_file_path + ) + + # Remove the output file: + os.remove(output_file_path) + + self.assertTrue(model.cv.name == "John Doe") diff --git a/tests/test_data_model.py b/tests/test_data_model.py index c5f0449..4ffbf5b 100644 --- a/tests/test_data_model.py +++ b/tests/test_data_model.py @@ -791,17 +791,14 @@ class TestDataModel(unittest.TestCase): # Read the generated JSON schema: with open(path_to_generated_schema, "r") as f: generated_json_schema = json.load(f) - + # Remove the generated JSON schema: os.remove(path_to_generated_schema) - + # Read the repository's current JSON schema: - path_to_schema = os.path.join( - os.path.dirname(tests_directory), - "schema.json" - ) + path_to_schema = os.path.join(os.path.dirname(tests_directory), "schema.json") with open(path_to_schema, "r") as f: current_json_schema = json.load(f) - + # Compare the two JSON schemas: - self.assertEqual(generated_json_schema, current_json_schema) \ No newline at end of file + self.assertEqual(generated_json_schema, current_json_schema) diff --git a/tests/test_rendering.py b/tests/test_rendering.py index 6ba11e8..58451db 100644 --- a/tests/test_rendering.py +++ b/tests/test_rendering.py @@ -470,10 +470,14 @@ class TestRendering(unittest.TestCase): reference_pdf_file = pdf_file.replace("_test.pdf", "_reference.pdf") reference_pdf_file_size = os.path.getsize(reference_pdf_file) pdf_file_size = os.path.getsize(pdf_file) + + # Remove the output file: + os.remove(pdf_file) + ratio = min(reference_pdf_file_size, pdf_file_size) / max( reference_pdf_file_size, pdf_file_size ) - self.assertTrue(ratio > 0.99, msg="PDF file didn't match the reference.") + self.assertTrue(ratio > 0.98, msg="PDF file didn't match the reference.") nonexistent_latex_file_path = os.path.join( os.path.dirname(__file__), "reference_files", "nonexistent.tex" @@ -484,47 +488,3 @@ class TestRendering(unittest.TestCase): FileNotFoundError, msg="File not found error didn't raise." ): rendering.run_latex(nonexistent_latex_file_path) - - def test_entry_point(self): - # Change the working directory to the root of the project: - workspace_path = os.path.dirname(os.path.dirname(__file__)) - - test_input_file_path = os.path.join( - workspace_path, "tests", "reference_files", "John_Doe_CV_test.yaml" - ) - subprocess.run( - [sys.executable, "-m", "rendercv", test_input_file_path], - check=True, - ) - - # Read the necessary information and remove the output directory: - output_file_path = os.path.join(workspace_path, "output", "John_Doe_CV.pdf") - pdf_file_size = os.path.getsize(output_file_path) - file_exists = os.path.exists(output_file_path) - shutil.rmtree(os.path.join(workspace_path, "output")) - - # Check if the output file exists: - self.assertTrue(file_exists, msg="PDF file couldn't be generated.") - - # Compare the pdf file with the reference pdf file: - reference_pdf_file = os.path.join( - workspace_path, "tests", "reference_files", "John_Doe_CV_reference.pdf" - ) - reference_pdf_file_size = os.path.getsize(reference_pdf_file) - ratio = min(reference_pdf_file_size, pdf_file_size) / max( - reference_pdf_file_size, pdf_file_size - ) - self.assertTrue(ratio > 0.99, msg="PDF file didn't match the reference.") - - # Wrong input: - with self.subTest(msg="Wrong input"): - with self.assertRaises(subprocess.CalledProcessError): - subprocess.run( - [ - sys.executable, - "-m", - "rendercv", - "wrong_input.yaml", - ], - check=True, - )