eBooru/test/unit/pool_test.rb

346 lines
8.8 KiB
Ruby
Raw Permalink Normal View History

# frozen_string_literal: true
2014-04-14 17:32:01 -04:00
require "test_helper"
2010-02-11 14:59:58 -05:00
class PoolTest < ActiveSupport::TestCase
setup do
@user = create(:user, created_at: 1.month.ago)
CurrentUser.user = @user
end
2013-03-19 08:10:10 -04:00
2011-06-10 01:02:09 -04:00
context "A name" do
setup do
@pool = create(:pool, name: "xxx")
2011-06-10 01:02:09 -04:00
end
2013-03-19 08:10:10 -04:00
2011-06-10 01:02:09 -04:00
should "be mapped to a pool id" do
2011-10-22 19:01:27 -04:00
assert_equal(@pool.id, Pool.name_to_id("xxx"))
2011-06-10 01:02:09 -04:00
end
end
2013-03-19 08:10:10 -04:00
2014-04-14 17:32:01 -04:00
context "A multibyte character name" do
setup do
@mb_pool = create(:pool, name: "àáâãäå")
2014-04-14 17:32:01 -04:00
end
should "be mapped to a pool id" do
assert_equal(@mb_pool.id, Pool.name_to_id("àáâãäå"))
end
end
2011-06-10 01:02:09 -04:00
context "An id number" do
setup do
@pool = create(:pool)
2011-06-10 01:02:09 -04:00
end
2013-03-19 08:10:10 -04:00
2011-06-10 01:02:09 -04:00
should "be mapped to a pool id" do
assert_equal(@pool.id, Pool.name_to_id(@pool.id.to_s))
end
end
2013-03-19 08:10:10 -04:00
context "Creating a pool" do
setup do
@posts = create_list(:post, 5)
@pool = create(:pool, post_ids: @posts.map(&:id))
end
should "initialize the post count" do
assert_equal(@posts.size, @pool.post_count)
end
should "synchronize the posts with the pool" do
assert_equal(@posts.map(&:id), @pool.post_ids)
@posts.each(&:reload)
2021-01-23 17:07:02 -05:00
assert_equal(["pool:#{@pool.id}"] * @posts.size, @posts.map(&:pool_string))
end
end
2011-06-10 01:02:09 -04:00
context "Reverting a pool" do
setup do
@pool = create(:pool)
@p1 = create(:post)
@p2 = create(:post)
@p3 = create(:post)
as(@user, "1.2.3.4") do
2016-12-14 21:09:45 -05:00
@pool.add!(@p1)
2018-05-09 14:56:01 -04:00
@pool.reload
2016-12-14 21:09:45 -05:00
end
as(@user, "1.2.3.5") do
2016-12-14 21:09:45 -05:00
@pool.add!(@p2)
2018-05-09 14:56:01 -04:00
@pool.reload
2016-12-14 21:09:45 -05:00
end
as(@user, "1.2.3.6") do
2016-12-14 21:09:45 -05:00
@pool.add!(@p3)
2018-05-09 14:56:01 -04:00
@pool.reload
2016-12-14 21:09:45 -05:00
end
as(@user, "1.2.3.7") do
2016-12-14 21:09:45 -05:00
@pool.remove!(@p1)
2018-05-09 14:56:01 -04:00
@pool.reload
2016-12-14 21:09:45 -05:00
end
as(@user, "1.2.3.8") do
2016-12-14 21:09:45 -05:00
version = @pool.versions[1]
@pool.revert_to!(version)
2018-05-09 14:56:01 -04:00
@pool.reload
2016-12-14 21:09:45 -05:00
end
2011-06-10 01:02:09 -04:00
end
2013-03-19 08:10:10 -04:00
2011-06-10 01:02:09 -04:00
should "have the correct versions" do
assert_equal(6, @pool.versions.size)
2016-12-14 21:09:45 -05:00
assert_equal([], @pool.versions.all[0].post_ids)
assert_equal([@p1.id], @pool.versions.all[1].post_ids)
assert_equal([@p1.id, @p2.id], @pool.versions.all[2].post_ids)
assert_equal([@p1.id, @p2.id, @p3.id], @pool.versions.all[3].post_ids)
assert_equal([@p2.id, @p3.id], @pool.versions.all[4].post_ids)
2011-06-10 01:02:09 -04:00
end
2013-03-19 08:10:10 -04:00
2011-06-10 01:02:09 -04:00
should "update its post_ids" do
assert_equal([@p1.id], @pool.post_ids)
2011-06-10 01:02:09 -04:00
end
2013-03-19 08:10:10 -04:00
2011-06-10 01:02:09 -04:00
should "update any old posts that were removed" do
@p2.reload
assert_equal("", @p2.pool_string)
end
2013-03-19 08:10:10 -04:00
2011-06-10 01:02:09 -04:00
should "update any new posts that were added" do
@p1.reload
2021-01-23 17:07:02 -05:00
assert_equal("pool:#{@pool.id}", @p1.pool_string)
2011-06-10 01:02:09 -04:00
end
end
2013-03-19 08:10:10 -04:00
2011-06-10 01:02:09 -04:00
context "Updating a pool" do
setup do
@pool = create(:pool, category: "series")
@p1 = create(:post)
@p2 = create(:post)
2011-06-10 01:02:09 -04:00
end
2013-03-19 08:10:10 -04:00
2011-06-10 01:02:09 -04:00
context "by adding a new post" do
setup do
@pool.add!(@p1)
end
2013-03-19 08:10:10 -04:00
context "by #attributes=" do
setup do
@pool.attributes = {post_ids: [@p1.id, @p2.id]}
@pool.synchronize
@pool.save
end
should "initialize the post count" do
assert_equal(2, @pool.post_count)
end
end
2011-06-10 01:02:09 -04:00
should "add the post to the pool" do
assert_equal([@p1.id], @pool.post_ids)
2011-06-10 01:02:09 -04:00
end
2013-03-19 08:10:10 -04:00
2011-06-10 01:02:09 -04:00
should "add the pool to the post" do
2021-01-23 17:07:02 -05:00
assert_equal("pool:#{@pool.id}", @p1.pool_string)
2011-06-10 01:02:09 -04:00
end
2013-03-19 08:10:10 -04:00
2011-06-10 01:02:09 -04:00
should "increment the post count" do
assert_equal(1, @pool.post_count)
end
2013-03-19 08:10:10 -04:00
2011-06-10 01:02:09 -04:00
context "to a pool that already has the post" do
setup do
@pool.add!(@p1)
end
2013-03-19 08:10:10 -04:00
2011-06-10 01:02:09 -04:00
should "not double add the post to the pool" do
assert_equal([@p1.id], @pool.post_ids)
2011-06-10 01:02:09 -04:00
end
2011-03-30 15:11:22 -04:00
2011-06-10 01:02:09 -04:00
should "not double add the pool to the post" do
2021-01-23 17:07:02 -05:00
assert_equal("pool:#{@pool.id}", @p1.pool_string)
2011-06-10 01:02:09 -04:00
end
2011-03-30 15:11:22 -04:00
2011-06-10 01:02:09 -04:00
should "not double increment the post count" do
assert_equal(1, @pool.post_count)
end
end
end
2013-03-19 08:10:10 -04:00
2011-06-10 01:02:09 -04:00
context "by removing a post" do
setup do
@pool.add!(@p1)
end
2013-03-19 08:10:10 -04:00
2011-06-10 01:02:09 -04:00
context "that is in the pool" do
setup do
@pool.remove!(@p1)
end
should "remove the post from the pool" do
assert_equal([], @pool.post_ids)
2011-06-10 01:02:09 -04:00
end
2011-03-30 15:11:22 -04:00
2011-06-10 01:02:09 -04:00
should "remove the pool from the post" do
assert_equal("", @p1.pool_string)
end
should "update the post count" do
assert_equal(0, @pool.post_count)
end
end
2013-03-19 08:10:10 -04:00
2011-06-10 01:02:09 -04:00
context "that is not in the pool" do
setup do
@pool.remove!(@p2)
end
2013-03-19 08:10:10 -04:00
2011-06-10 01:02:09 -04:00
should "not affect the pool" do
assert_equal([@p1.id], @pool.post_ids)
2011-06-10 01:02:09 -04:00
end
2013-03-19 08:10:10 -04:00
2011-06-10 01:02:09 -04:00
should "not affect the post" do
2021-01-23 17:07:02 -05:00
assert_equal("pool:#{@pool.id}", @p1.pool_string)
2011-06-10 01:02:09 -04:00
end
2013-03-19 08:10:10 -04:00
2011-06-10 01:02:09 -04:00
should "not affect the post count" do
assert_equal(1, @pool.post_count)
end
end
end
2013-03-19 08:10:10 -04:00
context "by changing the category" do
setup do
Danbooru.config.stubs(:pool_category_change_limit).returns(1)
@pool.add!(@p1)
@pool.add!(@p2)
end
should "not allow Members to change the category of large pools" do
@member = create(:member_user)
as(@member) { @pool.update(category: "collection") }
assert_equal(["You cannot change the category of pools with greater than 1 posts"], @pool.errors[:base])
end
should "allow janitors to change the category of large pools" do
@janitor = create(:janitor_user)
as(@janitor) { @pool.update(category: "collection") }
assert_equal(true, @pool.valid?)
assert_equal("collection", @pool.category)
assert_equal("pool:#{@pool.id}", @p1.reload.pool_string)
assert_equal("pool:#{@pool.id}", @p2.reload.pool_string)
end
end
2011-06-10 01:02:09 -04:00
should "create new versions for each distinct user" do
2014-04-14 17:32:01 -04:00
assert_equal(1, @pool.versions.size)
user2 = create(:user, created_at: 1.month.ago)
2014-04-14 17:32:01 -04:00
as(user2, "127.0.0.2") do
@pool.post_ids = [@p1.id]
2014-04-14 17:32:01 -04:00
@pool.save
end
@pool.reload
assert_equal(2, @pool.versions.size)
assert_equal(user2.id, @pool.versions.last.updater_id)
assert_equal("127.0.0.2", @pool.versions.last.updater_ip_addr.to_s)
2014-04-14 17:32:01 -04:00
as(user2, "127.0.0.3") do
@pool.post_ids = [@p1.id, @p2.id]
2014-04-14 17:32:01 -04:00
@pool.save
end
@pool.reload
2017-04-04 15:25:03 -04:00
assert_equal(3, @pool.versions.size)
assert_equal(user2.id, @pool.versions.last.updater_id)
assert_equal("127.0.0.3", @pool.versions.last.updater_ip_addr.to_s)
end
should "should create a version if the name changes" do
assert_difference("@pool.versions.size", 1) do
@pool.update(name: "blah")
assert_equal("blah", @pool.versions.last.name)
end
assert_equal(2, @pool.versions.size)
end
2011-06-10 01:02:09 -04:00
should "know what its post ids were previously" do
@pool.post_ids = [@p1.id]
assert_equal([], @pool.post_ids_was)
2011-06-10 01:02:09 -04:00
end
2013-03-19 08:10:10 -04:00
2011-06-10 01:02:09 -04:00
should "normalize its name" do
@pool.update(:name => " A B ")
assert_equal("A_B", @pool.name)
@pool.update(:name => "__A__B__")
2013-02-20 16:24:59 -05:00
assert_equal("A_B", @pool.name)
2011-06-10 01:02:09 -04:00
end
2013-03-19 08:10:10 -04:00
2011-06-10 01:02:09 -04:00
should "normalize its post ids" do
@pool.update(category: "collection", post_ids: [1, 2, 2, 3, 1])
assert_equal([1, 2, 3], @pool.post_ids)
2010-02-13 05:11:27 -05:00
end
context "when validating names" do
["foo,bar", "foo*bar", "123", "--", "___", " ", "any", "none", "series", "collection"].each do |bad_name|
should_not allow_value(bad_name).for(:name)
end
["_-_", " - "].each do |good_name|
should allow_value(good_name).for(:name)
end
end
2010-02-13 05:11:27 -05:00
end
2013-03-19 08:10:10 -04:00
2011-06-10 01:02:09 -04:00
context "An existing pool" do
setup do
@pool = create(:pool)
@p1 = create(:post)
@p2 = create(:post)
@p3 = create(:post)
2011-06-10 01:02:09 -04:00
@pool.add!(@p1)
@pool.add!(@p2)
@pool.add!(@p3)
end
2013-03-19 08:10:10 -04:00
2011-06-10 01:02:09 -04:00
context "that is synchronized" do
setup do
@pool.reload
@pool.post_ids = [@p2.id]
2011-06-11 17:05:46 -04:00
@pool.synchronize!
2011-06-10 01:02:09 -04:00
end
2013-03-19 08:10:10 -04:00
2011-06-10 01:02:09 -04:00
should "update the pool" do
@pool.reload
assert_equal(1, @pool.post_count)
assert_equal([@p2.id], @pool.post_ids)
2011-06-10 01:02:09 -04:00
end
2013-03-19 08:10:10 -04:00
2011-06-10 01:02:09 -04:00
should "update the posts" do
@p1.reload
@p2.reload
@p3.reload
assert_equal("", @p1.pool_string)
2021-01-23 17:07:02 -05:00
assert_equal("pool:#{@pool.id}", @p2.pool_string)
2011-06-10 01:02:09 -04:00
assert_equal("", @p3.pool_string)
end
end
2013-03-19 08:10:10 -04:00
2011-06-10 01:02:09 -04:00
should "find the neighbors for the first post" do
assert_nil(@pool.previous_post_id(@p1.id))
assert_equal(@p2.id, @pool.next_post_id(@p1.id))
2011-06-10 01:02:09 -04:00
end
2013-03-19 08:10:10 -04:00
2011-06-10 01:02:09 -04:00
should "find the neighbors for the middle post" do
assert_equal(@p1.id, @pool.previous_post_id(@p2.id))
assert_equal(@p3.id, @pool.next_post_id(@p2.id))
2011-06-10 01:02:09 -04:00
end
2013-03-19 08:10:10 -04:00
2011-06-10 01:02:09 -04:00
should "find the neighbors for the last post" do
assert_equal(@p2.id, @pool.previous_post_id(@p3.id))
assert_nil(@pool.next_post_id(@p3.id))
2011-06-10 01:02:09 -04:00
end
end
2010-02-11 14:59:58 -05:00
end