2024-02-25 12:15:55 -05:00
|
|
|
# frozen_string_literal: true
|
2014-04-14 17:32:01 -04:00
|
|
|
|
2024-02-25 12:15:55 -05:00
|
|
|
require "test_helper"
|
2010-02-11 14:59:58 -05:00
|
|
|
|
|
|
|
class PoolTest < ActiveSupport::TestCase
|
2010-08-26 14:36:02 -04:00
|
|
|
setup do
|
2022-11-25 15:06:54 -05:00
|
|
|
@user = create(:user, created_at: 1.month.ago)
|
2022-11-25 08:32:56 -05:00
|
|
|
CurrentUser.user = @user
|
2010-08-26 14:36:02 -04:00
|
|
|
end
|
2013-03-19 08:10:10 -04:00
|
|
|
|
2011-06-10 01:02:09 -04:00
|
|
|
context "A name" do
|
|
|
|
setup do
|
2022-11-25 15:06:54 -05:00
|
|
|
@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
|
2022-11-25 15:06:54 -05:00
|
|
|
@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
|
2022-11-25 15:06:54 -05:00
|
|
|
@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
|
|
|
|
2018-04-26 14:14:01 -04:00
|
|
|
context "Creating a pool" do
|
|
|
|
setup do
|
2022-11-25 15:06:54 -05:00
|
|
|
@posts = create_list(:post, 5)
|
|
|
|
@pool = create(:pool, post_ids: @posts.map(&:id))
|
2018-04-26 14:14:01 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
should "initialize the post count" do
|
|
|
|
assert_equal(@posts.size, @pool.post_count)
|
|
|
|
end
|
2018-11-10 13:37:08 -05:00
|
|
|
|
|
|
|
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))
|
2018-11-10 13:37:08 -05:00
|
|
|
end
|
2018-04-26 14:14:01 -04:00
|
|
|
end
|
|
|
|
|
2011-06-10 01:02:09 -04:00
|
|
|
context "Reverting a pool" do
|
|
|
|
setup do
|
2022-11-25 15:06:54 -05:00
|
|
|
@pool = create(:pool)
|
|
|
|
@p1 = create(:post)
|
|
|
|
@p2 = create(:post)
|
|
|
|
@p3 = create(:post)
|
2022-11-26 09:20:15 -05:00
|
|
|
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
|
2022-11-26 09:20:15 -05:00
|
|
|
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
|
2022-11-26 09:20:15 -05:00
|
|
|
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
|
2022-11-26 09:20:15 -05:00
|
|
|
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
|
2022-11-26 09:20:15 -05:00
|
|
|
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
|
2018-11-08 12:32:58 -05:00
|
|
|
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
|
2022-11-25 15:06:54 -05:00
|
|
|
@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
|
|
|
|
2018-04-26 14:14:01 -04:00
|
|
|
context "by #attributes=" do
|
|
|
|
setup do
|
2018-11-08 12:32:58 -05:00
|
|
|
@pool.attributes = {post_ids: [@p1.id, @p2.id]}
|
2018-04-26 14:14:01 -04:00
|
|
|
@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
|
2018-11-08 12:32:58 -05:00
|
|
|
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
|
2018-11-08 12:32:58 -05:00
|
|
|
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
|
2018-11-08 12:32:58 -05:00
|
|
|
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
|
2018-11-08 12:32:58 -05:00
|
|
|
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
|
|
|
|
2018-11-07 17:10:16 -05: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
|
2022-11-25 15:06:54 -05:00
|
|
|
@member = create(:member_user)
|
2018-11-07 17:10:16 -05:00
|
|
|
as(@member) { @pool.update(category: "collection") }
|
|
|
|
|
|
|
|
assert_equal(["You cannot change the category of pools with greater than 1 posts"], @pool.errors[:base])
|
|
|
|
end
|
|
|
|
|
2019-06-29 15:58:28 -04:00
|
|
|
should "allow janitors to change the category of large pools" do
|
2022-11-25 15:06:54 -05:00
|
|
|
@janitor = create(:janitor_user)
|
2019-06-29 15:58:28 -04:00
|
|
|
as(@janitor) { @pool.update(category: "collection") }
|
2018-11-07 17:10:16 -05:00
|
|
|
|
|
|
|
assert_equal(true, @pool.valid?)
|
|
|
|
assert_equal("collection", @pool.category)
|
2021-02-06 00:02:18 -05:00
|
|
|
assert_equal("pool:#{@pool.id}", @p1.reload.pool_string)
|
|
|
|
assert_equal("pool:#{@pool.id}", @p2.reload.pool_string)
|
2018-11-07 17:10:16 -05:00
|
|
|
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)
|
2022-11-25 15:06:54 -05:00
|
|
|
user2 = create(:user, created_at: 1.month.ago)
|
2014-04-14 17:32:01 -04:00
|
|
|
|
2022-11-26 09:20:15 -05:00
|
|
|
as(user2, "127.0.0.2") do
|
2018-11-08 12:32:58 -05:00
|
|
|
@pool.post_ids = [@p1.id]
|
2014-04-14 17:32:01 -04:00
|
|
|
@pool.save
|
|
|
|
end
|
|
|
|
|
|
|
|
@pool.reload
|
|
|
|
assert_equal(2, @pool.versions.size)
|
2018-10-30 15:32:55 -04:00
|
|
|
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
|
|
|
|
2022-11-26 09:20:15 -05:00
|
|
|
as(user2, "127.0.0.3") do
|
2018-11-08 12:32:58 -05:00
|
|
|
@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)
|
2018-10-30 15:32:55 -04:00
|
|
|
assert_equal(user2.id, @pool.versions.last.updater_id)
|
|
|
|
assert_equal("127.0.0.3", @pool.versions.last.updater_ip_addr.to_s)
|
2017-04-01 04:32:02 -04:00
|
|
|
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
|
2017-04-14 15:28:58 -04:00
|
|
|
assert_equal(2, @pool.versions.size)
|
2017-04-01 04:32:02 -04:00
|
|
|
end
|
|
|
|
|
2011-06-10 01:02:09 -04:00
|
|
|
should "know what its post ids were previously" do
|
2018-11-08 12:32:58 -05:00
|
|
|
@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
|
2017-08-10 19:16:50 -04:00
|
|
|
@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
|
2018-11-08 12:32:58 -05:00
|
|
|
@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
|
2017-08-10 19:16:50 -04:00
|
|
|
|
|
|
|
context "when validating names" do
|
2021-03-11 03:06:20 -05:00
|
|
|
["foo,bar", "foo*bar", "123", "--", "___", " ", "any", "none", "series", "collection"].each do |bad_name|
|
2018-11-07 17:10:16 -05:00
|
|
|
should_not allow_value(bad_name).for(:name)
|
2017-08-10 19:16:50 -04:00
|
|
|
end
|
2021-03-11 03:06:20 -05:00
|
|
|
|
|
|
|
["_-_", " - "].each do |good_name|
|
|
|
|
should allow_value(good_name).for(:name)
|
|
|
|
end
|
2017-08-10 19:16:50 -04:00
|
|
|
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
|
2022-11-25 15:06:54 -05:00
|
|
|
@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
|
2018-11-08 12:32:58 -05:00
|
|
|
@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)
|
2018-11-08 12:32:58 -05:00
|
|
|
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
|
2018-11-04 20:40:57 -05:00
|
|
|
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
|
2018-11-04 20:40:57 -05:00
|
|
|
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
|
2018-11-04 20:40:57 -05:00
|
|
|
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
|