rendercv/tests/test_cli.py

202 lines
5.2 KiB
Python
Raw Normal View History

2024-02-11 18:36:56 +00:00
import os
2024-02-18 16:19:59 +00:00
import shutil
2024-02-11 18:36:56 +00:00
2024-02-11 13:37:10 +00:00
import rendercv.cli as cli
2024-02-14 18:50:00 +00:00
import rendercv.data_models as dm
2024-02-09 19:32:58 +00:00
import pydantic
import ruamel.yaml
import pytest
2024-02-11 13:37:10 +00:00
import typer.testing
2024-02-18 16:19:59 +00:00
import time_machine
2024-02-11 13:37:10 +00:00
2024-02-09 19:32:58 +00:00
def test_welcome():
2024-02-11 13:37:10 +00:00
cli.welcome()
2024-02-09 19:32:58 +00:00
def test_warning():
2024-02-11 13:37:10 +00:00
cli.warning("This is a warning message.")
2024-02-09 19:32:58 +00:00
def test_error():
2024-02-11 13:37:10 +00:00
cli.error("This is an error message.")
2024-02-09 19:32:58 +00:00
def test_information():
2024-02-11 13:37:10 +00:00
cli.information("This is an information message.")
2024-02-09 19:32:58 +00:00
def test_get_error_message_and_location_and_value_from_a_custom_error():
error_string = "('error message', 'location', 'value')"
2024-02-11 13:37:10 +00:00
result = cli.get_error_message_and_location_and_value_from_a_custom_error(
2024-02-09 19:32:58 +00:00
error_string
)
assert result == ("error message", "location", "value")
2024-02-14 18:50:00 +00:00
error_string = """("er'ror message", 'location', 'value')"""
result = cli.get_error_message_and_location_and_value_from_a_custom_error(
error_string
)
assert result == ("er'ror message", "location", "value")
2024-02-09 19:32:58 +00:00
error_string = "error message"
2024-02-11 13:37:10 +00:00
result = cli.get_error_message_and_location_and_value_from_a_custom_error(
2024-02-09 19:32:58 +00:00
error_string
)
2024-02-14 18:50:00 +00:00
assert result == (None, None, None)
2024-02-09 19:32:58 +00:00
2024-02-14 18:50:00 +00:00
@pytest.mark.parametrize(
"data_model_class, invalid_model",
[
(
dm.EducationEntry,
{
"institution": "Boğaziçi University",
"area": "Mechanical Engineering",
"degree": "BS",
"date": "2028-12-08",
},
),
(
dm.EducationEntry,
{
"area": "Mechanical Engineering",
"extra": "Extra",
},
),
(
dm.ExperienceEntry,
{
"company": "CERN",
},
),
(
dm.ExperienceEntry,
{
"position": "Researcher",
},
),
(
dm.ExperienceEntry,
{
"company": "CERN",
"position": "Researcher",
"stat_date": "2023-12-08",
"end_date": "INVALID END DATE",
},
),
(
dm.PublicationEntry,
{
"doi": "10.1109/TASC.2023.3340648",
},
),
(
dm.ExperienceEntry,
{
"authors": ["John Doe", "Jane Doe"],
},
),
(
dm.OneLineEntry,
{
"name": "My One Line Entry",
},
),
(
dm.NormalEntry,
{
"name": "My Entry",
},
),
(
dm.CurriculumVitae,
{
"name": "John Doe",
"sections": {
"education": [
{
"institution": "Boğaziçi University",
"area": "Mechanical Engineering",
"degree": "BS",
"date": "2028-12-08",
},
{
"degree": "BS",
},
]
},
},
),
],
)
def test_handle_validation_error(data_model_class, invalid_model):
try:
data_model_class(**invalid_model)
except pydantic.ValidationError as e:
cli.handle_validation_error(e)
2024-02-09 19:32:58 +00:00
@pytest.mark.parametrize(
"exception",
[
2024-02-24 19:48:34 +00:00
ruamel.yaml.YAMLError("message"),
RuntimeError("message"),
FileNotFoundError("message"),
ValueError("message"),
UnicodeDecodeError("utf-8", b"", 1, 2, "message"),
],
2024-02-09 19:32:58 +00:00
)
def test_handle_exceptions(exception):
2024-02-11 13:37:10 +00:00
@cli.handle_exceptions
2024-02-09 19:32:58 +00:00
def function_that_raises_exception():
2024-02-24 19:48:34 +00:00
raise exception
2024-02-09 19:32:58 +00:00
function_that_raises_exception()
def test_live_progress_reporter_class():
2024-02-11 13:37:10 +00:00
with cli.LiveProgressReporter(number_of_steps=3) as progress:
2024-02-09 19:32:58 +00:00
progress.start_a_step("Test step 1")
progress.finish_the_current_step()
progress.start_a_step("Test step 2")
progress.finish_the_current_step()
progress.start_a_step("Test step 3")
progress.finish_the_current_step()
2024-02-11 18:36:56 +00:00
runner = typer.testing.CliRunner()
2024-02-18 16:19:59 +00:00
@time_machine.travel("2024-01-01")
def test_render_command(tmp_path, input_file_path):
# copy input file to the temporary directory to create the output directory there:
input_file_path = shutil.copy(input_file_path, tmp_path)
result = runner.invoke(cli.app, ["render", str(input_file_path)])
2024-02-11 18:36:56 +00:00
assert result.exit_code == 0
assert "Your CV is rendered!" in result.stdout
2024-02-20 19:16:57 +00:00
def test_render_command_with_use_local_latex_option(tmp_path, input_file_path):
# copy input file to the temporary directory to create the output directory there:
input_file_path = shutil.copy(input_file_path, tmp_path)
runner.invoke(
cli.app,
["render", str(input_file_path), "--use-local-latex-command", "pdflatex"],
)
2024-02-20 19:16:57 +00:00
2024-02-11 18:36:56 +00:00
def test_new_command(tmp_path):
# change the current working directory to the temporary directory:
os.chdir(tmp_path)
result = runner.invoke(cli.app, ["new", "John Doe"])
assert result.exit_code == 0
assert "Your RenderCV input file has been created" in result.stdout