Private
Public Access
FilterRule had no indexes beyond the default primary/foreign-key ones, and apply_filter_rules_for_feed issued 2 queries per subscriber (its enabled rules, its subscription tags) inside a loop over every subscriber of the feed — both cost grew with subscriber/rule count on every single feed poll. Adds a composite index on (user, is_enabled, priority), matching the exact filter+order shape every caller already uses (this task, apps.rules.tasks' backlog re-apply). apply_filter_rules_for_feed now fetches rules and subscription tags once for the whole feed's subscriber set, grouped by user/subscription in Python, instead of once per subscriber. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
190 lines
7.0 KiB
Python
190 lines
7.0 KiB
Python
import pytest
|
|
from django.contrib.auth import get_user_model
|
|
|
|
from apps.feeds.models import Feed, Subscription
|
|
from apps.rules.models import FilterRule
|
|
|
|
from .models import STARRED_TAG_NAME, Article, ArticleTag, SubscriptionTag, Tag, UserArticleState
|
|
from .tasks import apply_filter_rules_for_feed
|
|
|
|
User = get_user_model()
|
|
|
|
|
|
def is_starred(user, article):
|
|
return ArticleTag.objects.filter(
|
|
user=user, article=article, tag__is_system=True, tag__name=STARRED_TAG_NAME
|
|
).exists()
|
|
|
|
|
|
@pytest.fixture
|
|
def feed(db):
|
|
return Feed.objects.create(feed_url="https://example.com/feed.xml", title="Example Feed")
|
|
|
|
|
|
@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.mark.django_db
|
|
def test_dispatch_applies_rules_only_for_subscribers_with_matching_rules(feed, alice, bob):
|
|
Subscription.objects.create(user=alice, feed=feed)
|
|
Subscription.objects.create(user=bob, feed=feed)
|
|
FilterRule.objects.create(
|
|
user=alice,
|
|
match_field=FilterRule.FIELD_TITLE,
|
|
match_type=FilterRule.MATCH_CONTAINS,
|
|
match_value="Python",
|
|
action=FilterRule.ACTION_STAR,
|
|
)
|
|
article = Article.objects.create(feed=feed, guid="guid-1", title="Python 4 released")
|
|
|
|
apply_filter_rules_for_feed(feed.id, [article.pk])
|
|
|
|
assert is_starred(alice, article) is True
|
|
# Bob has no rules, so he should get no state row at all.
|
|
assert not UserArticleState.objects.filter(user=bob, article=article).exists()
|
|
|
|
|
|
@pytest.mark.django_db
|
|
def test_feed_scoped_rule_does_not_apply_to_other_feeds(alice):
|
|
feed_a = Feed.objects.create(feed_url="https://a.example.com/feed.xml")
|
|
feed_b = Feed.objects.create(feed_url="https://b.example.com/feed.xml")
|
|
Subscription.objects.create(user=alice, feed=feed_a)
|
|
Subscription.objects.create(user=alice, feed=feed_b)
|
|
|
|
FilterRule.objects.create(
|
|
user=alice,
|
|
feed=feed_a,
|
|
match_field=FilterRule.FIELD_TITLE,
|
|
match_type=FilterRule.MATCH_CONTAINS,
|
|
match_value="Python",
|
|
action=FilterRule.ACTION_STAR,
|
|
)
|
|
|
|
article_a = Article.objects.create(feed=feed_a, guid="guid-1", title="Python news")
|
|
article_b = Article.objects.create(feed=feed_b, guid="guid-2", title="Python news too")
|
|
|
|
apply_filter_rules_for_feed(feed_a.id, [article_a.pk])
|
|
apply_filter_rules_for_feed(feed_b.id, [article_b.pk])
|
|
|
|
assert is_starred(alice, article_a) is True
|
|
assert not UserArticleState.objects.filter(user=alice, article=article_b).exists()
|
|
|
|
|
|
@pytest.mark.django_db
|
|
def test_tag_scoped_rule_only_applies_to_feeds_carrying_that_tag(alice, feed):
|
|
other_feed = Feed.objects.create(feed_url="https://other.example.com/feed.xml")
|
|
tagged_sub = Subscription.objects.create(user=alice, feed=feed)
|
|
Subscription.objects.create(user=alice, feed=other_feed)
|
|
tag = Tag.objects.create(user=alice, name="Tech")
|
|
SubscriptionTag.objects.create(user=alice, subscription=tagged_sub, tag=tag)
|
|
|
|
FilterRule.objects.create(
|
|
user=alice,
|
|
tag=tag,
|
|
match_field=FilterRule.FIELD_TITLE,
|
|
match_type=FilterRule.MATCH_CONTAINS,
|
|
match_value="Python",
|
|
action=FilterRule.ACTION_STAR,
|
|
)
|
|
|
|
tagged_article = Article.objects.create(feed=feed, guid="guid-1", title="Python in tagged feed")
|
|
untagged_article = Article.objects.create(
|
|
feed=other_feed, guid="guid-2", title="Python in untagged feed"
|
|
)
|
|
|
|
apply_filter_rules_for_feed(feed.id, [tagged_article.pk])
|
|
apply_filter_rules_for_feed(other_feed.id, [untagged_article.pk])
|
|
|
|
assert is_starred(alice, tagged_article) is True
|
|
assert not UserArticleState.objects.filter(user=alice, article=untagged_article).exists()
|
|
|
|
|
|
@pytest.mark.django_db
|
|
def test_rule_scoped_to_one_tag_still_applies_to_a_feed_with_multiple_tags(alice, feed):
|
|
subscription = Subscription.objects.create(user=alice, feed=feed)
|
|
tech_tag = Tag.objects.create(user=alice, name="Tech")
|
|
news_tag = Tag.objects.create(user=alice, name="News")
|
|
SubscriptionTag.objects.create(user=alice, subscription=subscription, tag=tech_tag)
|
|
SubscriptionTag.objects.create(user=alice, subscription=subscription, tag=news_tag)
|
|
|
|
FilterRule.objects.create(
|
|
user=alice,
|
|
tag=news_tag,
|
|
match_field=FilterRule.FIELD_TITLE,
|
|
match_type=FilterRule.MATCH_CONTAINS,
|
|
match_value="Python",
|
|
action=FilterRule.ACTION_STAR,
|
|
)
|
|
article = Article.objects.create(feed=feed, guid="guid-1", title="Python news")
|
|
|
|
apply_filter_rules_for_feed(feed.id, [article.pk])
|
|
|
|
assert is_starred(alice, article) is True
|
|
|
|
|
|
@pytest.mark.django_db
|
|
def test_disabled_rule_does_not_apply(feed, alice):
|
|
Subscription.objects.create(user=alice, feed=feed)
|
|
FilterRule.objects.create(
|
|
user=alice,
|
|
match_field=FilterRule.FIELD_TITLE,
|
|
match_type=FilterRule.MATCH_CONTAINS,
|
|
match_value="Python",
|
|
action=FilterRule.ACTION_STAR,
|
|
is_enabled=False,
|
|
)
|
|
article = Article.objects.create(feed=feed, guid="guid-1", title="Python 4 released")
|
|
|
|
apply_filter_rules_for_feed(feed.id, [article.pk])
|
|
|
|
assert not UserArticleState.objects.filter(user=alice, article=article).exists()
|
|
|
|
|
|
@pytest.mark.django_db
|
|
def test_dispatch_with_empty_article_ids_is_a_noop(feed):
|
|
assert apply_filter_rules_for_feed(feed.id, []) == 0
|
|
|
|
|
|
@pytest.mark.django_db
|
|
def test_dispatch_fetches_rules_and_tags_once_per_feed_not_once_per_subscriber(feed):
|
|
"""Rules/subscription tags are batched across every subscriber of the
|
|
feed in one query each (C9), not re-queried per subscriber — assert
|
|
this by checking the query count stays flat as subscribers grow."""
|
|
from django.db import connection
|
|
from django.test.utils import CaptureQueriesContext
|
|
|
|
users = [User.objects.create_user(f"user{i}", password="pw-123456") for i in range(5)]
|
|
for user in users:
|
|
sub = Subscription.objects.create(user=user, feed=feed)
|
|
tag = Tag.objects.create(user=user, name="Tech")
|
|
SubscriptionTag.objects.create(user=user, subscription=sub, tag=tag)
|
|
FilterRule.objects.create(
|
|
user=user,
|
|
tag=tag,
|
|
match_field=FilterRule.FIELD_TITLE,
|
|
match_type=FilterRule.MATCH_CONTAINS,
|
|
match_value="Python",
|
|
action=FilterRule.ACTION_STAR,
|
|
)
|
|
article = Article.objects.create(feed=feed, guid="guid-1", title="Python news")
|
|
|
|
with CaptureQueriesContext(connection) as ctx:
|
|
apply_filter_rules_for_feed(feed.id, [article.pk])
|
|
|
|
# Exactly one SELECT against each table regardless of subscriber
|
|
# count (5 here) — a regression back to per-subscription querying
|
|
# would show up as 5 of each instead of 1.
|
|
rule_selects = [q for q in ctx.captured_queries if "rules_filterrule" in q["sql"]]
|
|
tag_selects = [q for q in ctx.captured_queries if "articles_subscriptiontag" in q["sql"]]
|
|
assert len(rule_selects) == 1
|
|
assert len(tag_selects) == 1
|
|
for user in users:
|
|
assert is_starred(user, article) is True
|