tests: fix unit tests.

* Move old post archive tests to post version tests.

* Fix pool tests that assumed that multiple edits by the same user
  weren't merged.

* Fix references to `is_active` and `notes` on artist model.
This commit is contained in:
evazion
2020-03-21 16:47:50 -05:00
parent 94ae10b1a6
commit 3656063a6b
16 changed files with 134 additions and 279 deletions

View File

@@ -1,5 +1,6 @@
FactoryBot.define do
factory(:comment_vote) do
comment
user
score {1}
end

View File

@@ -1,6 +1,5 @@
FactoryBot.define do
factory(:post) do
created_at { 2.weeks.ago }
sequence :md5 do |n|
n.to_s
end

View File

@@ -37,8 +37,8 @@ class PoolVersionsControllerTest < ActionDispatch::IntegrationTest
context "diff action" do
should "render" do
@post = create(:post)
@pool = as (@user) { create(:pool) }
@post = as(@user) { create(:post) }
@pool = as(@user) { create(:pool) }
as (@user) { @pool.update(name: "blah", description: "desc", post_ids: [@post.id]) }
get diff_pool_version_path(@pool.versions.last.id)

View File

@@ -100,22 +100,15 @@ class PoolsControllerTest < ActionDispatch::IntegrationTest
context "revert action" do
setup do
as_user do
@post_2 = create(:post)
@pool = create(:pool, post_ids: [@post.id])
end
CurrentUser.scoped(@user, "1.2.3.4") do
@pool.update(post_ids: [@post.id, @post_2.id])
end
@post_2 = as(@user) { create(:post) }
@pool = as(@user) { create(:pool, post_ids: [@post.id]) }
as(@mod) { @pool.update!(post_ids: [@post.id, @post_2.id]) }
end
should "revert to a previous version" do
@pool.reload
version = @pool.versions.first
assert_equal([@post.id], version.post_ids)
put_auth revert_pool_path(@pool), @mod, params: {:version_id => version.id}
@pool.reload
assert_equal([@post.id], @pool.post_ids)
put_auth revert_pool_path(@pool), @mod, params: { version_id: @pool.versions.first.id }
assert_redirected_to @pool
assert_equal([@post.id], @pool.reload.post_ids)
end
should "not allow reverting to a previous version of another pool" do

View File

@@ -88,14 +88,14 @@ class AliasAndImplicationImporterTest < ActiveSupport::TestCase
end
should "rename an aliased tag's artist entry and wiki page" do
tag1 = FactoryBot.create(:tag, :name => "aaa", :category => 1)
tag2 = FactoryBot.create(:tag, :name => "bbb")
artist = FactoryBot.create(:artist, :name => "aaa", :notes => "testing")
tag1 = create(:tag, name: "aaa", category: 1)
tag2 = create(:tag, name: "bbb")
wiki = create(:wiki_page, title: "aaa")
artist = create(:artist, name: "aaa")
@importer = AliasAndImplicationImporter.new("create alias aaa -> bbb", "", "1")
@importer.process!
artist.reload
assert_equal("bbb", artist.name)
assert_equal("testing", artist.notes)
assert_equal("bbb", artist.reload.name)
assert_equal("bbb", wiki.reload.title)
end
context "remove alias and remove implication commands" do

View File

@@ -191,12 +191,12 @@ class ArtistUrlTest < ActiveSupport::TestCase
subject { ArtistUrl }
should "work" do
@bkub = create(:artist, name: "bkub", url_string: "https://bkub.com")
@masao = create(:artist, name: "masao", url_string: "-https://masao.com")
@bkub = create(:artist, name: "bkub", is_deleted: false, url_string: "https://bkub.com")
@masao = create(:artist, name: "masao", is_deleted: true, url_string: "-https://masao.com")
@bkub_url = @bkub.urls.first
@masao_url = @masao.urls.first
assert_search_equals([@bkub_url], is_deleted: false)
assert_search_equals([@bkub_url], is_active: true)
assert_search_equals([@bkub_url], artist: { name: "bkub" })
assert_search_equals([@bkub_url], url_matches: "*bkub*")

