Files
wehub-resource-sync c889a57b6b
Test Suites / Build CI Environment (push) Has been cancelled
Test Suites / Basic Tests (push) Has been cancelled
Test Suites / End-to-End Tests (push) Has been cancelled
Test Suites / CLI Tests (push) Has been cancelled
Test Suites / Slow End-to-End Tests (push) Has been cancelled
Test Suites / Graph Database Tests (push) Has been cancelled
Test Suites / Vector DB Tests (push) Has been cancelled
Test Suites / Temporal Graph Test (push) Has been cancelled
Test Suites / Search Test on Different DBs (push) Has been cancelled
Test Suites / Example Tests (push) Has been cancelled
Test Suites / Notebook Tests (push) Has been cancelled
Test Suites / OS and Python Tests Ubuntu (push) Has been cancelled
Test Suites / OS and Python Tests Extended (push) Has been cancelled
Test Suites / LLM Test Suite (push) Has been cancelled
Test Suites / S3 File Storage Test (push) Has been cancelled
Test Suites / Run Integration Tests (push) Has been cancelled
Test Suites / MCP Tests (push) Has been cancelled
Test Suites / Docker Compose Test (push) Has been cancelled
Test Suites / Docker CI test (push) Has been cancelled
Test Suites / Relational DB Migration Tests (push) Has been cancelled
Test Suites / Distributed Cognee Test (push) Has been cancelled
Test Suites / DB Examples Tests (push) Has been cancelled
Test Suites / Test Completion Status (push) Has been cancelled
Test Suites / Claude Code Review (push) Has been cancelled
Test Suites / basic checks (push) Has been cancelled
build | Build and Push Cognee MCP Docker Image to dockerhub / docker-build-and-push (push) Has been cancelled
Scorecard supply-chain security / Scorecard analysis (push) Has been cancelled
build | Build and Push Docker Image to dockerhub / docker-build-and-push (push) Has been cancelled
Weighted Edges Tests / Test Weighted Edges Core Functionality (3.11) (push) Has been cancelled
Weighted Edges Tests / Test Weighted Edges Core Functionality (3.12) (push) Has been cancelled
Weighted Edges Tests / Test Weighted Edges with Different Graph Databases (kuzu, kuzu) (push) Has been cancelled
Weighted Edges Tests / Test Weighted Edges with Different Graph Databases (neo4j, neo4j) (push) Has been cancelled
Weighted Edges Tests / Test Weighted Edges Examples (push) Has been cancelled
Weighted Edges Tests / Code Quality for Weighted Edges (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 13:02:24 +08:00

114 lines
4.3 KiB
Python

import os
import tempfile
import pytest
from pathlib import Path
from cognee.infrastructure.files.utils.open_data_file import open_data_file
class TestOpenDataFile:
"""Test cases for open_data_file function with file:// URL handling."""
@pytest.mark.asyncio
async def test_regular_file_path(self):
"""Test that regular file paths work as before."""
with tempfile.NamedTemporaryFile(mode="w", delete=False, suffix=".txt") as f:
test_content = "Test content for regular file path"
f.write(test_content)
temp_file_path = f.name
try:
async with open_data_file(temp_file_path, mode="r") as f:
content = f.read()
assert content == test_content
finally:
os.unlink(temp_file_path)
@pytest.mark.asyncio
async def test_file_url_text_mode(self):
"""Test that file:// URLs work correctly in text mode."""
with tempfile.NamedTemporaryFile(mode="w", delete=False, suffix=".txt") as f:
test_content = "Test content for file:// URL handling"
f.write(test_content)
temp_file_path = f.name
try:
# Use pathlib.Path.as_uri() for proper cross-platform file URL creation
file_url = Path(temp_file_path).as_uri()
async with open_data_file(file_url, mode="r") as f:
content = f.read()
assert content == test_content
finally:
os.unlink(temp_file_path)
@pytest.mark.asyncio
async def test_file_url_binary_mode(self):
"""Test that file:// URLs work correctly in binary mode."""
with tempfile.NamedTemporaryFile(mode="w", delete=False, suffix=".txt") as f:
test_content = "Test content for binary mode"
f.write(test_content)
temp_file_path = f.name
try:
# Use pathlib.Path.as_uri() for proper cross-platform file URL creation
file_url = Path(temp_file_path).as_uri()
async with open_data_file(file_url, mode="rb") as f:
content = f.read()
assert content == test_content.encode()
finally:
os.unlink(temp_file_path)
@pytest.mark.asyncio
async def test_file_url_with_encoding(self):
"""Test that file:// URLs work with specific encoding."""
with tempfile.NamedTemporaryFile(
mode="w", delete=False, suffix=".txt", encoding="utf-8"
) as f:
test_content = "Test content with UTF-8: café ☕"
f.write(test_content)
temp_file_path = f.name
try:
# Use pathlib.Path.as_uri() for proper cross-platform file URL creation
file_url = Path(temp_file_path).as_uri()
async with open_data_file(file_url, mode="r", encoding="utf-8") as f:
content = f.read()
assert content == test_content
finally:
os.unlink(temp_file_path)
@pytest.mark.asyncio
async def test_file_url_nonexistent_file(self):
"""Test that file:// URLs raise appropriate error for nonexistent files."""
file_url = "file:///nonexistent/path/to/file.txt"
with pytest.raises(FileNotFoundError):
async with open_data_file(file_url, mode="r") as f:
f.read()
@pytest.mark.asyncio
async def test_multiple_file_prefixes(self):
"""Test that multiple file:// prefixes are handled correctly."""
with tempfile.NamedTemporaryFile(mode="w", delete=False, suffix=".txt") as f:
test_content = "Test content"
f.write(test_content)
temp_file_path = f.name
try:
# Even if someone accidentally adds multiple file:// prefixes
# Use proper file URL creation first
proper_file_url = Path(temp_file_path).as_uri()
file_url = f"file://{proper_file_url}"
async with open_data_file(file_url, mode="r") as f:
content = f.read()
# This should work because we only replace the first occurrence
assert content == test_content
except FileNotFoundError:
# This is expected behavior - only the first file:// should be stripped
pass
finally:
os.unlink(temp_file_path)
if __name__ == "__main__":
pytest.main([__file__, "-v"])