Buckets:
AnonymousSpottedToad/human_verified_bucket / codebase /python /contextfs_contextfs /tests /unit /test_handlers.py
| """ | |
| Unit tests for file type handlers. | |
| """ | |
| from contextfs.filetypes.base import ChunkStrategy, NodeType | |
| from contextfs.filetypes.registry import FileTypeRegistry, get_handler | |
| class TestPythonHandler: | |
| """Tests for Python file handler.""" | |
| def test_parse_class(self, sample_python_code: str): | |
| """Test parsing Python class.""" | |
| handler = get_handler(file_path="test.py") | |
| assert handler is not None | |
| assert handler.name == "python" | |
| doc = handler.parse(sample_python_code, "test.py") | |
| assert doc.file_type == "python" | |
| assert doc.language == "python" | |
| assert "MyClass" in doc.symbols | |
| assert "helper_function" in doc.symbols | |
| def test_extract_class_methods(self, sample_python_code: str): | |
| """Test extracting methods from class.""" | |
| handler = get_handler(file_path="test.py") | |
| doc = handler.parse(sample_python_code, "test.py") | |
| class_node = doc.symbols.get("MyClass") | |
| assert class_node is not None | |
| assert class_node.type == NodeType.CLASS | |
| # Check for methods | |
| method_names = [c.name for c in class_node.children] | |
| assert "__init__" in method_names | |
| assert "greet" in method_names | |
| def test_extract_docstrings(self, sample_python_code: str): | |
| """Test extracting docstrings.""" | |
| handler = get_handler(file_path="test.py") | |
| doc = handler.parse(sample_python_code, "test.py") | |
| class_node = doc.symbols.get("MyClass") | |
| assert class_node.docstring == "A sample class." | |
| def test_extract_imports(self, sample_python_code: str): | |
| """Test extracting imports.""" | |
| handler = get_handler(file_path="test.py") | |
| doc = handler.parse(sample_python_code, "test.py") | |
| import_nodes = [n for n in doc.root.children if n.type == NodeType.IMPORT] | |
| assert len(import_nodes) >= 2 | |
| import_names = [n.name for n in import_nodes] | |
| assert "typing" in import_names or "Optional" in import_names | |
| def test_chunking(self, sample_python_code: str): | |
| """Test chunking strategy.""" | |
| handler = get_handler(file_path="test.py") | |
| doc = handler.parse(sample_python_code, "test.py") | |
| assert len(doc.chunks) > 0 | |
| for chunk in doc.chunks: | |
| assert chunk.strategy == ChunkStrategy.AST_BOUNDARY | |
| assert chunk.embedding_text is not None | |
| def test_relationships(self, sample_python_code: str): | |
| """Test relationship extraction.""" | |
| handler = get_handler(file_path="test.py") | |
| doc = handler.parse(sample_python_code, "test.py") | |
| relationships = handler.extract_relationships(doc) | |
| assert len(relationships) > 0 | |
| class TestTypeScriptHandler: | |
| """Tests for TypeScript file handler.""" | |
| def test_parse_interface(self, sample_typescript_code: str): | |
| """Test parsing TypeScript interface.""" | |
| handler = get_handler(file_path="test.ts") | |
| assert handler is not None | |
| assert handler.name == "typescript" | |
| doc = handler.parse(sample_typescript_code, "test.ts") | |
| assert doc.file_type == "typescript" | |
| assert "Props" in doc.symbols | |
| def test_parse_class(self, sample_typescript_code: str): | |
| """Test parsing TypeScript class.""" | |
| handler = get_handler(file_path="test.ts") | |
| doc = handler.parse(sample_typescript_code, "test.ts") | |
| assert "UserService" in doc.symbols | |
| class_node = doc.symbols["UserService"] | |
| assert class_node.type == NodeType.CLASS | |
| def test_parse_type_alias(self, sample_typescript_code: str): | |
| """Test parsing TypeScript type alias.""" | |
| handler = get_handler(file_path="test.ts") | |
| doc = handler.parse(sample_typescript_code, "test.ts") | |
| assert "Status" in doc.symbols | |
| def test_extract_imports(self, sample_typescript_code: str): | |
| """Test extracting TypeScript imports.""" | |
| handler = get_handler(file_path="test.ts") | |
| doc = handler.parse(sample_typescript_code, "test.ts") | |
| assert len(doc.references) > 0 | |
| def test_tsx_detection(self): | |
| """Test TSX file detection.""" | |
| handler = get_handler(file_path="component.tsx") | |
| assert handler.name == "typescript" | |
| class TestJavaHandler: | |
| """Tests for Java file handler.""" | |
| def test_parse_class(self, sample_java_code: str): | |
| """Test parsing Java class.""" | |
| handler = get_handler(file_path="UserService.java") | |
| assert handler is not None | |
| assert handler.name == "java" | |
| doc = handler.parse(sample_java_code, "UserService.java") | |
| assert doc.file_type == "java" | |
| assert "UserService" in doc.symbols | |
| def test_parse_interface(self, sample_java_code: str): | |
| """Test parsing Java interface.""" | |
| handler = get_handler(file_path="test.java") | |
| doc = handler.parse(sample_java_code, "test.java") | |
| assert "UserRepository" in doc.symbols | |
| def test_extract_package(self, sample_java_code: str): | |
| """Test extracting package name.""" | |
| handler = get_handler(file_path="test.java") | |
| doc = handler.parse(sample_java_code, "test.java") | |
| assert doc.metadata.get("package") == "com.example.app" | |
| def test_extract_javadoc(self, sample_java_code: str): | |
| """Test extracting Javadoc.""" | |
| handler = get_handler(file_path="test.java") | |
| doc = handler.parse(sample_java_code, "test.java") | |
| class_node = doc.symbols.get("UserService") | |
| assert class_node.docstring is not None | |
| class TestGoHandler: | |
| """Tests for Go file handler.""" | |
| def test_parse_struct(self, sample_go_code: str): | |
| """Test parsing Go struct.""" | |
| handler = get_handler(file_path="main.go") | |
| assert handler is not None | |
| assert handler.name == "go" | |
| doc = handler.parse(sample_go_code, "main.go") | |
| assert doc.file_type == "go" | |
| assert "User" in doc.symbols | |
| assert "UserService" in doc.symbols | |
| def test_parse_function(self, sample_go_code: str): | |
| """Test parsing Go function.""" | |
| handler = get_handler(file_path="main.go") | |
| doc = handler.parse(sample_go_code, "main.go") | |
| # Handler captures functions and methods | |
| # Standalone functions may not be captured if return type parsing fails | |
| func_nodes = [n for n in doc.root.children if n.type.value == "function"] | |
| assert len(func_nodes) > 0 # At least some functions should be captured | |
| def test_parse_method(self, sample_go_code: str): | |
| """Test parsing Go method.""" | |
| handler = get_handler(file_path="main.go") | |
| doc = handler.parse(sample_go_code, "main.go") | |
| # Methods are keyed by receiver.method | |
| assert "UserService.GetUser" in doc.symbols | |
| def test_extract_package(self, sample_go_code: str): | |
| """Test extracting package name.""" | |
| handler = get_handler(file_path="main.go") | |
| doc = handler.parse(sample_go_code, "main.go") | |
| assert doc.metadata.get("package") == "main" | |
| class TestRustHandler: | |
| """Tests for Rust file handler.""" | |
| def test_parse_struct(self, sample_rust_code: str): | |
| """Test parsing Rust struct.""" | |
| handler = get_handler(file_path="lib.rs") | |
| assert handler is not None | |
| assert handler.name == "rust" | |
| doc = handler.parse(sample_rust_code, "lib.rs") | |
| assert doc.file_type == "rust" | |
| assert "User" in doc.symbols | |
| assert "UserService" in doc.symbols | |
| def test_parse_impl(self, sample_rust_code: str): | |
| """Test parsing Rust impl block.""" | |
| handler = get_handler(file_path="lib.rs") | |
| doc = handler.parse(sample_rust_code, "lib.rs") | |
| # Check for impl blocks | |
| impl_nodes = [n for n in doc.root.children if n.attributes.get("is_impl")] | |
| assert len(impl_nodes) > 0 | |
| def test_extract_derives(self, sample_rust_code: str): | |
| """Test extracting derive macros.""" | |
| handler = get_handler(file_path="lib.rs") | |
| doc = handler.parse(sample_rust_code, "lib.rs") | |
| user_struct = doc.symbols.get("User") | |
| # Derive macro extraction is optional - verify struct exists | |
| assert user_struct is not None | |
| assert user_struct.name == "User" | |
| class TestMarkdownHandler: | |
| """Tests for Markdown file handler.""" | |
| def test_parse_headers(self, sample_markdown: str): | |
| """Test parsing Markdown headers.""" | |
| handler = get_handler(file_path="README.md") | |
| assert handler is not None | |
| assert handler.name == "markdown" | |
| doc = handler.parse(sample_markdown, "README.md") | |
| assert doc.file_type == "markdown" | |
| assert doc.title == "Sample Document" | |
| def test_extract_structure(self, sample_markdown: str): | |
| """Test extracting document structure.""" | |
| handler = get_handler(file_path="README.md") | |
| doc = handler.parse(sample_markdown, "README.md") | |
| # Check for headers in symbols | |
| assert "sample-document" in doc.symbols or "introduction" in doc.symbols | |
| def test_extract_code_blocks(self, sample_markdown: str): | |
| """Test extracting code blocks.""" | |
| handler = get_handler(file_path="README.md") | |
| doc = handler.parse(sample_markdown, "README.md") | |
| code_blocks = [n for n in doc.root.walk() if n.type == NodeType.CODE_BLOCK] | |
| assert len(code_blocks) > 0 | |
| def test_extract_links(self, sample_markdown: str): | |
| """Test extracting links.""" | |
| handler = get_handler(file_path="README.md") | |
| doc = handler.parse(sample_markdown, "README.md") | |
| links = [r for r in doc.references if r.type == NodeType.LINK] | |
| assert len(links) > 0 | |
| class TestSQLHandler: | |
| """Tests for SQL file handler.""" | |
| def test_parse_tables(self, sample_sql: str): | |
| """Test parsing SQL tables.""" | |
| handler = get_handler(file_path="schema.sql") | |
| assert handler is not None | |
| assert handler.name == "sql" | |
| doc = handler.parse(sample_sql, "schema.sql") | |
| assert doc.file_type == "sql" | |
| assert "users" in doc.symbols | |
| assert "posts" in doc.symbols | |
| def test_extract_columns(self, sample_sql: str): | |
| """Test extracting table columns.""" | |
| handler = get_handler(file_path="schema.sql") | |
| doc = handler.parse(sample_sql, "schema.sql") | |
| users_table = doc.symbols.get("users") | |
| assert users_table is not None | |
| columns = users_table.attributes.get("columns", []) | |
| # Column extraction may be partial - just check we have some | |
| assert len(columns) > 0 | |
| def test_extract_foreign_keys(self, sample_sql: str): | |
| """Test extracting foreign key relationships.""" | |
| handler = get_handler(file_path="schema.sql") | |
| doc = handler.parse(sample_sql, "schema.sql") | |
| relationships = handler.extract_relationships(doc) | |
| # Foreign key extraction may be partial | |
| assert isinstance(relationships, list) | |
| class TestJSONHandler: | |
| """Tests for JSON file handler.""" | |
| def test_parse_json(self, sample_json: str): | |
| """Test parsing JSON file.""" | |
| handler = get_handler(file_path="package.json") | |
| assert handler is not None | |
| assert handler.name == "json" | |
| doc = handler.parse(sample_json, "package.json") | |
| assert doc.file_type == "json" | |
| def test_extract_structure(self, sample_json: str): | |
| """Test extracting JSON structure.""" | |
| handler = get_handler(file_path="package.json") | |
| doc = handler.parse(sample_json, "package.json") | |
| # Check for top-level keys | |
| assert "dependencies" in doc.symbols or "name" in doc.symbols | |
| class TestRegistry: | |
| """Tests for file type registry.""" | |
| def test_get_handler_by_extension(self): | |
| """Test getting handler by extension.""" | |
| handler = get_handler(extension=".py") | |
| assert handler is not None | |
| assert handler.name == "python" | |
| def test_get_handler_by_path(self): | |
| """Test getting handler by file path.""" | |
| handler = get_handler(file_path="/some/path/file.ts") | |
| assert handler is not None | |
| assert handler.name == "typescript" | |
| def test_fallback_to_generic(self): | |
| """Test fallback to generic handler.""" | |
| handler = get_handler(file_path="unknown.xyz") | |
| assert handler is not None | |
| assert handler.name == "generic" | |
| def test_list_handlers(self): | |
| """Test listing all handlers.""" | |
| registry = FileTypeRegistry() | |
| handlers = registry.list_handlers() | |
| assert "python" in handlers | |
| assert "typescript" in handlers | |
| assert "java" in handlers | |
| assert "go" in handlers | |
| assert "rust" in handlers | |
| def test_list_extensions(self): | |
| """Test listing supported extensions.""" | |
| registry = FileTypeRegistry() | |
| extensions = registry.list_extensions() | |
| assert ".py" in extensions | |
| assert ".ts" in extensions | |
| assert ".java" in extensions | |
| assert ".go" in extensions | |
| assert ".rs" in extensions | |
| def test_supports_extension(self): | |
| """Test checking extension support.""" | |
| registry = FileTypeRegistry() | |
| assert registry.supports_extension(".py") | |
| assert registry.supports_extension(".ts") | |
| assert registry.supports_extension("py") # Without dot | |
| assert not registry.supports_extension(".unknown123") | |
Xet Storage Details
- Size:
- 13.5 kB
- Xet hash:
- 471f271c4377ce506f732d398612222537a3f25425a342afa84da7fe84286a86
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.