added tests for post sets

This commit is contained in:
albert
2011-06-11 17:05:46 -04:00
parent ca7afc10be
commit 1a303fd3db
7 changed files with 202 additions and 23 deletions

View File

@@ -0,0 +1,4 @@
module PostSets
class Error < Exception
end
end

View File

@@ -21,6 +21,17 @@ module PostSets
offset == 0
end
def validate
super
validate_page
end
def validate_page
if page > 1_000
raise Error.new("You cannot explicitly specify the page after page 1000")
end
end
def page
@page ||= params[:page] ? params[:page].to_i : 1
end

View File

@@ -1,11 +1,7 @@
module PostSets
module Post
class Error < Exception ; end
attr_accessor :tags, :count, :wiki_page, :artist, :suggestions
def tags
@tags ||= ::Tag.normalize(params[:tags])
def tag_string
@tag_string ||= params[:tags].to_s.downcase
end
def count
@@ -13,12 +9,12 @@ module PostSets
end
def posts
@posts ||= slice(::Post.tag_match(tags))
@posts ||= slice(::Post.tag_match(tag_string))
end
def reload
super
@tags = nil
@tag_array = nil
@tag_string = nil
@count = nil
@wiki_page = nil
@@ -40,23 +36,16 @@ module PostSets
def is_single_tag?
tag_array.size == 1
end
def tag_array
@tag_array ||= ::Tag.scan_query(tags)
@tag_array ||= ::Tag.scan_query(tag_string)
end
def validate
super
validate_page
validate_query_count
end
def validate_page
if page > 1_000
raise Error.new("You cannot explicitly specify the page after page 1000")
end
end
def validate_query_count
if !CurrentUser.is_privileged? && tag_array.size > 2
raise Error.new("You can only search up to two tags at once with a basic account")

View File

@@ -54,7 +54,7 @@ class Pool < ActiveRecord::Base
def revert_to!(version)
self.post_ids = version.post_ids
synchronize_posts!
synchronize!
end
def contains?(post_id)
@@ -96,7 +96,7 @@ class Pool < ActiveRecord::Base
end
end
def synchronize_posts!
def synchronize!
added = post_id_array - post_id_array_was
removed = post_id_array_was - post_id_array

View File

@@ -212,7 +212,7 @@ class PoolTest < ActiveSupport::TestCase
setup do
@pool.reload
@pool.post_ids = "#{@p2.id}"
@pool.synchronize_posts!
@pool.synchronize!
end
should "update the pool" do

View File

@@ -13,9 +13,9 @@ module PostSets
@post_2 = Factory.create(:post)
@post_3 = Factory.create(:post)
@pool = Factory.create(:pool)
@pool.add_post!(@post_2)
@pool.add_post!(@post_1)
@pool.add_post!(@post_3)
@pool.add!(@post_2)
@pool.add!(@post_1)
@pool.add!(@post_3)
end
teardown do

View File

@@ -0,0 +1,175 @@
require_relative '../../test_helper'
module PostSets
class PostTest < ActiveSupport::TestCase
context "In all cases" do
setup do
@user = Factory.create(:user)
CurrentUser.user = @user
CurrentUser.ip_addr = "127.0.0.1"
MEMCACHE.flush_all
@post_1 = Factory.create(:post, :tag_string => "a")
@post_2 = Factory.create(:post, :tag_string => "b")
@post_3 = Factory.create(:post, :tag_string => "c")
end
teardown do
CurrentUser.user = nil
CurrentUser.ip_addr = nil
end
context "a sequential set for the 'a' tag query" do
setup do
@post_4 = Factory.create(:post, :tag_string => "a")
@post_5 = Factory.create(:post, :tag_string => "a")
end
context "with no before_id parameter" do
setup do
@set = PostSets::Base.new(:tags => "a")
@set.extend(PostSets::Sequential)
@set.extend(PostSets::Post)
@set.stubs(:limit).returns(1)
end
should "return the first element" do
assert_equal(@post_5.id, @set.posts.first.id)
end
end
context "with a before_id parameter for the first element" do
setup do
@set = PostSets::Base.new(:tags => "a", :before_id => @post_5.id)
@set.extend(PostSets::Sequential)
@set.extend(PostSets::Post)
@set.stubs(:limit).returns(1)
end
should "return the second element" do
assert_equal(@post_4.id, @set.posts.first.id)
end
end
context "with an after_id parameter for the second element" do
setup do
@set = PostSets::Base.new(:tags => "a", :after_id => @post_4.id)
@set.extend(PostSets::Sequential)
@set.extend(PostSets::Post)
@set.stubs(:limit).returns(1)
end
should "return the first element" do
assert_equal(@post_5.id, @set.posts.first.id)
end
end
end
context "a new numbered set for the 'a b' tag query" do
setup do
@set = PostSets::Base.new(:tags => "a b")
@set.extend(PostSets::Numbered)
@set.extend(PostSets::Post)
end
should "know it isn't a single tag" do
assert(!@set.is_single_tag?)
end
end
context "a new numbered set going to the 1,001st page" do
setup do
@set = PostSets::Base.new(:tags => "a", :page => 1_001)
@set.extend(PostSets::Numbered)
@set.extend(PostSets::Post)
end
should "not validate" do
assert_raises(PostSets::Error) do
@set.validate
end
end
end
context "a new numbered set for the 'a b c' tag query" do
setup do
@set = PostSets::Base.new(:tags => "a b c")
@set.extend(PostSets::Numbered)
@set.extend(PostSets::Post)
end
context "for a non-privileged user" do
should "not validate" do
assert_raises(PostSets::Error) do
@set.validate
end
end
end
context "for a privileged user" do
setup do
CurrentUser.user = Factory.create(:privileged_user)
end
should "not validate" do
assert_nothing_raised do
@set.validate
end
end
end
end
context "a new numbered set for the 'a' tag query" do
setup do
@set = PostSets::Base.new(:tags => "A")
@set.extend(PostSets::Numbered)
@set.extend(PostSets::Post)
end
should "validate" do
assert_nothing_raised do
@set.validate
end
end
should "know it is a single tag" do
assert(@set.is_single_tag?)
end
should "normalize its tag query" do
assert_equal("a", @set.tag_string)
end
should "find the count" do
assert_equal(1, @set.count)
end
should "find the posts" do
assert_equal(@post_1.id, @set.posts.first.id)
end
context "that has a matching wiki page" do
setup do
@wiki_page = Factory.create(:wiki_page, :title => "a")
end
should "find the wiki page" do
assert_not_nil(@set.wiki_page)
assert_equal(@wiki_page.id, @set.wiki_page.id)
end
end
context "that has a matching artist" do
setup do
@artist = Factory.create(:artist, :name => "a")
end
should "find the artist" do
assert_not_nil(@set.artist)
assert_equal(@artist.id, @set.artist.id)
end
end
end
end
end
end