Improve API

This commit is contained in:
Sina Atalay
2025-02-08 15:50:01 -05:00
parent 5c69b04c83
commit c6428adcd8
8 changed files with 700 additions and 87 deletions

View File

@@ -3,6 +3,7 @@
import copy
import filecmp
import itertools
import json
import pathlib
import shutil
import typing
@@ -157,6 +158,17 @@ def rendercv_data_model() -> data.RenderCVDataModel:
return data.create_a_sample_data_model()
@pytest.fixture
def rendercv_data_as_python_dictionary(
rendercv_data_model,
) -> dict:
"""Return a sample RenderCV data as a Python dictionary."""
data_model_as_json = rendercv_data_model.model_dump_json(
exclude_none=False, by_alias=True, exclude={"cv": {"sections", "photo"}}
)
return json.loads(data_model_as_json)
@pytest.fixture
def rendercv_empty_curriculum_vitae_data_model() -> data.CurriculumVitae:
"""Return an empty CurriculumVitae data model."""

View File

@@ -1,6 +1,134 @@
import pathlib
import tempfile
import rendercv
import rendercv.data
def test_create_contents_of_a_typst_file(input_file_path):
yaml_string = input_file_path.read_text()
assert isinstance(rendercv.create_contents_of_a_typst_file(yaml_string), str)
assert isinstance(
rendercv.create_contents_of_a_typst_file_from_a_yaml_string(yaml_string), str
)
def test_create_contents_of_a_typst_file_with_errors(
rendercv_data_as_python_dictionary,
):
rendercv_data_as_python_dictionary["cv"]["email"] = "invalid-email"
yaml_string = rendercv.data.generator.dictionary_to_yaml(
rendercv_data_as_python_dictionary
)
assert isinstance(
rendercv.create_contents_of_a_typst_file_from_a_yaml_string(yaml_string), list
)
def test_create_a_typst_file_from_a_yaml_string(input_file_path):
yaml_string = input_file_path.read_text()
output_file_path = pathlib.Path(tempfile.mktemp(suffix=".typst"))
errors = rendercv.create_a_typst_file_from_a_yaml_string(
yaml_string, output_file_path
)
assert errors is None
assert output_file_path.exists()
def test_create_a_typst_file_from_a_python_dictionary(
rendercv_data_as_python_dictionary,
):
output_file_path = pathlib.Path(tempfile.mktemp(suffix=".typst"))
errors = rendercv.create_a_typst_file_from_a_python_dictionary(
rendercv_data_as_python_dictionary, output_file_path
)
assert errors is None
assert output_file_path.exists()
def test_create_contents_of_a_markdown_file_from_a_yaml_string(input_file_path):
yaml_string = input_file_path.read_text()
result = rendercv.create_contents_of_a_markdown_file_from_a_yaml_string(yaml_string)
assert isinstance(result, str)
def test_create_contents_of_a_markdown_file_from_a_python_dictionary(
rendercv_data_as_python_dictionary,
):
result = rendercv.create_contents_of_a_markdown_file_from_a_python_dictionary(
rendercv_data_as_python_dictionary
)
assert isinstance(result, str)
def test_create_a_markdown_file_from_a_yaml_string(input_file_path):
yaml_string = input_file_path.read_text()
output_file_path = pathlib.Path(tempfile.mktemp(suffix=".md"))
errors = rendercv.create_a_markdown_file_from_a_yaml_string(
yaml_string, output_file_path
)
assert errors is None
assert output_file_path.exists()
def test_create_a_markdown_file_from_a_python_dictionary(
rendercv_data_as_python_dictionary,
):
output_file_path = pathlib.Path(tempfile.mktemp(suffix=".md"))
errors = rendercv.create_a_markdown_file_from_a_python_dictionary(
rendercv_data_as_python_dictionary, output_file_path
)
assert errors is None
assert output_file_path.exists()
def test_create_an_html_file_from_a_yaml_string(input_file_path):
yaml_string = input_file_path.read_text()
output_file_path = pathlib.Path(tempfile.mktemp(suffix=".html"))
errors = rendercv.create_an_html_file_from_a_yaml_string(
yaml_string, output_file_path
)
assert errors is None
assert output_file_path.exists()
def test_create_an_html_file_from_a_python_dictionary(
rendercv_data_as_python_dictionary,
):
output_file_path = pathlib.Path(tempfile.mktemp(suffix=".html"))
errors = rendercv.create_an_html_file_from_a_python_dictionary(
rendercv_data_as_python_dictionary, output_file_path
)
assert errors is None
assert output_file_path.exists()
def test_create_a_pdf_from_a_yaml_string(input_file_path):
yaml_string = input_file_path.read_text()
output_file_path = pathlib.Path(tempfile.mktemp(suffix=".pdf"))
errors = rendercv.create_a_pdf_from_a_yaml_string(yaml_string, output_file_path)
assert errors is None
assert output_file_path.exists()
def test_create_a_pdf_from_a_python_dictionary(rendercv_data_as_python_dictionary):
output_file_path = pathlib.Path(tempfile.mktemp(suffix=".pdf"))
errors = rendercv.create_a_pdf_from_a_python_dictionary(
rendercv_data_as_python_dictionary, output_file_path
)
assert errors is None
assert output_file_path.exists()
def test_read_a_python_dictionary_and_return_a_data_model(
rendercv_data_as_python_dictionary,
):
result = rendercv.read_a_python_dictionary_and_return_a_data_model(
rendercv_data_as_python_dictionary
)
assert isinstance(result, rendercv.data.RenderCVDataModel)
def test_read_a_yaml_string_and_return_a_data_model(input_file_path):
yaml_string = input_file_path.read_text()
result = rendercv.read_a_yaml_string_and_return_a_data_model(yaml_string)
assert isinstance(result, rendercv.data.RenderCVDataModel)

