rendercv/tests/test_rendering.py

300 lines
11 KiB
Python
Raw Normal View History

2023-10-08 16:16:54 +00:00
import unittest
import os
from datetime import date
import shutil
from rendercv import rendering, data_model
2023-10-14 18:43:41 +00:00
class TestRendering(unittest.TestCase):
2023-10-12 17:42:15 +00:00
def test_markdown_to_latex(self):
input = "[link](www.example.com)"
expected = r"\href{www.example.com}{link}"
2023-10-12 17:42:15 +00:00
output = rendering.markdown_to_latex(input)
with self.subTest(msg="only one link"):
self.assertEqual(output, expected)
input = "[link](www.example.com) and [link2](www.example2.com)"
expected = (
2023-11-18 15:25:20 +00:00
r"\href{www.example.com}{link} and" r" \href{www.example2.com}{link2}"
2023-10-12 17:42:15 +00:00
)
output = rendering.markdown_to_latex(input)
with self.subTest(msg="two links"):
self.assertEqual(output, expected)
input = "[**link**](www.example.com)"
expected = r"\href{www.example.com}{\textbf{link}}"
2023-10-12 17:42:15 +00:00
output = rendering.markdown_to_latex(input)
with self.subTest(msg="bold link"):
self.assertEqual(output, expected)
input = "[*link*](www.example.com)"
expected = r"\href{www.example.com}{\textit{link}}"
2023-10-12 17:42:15 +00:00
output = rendering.markdown_to_latex(input)
with self.subTest(msg="italic link"):
self.assertEqual(output, expected)
input = "[*link*](www.example.com) and [**link2**](www.example2.com)"
expected = (
r"\href{www.example.com}{\textit{link}} and"
r" \href{www.example2.com}{\textbf{link2}}"
2023-10-12 17:42:15 +00:00
)
output = rendering.markdown_to_latex(input)
with self.subTest(msg="italic and bold links"):
self.assertEqual(output, expected)
input = "**bold**, *italic*, and [link](www.example.com)"
expected = (
2023-11-18 15:25:20 +00:00
r"\textbf{bold}, \textit{italic}, and" r" \href{www.example.com}{link}"
2023-10-12 17:42:15 +00:00
)
output = rendering.markdown_to_latex(input)
with self.subTest(msg="bold, italic, and link"):
self.assertEqual(output, expected)
# invalid input:
input = 20
with self.subTest(msg="float input"):
with self.assertRaises(ValueError):
2023-10-13 20:32:23 +00:00
rendering.markdown_to_latex(input) # type: ignore
2023-10-12 17:42:15 +00:00
def test_markdown_link_to_url(self):
input = "[link](www.example.com)"
expected = "www.example.com"
output = rendering.markdown_link_to_url(input)
with self.subTest(msg="only one link"):
self.assertEqual(output, expected)
input = "[**link**](www.example.com)"
expected = "www.example.com"
output = rendering.markdown_link_to_url(input)
with self.subTest(msg="bold link"):
self.assertEqual(output, expected)
input = "[*link*](www.example.com)"
expected = "www.example.com"
output = rendering.markdown_link_to_url(input)
with self.subTest(msg="italic link"):
self.assertEqual(output, expected)
# invalid input:
input = 20
with self.subTest(msg="float input"):
with self.assertRaises(ValueError):
2023-10-13 20:32:23 +00:00
rendering.markdown_link_to_url(input) # type: ignore
2023-10-12 17:42:15 +00:00
input = "not a markdown link"
with self.subTest(msg="invalid input"):
with self.assertRaises(ValueError):
rendering.markdown_link_to_url(input)
input = "[]()"
with self.subTest(msg="empty link"):
with self.assertRaises(ValueError):
rendering.markdown_link_to_url(input)
2023-10-18 17:33:52 +00:00
def test_make_it_something(self):
# invalid input:
input = "test"
keyword = "invalid keyword"
with self.subTest(msg="invalid keyword"):
with self.assertRaises(ValueError):
rendering.make_it_something(input, keyword)
2023-10-12 17:42:15 +00:00
def test_make_it_bold(self):
input = "some text"
expected = r"\textbf{some text}"
output = rendering.make_it_bold(input)
with self.subTest(msg="without match_str input"):
self.assertEqual(output, expected)
match_str = "text"
expected = r"some \textbf{text}"
output = rendering.make_it_bold(input, match_str)
with self.subTest(msg="with match_str input"):
self.assertEqual(output, expected)
2023-10-13 20:32:23 +00:00
match_str = 2423
with self.subTest(msg="invalid match_str input"):
with self.assertRaises(ValueError):
rendering.make_it_bold(input, match_str) # type: ignore
2023-10-12 17:42:15 +00:00
input = 20
with self.subTest(msg="float input"):
with self.assertRaises(ValueError):
2023-10-13 20:32:23 +00:00
rendering.make_it_bold(input) # type: ignore
2023-10-12 17:42:15 +00:00
def test_make_it_underlined(self):
input = "some text"
expected = r"\underline{some text}"
output = rendering.make_it_underlined(input)
with self.subTest(msg="without match_str input"):
self.assertEqual(output, expected)
input = "some text"
match_str = "text"
expected = r"some \underline{text}"
output = rendering.make_it_underlined(input, match_str)
with self.subTest(msg="with match_str input"):
self.assertEqual(output, expected)
input = 20
with self.subTest(msg="float input"):
with self.assertRaises(ValueError):
2023-10-13 20:32:23 +00:00
rendering.make_it_underlined(input) # type: ignore
2023-10-12 17:42:15 +00:00
def test_make_it_italic(self):
input = "some text"
expected = r"\textit{some text}"
output = rendering.make_it_italic(input)
with self.subTest(msg="without match_str input"):
self.assertEqual(output, expected)
input = "some text"
match_str = "text"
expected = r"some \textit{text}"
output = rendering.make_it_italic(input, match_str)
with self.subTest(msg="with match_str input"):
self.assertEqual(output, expected)
input = 20
with self.subTest(msg="float input"):
with self.assertRaises(ValueError):
2023-10-13 20:32:23 +00:00
rendering.make_it_italic(input) # type: ignore
2023-10-12 17:42:15 +00:00
def test_divide_length_by(self):
lengths = [
"10cm",
"10.24in",
"10 pt",
"10.24 mm",
"10.24 em",
"1024 ex",
]
divider = 10
expected = [
"1.0 cm",
"1.024 in",
"1.0 pt",
"1.024 mm",
"1.024 em",
"102.4 ex",
]
for length, exp in zip(lengths, expected):
2023-10-12 18:09:01 +00:00
with self.subTest(length=length, msg="valid input"):
2023-10-12 17:42:15 +00:00
self.assertEqual(rendering.divide_length_by(length, divider), exp)
def test_get_today(self):
2023-11-29 17:08:32 +00:00
expected = date.today().strftime("%B, %Y")
2023-10-12 17:42:15 +00:00
result = rendering.get_today()
2023-10-12 18:09:01 +00:00
self.assertEqual(expected, result, msg="Today's date is not correct.")
2023-10-12 17:42:15 +00:00
def test_get_path_to_font_directory(self):
font_name = "test"
expected = os.path.join(
os.path.dirname(os.path.dirname(__file__)),
"rendercv",
"templates",
"fonts",
font_name,
)
result = rendering.get_path_to_font_directory(font_name)
2023-10-12 18:09:01 +00:00
self.assertEqual(expected, result, msg="Font directory path is not correct.")
2023-10-12 17:42:15 +00:00
def test_render_template(self):
2023-11-18 15:25:20 +00:00
# Read the reference YAML file:
input_file_path = os.path.join(
os.path.dirname(__file__),
"reference_files",
"John_Doe_CV_yaml_reference.yaml",
)
data = data_model.read_input_file(input_file_path)
2023-10-18 17:33:52 +00:00
output_file_path = rendering.render_template(
data=data, output_path=os.path.dirname(__file__)
)
2023-10-12 17:42:15 +00:00
# Check if the output file exists:
2023-10-12 18:09:01 +00:00
self.assertTrue(
os.path.exists(output_file_path), msg="LaTeX file couldn't be generated."
)
2023-10-12 17:42:15 +00:00
# Compare the output file with the reference file:
reference_file_path = os.path.join(
2023-11-18 15:25:20 +00:00
os.path.dirname(__file__),
"reference_files",
"John_Doe_CV_tex_reference.tex",
2023-10-12 17:42:15 +00:00
)
with open(output_file_path, "r") as file:
output = file.read()
with open(reference_file_path, "r") as file:
reference = file.read()
reference = reference.replace("REPLACETHISWITHTODAY", rendering.get_today())
2023-10-12 18:09:01 +00:00
self.assertEqual(
output, reference, msg="LaTeX file didn't match the reference."
)
2023-10-12 17:42:15 +00:00
# Check if the font directory exists:
2023-10-18 17:33:52 +00:00
output_folder_path = os.path.dirname(output_file_path)
2023-10-12 17:42:15 +00:00
font_directory_path = os.path.join(output_folder_path, "fonts")
2023-10-12 18:09:01 +00:00
self.assertTrue(
os.path.exists(font_directory_path), msg="Font directory doesn't exist."
)
2023-10-12 17:42:15 +00:00
required_files = [
2023-11-18 15:25:20 +00:00
f"{data.design.font}-Italic.ttf",
f"{data.design.font}-Regular.ttf",
f"{data.design.font}-Bold.ttf",
f"{data.design.font}-BoldItalic.ttf",
2023-10-12 17:42:15 +00:00
]
font_files = os.listdir(font_directory_path)
for required_file in required_files:
with self.subTest(required_file=required_file):
2023-10-12 18:09:01 +00:00
self.assertIn(
required_file,
font_files,
msg=f"Font file ({required_file}) is missing.",
)
2023-10-12 17:42:15 +00:00
# Remove the output directory:
shutil.rmtree(output_folder_path)
2023-10-08 16:16:54 +00:00
2023-10-09 18:06:07 +00:00
def test_run_latex(self):
latex_file_path = os.path.join(
2023-11-18 15:25:20 +00:00
os.path.dirname(__file__),
"reference_files",
"John_Doe_CV_tex_reference.tex",
2023-10-09 18:06:07 +00:00
)
2023-10-08 17:15:42 +00:00
2023-10-12 16:08:00 +00:00
with self.subTest(msg="Existent file name"):
pdf_file = rendering.run_latex(latex_file_path)
# Check if the output file exists:
2023-10-12 18:09:01 +00:00
self.assertTrue(
os.path.exists(pdf_file), msg="PDF file couldn't be generated."
)
2023-10-12 16:08:00 +00:00
2023-10-12 17:42:15 +00:00
# Compare the pdf file with the reference pdf file:
2023-11-18 15:25:20 +00:00
reference_pdf_file = pdf_file.replace(
"_tex_reference.pdf", "_pdf_reference.pdf"
)
2023-10-12 18:09:01 +00:00
reference_pdf_file_size = os.path.getsize(reference_pdf_file)
pdf_file_size = os.path.getsize(pdf_file)
2023-10-20 17:44:55 +00:00
# Remove the output file:
os.remove(pdf_file)
2023-10-12 18:09:01 +00:00
ratio = min(reference_pdf_file_size, pdf_file_size) / max(
reference_pdf_file_size, pdf_file_size
2023-10-12 17:42:15 +00:00
)
2023-10-20 17:44:55 +00:00
self.assertTrue(ratio > 0.98, msg="PDF file didn't match the reference.")
2023-10-12 17:42:15 +00:00
2023-10-12 16:08:00 +00:00
nonexistent_latex_file_path = os.path.join(
os.path.dirname(__file__), "reference_files", "nonexistent.tex"
2023-10-11 16:01:36 +00:00
)
2023-10-12 16:08:00 +00:00
with self.subTest(msg="Nonexistent file name"):
2023-10-12 18:09:01 +00:00
with self.assertRaises(
FileNotFoundError, msg="File not found error didn't raise."
):
2023-10-12 16:08:00 +00:00
rendering.run_latex(nonexistent_latex_file_path)