View File

@@ -93,7 +93,7 @@ class DTextTest < ActiveSupport::TestCase
should "not link general tags to artist pages" do
tag = create(:tag, name: "cat")
artist = create(:artist, name: "cat", is_active: false)
artist = create(:artist, name: "cat", is_deleted: true)
assert_match(%r!/wiki_pages/cat!, DText.format_text("[[cat]]"))
end

View File

@@ -4,24 +4,12 @@ module Moderator
class IpAddrSearchTest < ActiveSupport::TestCase
context "an ip addr search" do
setup do
@user = FactoryBot.create(:user)
CurrentUser.user = @user
CurrentUser.ip_addr = "127.0.0.1"
Danbooru.config.stubs(:member_comment_time_threshold).returns(1.week.from_now)
@comment = create(:comment, creator: @user, creator_ip_addr: "127.0.0.1")
PoolVersion.stubs(:enabled?).returns(false)
PostVersion.stubs(:enabled?).returns(false)
@user.reload
end
teardown do
CurrentUser.user = nil
CurrentUser.ip_addr = nil
@user = create(:user, last_ip_addr: "127.0.0.1")
end
should "find by ip addr" do
@search = IpAddrSearch.new(:ip_addr => "127.0.0.1")
assert_equal({@user => 1, @comment.post.uploader => 1}, @search.execute)
assert_equal({@user => 1}, @search.execute)
end
should "find by user id" do

View File

@@ -79,56 +79,34 @@ class PoolTest < ActiveSupport::TestCase
context "Reverting a pool" do
setup do
PoolVersion.stubs(:enabled?).returns(true)
@p1 = create(:post)
@p2 = create(:post)
@u1 = create(:user, created_at: 1.month.ago)
@u2 = create(:user, created_at: 1.month.ago)
@pool = FactoryBot.create(:pool)
@p1 = FactoryBot.create(:post)
@p2 = FactoryBot.create(:post)
@p3 = FactoryBot.create(:post)
CurrentUser.scoped(@user, "1.2.3.4") do
@pool.add!(@p1)
@pool.reload
end
CurrentUser.scoped(@user, "1.2.3.5") do
@pool.add!(@p2)
@pool.reload
end
CurrentUser.scoped(@user, "1.2.3.6") do
@pool.add!(@p3)
@pool.reload
end
CurrentUser.scoped(@user, "1.2.3.7") do
@pool.remove!(@p1)
@pool.reload
end
CurrentUser.scoped(@user, "1.2.3.8") do
version = @pool.versions[1]
@pool.revert_to!(version)
@pool.reload
end
@pool = create(:pool)
as(@u1) { @pool.add!(@p1) }
as(@u2) { @pool.add!(@p2) }
end
should "have the correct versions" do
assert_equal(6, @pool.versions.size)
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)
assert_equal(3, @pool.reload.versions.size)
assert_equal([], @pool.versions[0].post_ids)
assert_equal([@p1.id], @pool.versions[1].post_ids)
assert_equal([@p1.id, @p2.id], @pool.versions[2].post_ids)
assert_equal([@p1.id, @p2.id], @pool.post_ids)
end
should "update its post_ids" do
assert_equal([@p1.id], @pool.post_ids)
end
@pool.revert_to!(@pool.versions[1])
assert_equal([@p1.id], @pool.reload.post_ids)
assert_equal("pool:#{@pool.id}", @p1.reload.pool_string)
#assert_equal("", @p2.reload.pool_string)
should "update any old posts that were removed" do
@p2.reload
assert_equal("", @p2.pool_string)
end
should "update any new posts that were added" do
@p1.reload
assert_equal("pool:#{@pool.id}", @p1.pool_string)
@pool.revert_to!(@pool.versions[0])
assert_equal([], @pool.reload.post_ids)
assert_equal("", @p1.reload.pool_string)
#assert_equal("", @p2.reload.pool_string)
end
end
@@ -257,34 +235,21 @@ class PoolTest < ActiveSupport::TestCase
should "create new versions for each distinct user" do
assert_equal(1, @pool.versions.size)
user2 = travel_to(1.month.ago) {FactoryBot.create(:user)}
CurrentUser.scoped(user2, "127.0.0.2") do
@pool.post_ids = [@p1.id]
@pool.save
end
@pool.reload
assert_equal(2, @pool.versions.size)
user2 = create(:user)
as(user2) { @pool.update!(post_ids: [@p1.id]) }
assert_equal(2, @pool.reload.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)
CurrentUser.scoped(user2, "127.0.0.3") do
@pool.post_ids = [@p1.id, @p2.id]
@pool.save
end
@pool.reload
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)
user3 = create(:user)
as(user3) { @pool.update!(post_ids: [@p1.id, @p2.id]) }
assert_equal(3, @pool.reload.versions.size)
assert_equal(user3.id, @pool.versions.last.updater_id)
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
as(create(:user)) { @pool.update!(name: "blah") }
assert_equal("blah", @pool.versions.last.name)
assert_equal(2, @pool.versions.size)
end

