Files
Evection/apps/search/test_sqlite_backend.py
ksmithandClaude Sonnet 5 8653dbeaf9 Add full-text search with per-backend SQLite/Postgres implementations
Implements the backend-agnostic search interface from the plan,
selected at runtime by connection.vendor:

- SQLite: an FTS5 virtual table (external-content, mirroring
  Article.title/content_html) created via a vendor-conditional
  migration, kept in sync by insert/update/delete triggers. Queries
  wrap the whole user input as a single FTS5 phrase so punctuation in
  a search box can't break MATCH syntax. `reindex_search` management
  command rebuilds it after bulk imports.
- Postgres: computes the search vector on the fly per the plan's
  decision (title weighted above content, no stored/indexed column)
  rather than diverging the schema from SQLite.

Both backends scope results to the requesting user's own subscriptions
and share the stream/detail article-row template and read/star/save
actions via a new apps.articles.queries.with_user_state() helper,
extracted from apps.reader.views to avoid duplicating that annotation
logic for search results.

Verified live: the SQLite path end-to-end in-browser against the real
Hacker News subscription (search UI, phrase matching, no-match case),
and the Postgres path against a throwaway Postgres container (search,
feed-scoping, and title-vs-content ranking), torn down afterward — not
part of this project's own infrastructure.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-10-30 16:37:22 -06:00

128 lines
4.1 KiB
Python

import pytest
from django.contrib.auth import get_user_model
from django.db import connection
from apps.articles.models import Article
from apps.feeds.models import Feed, Subscription
from .backends.sqlite_fts import SQLiteFTSBackend
User = get_user_model()
pytestmark = pytest.mark.skipif(
connection.vendor != "sqlite",
reason="SQLite-only backend; the articles_fts virtual table only exists on SQLite.",
)
@pytest.fixture
def alice(db):
return User.objects.create_user("alice", password="pw-alice-123")
@pytest.fixture
def bob(db):
return User.objects.create_user("bob", password="pw-bob-123")
@pytest.fixture
def feed(db):
return Feed.objects.create(feed_url="https://example.com/feed.xml", title="Example Feed")
@pytest.mark.django_db
def test_search_finds_article_by_title(alice, feed):
Subscription.objects.create(user=alice, feed=feed)
article = Article.objects.create(feed=feed, guid="g1", title="Django 5 release notes")
Article.objects.create(feed=feed, guid="g2", title="Completely unrelated")
results = SQLiteFTSBackend().search("Django", alice)
assert [a.pk for a in results] == [article.pk]
@pytest.mark.django_db
def test_search_finds_article_by_content(alice, feed):
Subscription.objects.create(user=alice, feed=feed)
article = Article.objects.create(
feed=feed,
guid="g1",
title="Weekly update",
content_html="<p>We shipped WireGuard support.</p>",
)
results = SQLiteFTSBackend().search("WireGuard", alice)
assert [a.pk for a in results] == [article.pk]
@pytest.mark.django_db
def test_search_only_returns_subscribed_feeds(alice, bob, feed):
other_feed = Feed.objects.create(feed_url="https://other.example.com/feed.xml")
Subscription.objects.create(user=alice, feed=feed)
Subscription.objects.create(user=bob, feed=other_feed)
Article.objects.create(feed=feed, guid="g1", title="Django in feed A")
article_b = Article.objects.create(feed=other_feed, guid="g2", title="Django in feed B")
alice_results = SQLiteFTSBackend().search("Django", alice)
bob_results = SQLiteFTSBackend().search("Django", bob)
assert len(alice_results) == 1
assert [a.pk for a in bob_results] == [article_b.pk]
@pytest.mark.django_db
def test_search_with_no_matches_returns_empty(alice, feed):
Subscription.objects.create(user=alice, feed=feed)
Article.objects.create(feed=feed, guid="g1", title="Something else entirely")
assert SQLiteFTSBackend().search("nonexistentword", alice) == []
@pytest.mark.django_db
def test_search_handles_special_characters_without_crashing(alice, feed):
Subscription.objects.create(user=alice, feed=feed)
Article.objects.create(feed=feed, guid="g1", title="C++ vs Rust: a comparison (2024)")
# Characters that are meaningful in raw FTS5 query syntax shouldn't
# raise — the backend wraps the whole query as a phrase.
results = SQLiteFTSBackend().search("C++ vs Rust: a comparison (2024)", alice)
assert len(results) == 1
@pytest.mark.django_db
def test_search_index_updates_on_article_edit(alice, feed):
Subscription.objects.create(user=alice, feed=feed)
article = Article.objects.create(feed=feed, guid="g1", title="Old title")
assert SQLiteFTSBackend().search("New", alice) == []
article.title = "New title"
article.save()
results = SQLiteFTSBackend().search("New", alice)
assert [a.pk for a in results] == [article.pk]
@pytest.mark.django_db
def test_search_index_updates_on_article_delete(alice, feed):
Subscription.objects.create(user=alice, feed=feed)
article = Article.objects.create(feed=feed, guid="g1", title="Removable article")
assert len(SQLiteFTSBackend().search("Removable", alice)) == 1
article.delete()
assert SQLiteFTSBackend().search("Removable", alice) == []
@pytest.mark.django_db
def test_user_with_no_subscriptions_gets_empty_results(alice):
assert SQLiteFTSBackend().search("anything", alice) == []
@pytest.mark.django_db
def test_empty_query_returns_empty(alice, feed):
Subscription.objects.create(user=alice, feed=feed)
assert SQLiteFTSBackend().search("", alice) == []
assert SQLiteFTSBackend().search(" ", alice) == []