tests: fix alias/implication tests.

This commit is contained in:
evazion
2019-08-17 02:41:07 -05:00
parent 868a2256d1
commit 145894fe8b
5 changed files with 33 additions and 23 deletions

View File

@@ -57,8 +57,9 @@ module TestHelpers
# XXX replace with `perform_enqueued_jobs` after rails 6 upgrade. # XXX replace with `perform_enqueued_jobs` after rails 6 upgrade.
def workoff_active_jobs def workoff_active_jobs
queue_adapter.enqueued_jobs.each do |job_data| queue_adapter.enqueued_jobs.each do |job_data|
klass = job_data[:job] args = ActiveJob::Arguments.deserialize(job_data[:args])
klass.perform_now(*job_data[:args]) job = job_data[:job].new(*args)
job.perform_now
end end
end end
end end

View File

@@ -108,8 +108,9 @@ class ArtistTest < ActiveSupport::TestCase
end end
should "create a new tag implication" do should "create a new tag implication" do
workoff_active_jobs
assert_equal(1, TagImplication.where(:antecedent_name => "aaa", :consequent_name => "banned_artist").count) assert_equal(1, TagImplication.where(:antecedent_name => "aaa", :consequent_name => "banned_artist").count)
assert_equal("aaa banned_artist", @post.tag_string) assert_equal("aaa banned_artist", @post.reload.tag_string)
end end
should "set the approver of the banned_artist implication" do should "set the approver of the banned_artist implication" do

View File

@@ -72,6 +72,9 @@ class BulkUpdateRequestTest < ActiveSupport::TestCase
@bur = FactoryBot.create(:bulk_update_request, :script => @script) @bur = FactoryBot.create(:bulk_update_request, :script => @script)
@bur.approve!(@admin) @bur.approve!(@admin)
assert_enqueued_jobs(2)
workoff_active_jobs
@ta = TagAlias.where(:antecedent_name => "foo", :consequent_name => "bar").first @ta = TagAlias.where(:antecedent_name => "foo", :consequent_name => "bar").first
@ti = TagImplication.where(:antecedent_name => "bar", :consequent_name => "baz").first @ti = TagImplication.where(:antecedent_name => "bar", :consequent_name => "baz").first
end end

View File