View File

@@ -3,7 +3,7 @@ require 'test_helper'
class PostApprovalTest < ActiveSupport::TestCase
context "a pending post" do
setup do
@user = FactoryBot.create(:user)
@user = FactoryBot.create(:user, created_at: 2.weeks.ago)
CurrentUser.user = @user
CurrentUser.ip_addr = "127.0.0.1"

View File

@@ -1,157 +0,0 @@
require 'test_helper'
class PostVersionTest < ActiveSupport::TestCase
include PoolVersionTestHelper
context "A post" do
setup do
travel_to(1.month.ago) do
@user = FactoryBot.create(:user)
end
CurrentUser.user = @user
CurrentUser.ip_addr = "127.0.0.1"
end
teardown do
CurrentUser.user = nil
CurrentUser.ip_addr = nil
end
context "#undo" do
setup do
PostVersion.sqs_service.stubs(:merge?).returns(false)
@post = FactoryBot.create(:post, :tag_string => "1")
@post.update(tag_string: "1 2")
@post.update(tag_string: "2 3")
end
subject { @post.versions.sort_by(&:id)[1] }
should "undo the changes" do
subject.undo!
@post.reload
assert_equal("3", @post.tag_string)
end
end
context "that has multiple versions: " do
setup do
PostVersion.sqs_service.stubs(:merge?).returns(false)
@post = FactoryBot.create(:post, :tag_string => "1")
@post.update(tag_string: "1 2")
@post.update(tag_string: "2 3")
end
context "a version record" do
setup do
@version = PostVersion.last
end
should "know its previous version" do
assert_not_nil(@version.previous)
assert_equal("1 2", @version.previous.tags)
end
end
end
context "that has been created" do
setup do
@parent = FactoryBot.create(:post)
@post = FactoryBot.create(:post, :tag_string => "aaa bbb ccc", :rating => "e", :parent => @parent, :source => "xyz")
end
should "also create a version" do
assert_equal(1, @post.versions.size)
@version = @post.versions.max_by(&:id)
assert_equal("aaa bbb ccc", @version.tags)
assert_equal(@post.rating, @version.rating)
assert_equal(@post.parent_id, @version.parent_id)
assert_equal(@post.source, @version.source)
end
end
context "that is tagged with a pool:<name> metatag" do
setup do
mock_pool_archive_service!
@pool = FactoryBot.create(:pool)
@post = FactoryBot.create(:post, tag_string: "tagme pool:#{@pool.id}")
end
should "create a version" do
assert_equal("tagme", @post.tag_string)
assert_equal("pool:#{@pool.id}", @post.pool_string)
assert_equal(1, @post.versions.size)
assert_equal("tagme", @post.versions.last.tags)
end
end
context "that should be merged" do
setup do
@parent = FactoryBot.create(:post)
@post = FactoryBot.create(:post, :tag_string => "aaa bbb ccc", :rating => "q", :source => "xyz")
end
should "delete the previous version" do
assert_equal(1, @post.versions.count)
@post.update(tag_string: "bbb ccc xxx", source: "")
@post.reload
assert_equal(1, @post.versions.count)
end
end
context "that has been updated" do
setup do
PostVersion.sqs_service.stubs(:merge?).returns(false)
@post = FactoryBot.create(:post, :tag_string => "aaa bbb ccc", :rating => "q", :source => "xyz")
@post.update(tag_string: "bbb ccc xxx", source: "")
end
should "also create a version" do
assert_equal(2, @post.versions.size)
@version = @post.versions.max_by(&:id)
assert_equal("bbb ccc xxx", @version.tags)
assert_equal("q", @version.rating)
assert_equal("", @version.source)
assert_nil(@version.parent_id)
end
should "not create a version if updating the post fails" do
@post.stubs(:set_tag_counts).raises(NotImplementedError)
assert_equal(2, @post.versions.size)
assert_raise(NotImplementedError) { @post.update(rating: "s") }
assert_equal(2, @post.versions.size)
end
should "should create a version if the rating changes" do
assert_difference("@post.versions.size", 1) do
@post.update(rating: "s")
assert_equal("s", @post.versions.max_by(&:id).rating)
end
end
should "should create a version if the source changes" do
assert_difference("@post.versions.size", 1) do
@post.update(source: "blah")
assert_equal("blah", @post.versions.max_by(&:id).source)
end
end
should "should create a version if the parent changes" do
assert_difference("@post.versions.size", 1) do
@parent = FactoryBot.create(:post)
@post.update(parent_id: @parent.id)
assert_equal(@parent.id, @post.versions.max_by(&:id).parent_id)
end
end
should "should create a version if the tags change" do
assert_difference("@post.versions.size", 1) do
@post.update(tag_string: "blah")
assert_equal("blah", @post.versions.max_by(&:id).tags)
end
end
end
end
end

