From 49e3013a30b2ba639404ed772b3a2119d6b1176e Mon Sep 17 00:00:00 2001 From: Sina Atalay Date: Tue, 30 Apr 2024 00:42:10 +0300 Subject: [PATCH] tests: improve tests for higher coverage --- tests/test_cli.py | 56 ++++++++++++++++++++++++++++++ tests/test_renderer.py | 78 ++++++++++++++++++++++++++++++++++++++---- 2 files changed, 128 insertions(+), 6 deletions(-) diff --git a/tests/test_cli.py b/tests/test_cli.py index 9c1b07a..eee04d4 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -182,6 +182,21 @@ def test_copy_templates(tmp_path, folder_name): assert copied_path.exists() +@pytest.mark.parametrize( + "folder_name", + ["markdown"] + dm.available_themes, +) +def test_copy_templates_destinations_exist(tmp_path, folder_name): + (tmp_path / folder_name).mkdir() + + copied_path = cli.copy_templates( + folder_name=folder_name, + copy_to=tmp_path, + ) + + assert copied_path is None + + runner = typer.testing.CliRunner() @@ -395,6 +410,16 @@ def test_render_command_with_dont_generate_png(tmp_path, input_file_path): assert not png_file_path.exists() +def test_render_command_with_local_latex_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) + + runner.invoke( + cli.app, + ["render", str(input_file_path), "--local-latex-command", "pdflatex"], + ) + + def test_new_command(tmp_path): # change the current working directory to the temporary directory: os.chdir(tmp_path) @@ -409,6 +434,15 @@ def test_new_command(tmp_path): assert input_file_path.exists() +def test_new_command_with_invalid_theme(tmp_path): + # change the current working directory to the temporary directory: + os.chdir(tmp_path) + + result = runner.invoke(cli.app, ["new", "John Doe", "--theme", "invalid_theme"]) + + assert "The theme should be one of the following" in result.stdout + + def test_new_command_with_dont_create_theme_source_files(tmp_path): # change the current working directory to the temporary directory: os.chdir(tmp_path) @@ -427,3 +461,25 @@ def test_new_command_with_dont_create_markdown_source_files(tmp_path): markdown_source_files_path = tmp_path / "markdown" assert not markdown_source_files_path.exists() + + +def test_new_command_with_only_input_file(tmp_path): + # change the current working directory to the temporary directory: + os.chdir(tmp_path) + runner.invoke( + cli.app, + [ + "new", + "John Doe", + "--dont-create-markdown-source-files", + "--dont-create-theme-source-files", + ], + ) + + markdown_source_files_path = tmp_path / "markdown" + theme_source_files_path = tmp_path / "classic" + input_file_path = tmp_path / "John_Doe_CV.yaml" + + assert not markdown_source_files_path.exists() + assert not theme_source_files_path.exists() + assert input_file_path.exists() diff --git a/tests/test_renderer.py b/tests/test_renderer.py index 44fa9b9..e736aa4 100644 --- a/tests/test_renderer.py +++ b/tests/test_renderer.py @@ -334,7 +334,6 @@ def test_if_generate_latex_file_can_create_a_new_directory( tmp_path, rendercv_data_model ): new_directory = tmp_path / "new_directory" - new_directory.mkdir() latex_file_path = r.generate_latex_file(rendercv_data_model, new_directory) @@ -384,7 +383,6 @@ def test_if_generate_markdown_file_can_create_a_new_directory( tmp_path, rendercv_data_model ): new_directory = tmp_path / "new_directory" - new_directory.mkdir() latex_file_path = r.generate_markdown_file(rendercv_data_model, new_directory) @@ -473,6 +471,11 @@ def test_copy_theme_files_to_output_directory_custom_theme( ) +def test_copy_theme_files_to_output_directory_nonexistent_theme(): + with pytest.raises(FileNotFoundError): + r.copy_theme_files_to_output_directory("nonexistent_theme", pathlib.Path(".")) + + @pytest.mark.parametrize( "theme_name", dm.available_themes, @@ -558,7 +561,7 @@ def test_latex_to_pdf( ) -def test_latex_to_pdf_invalid_latex_file(): +def test_latex_to_pdf_nonexistent_latex_file(): with pytest.raises(FileNotFoundError): file_path = pathlib.Path("file_doesnt_exist.tex") r.latex_to_pdf(file_path) @@ -596,10 +599,10 @@ def test_markdown_to_html( / markdown_file_name ) - # copy the latex source to the output path + # copy the markdown source to the output path shutil.copy(markdown_source_path, output_directory_path) - # convert the latex code to a md + # convert markdown to html r.markdown_to_html(output_directory_path / markdown_file_name) assert run_a_function_and_check_if_output_is_the_same_as_reference( @@ -609,7 +612,70 @@ def test_markdown_to_html( ) -def test_markdown_to_html_invalid_markdown_file(): +def test_markdown_to_html_nonexistent_markdown_file(): with pytest.raises(FileNotFoundError): file_path = pathlib.Path("file_doesnt_exist.md") r.markdown_to_html(file_path) + + +def test_pdf_to_pngs_single_page( + run_a_function_and_check_if_output_is_the_same_as_reference, +): + output_file_name = "classic_empty_1.png" + reference_file_name = "classic_empty.png" + + def generate_pngs(output_directory_path, reference_file_or_directory_path): + pdf_file_name = "classic_empty.pdf" + + pdf_path = ( + reference_file_or_directory_path.parent.parent + / "test_latex_to_pdf" + / pdf_file_name + ) + + # copy the markdown source to the output path + shutil.copy(pdf_path, output_directory_path) + + # convert pdf to pngs + r.pdf_to_pngs(output_directory_path / pdf_file_name) + + assert run_a_function_and_check_if_output_is_the_same_as_reference( + generate_pngs, + reference_file_or_directory_name=reference_file_name, + output_file_name=output_file_name, + ) + + +def test_pdf_to_pngs( + run_a_function_and_check_if_output_is_the_same_as_reference, +): + reference_directory_name = "pngs" + + def generate_pngs(output_directory_path, reference_file_or_directory_path): + pdf_file_name = "classic_filled.pdf" + + pdf_path = ( + reference_file_or_directory_path.parent.parent + / "test_latex_to_pdf" + / pdf_file_name + ) + + # copy the markdown source to the output path + shutil.copy(pdf_path, output_directory_path) + + # convert pdf to pngs + r.pdf_to_pngs(output_directory_path / pdf_file_name) + + # remove the pdf file + (output_directory_path / pdf_file_name).unlink() + + assert run_a_function_and_check_if_output_is_the_same_as_reference( + generate_pngs, + reference_directory_name, + ) + + +def test_pdf_to_png_nonexistent_pdf_file(): + with pytest.raises(FileNotFoundError): + file_path = pathlib.Path("file_doesnt_exist.pdf") + r.pdf_to_pngs(file_path)