View File

@@ -895,81 +895,93 @@ def test_make_keywords_bold_in_a_string():
def test_bold_keywords():
data_model = data.RenderCVDataModel(
cv=data.CurriculumVitae(
name="John Doe",
sections={
"test": [
"test_keyword_1",
],
data_model_as_dict = {
"cv": {
"sections": {
"test": ["test_keyword_1"],
"test2": [
data.EducationEntry(
institution="Test Institution",
area="Test Area",
highlights=["test_keyword_2"],
summary="test_keyword_3 test_keyword_4",
),
{
"institution": "Test Institution",
"area": "Test Area",
"degree": None,
"date": None,
"start_date": None,
"end_date": None,
"location": None,
"summary": "test_keyword_3 test_keyword_4",
"highlights": ["test_keyword_2"],
}
],
"test3": [
data.ExperienceEntry(
company="Test Company",
position="Test Position",
highlights=["test_keyword_5", "test_keyword_6"],
summary="test_keyword_6 test_keyword_7",
),
{
"company": "Test Company",
"position": "Test Position",
"date": None,
"start_date": None,
"end_date": None,
"location": None,
"summary": "test_keyword_6 test_keyword_7",
"highlights": ["test_keyword_5", "test_keyword_6"],
}
],
"test4": [
data.NormalEntry(
name="Test",
highlights=["test_keyword_2"],
summary="test_keyword_3 test_keyword_4",
),
],
"test5": [
data.PublicationEntry(
title="Test Institution",
authors=["Test Author"],
),
],
"test6": [
data.BulletEntry(
bullet="test_keyword_3 test_keyword_4",
),
{
"name": "Test",
"date": None,
"start_date": None,
"end_date": None,
"location": None,
"summary": "test_keyword_3 test_keyword_4",
"highlights": ["test_keyword_2"],
}
],
"test6": [{"bullet": "test_keyword_3 test_keyword_4"}],
"test7": [
data.OneLineEntry(
label="Test Institution",
details="test_keyword_3 test_keyword_4",
),
{
"label": "Test Institution",
"details": "test_keyword_3 test_keyword_4",
}
],
},
)
},
"rendercv_settings": {
"bold_keywords": [
"test_keyword_1",
"test_keyword_2",
"test_keyword_3",
"test_keyword_4",
"test_keyword_5",
"test_keyword_6",
"test_keyword_7",
],
},
}
data_model = data.validate_input_dictionary_and_return_the_data_model(
data_model_as_dict
)
for section in data_model.cv.sections:
for entry in section.entries:
if section.title == "test":
if section.title == "Test":
assert "**test_keyword_1**" in entry
elif section.title == "test2":
elif section.title == "Test2":
assert "**test_keyword_2**" in entry.highlights[0]
assert "**test_keyword_3**" in entry.summary
assert "**test_keyword_4**" in entry.summary
elif section.title == "test3":
elif section.title == "Test3":
assert "**test_keyword_5**" in entry.highlights[0]
assert "**test_keyword_6**" in entry.highlights[1]
assert "**test_keyword_6**" in entry.summary
assert "**test_keyword_7**" in entry.summary
elif section.title == "test4":
elif section.title == "Test4":
assert "**test_keyword_2**" in entry.highlights[0]
assert "**test_keyword_3**" in entry.summary
assert "**test_keyword_4**" in entry.summary
elif section.title == "test5":
assert "**test_keyword_3**" in entry.summary
assert "**test_keyword_4**" in entry.summary
elif section.title == "test6":
elif section.title == "Test6":
assert "**test_keyword_3**" in entry.bullet
assert "**test_keyword_4**" in entry.bullet
elif section.title == "test7":
elif section.title == "Test7":
assert "**test_keyword_3**" in entry.details
assert "**test_keyword_4**" in entry.details