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.postgres import PostgresSearchBackend User = get_user_model() pytestmark = pytest.mark.skipif( connection.vendor != "postgresql", reason="Postgres-only backend; run with DATABASE_URL pointed at a Postgres instance.", ) @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 = list(PostgresSearchBackend().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="

We shipped WireGuard support.

", ) results = list(PostgresSearchBackend().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 = list(PostgresSearchBackend().search("Django", alice)) bob_results = list(PostgresSearchBackend().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_title_matches_rank_above_content_only_matches(alice, feed): Subscription.objects.create(user=alice, feed=feed) title_match = Article.objects.create(feed=feed, guid="g1", title="Django release") content_match = Article.objects.create( feed=feed, guid="g2", title="Something else", content_html="

mentions Django

" ) results = list(PostgresSearchBackend().search("Django", alice)) assert [a.pk for a in results] == [title_match.pk, content_match.pk] @pytest.mark.django_db def test_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 list(PostgresSearchBackend().search("nonexistentword", alice)) == [] @pytest.mark.django_db def test_empty_query_returns_empty(alice, feed): Subscription.objects.create(user=alice, feed=feed) assert list(PostgresSearchBackend().search("", alice)) == []