add tag subscriptions
This commit is contained in:
5
test/factories/tag_subscription.rb
Normal file
5
test/factories/tag_subscription.rb
Normal 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
11
test/fixtures/tag_subscriptions.yml
vendored
Normal 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
|
||||
@@ -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")
|
||||
|
||||
65
test/unit/tag_subscription_test.rb
Normal file
65
test/unit/tag_subscription_test.rb
Normal 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
|
||||
Reference in New Issue
Block a user