2025-01-30 16:35:17 +00:00
|
|
|
import pytest
|
|
|
|
|
from src.investment.smart_chunking_funcs import parse_chunk, stitch_chunks
|
|
|
|
|
|
2025-08-05 20:46:19 +00:00
|
|
|
|
2025-01-30 16:35:17 +00:00
|
|
|
def test_parse_chunk_valid():
|
|
|
|
|
chunk = "chunk 001: This is a test chunk."
|
|
|
|
|
chunk_number, content = parse_chunk(chunk)
|
|
|
|
|
assert chunk_number == 1
|
|
|
|
|
assert content == "This is a test chunk."
|
|
|
|
|
|
2025-08-05 20:46:19 +00:00
|
|
|
|
2025-01-30 16:35:17 +00:00
|
|
|
def test_parse_chunk_valid_with_large_number():
|
|
|
|
|
chunk = "chunk 123: Another test chunk with a larger number."
|
|
|
|
|
chunk_number, content = parse_chunk(chunk)
|
|
|
|
|
assert chunk_number == 123
|
|
|
|
|
assert content == "Another test chunk with a larger number."
|
|
|
|
|
|
2025-08-05 20:46:19 +00:00
|
|
|
|
2025-01-30 16:35:17 +00:00
|
|
|
def test_parse_chunk_empty_content():
|
|
|
|
|
chunk = "chunk 005: "
|
|
|
|
|
chunk_number, content = parse_chunk(chunk)
|
|
|
|
|
assert chunk_number == 5
|
|
|
|
|
assert content == ""
|
|
|
|
|
|
2025-08-05 20:46:19 +00:00
|
|
|
|
2025-01-30 16:35:17 +00:00
|
|
|
def test_parse_chunk_invalid_format():
|
|
|
|
|
chunk = "invalid chunk format"
|
|
|
|
|
with pytest.raises(ValueError):
|
|
|
|
|
parse_chunk(chunk)
|
|
|
|
|
|
2025-08-05 20:46:19 +00:00
|
|
|
|
2025-01-30 16:35:17 +00:00
|
|
|
def test_parse_chunk_short_prefix():
|
|
|
|
|
chunk = "chunk 01: Short prefix"
|
|
|
|
|
with pytest.raises(ValueError):
|
|
|
|
|
parse_chunk(chunk)
|
|
|
|
|
|
2025-08-05 20:46:19 +00:00
|
|
|
|
2025-01-30 16:35:17 +00:00
|
|
|
def test_stitch_chunks_empty_list():
|
|
|
|
|
assert stitch_chunks([]) == ""
|
|
|
|
|
|
2025-08-05 20:46:19 +00:00
|
|
|
|
2025-01-30 16:35:17 +00:00
|
|
|
def test_stitch_chunks_single_chunk():
|
|
|
|
|
assert stitch_chunks(["chunk 001: hello world"]) == "hello world"
|
|
|
|
|
|
2025-08-05 20:46:19 +00:00
|
|
|
|
2025-01-30 16:35:17 +00:00
|
|
|
def test_stitch_chunks_two_chunks():
|
2025-08-05 20:46:19 +00:00
|
|
|
chunks = ["chunk 001: hello world", "chunk 002: world and more"]
|
2025-01-30 16:35:17 +00:00
|
|
|
assert stitch_chunks(chunks, overlap_size=5) == "hello world and more"
|
|
|
|
|
|
2025-08-05 20:46:19 +00:00
|
|
|
|
2025-01-30 16:35:17 +00:00
|
|
|
def test_stitch_chunks_out_of_order():
|
2025-08-05 20:46:19 +00:00
|
|
|
chunks = ["chunk 002: world", "chunk 001: hello"]
|
2025-05-15 19:51:19 +00:00
|
|
|
assert stitch_chunks(chunks, overlap_size=0) in ["helloworld", "hello\nworld"]
|
2025-01-30 16:35:17 +00:00
|
|
|
|
2025-08-05 20:46:19 +00:00
|
|
|
|
2025-01-30 16:35:17 +00:00
|
|
|
def test_stitch_noncontiguous_chunks():
|
2025-08-05 20:46:19 +00:00
|
|
|
chunks = ["chunk 001: hello", "chunk 003: world"]
|
|
|
|
|
assert stitch_chunks(chunks, overlap_size=10) in [
|
|
|
|
|
"helloworld",
|
|
|
|
|
"hello\nworld",
|
|
|
|
|
] # the overlap size will be ignored in the noncontiguous case
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_stitch_long_overlap(): # not enough text to overlap, so we just append them
|
|
|
|
|
chunks = ["chunk 001: hello", "chunk 002: world"]
|
2025-05-15 19:51:19 +00:00
|
|
|
assert stitch_chunks(chunks, overlap_size=10) in ["helloworld", "hello\nworld"]
|
2025-01-30 16:35:17 +00:00
|
|
|
|
2025-08-05 20:46:19 +00:00
|
|
|
|
2025-01-30 16:35:17 +00:00
|
|
|
def test_stitch_chunks_negative_overlap():
|
2025-08-05 20:46:19 +00:00
|
|
|
chunks = ["chunk 001: hello", "chunk 002: world"]
|
2025-01-30 16:35:17 +00:00
|
|
|
with pytest.raises(ValueError):
|
2025-08-05 20:46:19 +00:00
|
|
|
stitch_chunks(chunks, overlap_size=-1)
|