2025-09-26 10:53:32 +01:00
|
|
|
"""
|
|
|
|
|
Unit tests for trustgraph.chunking.recursive
|
|
|
|
|
Testing parameter override functionality for chunk-size and chunk-overlap
|
|
|
|
|
"""
|
|
|
|
|
|
2025-08-08 18:59:27 +01:00
|
|
|
import pytest
|
2025-09-26 10:53:32 +01:00
|
|
|
from unittest.mock import AsyncMock, MagicMock, patch
|
|
|
|
|
from unittest import IsolatedAsyncioTestCase
|
|
|
|
|
|
|
|
|
|
# Import the service under test
|
|
|
|
|
from trustgraph.chunking.recursive.chunker import Processor
|
2025-08-08 18:59:27 +01:00
|
|
|
from trustgraph.schema import TextDocument, Chunk, Metadata
|
2025-09-26 10:53:32 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
class MockAsyncProcessor:
|
|
|
|
|
def __init__(self, **params):
|
|
|
|
|
self.config_handlers = []
|
|
|
|
|
self.id = params.get('id', 'test-service')
|
|
|
|
|
self.specifications = []
|
Incremental / large document loading (#659)
Tech spec
BlobStore (trustgraph-flow/trustgraph/librarian/blob_store.py):
- get_stream() - yields document content in chunks for streaming retrieval
- create_multipart_upload() - initializes S3 multipart upload, returns
upload_id
- upload_part() - uploads a single part, returns etag
- complete_multipart_upload() - finalizes upload with part etags
- abort_multipart_upload() - cancels and cleans up
Cassandra schema (trustgraph-flow/trustgraph/tables/library.py):
- New upload_session table with 24-hour TTL
- Index on user for listing sessions
- Prepared statements for all operations
- Methods: create_upload_session(), get_upload_session(),
update_upload_session_chunk(), delete_upload_session(),
list_upload_sessions()
- Schema extended with UploadSession, UploadProgress, and new
request/response fields
- Librarian methods: begin_upload, upload_chunk, complete_upload,
abort_upload, get_upload_status, list_uploads
- Service routing for all new operations
- Python SDK with transparent chunked upload:
- add_document() auto-switches to chunked for files > 10MB
- Progress callback support (on_progress)
- get_pending_uploads(), get_upload_status(), abort_upload(),
resume_upload()
- Document table: Added parent_id and document_type columns with index
- Document schema (knowledge/document.py): Added document_id field for
streaming retrieval
- Librarian operations:
- add-child-document for extracted PDF pages
- list-children to get child documents
- stream-document for chunked content retrieval
- Cascade delete removes children when parent is deleted
- list-documents filters children by default
- PDF decoder (decoding/pdf/pdf_decoder.py): Updated to stream large
documents from librarian API to temp file
- Librarian service (librarian/service.py): Sends document_id instead of
content for large PDFs (>2MB)
- Deprecated tools (load_pdf.py, load_text.py): Added deprecation
warnings directing users to tg-add-library-document +
tg-start-library-processing
Remove load_pdf and load_text utils
Move chunker/librarian comms to base class
Updating tests
2026-03-04 16:57:58 +00:00
|
|
|
self.pubsub = MagicMock()
|
|
|
|
|
self.taskgroup = params.get('taskgroup', MagicMock())
|
2025-09-26 10:53:32 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
class TestRecursiveChunkerSimple(IsolatedAsyncioTestCase):
|
|
|
|
|
"""Test Recursive chunker functionality"""
|
|
|
|
|
|
Incremental / large document loading (#659)
Tech spec
BlobStore (trustgraph-flow/trustgraph/librarian/blob_store.py):
- get_stream() - yields document content in chunks for streaming retrieval
- create_multipart_upload() - initializes S3 multipart upload, returns
upload_id
- upload_part() - uploads a single part, returns etag
- complete_multipart_upload() - finalizes upload with part etags
- abort_multipart_upload() - cancels and cleans up
Cassandra schema (trustgraph-flow/trustgraph/tables/library.py):
- New upload_session table with 24-hour TTL
- Index on user for listing sessions
- Prepared statements for all operations
- Methods: create_upload_session(), get_upload_session(),
update_upload_session_chunk(), delete_upload_session(),
list_upload_sessions()
- Schema extended with UploadSession, UploadProgress, and new
request/response fields
- Librarian methods: begin_upload, upload_chunk, complete_upload,
abort_upload, get_upload_status, list_uploads
- Service routing for all new operations
- Python SDK with transparent chunked upload:
- add_document() auto-switches to chunked for files > 10MB
- Progress callback support (on_progress)
- get_pending_uploads(), get_upload_status(), abort_upload(),
resume_upload()
- Document table: Added parent_id and document_type columns with index
- Document schema (knowledge/document.py): Added document_id field for
streaming retrieval
- Librarian operations:
- add-child-document for extracted PDF pages
- list-children to get child documents
- stream-document for chunked content retrieval
- Cascade delete removes children when parent is deleted
- list-documents filters children by default
- PDF decoder (decoding/pdf/pdf_decoder.py): Updated to stream large
documents from librarian API to temp file
- Librarian service (librarian/service.py): Sends document_id instead of
content for large PDFs (>2MB)
- Deprecated tools (load_pdf.py, load_text.py): Added deprecation
warnings directing users to tg-add-library-document +
tg-start-library-processing
Remove load_pdf and load_text utils
Move chunker/librarian comms to base class
Updating tests
2026-03-04 16:57:58 +00:00
|
|
|
@patch('trustgraph.base.chunking_service.Consumer')
|
|
|
|
|
@patch('trustgraph.base.chunking_service.Producer')
|
2025-09-26 10:53:32 +01:00
|
|
|
@patch('trustgraph.base.async_processor.AsyncProcessor', MockAsyncProcessor)
|
Incremental / large document loading (#659)
Tech spec
BlobStore (trustgraph-flow/trustgraph/librarian/blob_store.py):
- get_stream() - yields document content in chunks for streaming retrieval
- create_multipart_upload() - initializes S3 multipart upload, returns
upload_id
- upload_part() - uploads a single part, returns etag
- complete_multipart_upload() - finalizes upload with part etags
- abort_multipart_upload() - cancels and cleans up
Cassandra schema (trustgraph-flow/trustgraph/tables/library.py):
- New upload_session table with 24-hour TTL
- Index on user for listing sessions
- Prepared statements for all operations
- Methods: create_upload_session(), get_upload_session(),
update_upload_session_chunk(), delete_upload_session(),
list_upload_sessions()
- Schema extended with UploadSession, UploadProgress, and new
request/response fields
- Librarian methods: begin_upload, upload_chunk, complete_upload,
abort_upload, get_upload_status, list_uploads
- Service routing for all new operations
- Python SDK with transparent chunked upload:
- add_document() auto-switches to chunked for files > 10MB
- Progress callback support (on_progress)
- get_pending_uploads(), get_upload_status(), abort_upload(),
resume_upload()
- Document table: Added parent_id and document_type columns with index
- Document schema (knowledge/document.py): Added document_id field for
streaming retrieval
- Librarian operations:
- add-child-document for extracted PDF pages
- list-children to get child documents
- stream-document for chunked content retrieval
- Cascade delete removes children when parent is deleted
- list-documents filters children by default
- PDF decoder (decoding/pdf/pdf_decoder.py): Updated to stream large
documents from librarian API to temp file
- Librarian service (librarian/service.py): Sends document_id instead of
content for large PDFs (>2MB)
- Deprecated tools (load_pdf.py, load_text.py): Added deprecation
warnings directing users to tg-add-library-document +
tg-start-library-processing
Remove load_pdf and load_text utils
Move chunker/librarian comms to base class
Updating tests
2026-03-04 16:57:58 +00:00
|
|
|
def test_processor_initialization_basic(self, mock_producer, mock_consumer):
|
2025-09-26 10:53:32 +01:00
|
|
|
"""Test basic processor initialization"""
|
|
|
|
|
# Arrange
|
|
|
|
|
config = {
|
|
|
|
|
'id': 'test-chunker',
|
|
|
|
|
'chunk_size': 1500,
|
|
|
|
|
'chunk_overlap': 150,
|
|
|
|
|
'concurrency': 1,
|
|
|
|
|
'taskgroup': AsyncMock()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# Act
|
|
|
|
|
processor = Processor(**config)
|
|
|
|
|
|
|
|
|
|
# Assert
|
|
|
|
|
assert processor.default_chunk_size == 1500
|
|
|
|
|
assert processor.default_chunk_overlap == 150
|
|
|
|
|
assert hasattr(processor, 'text_splitter')
|
|
|
|
|
|
|
|
|
|
# Verify parameter specs are registered
|
|
|
|
|
param_specs = [spec for spec in processor.specifications
|
|
|
|
|
if hasattr(spec, 'name') and spec.name in ['chunk-size', 'chunk-overlap']]
|
|
|
|
|
assert len(param_specs) == 2
|
|
|
|
|
|
Incremental / large document loading (#659)
Tech spec
BlobStore (trustgraph-flow/trustgraph/librarian/blob_store.py):
- get_stream() - yields document content in chunks for streaming retrieval
- create_multipart_upload() - initializes S3 multipart upload, returns
upload_id
- upload_part() - uploads a single part, returns etag
- complete_multipart_upload() - finalizes upload with part etags
- abort_multipart_upload() - cancels and cleans up
Cassandra schema (trustgraph-flow/trustgraph/tables/library.py):
- New upload_session table with 24-hour TTL
- Index on user for listing sessions
- Prepared statements for all operations
- Methods: create_upload_session(), get_upload_session(),
update_upload_session_chunk(), delete_upload_session(),
list_upload_sessions()
- Schema extended with UploadSession, UploadProgress, and new
request/response fields
- Librarian methods: begin_upload, upload_chunk, complete_upload,
abort_upload, get_upload_status, list_uploads
- Service routing for all new operations
- Python SDK with transparent chunked upload:
- add_document() auto-switches to chunked for files > 10MB
- Progress callback support (on_progress)
- get_pending_uploads(), get_upload_status(), abort_upload(),
resume_upload()
- Document table: Added parent_id and document_type columns with index
- Document schema (knowledge/document.py): Added document_id field for
streaming retrieval
- Librarian operations:
- add-child-document for extracted PDF pages
- list-children to get child documents
- stream-document for chunked content retrieval
- Cascade delete removes children when parent is deleted
- list-documents filters children by default
- PDF decoder (decoding/pdf/pdf_decoder.py): Updated to stream large
documents from librarian API to temp file
- Librarian service (librarian/service.py): Sends document_id instead of
content for large PDFs (>2MB)
- Deprecated tools (load_pdf.py, load_text.py): Added deprecation
warnings directing users to tg-add-library-document +
tg-start-library-processing
Remove load_pdf and load_text utils
Move chunker/librarian comms to base class
Updating tests
2026-03-04 16:57:58 +00:00
|
|
|
@patch('trustgraph.base.chunking_service.Consumer')
|
|
|
|
|
@patch('trustgraph.base.chunking_service.Producer')
|
2025-09-26 10:53:32 +01:00
|
|
|
@patch('trustgraph.base.async_processor.AsyncProcessor', MockAsyncProcessor)
|
Incremental / large document loading (#659)
Tech spec
BlobStore (trustgraph-flow/trustgraph/librarian/blob_store.py):
- get_stream() - yields document content in chunks for streaming retrieval
- create_multipart_upload() - initializes S3 multipart upload, returns
upload_id
- upload_part() - uploads a single part, returns etag
- complete_multipart_upload() - finalizes upload with part etags
- abort_multipart_upload() - cancels and cleans up
Cassandra schema (trustgraph-flow/trustgraph/tables/library.py):
- New upload_session table with 24-hour TTL
- Index on user for listing sessions
- Prepared statements for all operations
- Methods: create_upload_session(), get_upload_session(),
update_upload_session_chunk(), delete_upload_session(),
list_upload_sessions()
- Schema extended with UploadSession, UploadProgress, and new
request/response fields
- Librarian methods: begin_upload, upload_chunk, complete_upload,
abort_upload, get_upload_status, list_uploads
- Service routing for all new operations
- Python SDK with transparent chunked upload:
- add_document() auto-switches to chunked for files > 10MB
- Progress callback support (on_progress)
- get_pending_uploads(), get_upload_status(), abort_upload(),
resume_upload()
- Document table: Added parent_id and document_type columns with index
- Document schema (knowledge/document.py): Added document_id field for
streaming retrieval
- Librarian operations:
- add-child-document for extracted PDF pages
- list-children to get child documents
- stream-document for chunked content retrieval
- Cascade delete removes children when parent is deleted
- list-documents filters children by default
- PDF decoder (decoding/pdf/pdf_decoder.py): Updated to stream large
documents from librarian API to temp file
- Librarian service (librarian/service.py): Sends document_id instead of
content for large PDFs (>2MB)
- Deprecated tools (load_pdf.py, load_text.py): Added deprecation
warnings directing users to tg-add-library-document +
tg-start-library-processing
Remove load_pdf and load_text utils
Move chunker/librarian comms to base class
Updating tests
2026-03-04 16:57:58 +00:00
|
|
|
async def test_chunk_document_with_chunk_size_override(self, mock_producer, mock_consumer):
|
2025-09-26 10:53:32 +01:00
|
|
|
"""Test chunk_document with chunk-size parameter override"""
|
|
|
|
|
# Arrange
|
|
|
|
|
config = {
|
|
|
|
|
'id': 'test-chunker',
|
|
|
|
|
'chunk_size': 1000, # Default chunk size
|
|
|
|
|
'chunk_overlap': 100,
|
|
|
|
|
'concurrency': 1,
|
|
|
|
|
'taskgroup': AsyncMock()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
processor = Processor(**config)
|
|
|
|
|
|
|
|
|
|
# Mock message and flow
|
|
|
|
|
mock_message = MagicMock()
|
|
|
|
|
mock_consumer = MagicMock()
|
|
|
|
|
mock_flow = MagicMock()
|
|
|
|
|
mock_flow.side_effect = lambda param: {
|
|
|
|
|
"chunk-size": 2000, # Override chunk size
|
|
|
|
|
"chunk-overlap": None # Use default chunk overlap
|
|
|
|
|
}.get(param)
|
|
|
|
|
|
|
|
|
|
# Act
|
|
|
|
|
chunk_size, chunk_overlap = await processor.chunk_document(
|
|
|
|
|
mock_message, mock_consumer, mock_flow, 1000, 100
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
# Assert
|
|
|
|
|
assert chunk_size == 2000 # Should use overridden value
|
|
|
|
|
assert chunk_overlap == 100 # Should use default value
|
|
|
|
|
|
Incremental / large document loading (#659)
Tech spec
BlobStore (trustgraph-flow/trustgraph/librarian/blob_store.py):
- get_stream() - yields document content in chunks for streaming retrieval
- create_multipart_upload() - initializes S3 multipart upload, returns
upload_id
- upload_part() - uploads a single part, returns etag
- complete_multipart_upload() - finalizes upload with part etags
- abort_multipart_upload() - cancels and cleans up
Cassandra schema (trustgraph-flow/trustgraph/tables/library.py):
- New upload_session table with 24-hour TTL
- Index on user for listing sessions
- Prepared statements for all operations
- Methods: create_upload_session(), get_upload_session(),
update_upload_session_chunk(), delete_upload_session(),
list_upload_sessions()
- Schema extended with UploadSession, UploadProgress, and new
request/response fields
- Librarian methods: begin_upload, upload_chunk, complete_upload,
abort_upload, get_upload_status, list_uploads
- Service routing for all new operations
- Python SDK with transparent chunked upload:
- add_document() auto-switches to chunked for files > 10MB
- Progress callback support (on_progress)
- get_pending_uploads(), get_upload_status(), abort_upload(),
resume_upload()
- Document table: Added parent_id and document_type columns with index
- Document schema (knowledge/document.py): Added document_id field for
streaming retrieval
- Librarian operations:
- add-child-document for extracted PDF pages
- list-children to get child documents
- stream-document for chunked content retrieval
- Cascade delete removes children when parent is deleted
- list-documents filters children by default
- PDF decoder (decoding/pdf/pdf_decoder.py): Updated to stream large
documents from librarian API to temp file
- Librarian service (librarian/service.py): Sends document_id instead of
content for large PDFs (>2MB)
- Deprecated tools (load_pdf.py, load_text.py): Added deprecation
warnings directing users to tg-add-library-document +
tg-start-library-processing
Remove load_pdf and load_text utils
Move chunker/librarian comms to base class
Updating tests
2026-03-04 16:57:58 +00:00
|
|
|
@patch('trustgraph.base.chunking_service.Consumer')
|
|
|
|
|
@patch('trustgraph.base.chunking_service.Producer')
|
2025-09-26 10:53:32 +01:00
|
|
|
@patch('trustgraph.base.async_processor.AsyncProcessor', MockAsyncProcessor)
|
Incremental / large document loading (#659)
Tech spec
BlobStore (trustgraph-flow/trustgraph/librarian/blob_store.py):
- get_stream() - yields document content in chunks for streaming retrieval
- create_multipart_upload() - initializes S3 multipart upload, returns
upload_id
- upload_part() - uploads a single part, returns etag
- complete_multipart_upload() - finalizes upload with part etags
- abort_multipart_upload() - cancels and cleans up
Cassandra schema (trustgraph-flow/trustgraph/tables/library.py):
- New upload_session table with 24-hour TTL
- Index on user for listing sessions
- Prepared statements for all operations
- Methods: create_upload_session(), get_upload_session(),
update_upload_session_chunk(), delete_upload_session(),
list_upload_sessions()
- Schema extended with UploadSession, UploadProgress, and new
request/response fields
- Librarian methods: begin_upload, upload_chunk, complete_upload,
abort_upload, get_upload_status, list_uploads
- Service routing for all new operations
- Python SDK with transparent chunked upload:
- add_document() auto-switches to chunked for files > 10MB
- Progress callback support (on_progress)
- get_pending_uploads(), get_upload_status(), abort_upload(),
resume_upload()
- Document table: Added parent_id and document_type columns with index
- Document schema (knowledge/document.py): Added document_id field for
streaming retrieval
- Librarian operations:
- add-child-document for extracted PDF pages
- list-children to get child documents
- stream-document for chunked content retrieval
- Cascade delete removes children when parent is deleted
- list-documents filters children by default
- PDF decoder (decoding/pdf/pdf_decoder.py): Updated to stream large
documents from librarian API to temp file
- Librarian service (librarian/service.py): Sends document_id instead of
content for large PDFs (>2MB)
- Deprecated tools (load_pdf.py, load_text.py): Added deprecation
warnings directing users to tg-add-library-document +
tg-start-library-processing
Remove load_pdf and load_text utils
Move chunker/librarian comms to base class
Updating tests
2026-03-04 16:57:58 +00:00
|
|
|
async def test_chunk_document_with_chunk_overlap_override(self, mock_producer, mock_consumer):
|
2025-09-26 10:53:32 +01:00
|
|
|
"""Test chunk_document with chunk-overlap parameter override"""
|
|
|
|
|
# Arrange
|
|
|
|
|
config = {
|
|
|
|
|
'id': 'test-chunker',
|
|
|
|
|
'chunk_size': 1000,
|
|
|
|
|
'chunk_overlap': 100, # Default chunk overlap
|
|
|
|
|
'concurrency': 1,
|
|
|
|
|
'taskgroup': AsyncMock()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
processor = Processor(**config)
|
|
|
|
|
|
|
|
|
|
# Mock message and flow
|
|
|
|
|
mock_message = MagicMock()
|
|
|
|
|
mock_consumer = MagicMock()
|
|
|
|
|
mock_flow = MagicMock()
|
|
|
|
|
mock_flow.side_effect = lambda param: {
|
|
|
|
|
"chunk-size": None, # Use default chunk size
|
|
|
|
|
"chunk-overlap": 200 # Override chunk overlap
|
|
|
|
|
}.get(param)
|
|
|
|
|
|
|
|
|
|
# Act
|
|
|
|
|
chunk_size, chunk_overlap = await processor.chunk_document(
|
|
|
|
|
mock_message, mock_consumer, mock_flow, 1000, 100
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
# Assert
|
|
|
|
|
assert chunk_size == 1000 # Should use default value
|
|
|
|
|
assert chunk_overlap == 200 # Should use overridden value
|
|
|
|
|
|
Incremental / large document loading (#659)
Tech spec
BlobStore (trustgraph-flow/trustgraph/librarian/blob_store.py):
- get_stream() - yields document content in chunks for streaming retrieval
- create_multipart_upload() - initializes S3 multipart upload, returns
upload_id
- upload_part() - uploads a single part, returns etag
- complete_multipart_upload() - finalizes upload with part etags
- abort_multipart_upload() - cancels and cleans up
Cassandra schema (trustgraph-flow/trustgraph/tables/library.py):
- New upload_session table with 24-hour TTL
- Index on user for listing sessions
- Prepared statements for all operations
- Methods: create_upload_session(), get_upload_session(),
update_upload_session_chunk(), delete_upload_session(),
list_upload_sessions()
- Schema extended with UploadSession, UploadProgress, and new
request/response fields
- Librarian methods: begin_upload, upload_chunk, complete_upload,
abort_upload, get_upload_status, list_uploads
- Service routing for all new operations
- Python SDK with transparent chunked upload:
- add_document() auto-switches to chunked for files > 10MB
- Progress callback support (on_progress)
- get_pending_uploads(), get_upload_status(), abort_upload(),
resume_upload()
- Document table: Added parent_id and document_type columns with index
- Document schema (knowledge/document.py): Added document_id field for
streaming retrieval
- Librarian operations:
- add-child-document for extracted PDF pages
- list-children to get child documents
- stream-document for chunked content retrieval
- Cascade delete removes children when parent is deleted
- list-documents filters children by default
- PDF decoder (decoding/pdf/pdf_decoder.py): Updated to stream large
documents from librarian API to temp file
- Librarian service (librarian/service.py): Sends document_id instead of
content for large PDFs (>2MB)
- Deprecated tools (load_pdf.py, load_text.py): Added deprecation
warnings directing users to tg-add-library-document +
tg-start-library-processing
Remove load_pdf and load_text utils
Move chunker/librarian comms to base class
Updating tests
2026-03-04 16:57:58 +00:00
|
|
|
@patch('trustgraph.base.chunking_service.Consumer')
|
|
|
|
|
@patch('trustgraph.base.chunking_service.Producer')
|
2025-09-26 10:53:32 +01:00
|
|
|
@patch('trustgraph.base.async_processor.AsyncProcessor', MockAsyncProcessor)
|
Incremental / large document loading (#659)
Tech spec
BlobStore (trustgraph-flow/trustgraph/librarian/blob_store.py):
- get_stream() - yields document content in chunks for streaming retrieval
- create_multipart_upload() - initializes S3 multipart upload, returns
upload_id
- upload_part() - uploads a single part, returns etag
- complete_multipart_upload() - finalizes upload with part etags
- abort_multipart_upload() - cancels and cleans up
Cassandra schema (trustgraph-flow/trustgraph/tables/library.py):
- New upload_session table with 24-hour TTL
- Index on user for listing sessions
- Prepared statements for all operations
- Methods: create_upload_session(), get_upload_session(),
update_upload_session_chunk(), delete_upload_session(),
list_upload_sessions()
- Schema extended with UploadSession, UploadProgress, and new
request/response fields
- Librarian methods: begin_upload, upload_chunk, complete_upload,
abort_upload, get_upload_status, list_uploads
- Service routing for all new operations
- Python SDK with transparent chunked upload:
- add_document() auto-switches to chunked for files > 10MB
- Progress callback support (on_progress)
- get_pending_uploads(), get_upload_status(), abort_upload(),
resume_upload()
- Document table: Added parent_id and document_type columns with index
- Document schema (knowledge/document.py): Added document_id field for
streaming retrieval
- Librarian operations:
- add-child-document for extracted PDF pages
- list-children to get child documents
- stream-document for chunked content retrieval
- Cascade delete removes children when parent is deleted
- list-documents filters children by default
- PDF decoder (decoding/pdf/pdf_decoder.py): Updated to stream large
documents from librarian API to temp file
- Librarian service (librarian/service.py): Sends document_id instead of
content for large PDFs (>2MB)
- Deprecated tools (load_pdf.py, load_text.py): Added deprecation
warnings directing users to tg-add-library-document +
tg-start-library-processing
Remove load_pdf and load_text utils
Move chunker/librarian comms to base class
Updating tests
2026-03-04 16:57:58 +00:00
|
|
|
async def test_chunk_document_with_both_parameters_override(self, mock_producer, mock_consumer):
|
2025-09-26 10:53:32 +01:00
|
|
|
"""Test chunk_document with both chunk-size and chunk-overlap overrides"""
|
|
|
|
|
# Arrange
|
|
|
|
|
config = {
|
|
|
|
|
'id': 'test-chunker',
|
|
|
|
|
'chunk_size': 1000,
|
|
|
|
|
'chunk_overlap': 100,
|
|
|
|
|
'concurrency': 1,
|
|
|
|
|
'taskgroup': AsyncMock()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
processor = Processor(**config)
|
|
|
|
|
|
|
|
|
|
# Mock message and flow
|
|
|
|
|
mock_message = MagicMock()
|
|
|
|
|
mock_consumer = MagicMock()
|
|
|
|
|
mock_flow = MagicMock()
|
|
|
|
|
mock_flow.side_effect = lambda param: {
|
|
|
|
|
"chunk-size": 1500, # Override chunk size
|
|
|
|
|
"chunk-overlap": 150 # Override chunk overlap
|
|
|
|
|
}.get(param)
|
|
|
|
|
|
|
|
|
|
# Act
|
|
|
|
|
chunk_size, chunk_overlap = await processor.chunk_document(
|
|
|
|
|
mock_message, mock_consumer, mock_flow, 1000, 100
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
# Assert
|
|
|
|
|
assert chunk_size == 1500 # Should use overridden value
|
|
|
|
|
assert chunk_overlap == 150 # Should use overridden value
|
|
|
|
|
|
Incremental / large document loading (#659)
Tech spec
BlobStore (trustgraph-flow/trustgraph/librarian/blob_store.py):
- get_stream() - yields document content in chunks for streaming retrieval
- create_multipart_upload() - initializes S3 multipart upload, returns
upload_id
- upload_part() - uploads a single part, returns etag
- complete_multipart_upload() - finalizes upload with part etags
- abort_multipart_upload() - cancels and cleans up
Cassandra schema (trustgraph-flow/trustgraph/tables/library.py):
- New upload_session table with 24-hour TTL
- Index on user for listing sessions
- Prepared statements for all operations
- Methods: create_upload_session(), get_upload_session(),
update_upload_session_chunk(), delete_upload_session(),
list_upload_sessions()
- Schema extended with UploadSession, UploadProgress, and new
request/response fields
- Librarian methods: begin_upload, upload_chunk, complete_upload,
abort_upload, get_upload_status, list_uploads
- Service routing for all new operations
- Python SDK with transparent chunked upload:
- add_document() auto-switches to chunked for files > 10MB
- Progress callback support (on_progress)
- get_pending_uploads(), get_upload_status(), abort_upload(),
resume_upload()
- Document table: Added parent_id and document_type columns with index
- Document schema (knowledge/document.py): Added document_id field for
streaming retrieval
- Librarian operations:
- add-child-document for extracted PDF pages
- list-children to get child documents
- stream-document for chunked content retrieval
- Cascade delete removes children when parent is deleted
- list-documents filters children by default
- PDF decoder (decoding/pdf/pdf_decoder.py): Updated to stream large
documents from librarian API to temp file
- Librarian service (librarian/service.py): Sends document_id instead of
content for large PDFs (>2MB)
- Deprecated tools (load_pdf.py, load_text.py): Added deprecation
warnings directing users to tg-add-library-document +
tg-start-library-processing
Remove load_pdf and load_text utils
Move chunker/librarian comms to base class
Updating tests
2026-03-04 16:57:58 +00:00
|
|
|
@patch('trustgraph.base.chunking_service.Consumer')
|
|
|
|
|
@patch('trustgraph.base.chunking_service.Producer')
|
2025-09-26 10:53:32 +01:00
|
|
|
@patch('trustgraph.chunking.recursive.chunker.RecursiveCharacterTextSplitter')
|
|
|
|
|
@patch('trustgraph.base.async_processor.AsyncProcessor', MockAsyncProcessor)
|
Incremental / large document loading (#659)
Tech spec
BlobStore (trustgraph-flow/trustgraph/librarian/blob_store.py):
- get_stream() - yields document content in chunks for streaming retrieval
- create_multipart_upload() - initializes S3 multipart upload, returns
upload_id
- upload_part() - uploads a single part, returns etag
- complete_multipart_upload() - finalizes upload with part etags
- abort_multipart_upload() - cancels and cleans up
Cassandra schema (trustgraph-flow/trustgraph/tables/library.py):
- New upload_session table with 24-hour TTL
- Index on user for listing sessions
- Prepared statements for all operations
- Methods: create_upload_session(), get_upload_session(),
update_upload_session_chunk(), delete_upload_session(),
list_upload_sessions()
- Schema extended with UploadSession, UploadProgress, and new
request/response fields
- Librarian methods: begin_upload, upload_chunk, complete_upload,
abort_upload, get_upload_status, list_uploads
- Service routing for all new operations
- Python SDK with transparent chunked upload:
- add_document() auto-switches to chunked for files > 10MB
- Progress callback support (on_progress)
- get_pending_uploads(), get_upload_status(), abort_upload(),
resume_upload()
- Document table: Added parent_id and document_type columns with index
- Document schema (knowledge/document.py): Added document_id field for
streaming retrieval
- Librarian operations:
- add-child-document for extracted PDF pages
- list-children to get child documents
- stream-document for chunked content retrieval
- Cascade delete removes children when parent is deleted
- list-documents filters children by default
- PDF decoder (decoding/pdf/pdf_decoder.py): Updated to stream large
documents from librarian API to temp file
- Librarian service (librarian/service.py): Sends document_id instead of
content for large PDFs (>2MB)
- Deprecated tools (load_pdf.py, load_text.py): Added deprecation
warnings directing users to tg-add-library-document +
tg-start-library-processing
Remove load_pdf and load_text utils
Move chunker/librarian comms to base class
Updating tests
2026-03-04 16:57:58 +00:00
|
|
|
async def test_on_message_uses_flow_parameters(self, mock_splitter_class, mock_producer, mock_consumer):
|
2025-09-26 10:53:32 +01:00
|
|
|
"""Test that on_message method uses parameters from flow"""
|
|
|
|
|
# Arrange
|
|
|
|
|
mock_splitter = MagicMock()
|
|
|
|
|
mock_document = MagicMock()
|
|
|
|
|
mock_document.page_content = "Test chunk content"
|
|
|
|
|
mock_splitter.create_documents.return_value = [mock_document]
|
|
|
|
|
mock_splitter_class.return_value = mock_splitter
|
|
|
|
|
|
|
|
|
|
config = {
|
|
|
|
|
'id': 'test-chunker',
|
|
|
|
|
'chunk_size': 1000,
|
|
|
|
|
'chunk_overlap': 100,
|
|
|
|
|
'concurrency': 1,
|
|
|
|
|
'taskgroup': AsyncMock()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
processor = Processor(**config)
|
|
|
|
|
|
2026-03-05 18:36:10 +00:00
|
|
|
# Mock save_child_document to avoid waiting for librarian response
|
|
|
|
|
processor.save_child_document = AsyncMock(return_value="mock-doc-id")
|
|
|
|
|
|
2025-09-26 10:53:32 +01:00
|
|
|
# Mock message with TextDocument
|
|
|
|
|
mock_message = MagicMock()
|
|
|
|
|
mock_text_doc = MagicMock()
|
|
|
|
|
mock_text_doc.metadata = Metadata(
|
|
|
|
|
id="test-doc-123",
|
|
|
|
|
user="test-user",
|
|
|
|
|
collection="test-collection"
|
|
|
|
|
)
|
|
|
|
|
mock_text_doc.text = b"This is test document content"
|
Incremental / large document loading (#659)
Tech spec
BlobStore (trustgraph-flow/trustgraph/librarian/blob_store.py):
- get_stream() - yields document content in chunks for streaming retrieval
- create_multipart_upload() - initializes S3 multipart upload, returns
upload_id
- upload_part() - uploads a single part, returns etag
- complete_multipart_upload() - finalizes upload with part etags
- abort_multipart_upload() - cancels and cleans up
Cassandra schema (trustgraph-flow/trustgraph/tables/library.py):
- New upload_session table with 24-hour TTL
- Index on user for listing sessions
- Prepared statements for all operations
- Methods: create_upload_session(), get_upload_session(),
update_upload_session_chunk(), delete_upload_session(),
list_upload_sessions()
- Schema extended with UploadSession, UploadProgress, and new
request/response fields
- Librarian methods: begin_upload, upload_chunk, complete_upload,
abort_upload, get_upload_status, list_uploads
- Service routing for all new operations
- Python SDK with transparent chunked upload:
- add_document() auto-switches to chunked for files > 10MB
- Progress callback support (on_progress)
- get_pending_uploads(), get_upload_status(), abort_upload(),
resume_upload()
- Document table: Added parent_id and document_type columns with index
- Document schema (knowledge/document.py): Added document_id field for
streaming retrieval
- Librarian operations:
- add-child-document for extracted PDF pages
- list-children to get child documents
- stream-document for chunked content retrieval
- Cascade delete removes children when parent is deleted
- list-documents filters children by default
- PDF decoder (decoding/pdf/pdf_decoder.py): Updated to stream large
documents from librarian API to temp file
- Librarian service (librarian/service.py): Sends document_id instead of
content for large PDFs (>2MB)
- Deprecated tools (load_pdf.py, load_text.py): Added deprecation
warnings directing users to tg-add-library-document +
tg-start-library-processing
Remove load_pdf and load_text utils
Move chunker/librarian comms to base class
Updating tests
2026-03-04 16:57:58 +00:00
|
|
|
mock_text_doc.document_id = "" # No librarian fetch needed
|
2025-09-26 10:53:32 +01:00
|
|
|
mock_message.value.return_value = mock_text_doc
|
|
|
|
|
|
|
|
|
|
# Mock consumer and flow with parameter overrides
|
|
|
|
|
mock_consumer = MagicMock()
|
|
|
|
|
mock_producer = AsyncMock()
|
2026-03-05 18:36:10 +00:00
|
|
|
mock_triples_producer = AsyncMock()
|
2025-09-26 10:53:32 +01:00
|
|
|
mock_flow = MagicMock()
|
|
|
|
|
mock_flow.side_effect = lambda param: {
|
|
|
|
|
"chunk-size": 1500,
|
|
|
|
|
"chunk-overlap": 150,
|
2026-03-05 18:36:10 +00:00
|
|
|
"output": mock_producer,
|
|
|
|
|
"triples": mock_triples_producer,
|
2025-09-26 10:53:32 +01:00
|
|
|
}.get(param)
|
|
|
|
|
|
|
|
|
|
# Act
|
|
|
|
|
await processor.on_message(mock_message, mock_consumer, mock_flow)
|
|
|
|
|
|
|
|
|
|
# Assert
|
|
|
|
|
# Verify RecursiveCharacterTextSplitter was called with overridden parameters (last call)
|
|
|
|
|
actual_last_call = mock_splitter_class.call_args_list[-1]
|
|
|
|
|
assert actual_last_call.kwargs['chunk_size'] == 1500
|
|
|
|
|
assert actual_last_call.kwargs['chunk_overlap'] == 150
|
|
|
|
|
assert actual_last_call.kwargs['length_function'] == len
|
|
|
|
|
assert actual_last_call.kwargs['is_separator_regex'] == False
|
|
|
|
|
|
|
|
|
|
# Verify chunk was sent to output
|
|
|
|
|
mock_producer.send.assert_called_once()
|
|
|
|
|
sent_chunk = mock_producer.send.call_args[0][0]
|
|
|
|
|
assert isinstance(sent_chunk, Chunk)
|
|
|
|
|
|
Incremental / large document loading (#659)
Tech spec
BlobStore (trustgraph-flow/trustgraph/librarian/blob_store.py):
- get_stream() - yields document content in chunks for streaming retrieval
- create_multipart_upload() - initializes S3 multipart upload, returns
upload_id
- upload_part() - uploads a single part, returns etag
- complete_multipart_upload() - finalizes upload with part etags
- abort_multipart_upload() - cancels and cleans up
Cassandra schema (trustgraph-flow/trustgraph/tables/library.py):
- New upload_session table with 24-hour TTL
- Index on user for listing sessions
- Prepared statements for all operations
- Methods: create_upload_session(), get_upload_session(),
update_upload_session_chunk(), delete_upload_session(),
list_upload_sessions()
- Schema extended with UploadSession, UploadProgress, and new
request/response fields
- Librarian methods: begin_upload, upload_chunk, complete_upload,
abort_upload, get_upload_status, list_uploads
- Service routing for all new operations
- Python SDK with transparent chunked upload:
- add_document() auto-switches to chunked for files > 10MB
- Progress callback support (on_progress)
- get_pending_uploads(), get_upload_status(), abort_upload(),
resume_upload()
- Document table: Added parent_id and document_type columns with index
- Document schema (knowledge/document.py): Added document_id field for
streaming retrieval
- Librarian operations:
- add-child-document for extracted PDF pages
- list-children to get child documents
- stream-document for chunked content retrieval
- Cascade delete removes children when parent is deleted
- list-documents filters children by default
- PDF decoder (decoding/pdf/pdf_decoder.py): Updated to stream large
documents from librarian API to temp file
- Librarian service (librarian/service.py): Sends document_id instead of
content for large PDFs (>2MB)
- Deprecated tools (load_pdf.py, load_text.py): Added deprecation
warnings directing users to tg-add-library-document +
tg-start-library-processing
Remove load_pdf and load_text utils
Move chunker/librarian comms to base class
Updating tests
2026-03-04 16:57:58 +00:00
|
|
|
@patch('trustgraph.base.chunking_service.Consumer')
|
|
|
|
|
@patch('trustgraph.base.chunking_service.Producer')
|
2025-09-26 10:53:32 +01:00
|
|
|
@patch('trustgraph.base.async_processor.AsyncProcessor', MockAsyncProcessor)
|
Incremental / large document loading (#659)
Tech spec
BlobStore (trustgraph-flow/trustgraph/librarian/blob_store.py):
- get_stream() - yields document content in chunks for streaming retrieval
- create_multipart_upload() - initializes S3 multipart upload, returns
upload_id
- upload_part() - uploads a single part, returns etag
- complete_multipart_upload() - finalizes upload with part etags
- abort_multipart_upload() - cancels and cleans up
Cassandra schema (trustgraph-flow/trustgraph/tables/library.py):
- New upload_session table with 24-hour TTL
- Index on user for listing sessions
- Prepared statements for all operations
- Methods: create_upload_session(), get_upload_session(),
update_upload_session_chunk(), delete_upload_session(),
list_upload_sessions()
- Schema extended with UploadSession, UploadProgress, and new
request/response fields
- Librarian methods: begin_upload, upload_chunk, complete_upload,
abort_upload, get_upload_status, list_uploads
- Service routing for all new operations
- Python SDK with transparent chunked upload:
- add_document() auto-switches to chunked for files > 10MB
- Progress callback support (on_progress)
- get_pending_uploads(), get_upload_status(), abort_upload(),
resume_upload()
- Document table: Added parent_id and document_type columns with index
- Document schema (knowledge/document.py): Added document_id field for
streaming retrieval
- Librarian operations:
- add-child-document for extracted PDF pages
- list-children to get child documents
- stream-document for chunked content retrieval
- Cascade delete removes children when parent is deleted
- list-documents filters children by default
- PDF decoder (decoding/pdf/pdf_decoder.py): Updated to stream large
documents from librarian API to temp file
- Librarian service (librarian/service.py): Sends document_id instead of
content for large PDFs (>2MB)
- Deprecated tools (load_pdf.py, load_text.py): Added deprecation
warnings directing users to tg-add-library-document +
tg-start-library-processing
Remove load_pdf and load_text utils
Move chunker/librarian comms to base class
Updating tests
2026-03-04 16:57:58 +00:00
|
|
|
async def test_chunk_document_with_no_overrides(self, mock_producer, mock_consumer):
|
2025-09-26 10:53:32 +01:00
|
|
|
"""Test chunk_document when no parameters are overridden (flow returns None)"""
|
|
|
|
|
# Arrange
|
|
|
|
|
config = {
|
|
|
|
|
'id': 'test-chunker',
|
|
|
|
|
'chunk_size': 1000,
|
|
|
|
|
'chunk_overlap': 100,
|
|
|
|
|
'concurrency': 1,
|
|
|
|
|
'taskgroup': AsyncMock()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
processor = Processor(**config)
|
|
|
|
|
|
|
|
|
|
# Mock message and flow that returns None for all parameters
|
|
|
|
|
mock_message = MagicMock()
|
|
|
|
|
mock_consumer = MagicMock()
|
|
|
|
|
mock_flow = MagicMock()
|
|
|
|
|
mock_flow.return_value = None # No overrides
|
|
|
|
|
|
|
|
|
|
# Act
|
|
|
|
|
chunk_size, chunk_overlap = await processor.chunk_document(
|
|
|
|
|
mock_message, mock_consumer, mock_flow, 1000, 100
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
# Assert
|
|
|
|
|
assert chunk_size == 1000 # Should use default value
|
|
|
|
|
assert chunk_overlap == 100 # Should use default value
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
|
pytest.main([__file__])
|