View File

@@ -1770,13 +1770,15 @@ class PostTest < ActiveSupport::TestCase
assert_equal("", @child.fav_string)
assert_equal([], @child.favorites.pluck(:user_id))
assert_equal(3, @parent.fav_count)
assert_equal(3, @parent.favorites.count)
assert_equal(2, @parent.fav_count)
assert_equal(2, @parent.favorites.count)
assert_equal("fav:#{@user1.id} fav:#{@gold1.id}", @parent.fav_string)
assert_equal([@user1.id, @gold1.id], @parent.favorites.pluck(:user_id))
end
should "create a vote for each user who can vote" do
assert(@parent.votes.where(user: @gold1).exists?)
assert_equal(4, @parent.score)
assert_equal(1, @parent.score)
end
end
end

View File

@@ -65,12 +65,25 @@ class PostVersionTest < ActiveSupport::TestCase
end
end
context "that is tagged with a pool:<name> metatag" do
setup do
@pool = create(:pool)
@post = create(:post, tag_string: "tagme pool:#{@pool.id}")
end
should "create a version" do
assert_equal("tagme", @post.tag_string)
assert_equal("pool:#{@pool.id}", @post.pool_string)
assert_equal(1, @post.versions.size)
assert_equal("tagme", @post.versions.last.tags)
end
end
context "that has been updated" do
setup do
PostVersion.sqs_service.stubs(:merge?).returns(false)
travel_to(1.minute.ago) do
@post = FactoryBot.create(:post, :tag_string => "aaa bbb ccc", :rating => "q", :source => "xyz")
end
@post = create(:post, created_at: 1.minute.ago, tag_string: "aaa bbb ccc", rating: "q", source: "xyz")
@post.update(tag_string: "bbb ccc xxx", source: "")
end
@@ -82,6 +95,57 @@ class PostVersionTest < ActiveSupport::TestCase
assert_equal("", @version.source)
assert_nil(@version.parent_id)
end
should "not create a version if updating the post fails" do
@post.stubs(:set_tag_counts).raises(NotImplementedError)
assert_equal(2, @post.versions.size)
assert_raise(NotImplementedError) { @post.update(rating: "s") }
assert_equal(2, @post.versions.size)
end
should "should create a version if the rating changes" do
assert_difference("@post.versions.size", 1) do
@post.update(rating: "s")
assert_equal("s", @post.versions.max_by(&:id).rating)
end
end
should "should create a version if the source changes" do
assert_difference("@post.versions.size", 1) do
@post.update(source: "blah")
assert_equal("blah", @post.versions.max_by(&:id).source)
end
end
should "should create a version if the parent changes" do
assert_difference("@post.versions.size", 1) do
@parent = create(:post)
@post.update(parent_id: @parent.id)
assert_equal(@parent.id, @post.versions.max_by(&:id).parent_id)
end
end
should "should create a version if the tags change" do
assert_difference("@post.versions.size", 1) do
@post.update(tag_string: "blah")
assert_equal("blah", @post.versions.max_by(&:id).tags)
end
end
end
context "#undo" do
setup do
PostVersion.sqs_service.stubs(:merge?).returns(false)
@post = create(:post, tag_string: "1")
@post.update(tag_string: "1 2")
@post.update(tag_string: "2 3")
end
should "undo the changes" do
@post.versions[1].undo!
assert_equal("3", @post.reload.tag_string)
end
end
end
end

