From cd9f10c0301bcec1e2aa24b2c182e53baf65fa53 Mon Sep 17 00:00:00 2001 From: truePhilipp Date: Wed, 8 May 2024 12:11:03 +0000 Subject: [PATCH] added more tests for converter.py --- html_to_django/converter.py | 6 + tests/test_project/something/other.html | 20 +++ .../something/other.html | 20 +++ .../{test.n.html => test.html} | 0 tests/tests_converter.py | 124 ++++++++++++++++-- 5 files changed, 157 insertions(+), 13 deletions(-) create mode 100644 tests/test_project/something/other.html create mode 100644 tests/test_project_expected_result/something/other.html rename tests/test_project_expected_result/{test.n.html => test.html} (100%) diff --git a/html_to_django/converter.py b/html_to_django/converter.py index 0d06237..f2a1322 100644 --- a/html_to_django/converter.py +++ b/html_to_django/converter.py @@ -47,6 +47,9 @@ def convert_file(path: str, overwrite: bool = False) -> None: path (str): The path to the HTML file to be converted. overwrite (bool, optional): Whether to overwrite the original file. Defaults to False. """ + if not (os.path.exists(path) and os.path.isfile(path)): + raise FileNotFoundError + with open(path, "r") as file: soup = bs4.BeautifulSoup(file, "html.parser") @@ -75,6 +78,9 @@ def convert_dir(path: str, overwrite: bool = False) -> None: path (str): The path to the directory containing the HTML files to be converted. overwrite (bool, optional): Whether to overwrite the original files. Defaults to False. """ + if not (os.path.exists(path) and os.path.isdir(path)): + raise FileNotFoundError + for filepath in glob(f"{path}/**/*.html", recursive=True): convert_file(filepath, overwrite) diff --git a/tests/test_project/something/other.html b/tests/test_project/something/other.html new file mode 100644 index 0000000..435d58d --- /dev/null +++ b/tests/test_project/something/other.html @@ -0,0 +1,20 @@ + + + + + + Some Page + + +
+

Example

+

This is an example Page

+
+
+

List

+ +
+ + \ No newline at end of file diff --git a/tests/test_project_expected_result/something/other.html b/tests/test_project_expected_result/something/other.html new file mode 100644 index 0000000..1061ad3 --- /dev/null +++ b/tests/test_project_expected_result/something/other.html @@ -0,0 +1,20 @@ +
+

+ Example +

+

+ This is an example Page +

+
+
+

+ List +

+ +
diff --git a/tests/test_project_expected_result/test.n.html b/tests/test_project_expected_result/test.html similarity index 100% rename from tests/test_project_expected_result/test.n.html rename to tests/test_project_expected_result/test.html diff --git a/tests/tests_converter.py b/tests/tests_converter.py index 29838a9..4642217 100644 --- a/tests/tests_converter.py +++ b/tests/tests_converter.py @@ -1,21 +1,119 @@ import os import filecmp import shutil +import tempfile from unittest import TestCase -from html_to_django import converter +from bs4 import BeautifulSoup +from glob import glob +from html_to_django.converter import get_required_libraries, convert_file, convert_dir class ConverterTests(TestCase): + def test_get_required_libraries(self) -> None: + html = '
Content
' + soup = BeautifulSoup(html, 'html.parser') + expected = {'lib1', 'lib2', 'lib3'} + self.assertEqual(expected, get_required_libraries(soup)) + + def test_get_required_libraries_empty(self) -> None: + html = '
Content
' + soup = BeautifulSoup(html, 'html.parser') + expected: set[str] = set() + self.assertEqual(expected, get_required_libraries(soup)) + def test_convert_file(self) -> None: - shutil.copytree( - os.path.abspath("tests/test_project"), - os.path.abspath("tests/test_project_test_result"), - dirs_exist_ok=True - ) - converter.convert_file( - os.path.abspath("tests/test_project_test_result/test.html") - ) - self.assertTrue(filecmp.cmp( - os.path.abspath("tests/test_project_test_result/test.n.html"), - os.path.abspath("tests/test_project_expected_result/test.n.html") - )) + with tempfile.TemporaryDirectory() as tmpdir: + shutil.copytree( + "tests/test_project", + tmpdir, + dirs_exist_ok=True + ) + convert_file(f"{tmpdir}/test.html") + # verify that the new file exists and has the expected value + self.assertTrue(filecmp.cmp( + "tests/test_project_expected_result/test.html", + f"{tmpdir}/test.n.html" + )) + # verify that the source file is unchanged + self.assertTrue(filecmp.cmp( + "tests/test_project/test.html", + f"{tmpdir}/test.html" + )) + + def test_convert_file_overwrite(self) -> None: + with tempfile.TemporaryDirectory() as tmpdir: + shutil.copytree( + "tests/test_project", + tmpdir, + dirs_exist_ok=True + ) + convert_file(f"{tmpdir}/test.html", True) + # verify that the source file has been overwriten + self.assertTrue(filecmp.cmp( + "tests/test_project_expected_result/test.html", + f"{tmpdir}/test.html" + )) + # verify that no new file was created + self.assertFalse(os.path.exists(f"{tmpdir}/test.n.html")) + + def test_convert_file_nonexistent(self) -> None: + with self.assertRaises(FileNotFoundError): + convert_file(os.path.abspath("tests/test/project/nonexistent_file.html")) + + def test_convert_file_dir_passed(self) -> None: + with self.assertRaises(FileNotFoundError): + convert_file(os.path.abspath("tests/test_project")) + + def test_convert_dir(self) -> None: + with tempfile.TemporaryDirectory() as tmpdir: + shutil.copytree( + "tests/test_project", + tmpdir, + dirs_exist_ok=True + ) + convert_dir(f"{tmpdir}") + + for conversion_result in glob(f"{tmpdir}/**/*.n.html", recursive=True): + conversion_source = conversion_result.replace(".n.html", ".html") + + # verify that the new file exists and has the expected value + expected_result = conversion_source.replace(tmpdir, "tests/test_project_expected_result") + self.assertTrue(filecmp.cmp( + expected_result, + conversion_result + )) + + # verify that the source file is unchanged + original_source = conversion_source.replace(tmpdir, "tests/test_project") + self.assertTrue(filecmp.cmp( + original_source, + conversion_source + )) + + def test_convert_dir_overwrite(self) -> None: + with tempfile.TemporaryDirectory() as tmpdir: + shutil.copytree( + "tests/test_project", + tmpdir, + dirs_exist_ok=True + ) + convert_dir(f"{tmpdir}", True) + + for conversion_result in glob(f"{tmpdir}/**/*.html", recursive=True): + # verify that the source file has been overwritten + expected_result = conversion_result.replace(tmpdir, "tests/test_project_expected_result") + self.assertTrue(filecmp.cmp( + expected_result, + conversion_result + )) + + # verify that no new file was created + self.assertFalse(os.path.exists(conversion_result.replace(".html", ".n.html"))) + + def test_convert_dir_nonexistent(self) -> None: + with self.assertRaises(FileNotFoundError): + convert_dir("tests/test_project/nonexistent_folder") + + def test_convert_dir_file_passed(self) -> None: + with self.assertRaises(FileNotFoundError): + convert_dir("tests/test_project/test.html")