Private
Public Access
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>
58 lines
1.9 KiB
Python
58 lines
1.9 KiB
Python
import pytest
|
|
from django.contrib.auth import get_user_model
|
|
from django.urls import reverse
|
|
|
|
from apps.articles.models import Article, UserArticleState
|
|
from apps.feeds.models import Feed, Subscription
|
|
|
|
User = get_user_model()
|
|
|
|
|
|
@pytest.fixture
|
|
def alice(db):
|
|
return User.objects.create_user("alice", password="pw-alice-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_requires_login(client):
|
|
response = client.get(reverse("search:search"), {"q": "django"})
|
|
assert response.status_code == 302
|
|
|
|
|
|
@pytest.mark.django_db
|
|
def test_search_view_renders_matching_articles(client, alice, feed):
|
|
Subscription.objects.create(user=alice, feed=feed)
|
|
article = Article.objects.create(feed=feed, guid="g1", title="Django 5 release notes")
|
|
client.login(username="alice", password="pw-alice-123")
|
|
|
|
response = client.get(reverse("search:search"), {"q": "Django"})
|
|
|
|
assert response.status_code == 200
|
|
assert article.title in response.content.decode()
|
|
|
|
|
|
@pytest.mark.django_db
|
|
def test_search_view_with_no_query_shows_no_results_block(client, alice):
|
|
client.login(username="alice", password="pw-alice-123")
|
|
response = client.get(reverse("search:search"))
|
|
assert response.status_code == 200
|
|
assert b"article-list" not in response.content
|
|
|
|
|
|
@pytest.mark.django_db
|
|
def test_search_results_carry_user_state(client, alice, feed):
|
|
Subscription.objects.create(user=alice, feed=feed)
|
|
article = Article.objects.create(feed=feed, guid="g1", title="Django 5 release notes")
|
|
UserArticleState.objects.create(user=alice, article=article, is_starred=True)
|
|
client.login(username="alice", password="pw-alice-123")
|
|
|
|
response = client.get(reverse("search:search"), {"q": "Django"})
|
|
content = response.content.decode()
|
|
|
|
assert 'class="icon-btn active"' in content
|