Push correct test_search.py with matching API
This commit is contained in:
@@ -1,51 +1,95 @@
|
|||||||
"""Tests for search functionality."""
|
"""Tests for search module."""
|
||||||
|
|
||||||
import os
|
import os
|
||||||
import tempfile
|
import tempfile
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from snip.db.database import Database
|
from snip.db import Database
|
||||||
from snip.search.engine import SearchEngine
|
from snip.search import SearchEngine
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def db():
|
def search_engine():
|
||||||
|
"""Create search engine with in-memory database."""
|
||||||
with tempfile.NamedTemporaryFile(suffix=".db", delete=False) as f:
|
with tempfile.NamedTemporaryFile(suffix=".db", delete=False) as f:
|
||||||
db_path = f.name
|
db_path = f.name
|
||||||
database = Database(db_path)
|
db = Database(db_path)
|
||||||
database.init_db()
|
db.init_schema()
|
||||||
yield database
|
engine = SearchEngine(db_path)
|
||||||
|
|
||||||
|
db.create_snippet(
|
||||||
|
title="Python Hello World",
|
||||||
|
code="print('hello')",
|
||||||
|
description="A simple Python greeting",
|
||||||
|
language="python",
|
||||||
|
tags=["python", "hello", "beginner"],
|
||||||
|
)
|
||||||
|
db.create_snippet(
|
||||||
|
title="JavaScript Hello",
|
||||||
|
code="console.log('hello')",
|
||||||
|
description="JavaScript greeting",
|
||||||
|
language="javascript",
|
||||||
|
tags=["javascript", "hello"],
|
||||||
|
)
|
||||||
|
db.create_snippet(
|
||||||
|
title="Python List Comprehension",
|
||||||
|
code="[x**2 for x in range(10)]",
|
||||||
|
description="Python list comprehension example",
|
||||||
|
language="python",
|
||||||
|
tags=["python", "advanced"],
|
||||||
|
)
|
||||||
|
|
||||||
|
yield engine
|
||||||
os.unlink(db_path)
|
os.unlink(db_path)
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
def test_basic_search(search_engine):
|
||||||
def search_engine(db):
|
"""Test basic full-text search."""
|
||||||
return SearchEngine(db)
|
|
||||||
|
|
||||||
|
|
||||||
def test_search_basic(search_engine, db):
|
|
||||||
"""Test basic search."""
|
|
||||||
db.add_snippet(title="Hello World", code="print('hello')", language="python")
|
|
||||||
db.add_snippet(title="Goodbye", code="print('bye')", language="python")
|
|
||||||
|
|
||||||
results = search_engine.search("hello")
|
results = search_engine.search("hello")
|
||||||
assert len(results) >= 1
|
assert len(results) == 2
|
||||||
|
|
||||||
|
|
||||||
def test_search_with_language_filter(search_engine, db):
|
def test_search_with_language_filter(search_engine):
|
||||||
"""Test search with language filter."""
|
"""Test search with language filter."""
|
||||||
db.add_snippet(title="Python Hello", code="print('hello')", language="python")
|
|
||||||
db.add_snippet(title="JS Hello", code="console.log('hello')", language="javascript")
|
|
||||||
|
|
||||||
results = search_engine.search("hello", language="python")
|
results = search_engine.search("hello", language="python")
|
||||||
assert all(r["language"] == "python" for r in results)
|
assert len(results) == 1
|
||||||
|
assert results[0]["language"] == "python"
|
||||||
|
|
||||||
|
|
||||||
def test_search_ranking(search_engine, db):
|
def test_search_with_tag_filter(search_engine):
|
||||||
"""Test that search results are ranked."""
|
"""Test search with tag filter."""
|
||||||
db.add_snippet(title="Hello Function", code="def hello(): pass", language="python")
|
results = search_engine.search("*", tag="python")
|
||||||
db.add_snippet(title="Hello Class", code="class Hello: pass", language="python")
|
assert len(results) == 2
|
||||||
|
|
||||||
results = search_engine.search("hello")
|
|
||||||
assert len(results) >= 1
|
def test_search_no_query_returns_all(search_engine):
|
||||||
|
"""Test that empty search returns all snippets."""
|
||||||
|
results = search_engine.search("")
|
||||||
|
assert len(results) == 3
|
||||||
|
|
||||||
|
|
||||||
|
def test_highlight_matches(search_engine):
|
||||||
|
"""Test match highlighting."""
|
||||||
|
text = "This is a hello world example"
|
||||||
|
highlighted = search_engine.highlight(text, "hello world")
|
||||||
|
assert "**" in highlighted
|
||||||
|
|
||||||
|
|
||||||
|
def test_parse_query(search_engine):
|
||||||
|
"""Test query parsing."""
|
||||||
|
parsed = search_engine.parse_query("hello AND world")
|
||||||
|
assert len(parsed["terms"]) >= 1
|
||||||
|
|
||||||
|
|
||||||
|
def test_suggest_completions(search_engine):
|
||||||
|
"""Test tag autocomplete."""
|
||||||
|
suggestions = search_engine.suggest("pyt")
|
||||||
|
assert "python" in suggestions
|
||||||
|
|
||||||
|
|
||||||
|
def test_empty_query_with_filters(search_engine):
|
||||||
|
"""Test empty query with filters still applies filters."""
|
||||||
|
results = search_engine.search("", language="javascript")
|
||||||
|
assert len(results) == 1
|
||||||
|
assert results[0]["language"] == "javascript"
|
||||||
Reference in New Issue
Block a user