refactor: format file

This commit is contained in:
Igor Ilic 2025-06-30 12:26:43 +02:00
parent d4d12ab7b7
commit 2c7eecc93d

View file

@ -9,13 +9,13 @@ class TestOpenDataFile:
def test_regular_file_path(self): def test_regular_file_path(self):
"""Test that regular file paths work as before.""" """Test that regular file paths work as before."""
with tempfile.NamedTemporaryFile(mode='w', delete=False, suffix='.txt') as f: with tempfile.NamedTemporaryFile(mode="w", delete=False, suffix=".txt") as f:
test_content = "Test content for regular file path" test_content = "Test content for regular file path"
f.write(test_content) f.write(test_content)
temp_file_path = f.name temp_file_path = f.name
try: try:
with open_data_file(temp_file_path, mode='r') as f: with open_data_file(temp_file_path, mode="r") as f:
content = f.read() content = f.read()
assert content == test_content assert content == test_content
finally: finally:
@ -23,14 +23,14 @@ class TestOpenDataFile:
def test_file_url_text_mode(self): def test_file_url_text_mode(self):
"""Test that file:// URLs work correctly in text mode.""" """Test that file:// URLs work correctly in text mode."""
with tempfile.NamedTemporaryFile(mode='w', delete=False, suffix='.txt') as f: with tempfile.NamedTemporaryFile(mode="w", delete=False, suffix=".txt") as f:
test_content = "Test content for file:// URL handling" test_content = "Test content for file:// URL handling"
f.write(test_content) f.write(test_content)
temp_file_path = f.name temp_file_path = f.name
try: try:
file_url = f"file://{temp_file_path}" file_url = f"file://{temp_file_path}"
with open_data_file(file_url, mode='r') as f: with open_data_file(file_url, mode="r") as f:
content = f.read() content = f.read()
assert content == test_content assert content == test_content
finally: finally:
@ -38,14 +38,14 @@ class TestOpenDataFile:
def test_file_url_binary_mode(self): def test_file_url_binary_mode(self):
"""Test that file:// URLs work correctly in binary mode.""" """Test that file:// URLs work correctly in binary mode."""
with tempfile.NamedTemporaryFile(mode='w', delete=False, suffix='.txt') as f: with tempfile.NamedTemporaryFile(mode="w", delete=False, suffix=".txt") as f:
test_content = "Test content for binary mode" test_content = "Test content for binary mode"
f.write(test_content) f.write(test_content)
temp_file_path = f.name temp_file_path = f.name
try: try:
file_url = f"file://{temp_file_path}" file_url = f"file://{temp_file_path}"
with open_data_file(file_url, mode='rb') as f: with open_data_file(file_url, mode="rb") as f:
content = f.read() content = f.read()
assert content == test_content.encode() assert content == test_content.encode()
finally: finally:
@ -53,14 +53,16 @@ class TestOpenDataFile:
def test_file_url_with_encoding(self): def test_file_url_with_encoding(self):
"""Test that file:// URLs work with specific encoding.""" """Test that file:// URLs work with specific encoding."""
with tempfile.NamedTemporaryFile(mode='w', delete=False, suffix='.txt', encoding='utf-8') as f: with tempfile.NamedTemporaryFile(
mode="w", delete=False, suffix=".txt", encoding="utf-8"
) as f:
test_content = "Test content with UTF-8: café ☕" test_content = "Test content with UTF-8: café ☕"
f.write(test_content) f.write(test_content)
temp_file_path = f.name temp_file_path = f.name
try: try:
file_url = f"file://{temp_file_path}" file_url = f"file://{temp_file_path}"
with open_data_file(file_url, mode='r', encoding='utf-8') as f: with open_data_file(file_url, mode="r", encoding="utf-8") as f:
content = f.read() content = f.read()
assert content == test_content assert content == test_content
finally: finally:
@ -70,20 +72,20 @@ class TestOpenDataFile:
"""Test that file:// URLs raise appropriate error for nonexistent files.""" """Test that file:// URLs raise appropriate error for nonexistent files."""
file_url = "file:///nonexistent/path/to/file.txt" file_url = "file:///nonexistent/path/to/file.txt"
with pytest.raises(FileNotFoundError): with pytest.raises(FileNotFoundError):
with open_data_file(file_url, mode='r') as f: with open_data_file(file_url, mode="r") as f:
f.read() f.read()
def test_multiple_file_prefixes(self): def test_multiple_file_prefixes(self):
"""Test that multiple file:// prefixes are handled correctly.""" """Test that multiple file:// prefixes are handled correctly."""
with tempfile.NamedTemporaryFile(mode='w', delete=False, suffix='.txt') as f: with tempfile.NamedTemporaryFile(mode="w", delete=False, suffix=".txt") as f:
test_content = "Test content" test_content = "Test content"
f.write(test_content) f.write(test_content)
temp_file_path = f.name temp_file_path = f.name
try: try:
# Even if someone accidentally adds multiple file:// prefixes # Even if someone accidentally adds multiple file:// prefixes
file_url = f"file://file://{temp_file_path}" file_url = f"file://file://{temp_file_path}"
with open_data_file(file_url, mode='r') as f: with open_data_file(file_url, mode="r") as f:
content = f.read() content = f.read()
# This should work because we only replace the first occurrence # This should work because we only replace the first occurrence
assert content == test_content assert content == test_content
@ -95,4 +97,4 @@ class TestOpenDataFile:
if __name__ == "__main__": if __name__ == "__main__":
pytest.main([__file__, "-v"]) pytest.main([__file__, "-v"])