52 lines
2.1 KiB
Python
52 lines
2.1 KiB
Python
import os
|
|
import tempfile
|
|
from unittest import TestCase
|
|
|
|
import pytest
|
|
|
|
from amqp.adapter.file_downloader import ensure_directory_exists
|
|
|
|
|
|
class TestFileDownloader(TestCase):
|
|
def test_ensure_directory_exists(self):
|
|
"""
|
|
Test function for ensure_directory_exists. Creates a temporary directory
|
|
and files to test the versioning logic.
|
|
"""
|
|
|
|
# Create a temporary directory
|
|
with tempfile.TemporaryDirectory() as temp_dir:
|
|
# Test case 1: File does not exist
|
|
filepath1 = os.path.join(temp_dir, "test1.txt")
|
|
result1 = ensure_directory_exists(filepath1)
|
|
assert result1 == filepath1, f"Test Case 1 Failed: {result1} != {filepath1}"
|
|
# Create the file
|
|
with open(filepath1, "w") as f:
|
|
f.write("test")
|
|
|
|
# Test case 2: File exists, should create versioned file
|
|
filepath2 = os.path.join(temp_dir, "test2.txt")
|
|
result2 = ensure_directory_exists(filepath2)
|
|
assert result2 == filepath2, f"Test Case 2 Failed: {result2} != {filepath2}"
|
|
with open(filepath2, "w") as f:
|
|
f.write("test")
|
|
result2_v1 = ensure_directory_exists(filepath2)
|
|
assert result2_v1 == os.path.join(
|
|
temp_dir, "test2.1.txt"
|
|
), f"Test Case 2 Version 1 Failed: {result2_v1} != {os.path.join(temp_dir, 'test2.1.txt')}"
|
|
|
|
# Create a few versioned files
|
|
with open(os.path.join(temp_dir, "test3.txt"), "w") as f:
|
|
f.write("test")
|
|
with open(os.path.join(temp_dir, "test3.1.txt"), "w") as f:
|
|
f.write("test")
|
|
with open(os.path.join(temp_dir, "test3.2.txt"), "w") as f:
|
|
f.write("test")
|
|
filepath3 = os.path.join(temp_dir, "test3.txt")
|
|
result3_v3 = ensure_directory_exists(filepath3)
|
|
assert result3_v3 == os.path.join(
|
|
temp_dir, "test3.3.txt"
|
|
), f"Test Case 3 Version 3 Failed: {result3_v3} != {os.path.join(temp_dir, 'test3.3.txt')}"
|
|
|
|
print("All test cases passed!")
|