145 lines
4.3 KiB
Python
145 lines
4.3 KiB
Python
import pytest
|
|
from pathlib import Path
|
|
from codechunk.core.chunking import CodeChunker, ChunkPriority, ParsedChunk, ChunkMetadata
|
|
from codechunk.config import ChunkingConfig
|
|
|
|
|
|
class TestCodeChunker:
|
|
"""Tests for CodeChunker."""
|
|
|
|
def test_calculate_priority_high_for_main_function(self):
|
|
"""Test that 'main' function gets high priority."""
|
|
config = ChunkingConfig()
|
|
chunker = CodeChunker(config)
|
|
|
|
chunk = ParsedChunk(
|
|
name="main",
|
|
chunk_type="function",
|
|
content="def main():\n pass",
|
|
metadata=ChunkMetadata(
|
|
file_path=Path("test.py"),
|
|
file_name="test.py",
|
|
language="python",
|
|
start_line=1,
|
|
end_line=2,
|
|
line_count=2
|
|
)
|
|
)
|
|
|
|
result = chunker._calculate_priority(chunk)
|
|
|
|
assert result.priority >= 50
|
|
|
|
def test_calculate_priority_class_higher_than_function(self):
|
|
"""Test that classes get higher priority than functions."""
|
|
config = ChunkingConfig()
|
|
chunker = CodeChunker(config)
|
|
|
|
func_chunk = ParsedChunk(
|
|
name="helper",
|
|
chunk_type="function",
|
|
content="def helper():\n pass",
|
|
metadata=ChunkMetadata(
|
|
file_path=Path("test.py"),
|
|
file_name="test.py",
|
|
language="python",
|
|
start_line=1,
|
|
end_line=2,
|
|
line_count=2
|
|
)
|
|
)
|
|
|
|
class_chunk = ParsedChunk(
|
|
name="MyClass",
|
|
chunk_type="class",
|
|
content="class MyClass:\n pass",
|
|
metadata=ChunkMetadata(
|
|
file_path=Path("test.py"),
|
|
file_name="test.py",
|
|
language="python",
|
|
start_line=1,
|
|
end_line=2,
|
|
line_count=2
|
|
)
|
|
)
|
|
|
|
func_priority = chunker._calculate_priority(func_chunk)
|
|
class_priority = chunker._calculate_priority(class_chunk)
|
|
|
|
assert class_priority.priority > func_priority.priority
|
|
|
|
def test_remove_boilerplate_dunder_methods(self):
|
|
"""Test that dunder methods are detected."""
|
|
config = ChunkingConfig()
|
|
chunker = CodeChunker(config)
|
|
|
|
chunk = ParsedChunk(
|
|
name="MyClass.__str__",
|
|
chunk_type="function",
|
|
content="def __str__(self):\n return 'MyClass'",
|
|
metadata=ChunkMetadata(
|
|
file_path=Path("test.py"),
|
|
file_name="test.py",
|
|
language="python",
|
|
start_line=1,
|
|
end_line=2,
|
|
line_count=2
|
|
)
|
|
)
|
|
|
|
result = chunker._remove_boilerplate(chunk)
|
|
assert result.is_boilerplate is True
|
|
|
|
def test_sort_by_priority(self):
|
|
"""Test that chunks are sorted by priority."""
|
|
config = ChunkingConfig()
|
|
chunker = CodeChunker(config)
|
|
|
|
low_chunk = ParsedChunk(
|
|
name="helper",
|
|
chunk_type="function",
|
|
content="def helper():\n pass",
|
|
metadata=ChunkMetadata(
|
|
file_path=Path("test.py"),
|
|
file_name="test.py",
|
|
language="python",
|
|
start_line=1,
|
|
end_line=2,
|
|
line_count=2
|
|
),
|
|
priority=10
|
|
)
|
|
|
|
high_chunk = ParsedChunk(
|
|
name="main",
|
|
chunk_type="function",
|
|
content="def main():\n pass",
|
|
metadata=ChunkMetadata(
|
|
file_path=Path("test.py"),
|
|
file_name="test.py",
|
|
language="python",
|
|
start_line=1,
|
|
end_line=2,
|
|
line_count=2
|
|
),
|
|
priority=100
|
|
)
|
|
|
|
chunks = [low_chunk, high_chunk]
|
|
sorted_chunks = chunker._sort_by_priority(chunks)
|
|
|
|
assert sorted_chunks[0].name == "main"
|
|
assert sorted_chunks[1].name == "helper"
|
|
|
|
|
|
class TestChunkPriority:
|
|
"""Tests for ChunkPriority constants."""
|
|
|
|
def test_priority_values(self):
|
|
"""Test that priority constants have expected values."""
|
|
assert ChunkPriority.CRITICAL == 100
|
|
assert ChunkPriority.HIGH == 75
|
|
assert ChunkPriority.MEDIUM == 50
|
|
assert ChunkPriority.LOW == 25
|
|
assert ChunkPriority.MINIMAL == 10
|