From 56c2ed55ecb97d120f6417a47da8577c224e18ea Mon Sep 17 00:00:00 2001 From: TrisNol Date: Thu, 21 Sep 2023 16:54:23 +0200 Subject: [PATCH] test(data-extraction): Delay file creation in test_rename_latest_file to avoid same timestamps --- .../unternehmensregister/extract_test.py | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/tests/utils/data_extraction/unternehmensregister/extract_test.py b/tests/utils/data_extraction/unternehmensregister/extract_test.py index 21d4c50..e7cbe47 100644 --- a/tests/utils/data_extraction/unternehmensregister/extract_test.py +++ b/tests/utils/data_extraction/unternehmensregister/extract_test.py @@ -1,4 +1,3 @@ -import glob import os from tempfile import TemporaryDirectory @@ -15,7 +14,9 @@ def prepare_temporary_dir(directory: str, formats: list[str]) -> None: def test_rename_latest_file() -> None: - with TemporaryDirectory() as temp_dir: + import time + + with TemporaryDirectory("-4711") 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") @@ -24,15 +25,16 @@ def test_rename_latest_file() -> None: # Create files with different modification times with open(test_file1, "w") as f: f.write("Content 1") + time.sleep(0.15) with open(test_file2, "w") as f: f.write("Content 2") + time.sleep(0.15) with open(test_file3, "w") as f: f.write("Content 3") + time.sleep(0.15) # 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")) @@ -60,7 +62,7 @@ def test_rename_latest_file() -> None: def test_get_num_files_default_pattern() -> None: - with TemporaryDirectory() as temp_dir: + with TemporaryDirectory("-4712") as temp_dir: prepare_temporary_dir(temp_dir, ["xml", "xml", "xml"]) expected_result = 3 @@ -68,7 +70,7 @@ def test_get_num_files_default_pattern() -> None: def test_get_num_files_different_pattern() -> None: - with TemporaryDirectory() as temp_dir: + with TemporaryDirectory("-4713") as temp_dir: prepare_temporary_dir(temp_dir, ["xml", "txt", "json"]) num_files = extract.get_num_files(temp_dir, "*.txt") @@ -76,6 +78,6 @@ def test_get_num_files_different_pattern() -> None: def test_wait_for_download_condition() -> None: - with TemporaryDirectory() as temp_dir: + with TemporaryDirectory("-4714") as temp_dir: prepare_temporary_dir(temp_dir, ["xml", "txt"]) assert extract.wait_for_download_condition(temp_dir, 2) is False