@@ -127,7 +127,9 @@ class TagAliasTest < ActiveSupport::TestCase
tag2 = FactoryBot.create(:tag, :name => "bbb") tag2 = FactoryBot.create(:tag, :name => "bbb")
ss = FactoryBot.create(:saved_search, :query => "123 ... 456", :user => CurrentUser.user) ss = FactoryBot.create(:saved_search, :query => "123 ... 456", :user => CurrentUser.user)
ta = FactoryBot.create(:tag_alias, :antecedent_name => "...", :consequent_name => "bbb") ta = FactoryBot.create(:tag_alias, :antecedent_name => "...", :consequent_name => "bbb")
ta.approve!(approver: @admin) ta.approve!(approver: @admin)
workoff_active_jobs
assert_equal(%w(123 456 bbb), ss.reload.query.split.sort) assert_equal(%w(123 456 bbb), ss.reload.query.split.sort)
end end
@@ -139,6 +141,7 @@ class TagAliasTest < ActiveSupport::TestCase
ta = FactoryBot.create(:tag_alias, :antecedent_name => "aaa", :consequent_name => "ccc") ta = FactoryBot.create(:tag_alias, :antecedent_name => "aaa", :consequent_name => "ccc")
ta.approve!(approver: @admin) ta.approve!(approver: @admin)
workoff_active_jobs
assert_equal("bbb ccc", post1.reload.tag_string) assert_equal("bbb ccc", post1.reload.tag_string)
assert_equal("ccc ddd", post2.reload.tag_string) assert_equal("ccc ddd", post2.reload.tag_string)
@@ -157,8 +160,11 @@ class TagAliasTest < ActiveSupport::TestCase
should "move existing aliases" do should "move existing aliases" do
ta1 = FactoryBot.create(:tag_alias, :antecedent_name => "aaa", :consequent_name => "bbb", :status => "pending") ta1 = FactoryBot.create(:tag_alias, :antecedent_name => "aaa", :consequent_name => "bbb", :status => "pending")
ta2 = FactoryBot.create(:tag_alias, :antecedent_name => "bbb", :consequent_name => "ccc", :status => "pending") ta2 = FactoryBot.create(:tag_alias, :antecedent_name => "bbb", :consequent_name => "ccc", :status => "pending")
ta1.approve!(approver: @admin)
# XXX this is broken, it depends on the order the jobs are executed in.
ta2.approve!(approver: @admin) ta2.approve!(approver: @admin)
ta1.approve!(approver: @admin)
workoff_active_jobs
assert_equal("ccc", ta1.reload.consequent_name) assert_equal("ccc", ta1.reload.consequent_name)
end end
@@ -167,9 +173,9 @@ class TagAliasTest < ActiveSupport::TestCase
ti = FactoryBot.create(:tag_implication, :antecedent_name => "aaa", :consequent_name => "bbb") ti = FactoryBot.create(:tag_implication, :antecedent_name => "aaa", :consequent_name => "bbb")
ta = FactoryBot.create(:tag_alias, :antecedent_name => "bbb", :consequent_name => "ccc") ta = FactoryBot.create(:tag_alias, :antecedent_name => "bbb", :consequent_name => "ccc")
ta.approve!(approver: @admin) ta.approve!(approver: @admin)
workoff_active_jobs
ti.reload assert_equal("ccc", ti.reload.consequent_name)
assert_equal("ccc", ti.consequent_name)
end end
should "not push the antecedent's category to the consequent if the antecedent is general" do should "not push the antecedent's category to the consequent if the antecedent is general" do
@@ -184,7 +190,9 @@ class TagAliasTest < ActiveSupport::TestCase
tag1 = FactoryBot.create(:tag, :name => "aaa", :category => 1) tag1 = FactoryBot.create(:tag, :name => "aaa", :category => 1)
tag2 = FactoryBot.create(:tag, :name => "bbb", :category => 0) tag2 = FactoryBot.create(:tag, :name => "bbb", :category => 0)
ta = FactoryBot.create(:tag_alias, :antecedent_name => "aaa", :consequent_name => "bbb") ta = FactoryBot.create(:tag_alias, :antecedent_name => "aaa", :consequent_name => "bbb")
ta.approve!(approver: @admin) ta.approve!(approver: @admin)
workoff_active_jobs
assert_equal(1, tag2.reload.category) assert_equal(1, tag2.reload.category)
end end
@@ -205,32 +213,32 @@ class TagAliasTest < ActiveSupport::TestCase
@wiki1 = FactoryBot.create(:wiki_page, :title => "aaa") @wiki1 = FactoryBot.create(:wiki_page, :title => "aaa")
@wiki2 = FactoryBot.create(:wiki_page, :title => "bbb") @wiki2 = FactoryBot.create(:wiki_page, :title => "bbb")
@alias.approve!(approver: @admin) @alias.approve!(approver: @admin)
workoff_active_jobs
end end
@admin.reload # reload to get the forum post the approval created.
@topic.reload
end end
should "update the forum topic when approved" do should "update the forum topic when approved" do
assert_equal("[APPROVED] Tag alias: aaa -> bbb", @topic.title) assert_equal("[APPROVED] Tag alias: aaa -> bbb", @topic.reload.title)
assert_match(/The tag alias .* been approved/m, @topic.posts[-2].body) assert_match(/The tag alias .* been approved/m, @topic.posts.second.body)
end end
should "warn about conflicting wiki pages when approved" do should "warn about conflicting wiki pages when approved" do
assert_match(/has conflicting wiki pages/m, @topic.posts[-1].body) assert_match(/has conflicting wiki pages/m, @topic.posts.third.body)
end end
end end
should "update the topic when processed" do should "update the topic when processed" do
assert_difference("ForumPost.count") do assert_difference("ForumPost.count") do
@alias.approve!(approver: @admin) @alias.approve!(approver: @admin)
workoff_active_jobs
end end
end end
should "update the parent post" do should "update the parent post" do
previous = @post.body previous = @post.body
@alias.approve!(approver: @admin) @alias.approve!(approver: @admin)
@post.reload workoff_active_jobs
assert_not_equal(previous, @post.body) assert_not_equal(previous, @post.reload.body)
end end
should "update the topic when rejected" do should "update the topic when rejected" do
@@ -242,10 +250,9 @@ class TagAliasTest < ActiveSupport::TestCase
should "update the topic when failed" do should "update the topic when failed" do
@alias.stubs(:sleep).returns(true) @alias.stubs(:sleep).returns(true)
@alias.stubs(:update_posts).raises(Exception, "oh no") @alias.stubs(:update_posts).raises(Exception, "oh no")
@alias.approve!(approver: @admin) @alias.process!
@topic.reload
assert_equal("[FAILED] Tag alias: aaa -> bbb", @topic.title) assert_equal("[FAILED] Tag alias: aaa -> bbb", @topic.reload.title)
assert_match(/error: oh no/, @alias.status) assert_match(/error: oh no/, @alias.status)
assert_match(/The tag alias .* failed during processing/, @topic.posts.last.body) assert_match(/The tag alias .* failed during processing/, @topic.posts.last.body)
end end

View File

@@ -246,10 +246,9 @@ class TagImplicationTest < ActiveSupport::TestCase
ti1 = FactoryBot.create(:tag_implication, :antecedent_name => "aaa", :consequent_name => "xxx") ti1 = FactoryBot.create(:tag_implication, :antecedent_name => "aaa", :consequent_name => "xxx")
ti2 = FactoryBot.create(:tag_implication, :antecedent_name => "aaa", :consequent_name => "yyy") ti2 = FactoryBot.create(:tag_implication, :antecedent_name => "aaa", :consequent_name => "yyy")
perform_enqueued_jobs do ti1.approve!
ti1.approve! ti2.approve!
ti2.approve! workoff_active_jobs
end
assert_equal("aaa bbb ccc xxx yyy", p1.reload.tag_string) assert_equal("aaa bbb ccc xxx yyy", p1.reload.tag_string)
end end
@@ -264,9 +263,8 @@ class TagImplicationTest < ActiveSupport::TestCase
should "update the topic when processed" do should "update the topic when processed" do
assert_difference("ForumPost.count") do assert_difference("ForumPost.count") do
perform_enqueued_jobs do @implication.approve!
@implication.approve! workoff_active_jobs
end
end end
assert_match(/The tag implication .* has been approved/, @post.reload.body) assert_match(/The tag implication .* has been approved/, @post.reload.body)