mirror of
https://github.com/fhswf/aki_prj23_transparenzregister.git
synced 2025-12-18 10:40:45 +01:00
test(data-extraction): Include first unit tests
This commit is contained in:
@@ -0,0 +1,81 @@
|
||||
import glob
|
||||
import os
|
||||
from tempfile import TemporaryDirectory
|
||||
|
||||
from aki_prj23_transparenzregister.utils.data_extraction.unternehmensregister import (
|
||||
extract,
|
||||
)
|
||||
|
||||
|
||||
def prepare_temporary_dir(directory: str, formats: list[str]) -> None:
|
||||
for index in range(len(formats)):
|
||||
test_file = os.path.join(directory, f"file-{index}.{formats[index]}")
|
||||
with open(test_file, "w") as file:
|
||||
file.write(f"Hello There {index}")
|
||||
|
||||
|
||||
def test_rename_latest_file() -> None:
|
||||
with TemporaryDirectory() as temp_dir:
|
||||
# Create some test files in the temporary directory
|
||||
test_file1 = os.path.join(temp_dir, "file1.xml")
|
||||
test_file2 = os.path.join(temp_dir, "file2.xml")
|
||||
test_file3 = os.path.join(temp_dir, "file3.xml")
|
||||
|
||||
# Create files with different modification times
|
||||
with open(test_file1, "w") as f:
|
||||
f.write("Content 1")
|
||||
with open(test_file2, "w") as f:
|
||||
f.write("Content 2")
|
||||
with open(test_file3, "w") as f:
|
||||
f.write("Content 3")
|
||||
|
||||
# Rename the latest file to 'new_file.xml'
|
||||
extract.rename_latest_file(temp_dir, "new_file.xml")
|
||||
|
||||
glob.glob1(temp_dir, "*.xml")
|
||||
# Verify that 'file3.xml' is renamed to 'new_file.xml'
|
||||
assert not os.path.exists(test_file3)
|
||||
assert os.path.exists(os.path.join(temp_dir, "new_file.xml"))
|
||||
|
||||
# Verify that 'file1.xml' and 'file2.xml' are still present
|
||||
assert os.path.exists(test_file1)
|
||||
assert os.path.exists(test_file2)
|
||||
|
||||
# Verify that renaming with a different pattern works
|
||||
with open(test_file1, "w") as f:
|
||||
f.write("Content 4")
|
||||
with open(os.path.join(temp_dir, "file4.txt"), "w") as f:
|
||||
f.write("Content 5")
|
||||
|
||||
# Rename the latest .txt file to 'new_file.txt'
|
||||
extract.rename_latest_file(temp_dir, "new_file.txt", pattern="*.txt")
|
||||
|
||||
# Verify that 'file4.txt' is renamed to 'new_file.txt'
|
||||
assert not os.path.exists(os.path.join(temp_dir, "file4.txt"))
|
||||
assert os.path.exists(os.path.join(temp_dir, "new_file.txt"))
|
||||
|
||||
# Verify that 'file1.xml' is still present and unchanged
|
||||
with open(test_file1) as f:
|
||||
assert f.read() == "Content 4"
|
||||
|
||||
|
||||
def test_get_num_files_default_pattern() -> None:
|
||||
with TemporaryDirectory() as temp_dir:
|
||||
prepare_temporary_dir(temp_dir, ["xml", "xml", "xml"])
|
||||
|
||||
expected_result = 3
|
||||
assert extract.get_num_files(temp_dir) == expected_result
|
||||
|
||||
|
||||
def test_get_num_files_different_pattern() -> None:
|
||||
with TemporaryDirectory() as temp_dir:
|
||||
prepare_temporary_dir(temp_dir, ["xml", "txt", "json"])
|
||||
|
||||
num_files = extract.get_num_files(temp_dir, "*.txt")
|
||||
assert num_files == 1
|
||||
|
||||
|
||||
def test_wait_for_download_condition() -> None:
|
||||
with TemporaryDirectory() as temp_dir:
|
||||
prepare_temporary_dir(temp_dir, ["xml", "txt"])
|
||||
assert extract.wait_for_download_condition(temp_dir, 2) is False
|
||||
Reference in New Issue
Block a user