Removed async
This commit is contained in:
@@ -11,7 +11,7 @@ from unittest.mock import patch
|
||||
import pytest
|
||||
import pytest_asyncio
|
||||
from bson import ObjectId
|
||||
from mongomock_motor import AsyncMongoMockClient
|
||||
from mongomock.mongo_client import MongoClient
|
||||
|
||||
from app.models.document import FileType
|
||||
from app.services.document_service import DocumentService
|
||||
@@ -24,15 +24,15 @@ def cleanup_test_folder():
|
||||
shutil.rmtree("test_folder", ignore_errors=True)
|
||||
|
||||
|
||||
@pytest_asyncio.fixture
|
||||
async def in_memory_database():
|
||||
@pytest.fixture
|
||||
def in_memory_database():
|
||||
"""Create an in-memory database for testing."""
|
||||
client = AsyncMongoMockClient()
|
||||
client = MongoClient()
|
||||
return client.test_database
|
||||
|
||||
|
||||
@pytest_asyncio.fixture
|
||||
async def document_service(in_memory_database):
|
||||
def document_service(in_memory_database):
|
||||
"""Create DocumentService with in-memory repositories."""
|
||||
service = DocumentService(in_memory_database, objects_folder="test_folder")
|
||||
return service
|
||||
@@ -72,8 +72,7 @@ class TestCreateDocument:
|
||||
|
||||
@patch('app.services.document_service.magic.from_buffer')
|
||||
@patch('app.services.document_service.datetime')
|
||||
@pytest.mark.asyncio
|
||||
async def test_i_can_create_document_with_new_content(
|
||||
def test_i_can_create_document_with_new_content(
|
||||
self,
|
||||
mock_datetime,
|
||||
mock_magic,
|
||||
@@ -87,7 +86,7 @@ class TestCreateDocument:
|
||||
mock_magic.return_value = "application/pdf"
|
||||
|
||||
# Execute
|
||||
result = await document_service.create_document(
|
||||
result = document_service.create_document(
|
||||
"/test/test.pdf",
|
||||
sample_file_bytes,
|
||||
"utf-8"
|
||||
@@ -102,7 +101,7 @@ class TestCreateDocument:
|
||||
assert result.file_hash == document_service._calculate_file_hash(sample_file_bytes)
|
||||
|
||||
# Verify document created in database
|
||||
doc_in_db = await document_service.document_repository.find_document_by_id(result.id)
|
||||
doc_in_db = document_service.document_repository.find_document_by_id(result.id)
|
||||
assert doc_in_db is not None
|
||||
assert doc_in_db.id == result.id
|
||||
assert doc_in_db.filename == result.filename
|
||||
@@ -116,8 +115,7 @@ class TestCreateDocument:
|
||||
|
||||
@patch('app.services.document_service.magic.from_buffer')
|
||||
@patch('app.services.document_service.datetime')
|
||||
@pytest.mark.asyncio
|
||||
async def test_i_can_create_document_with_existing_content(
|
||||
def test_i_can_create_document_with_existing_content(
|
||||
self,
|
||||
mock_datetime,
|
||||
mock_magic,
|
||||
@@ -131,14 +129,14 @@ class TestCreateDocument:
|
||||
mock_magic.return_value = "application/pdf"
|
||||
|
||||
# Create first document
|
||||
first_doc = await document_service.create_document(
|
||||
first_doc = document_service.create_document(
|
||||
"/test/first.pdf",
|
||||
sample_file_bytes,
|
||||
"utf-8"
|
||||
)
|
||||
|
||||
# Create second document with same content
|
||||
second_doc = await document_service.create_document(
|
||||
second_doc = document_service.create_document(
|
||||
"/test/second.pdf",
|
||||
sample_file_bytes,
|
||||
"utf-8"
|
||||
@@ -149,37 +147,34 @@ class TestCreateDocument:
|
||||
assert first_doc.filename != second_doc.filename
|
||||
assert first_doc.filepath != second_doc.filepath
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_i_cannot_create_document_with_unsupported_file_type(
|
||||
def test_i_cannot_create_document_with_unsupported_file_type(
|
||||
self,
|
||||
document_service,
|
||||
sample_file_bytes
|
||||
):
|
||||
"""Test that unsupported file types raise ValueError."""
|
||||
with pytest.raises(ValueError, match="Unsupported file type"):
|
||||
await document_service.create_document(
|
||||
document_service.create_document(
|
||||
"/test/test.xyz", # Unsupported extension
|
||||
sample_file_bytes,
|
||||
"utf-8"
|
||||
)
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_i_cannot_create_document_with_empty_file_path(
|
||||
def test_i_cannot_create_document_with_empty_file_path(
|
||||
self,
|
||||
document_service,
|
||||
sample_file_bytes
|
||||
):
|
||||
"""Test that empty file path raises ValueError."""
|
||||
with pytest.raises(ValueError):
|
||||
await document_service.create_document(
|
||||
document_service.create_document(
|
||||
"", # Empty path
|
||||
sample_file_bytes,
|
||||
"utf-8"
|
||||
)
|
||||
|
||||
@patch('app.services.document_service.magic.from_buffer')
|
||||
@pytest.mark.asyncio
|
||||
async def test_i_can_create_document_with_empty_bytes(
|
||||
def test_i_can_create_document_with_empty_bytes(
|
||||
self,
|
||||
mock_magic,
|
||||
document_service
|
||||
@@ -189,7 +184,7 @@ class TestCreateDocument:
|
||||
mock_magic.return_value = "text/plain"
|
||||
|
||||
# Execute with empty bytes
|
||||
result = await document_service.create_document(
|
||||
result = document_service.create_document(
|
||||
"/test/empty.txt",
|
||||
b"", # Empty bytes
|
||||
"utf-8"
|
||||
@@ -203,8 +198,7 @@ class TestGetMethods:
|
||||
"""Tests for document retrieval methods."""
|
||||
|
||||
@patch('app.services.document_service.magic.from_buffer')
|
||||
@pytest.mark.asyncio
|
||||
async def test_i_can_get_document_by_id(
|
||||
def test_i_can_get_document_by_id(
|
||||
self,
|
||||
mock_magic,
|
||||
document_service,
|
||||
@@ -215,14 +209,14 @@ class TestGetMethods:
|
||||
mock_magic.return_value = "application/pdf"
|
||||
|
||||
# Create a document first
|
||||
created_doc = await document_service.create_document(
|
||||
created_doc = document_service.create_document(
|
||||
"/test/test.pdf",
|
||||
sample_file_bytes,
|
||||
"utf-8"
|
||||
)
|
||||
|
||||
# Execute
|
||||
result = await document_service.get_document_by_id(created_doc.id)
|
||||
result = document_service.get_document_by_id(created_doc.id)
|
||||
|
||||
# Verify
|
||||
assert result is not None
|
||||
@@ -230,8 +224,7 @@ class TestGetMethods:
|
||||
assert result.filename == created_doc.filename
|
||||
|
||||
@patch('app.services.document_service.magic.from_buffer')
|
||||
@pytest.mark.asyncio
|
||||
async def test_i_can_get_document_by_hash(
|
||||
def test_i_can_get_document_by_hash(
|
||||
self,
|
||||
mock_magic,
|
||||
document_service,
|
||||
@@ -242,14 +235,14 @@ class TestGetMethods:
|
||||
mock_magic.return_value = "application/pdf"
|
||||
|
||||
# Create a document first
|
||||
created_doc = await document_service.create_document(
|
||||
created_doc = document_service.create_document(
|
||||
"/test/test.pdf",
|
||||
sample_file_bytes,
|
||||
"utf-8"
|
||||
)
|
||||
|
||||
# Execute
|
||||
result = await document_service.get_document_by_hash(created_doc.file_hash)
|
||||
result = document_service.get_document_by_hash(created_doc.file_hash)
|
||||
|
||||
# Verify
|
||||
assert result is not None
|
||||
@@ -257,8 +250,7 @@ class TestGetMethods:
|
||||
assert result.filename == created_doc.filename
|
||||
|
||||
@patch('app.services.document_service.magic.from_buffer')
|
||||
@pytest.mark.asyncio
|
||||
async def test_i_can_get_document_by_filepath(
|
||||
def test_i_can_get_document_by_filepath(
|
||||
self,
|
||||
mock_magic,
|
||||
document_service,
|
||||
@@ -270,14 +262,14 @@ class TestGetMethods:
|
||||
test_path = "/test/unique_test.pdf"
|
||||
|
||||
# Create a document first
|
||||
created_doc = await document_service.create_document(
|
||||
created_doc = document_service.create_document(
|
||||
test_path,
|
||||
sample_file_bytes,
|
||||
"utf-8"
|
||||
)
|
||||
|
||||
# Execute
|
||||
result = await document_service.get_document_by_filepath(test_path)
|
||||
result = document_service.get_document_by_filepath(test_path)
|
||||
|
||||
# Verify
|
||||
assert result is not None
|
||||
@@ -285,8 +277,7 @@ class TestGetMethods:
|
||||
assert result.id == created_doc.id
|
||||
|
||||
@patch('app.services.document_service.magic.from_buffer')
|
||||
@pytest.mark.asyncio
|
||||
async def test_i_can_get_document_content(
|
||||
def test_i_can_get_document_content(
|
||||
self,
|
||||
mock_magic,
|
||||
document_service,
|
||||
@@ -297,38 +288,36 @@ class TestGetMethods:
|
||||
mock_magic.return_value = "application/pdf"
|
||||
|
||||
# Create a document first
|
||||
created_doc = await document_service.create_document(
|
||||
created_doc = document_service.create_document(
|
||||
"/test/test.pdf",
|
||||
sample_file_bytes,
|
||||
"utf-8"
|
||||
)
|
||||
|
||||
# Execute
|
||||
result = await document_service.get_document_content_by_hash(created_doc.file_hash)
|
||||
result = document_service.get_document_content_by_hash(created_doc.file_hash)
|
||||
|
||||
# Verify
|
||||
assert result == sample_file_bytes
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_i_cannot_get_nonexistent_document_by_id(
|
||||
def test_i_cannot_get_nonexistent_document_by_id(
|
||||
self,
|
||||
document_service
|
||||
):
|
||||
"""Test that nonexistent document returns None."""
|
||||
# Execute with random ObjectId
|
||||
result = await document_service.get_document_by_id(ObjectId())
|
||||
result = document_service.get_document_by_id(ObjectId())
|
||||
|
||||
# Verify
|
||||
assert result is None
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_i_cannot_get_nonexistent_document_by_hash(
|
||||
def test_i_cannot_get_nonexistent_document_by_hash(
|
||||
self,
|
||||
document_service
|
||||
):
|
||||
"""Test that nonexistent document hash returns None."""
|
||||
# Execute
|
||||
result = await document_service.get_document_by_hash("nonexistent_hash")
|
||||
result = document_service.get_document_by_hash("nonexistent_hash")
|
||||
|
||||
# Verify
|
||||
assert result is None
|
||||
@@ -338,8 +327,7 @@ class TestPaginationAndCounting:
|
||||
"""Tests for document listing and counting."""
|
||||
|
||||
@patch('app.services.document_service.magic.from_buffer')
|
||||
@pytest.mark.asyncio
|
||||
async def test_i_can_list_documents_with_pagination(
|
||||
def test_i_can_list_documents_with_pagination(
|
||||
self,
|
||||
mock_magic,
|
||||
document_service,
|
||||
@@ -351,25 +339,24 @@ class TestPaginationAndCounting:
|
||||
|
||||
# Create multiple documents
|
||||
for i in range(5):
|
||||
await document_service.create_document(
|
||||
document_service.create_document(
|
||||
f"/test/test{i}.pdf",
|
||||
sample_file_bytes + bytes(str(i), 'utf-8'), # Make each file unique
|
||||
"utf-8"
|
||||
)
|
||||
|
||||
# Execute with pagination
|
||||
result = await document_service.list_documents(skip=1, limit=2)
|
||||
result = document_service.list_documents(skip=1, limit=2)
|
||||
|
||||
# Verify
|
||||
assert len(result) == 2
|
||||
|
||||
# Test counting
|
||||
total_count = await document_service.count_documents()
|
||||
total_count = document_service.count_documents()
|
||||
assert total_count == 5
|
||||
|
||||
@patch('app.services.document_service.magic.from_buffer')
|
||||
@pytest.mark.asyncio
|
||||
async def test_i_can_count_documents(
|
||||
def test_i_can_count_documents(
|
||||
self,
|
||||
mock_magic,
|
||||
document_service,
|
||||
@@ -380,19 +367,19 @@ class TestPaginationAndCounting:
|
||||
mock_magic.return_value = "text/plain"
|
||||
|
||||
# Initially should be 0
|
||||
initial_count = await document_service.count_documents()
|
||||
initial_count = document_service.count_documents()
|
||||
assert initial_count == 0
|
||||
|
||||
# Create some documents
|
||||
for i in range(3):
|
||||
await document_service.create_document(
|
||||
document_service.create_document(
|
||||
f"/test/test{i}.txt",
|
||||
sample_file_bytes + bytes(str(i), 'utf-8'),
|
||||
"utf-8"
|
||||
)
|
||||
|
||||
# Execute
|
||||
final_count = await document_service.count_documents()
|
||||
final_count = document_service.count_documents()
|
||||
|
||||
# Verify
|
||||
assert final_count == 3
|
||||
@@ -402,8 +389,7 @@ class TestUpdateAndDelete:
|
||||
"""Tests for document update and deletion operations."""
|
||||
|
||||
@patch('app.services.document_service.magic.from_buffer')
|
||||
@pytest.mark.asyncio
|
||||
async def test_i_can_update_document_metadata(
|
||||
def test_i_can_update_document_metadata(
|
||||
self,
|
||||
mock_magic,
|
||||
document_service,
|
||||
@@ -414,7 +400,7 @@ class TestUpdateAndDelete:
|
||||
mock_magic.return_value = "application/pdf"
|
||||
|
||||
# Create a document first
|
||||
created_doc = await document_service.create_document(
|
||||
created_doc = document_service.create_document(
|
||||
"/test/test.pdf",
|
||||
sample_file_bytes,
|
||||
"utf-8"
|
||||
@@ -422,7 +408,7 @@ class TestUpdateAndDelete:
|
||||
|
||||
# Execute update
|
||||
update_data = {"metadata": {"page_count": 5}}
|
||||
result = await document_service.update_document(created_doc.id, update_data)
|
||||
result = document_service.update_document(created_doc.id, update_data)
|
||||
|
||||
# Verify
|
||||
assert result is not None
|
||||
@@ -433,14 +419,13 @@ class TestUpdateAndDelete:
|
||||
assert result.file_type == created_doc.file_type
|
||||
assert result.metadata == update_data['metadata']
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_i_can_update_document_content(
|
||||
def test_i_can_update_document_content(
|
||||
self,
|
||||
document_service,
|
||||
sample_file_bytes
|
||||
):
|
||||
# Create a document first
|
||||
created_doc = await document_service.create_document(
|
||||
created_doc = document_service.create_document(
|
||||
"/test/test.pdf",
|
||||
sample_file_bytes,
|
||||
"utf-8"
|
||||
@@ -448,7 +433,7 @@ class TestUpdateAndDelete:
|
||||
|
||||
# Execute update
|
||||
update_data = {"file_bytes": b"this is an updated file content"}
|
||||
result = await document_service.update_document(created_doc.id, update_data)
|
||||
result = document_service.update_document(created_doc.id, update_data)
|
||||
|
||||
assert result.filename == created_doc.filename
|
||||
assert result.filepath == created_doc.filepath
|
||||
@@ -460,8 +445,7 @@ class TestUpdateAndDelete:
|
||||
validate_file_saved(document_service, result.file_hash, b"this is an updated file content")
|
||||
|
||||
@patch('app.services.document_service.magic.from_buffer')
|
||||
@pytest.mark.asyncio
|
||||
async def test_i_can_delete_document_and_orphaned_content(
|
||||
def test_i_can_delete_document_and_orphaned_content(
|
||||
self,
|
||||
mock_magic,
|
||||
document_service,
|
||||
@@ -472,7 +456,7 @@ class TestUpdateAndDelete:
|
||||
mock_magic.return_value = "application/pdf"
|
||||
|
||||
# Create a document
|
||||
created_doc = await document_service.create_document(
|
||||
created_doc = document_service.create_document(
|
||||
"/test/test.pdf",
|
||||
sample_file_bytes,
|
||||
"utf-8"
|
||||
@@ -482,12 +466,12 @@ class TestUpdateAndDelete:
|
||||
validate_file_saved(document_service, created_doc.file_hash, sample_file_bytes)
|
||||
|
||||
# Execute deletion
|
||||
result = await document_service.delete_document(created_doc.id)
|
||||
result = document_service.delete_document(created_doc.id)
|
||||
|
||||
# Verify document and content are deleted
|
||||
assert result is True
|
||||
|
||||
deleted_doc = await document_service.get_document_by_id(created_doc.id)
|
||||
deleted_doc = document_service.get_document_by_id(created_doc.id)
|
||||
assert deleted_doc is None
|
||||
|
||||
# validate content is deleted
|
||||
@@ -496,8 +480,7 @@ class TestUpdateAndDelete:
|
||||
assert not os.path.exists(target_file_path)
|
||||
|
||||
@patch('app.services.document_service.magic.from_buffer')
|
||||
@pytest.mark.asyncio
|
||||
async def test_i_can_delete_document_without_affecting_shared_content(
|
||||
def test_i_can_delete_document_without_affecting_shared_content(
|
||||
self,
|
||||
mock_magic,
|
||||
document_service,
|
||||
@@ -508,13 +491,13 @@ class TestUpdateAndDelete:
|
||||
mock_magic.return_value = "application/pdf"
|
||||
|
||||
# Create two documents with same content
|
||||
doc1 = await document_service.create_document(
|
||||
doc1 = document_service.create_document(
|
||||
"/test/test1.pdf",
|
||||
sample_file_bytes,
|
||||
"utf-8"
|
||||
)
|
||||
|
||||
doc2 = await document_service.create_document(
|
||||
doc2 = document_service.create_document(
|
||||
"/test/test2.pdf",
|
||||
sample_file_bytes,
|
||||
"utf-8"
|
||||
@@ -524,14 +507,14 @@ class TestUpdateAndDelete:
|
||||
assert doc1.file_hash == doc2.file_hash
|
||||
|
||||
# Delete first document
|
||||
result = await document_service.delete_document(doc1.id)
|
||||
result = document_service.delete_document(doc1.id)
|
||||
assert result is True
|
||||
|
||||
# Verify first document is deleted but content still exists
|
||||
deleted_doc = await document_service.get_document_by_id(doc1.id)
|
||||
deleted_doc = document_service.get_document_by_id(doc1.id)
|
||||
assert deleted_doc is None
|
||||
|
||||
remaining_doc = await document_service.get_document_by_id(doc2.id)
|
||||
remaining_doc = document_service.get_document_by_id(doc2.id)
|
||||
assert remaining_doc is not None
|
||||
|
||||
validate_file_saved(document_service, doc2.file_hash, sample_file_bytes)
|
||||
|
||||
Reference in New Issue
Block a user