add tag subscriptions

This commit is contained in:
albert
2010-03-09 15:58:24 -05:00
parent cd0aa75dbc
commit 65561a0779
7 changed files with 2987 additions and 1 deletions

View File

@@ -0,0 +1,5 @@
Factory.define(:tag_subscription) do |f|
f.owner {|x| x.association(:user)}
f.name {Faker::Lorem.words.join(" ")}
f.is_visible_on_profile true
end

11
test/fixtures/tag_subscriptions.yml vendored Normal file
View File

@@ -0,0 +1,11 @@
# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html
# This model initially had no columns defined. If you add columns to the
# model remove the '{}' from the fixture names and add the columns immediately
# below each fixture, per the syntax in the comments below
#
one: {}
# column: value
#
two: {}
# column: value

View File

@@ -13,7 +13,7 @@ class RelatedTagCalculatorTest < ActiveSupport::TestCase
assert_equal({"bbb" => 3, "ccc" => 2, "ddd" => 1}, calculator.calculate_from_sample("aaa", 10))
end
should "calculate related tags for a tag" do
should "calculate typed related tags for a tag" do
posts = []
posts << Factory.create(:post, :tag_string => "aaa bbb art:ccc copy:ddd")
posts << Factory.create(:post, :tag_string => "aaa bbb art:ccc")

View File

@@ -0,0 +1,65 @@
require File.dirname(__FILE__) + '/../test_helper'
class TagSubscriptionTest < ActiveSupport::TestCase
context "A tag subscription" do
should "find the union of all posts for each tag in its tag query" do
posts = []
user = Factory.create(:user)
posts << Factory.create(:post, :tag_string => "aaa")
posts << Factory.create(:post, :tag_string => "bbb")
posts << Factory.create(:post, :tag_string => "ccc")
posts << Factory.create(:post, :tag_string => "ddd")
sub_1 = Factory.create(:tag_subscription, :tag_query => "aaa bbb", :owner => user, :name => "zzz")
sub_2 = Factory.create(:tag_subscription, :tag_query => "ccc", :owner => user, :name => "yyy")
assert_equal([posts[1].id, posts[0].id], TagSubscription.find_posts(user.id, "zzz").map(&:id))
assert_equal([posts[2].id, posts[1].id, posts[0].id], TagSubscription.find_posts(user.id).map(&:id))
end
should "cache its tag query results" do
posts = []
user = Factory.create(:user)
posts << Factory.create(:post, :tag_string => "aaa")
posts << Factory.create(:post, :tag_string => "bbb")
posts << Factory.create(:post, :tag_string => "ccc")
sub = Factory.create(:tag_subscription, :tag_query => "aaa bbb", :owner => user, :name => "zzz")
assert_equal("#{posts[1].id},#{posts[0].id}", sub.post_ids)
end
should "find posts based on its cached post ids" do
user = Factory.create(:user)
subs = []
subs << Factory.create(:tag_subscription, :tag_query => "aaa", :owner => user, :name => "zzz")
subs << Factory.create(:tag_subscription, :tag_query => "bbb", :owner => user, :name => "yyy")
assert_equal([], TagSubscription.find_posts(user.id))
assert_equal([], TagSubscription.find_posts(user.id, "zzz"))
assert_equal([], TagSubscription.find_posts(user.id, "yyy"))
posts = []
posts << Factory.create(:post, :tag_string => "aaa")
posts << Factory.create(:post, :tag_string => "bbb")
posts << Factory.create(:post, :tag_string => "ccc")
subs.each {|x| x.process; x.save}
assert_equal([posts[1].id, posts[0].id], TagSubscription.find_posts(user.id).map(&:id))
assert_equal([posts[0].id], TagSubscription.find_posts(user.id, "zzz").map(&:id))
assert_equal([posts[1].id], TagSubscription.find_posts(user.id, "yyy").map(&:id))
end
end
context "A tag subscription manager" do
should "process all active tag subscriptions" do
users = []
users << Factory.create(:user)
users << Factory.create(:user)
posts = []
posts << Factory.create(:post, :tag_string => "aaa")
posts << Factory.create(:post, :tag_string => "bbb")
posts << Factory.create(:post, :tag_string => "ccc")
subscriptions = []
subscriptions << Factory.create(:tag_subscription, :tag_query => "aaa", :owner => users[0])
subscriptions << Factory.create(:tag_subscription, :tag_query => "bbb", :owner => users[1])
TagSubscription.process_all
subscriptions.each {|x| x.reload}
assert_equal("#{posts[0].id}", subscriptions[0].post_ids)
assert_equal("#{posts[1].id}", subscriptions[1].post_ids)
end
end
end