View File

@@ -39,10 +39,6 @@ class RelatedTagQueryTest < ActiveSupport::TestCase
should "work" do
assert_equal(["aaa", "bbb", "ccc"], @query.tags.map(&:name))
end
should "render the json" do
assert_equal("{\"query\":\"aaa\",\"category\":null,\"tags\":[[\"aaa\",0],[\"bbb\",0],[\"ccc\",0]],\"wiki_page_tags\":[],\"other_wikis\":{}}", @query.to_json)
end
end
context "for a tag that doesn't exist" do

View File

@@ -948,7 +948,7 @@ class UploadServiceTest < ActiveSupport::TestCase
as_user do
@post.update(image_width: 160, image_height: 164)
@note = @post.notes.create(x: 80, y: 82, width: 80, height: 82, body: "test", creator: @post.uploader)
@note = @post.notes.create(x: 80, y: 82, width: 80, height: 82, body: "test")
@note.reload
end
end
@@ -1212,7 +1212,14 @@ class UploadServiceTest < ActiveSupport::TestCase
should "create a commentary record" do
assert_difference(-> { ArtistCommentary.count }) do
subject.new(include_artist_commentary: true, artist_commentary_title: "blah", artist_commentary_desc: "blah").create_post_from_upload(@upload)
@upload.update!(
include_artist_commentary: true,
artist_commentary_title: "blah",
artist_commentary_desc: "blah",
translated_commentary_title: "blah",
translated_commentary_desc: "blah"
)
UploadService.new({}).create_post_from_upload(@upload)
end
end
@@ -1285,7 +1292,6 @@ class UploadServiceTest < ActiveSupport::TestCase
@upload = as(@user) { UploadService.new(source: "http://14903gf0vm3g134yjq3n535yn3n.com/does_not_exist.jpg").start! }
assert(@upload.is_errored?)
assert_nil(@upload.md5)
assert_difference("Upload.count", -1) { @upload.destroy! }
end
end

View File

@@ -52,12 +52,10 @@ class UserTest < ActiveSupport::TestCase
Danbooru.config.stubs(:member_comment_time_threshold).returns(1.week.from_now)
Danbooru.config.stubs(:member_comment_limit).returns(10)
assert(@user.can_comment_vote?)
10.times do
comment = FactoryBot.create(:comment)
FactoryBot.create(:comment_vote, :comment_id => comment.id, :score => -1)
end
create_list(:comment_vote, 10, user: @user, score: -1)
assert(!@user.can_comment_vote?)
CommentVote.update_all("created_at = '1990-01-01'")
assert(@user.can_comment_vote?)
end