improve tests

This commit is contained in:
Sina Atalay 2023-10-20 19:44:55 +02:00
parent d1d5be586d
commit 36fd2b9d4b
3 changed files with 84 additions and 53 deletions

74
tests/test_cli.py Normal file
View File

@ -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")

View File

@ -796,10 +796,7 @@ class TestDataModel(unittest.TestCase):
os.remove(path_to_generated_schema) os.remove(path_to_generated_schema)
# Read the repository's current JSON schema: # Read the repository's current JSON schema:
path_to_schema = os.path.join( path_to_schema = os.path.join(os.path.dirname(tests_directory), "schema.json")
os.path.dirname(tests_directory),
"schema.json"
)
with open(path_to_schema, "r") as f: with open(path_to_schema, "r") as f:
current_json_schema = json.load(f) current_json_schema = json.load(f)

View File

@ -470,10 +470,14 @@ class TestRendering(unittest.TestCase):
reference_pdf_file = pdf_file.replace("_test.pdf", "_reference.pdf") reference_pdf_file = pdf_file.replace("_test.pdf", "_reference.pdf")
reference_pdf_file_size = os.path.getsize(reference_pdf_file) reference_pdf_file_size = os.path.getsize(reference_pdf_file)
pdf_file_size = os.path.getsize(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( ratio = min(reference_pdf_file_size, pdf_file_size) / max(
reference_pdf_file_size, pdf_file_size 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( nonexistent_latex_file_path = os.path.join(
os.path.dirname(__file__), "reference_files", "nonexistent.tex" 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." FileNotFoundError, msg="File not found error didn't raise."
): ):
rendering.run_latex(nonexistent_latex_file_path) 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,
)