From 1a303fd3db017dc5ed4d4b8e9df3ed1826c7afb1 Mon Sep 17 00:00:00 2001 From: albert Date: Sat, 11 Jun 2011 17:05:46 -0400 Subject: [PATCH] added tests for post sets --- app/logical/post_sets/error.rb | 4 + app/logical/post_sets/numbered.rb | 11 ++ app/logical/post_sets/post.rb | 23 +--- app/models/pool.rb | 4 +- test/unit/pool_test.rb | 2 +- test/unit/post_sets/pool_test.rb | 6 +- test/unit/post_sets/post_test.rb | 175 ++++++++++++++++++++++++++++++ 7 files changed, 202 insertions(+), 23 deletions(-) create mode 100644 app/logical/post_sets/error.rb create mode 100644 test/unit/post_sets/post_test.rb diff --git a/app/logical/post_sets/error.rb b/app/logical/post_sets/error.rb new file mode 100644 index 000000000..e266775a2 --- /dev/null +++ b/app/logical/post_sets/error.rb @@ -0,0 +1,4 @@ +module PostSets + class Error < Exception + end +end diff --git a/app/logical/post_sets/numbered.rb b/app/logical/post_sets/numbered.rb index bc33fbcd3..22c1ab2f4 100644 --- a/app/logical/post_sets/numbered.rb +++ b/app/logical/post_sets/numbered.rb @@ -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 diff --git a/app/logical/post_sets/post.rb b/app/logical/post_sets/post.rb index 8d5b90917..9c028a8f3 100644 --- a/app/logical/post_sets/post.rb +++ b/app/logical/post_sets/post.rb @@ -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") diff --git a/app/models/pool.rb b/app/models/pool.rb index b0185486d..20a572502 100644 --- a/app/models/pool.rb +++ b/app/models/pool.rb @@ -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 diff --git a/test/unit/pool_test.rb b/test/unit/pool_test.rb index f697cbfda..c85a67d1f 100644 --- a/test/unit/pool_test.rb +++ b/test/unit/pool_test.rb @@ -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 diff --git a/test/unit/post_sets/pool_test.rb b/test/unit/post_sets/pool_test.rb index be25e8e7e..54561acb3 100644 --- a/test/unit/post_sets/pool_test.rb +++ b/test/unit/post_sets/pool_test.rb @@ -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 diff --git a/test/unit/post_sets/post_test.rb b/test/unit/post_sets/post_test.rb new file mode 100644 index 000000000..49ddc6ce5 --- /dev/null +++ b/test/unit/post_sets/post_test.rb @@ -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