added more tests for converter.py

This commit is contained in:
truePhilipp committed 2024-05-08 12:11:03 +00:00
1 parent d4f53e49d9
commit cd9f10c030
5 files changed
+157 -13

No files matched your search

+6
View File
@@ -47,6 +47,9 @@ def convert_file(path: str, overwrite: bool = False) -> None:
path (str): The path to the HTML file to be converted. path (str): The path to the HTML file to be converted.
overwrite (bool, optional): Whether to overwrite the original file. Defaults to False. 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: with open(path, "r") as file:
soup = bs4.BeautifulSoup(file, "html.parser") 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. 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. 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): for filepath in glob(f"{path}/**/*.html", recursive=True):
convert_file(filepath, overwrite) convert_file(filepath, overwrite)
+20
View File
@@ -0,0 +1,20 @@
<!DOCTYPE html>
<html lang="en" dj-remove-default>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Some Page</title>
</head>
<body>
<div>
<h2>Example</h2>
<p>This is an example Page</p>
</div>
<div>
<h2>List</h2>
<ul>
<li dj-for-wrap="i in items" dj-var="i.name" dj-style="background-color;i.bg_color;"></li>
</ul>
</div>
</body>
</html>
@@ -0,0 +1,20 @@
<div>
<h2>
Example
</h2>
<p>
This is an example Page
</p>
</div>
<div>
<h2>
List
</h2>
<ul>
{% for i in items %}
<li style="background-color:{{ i.bg_color }};">
{{ i.name }}
</li>
{% endfor %}
</ul>
</div>
+111 -13
View File
@@ -1,21 +1,119 @@
import os import os
import filecmp import filecmp
import shutil import shutil
import tempfile
from unittest import TestCase 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): class ConverterTests(TestCase):
def test_get_required_libraries(self) -> None:
html = '<div dj-libs="lib1;lib2">Content</div><div dj-libs="lib3"></div>'
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 = '<div>Content</div>'
soup = BeautifulSoup(html, 'html.parser')
expected: set[str] = set()
self.assertEqual(expected, get_required_libraries(soup))
def test_convert_file(self) -> None: def test_convert_file(self) -> None:
shutil.copytree( with tempfile.TemporaryDirectory() as tmpdir:
os.path.abspath("tests/test_project"), shutil.copytree(
os.path.abspath("tests/test_project_test_result"), "tests/test_project",
dirs_exist_ok=True tmpdir,
) dirs_exist_ok=True
converter.convert_file( )
os.path.abspath("tests/test_project_test_result/test.html") convert_file(f"{tmpdir}/test.html")
) # verify that the new file exists and has the expected value
self.assertTrue(filecmp.cmp( self.assertTrue(filecmp.cmp(
os.path.abspath("tests/test_project_test_result/test.n.html"), "tests/test_project_expected_result/test.html",
os.path.abspath("tests/test_project_expected_result/test.n.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")