Files
danbooru/test/unit/alias_and_implication_importer_test.rb
evazion 71a3cc89fd BURs: don't try to move wikis/artists twice.
* Remove unnecessary rename_aliased_pages option. This option was always enabled.
* Don't try to rename the artist and wiki page inside AliasAndImplicationImporter
  when an alias is approved. This is already handled by TagAlias#process!.
2020-05-10 21:22:22 -05:00

137 lines
4.2 KiB
Ruby

require 'test_helper'
class AliasAndImplicationImporterTest < ActiveSupport::TestCase
context "The alias and implication importer" do
setup do
CurrentUser.user = FactoryBot.create(:admin_user)
CurrentUser.ip_addr = "127.0.0.1"
end
teardown do
CurrentUser.user = nil
CurrentUser.ip_addr = nil
end
context "category command" do
setup do
@tag = Tag.find_or_create_by_name("hello")
@list = "category hello -> artist\n"
@importer = AliasAndImplicationImporter.new(@list, nil)
end
should "work" do
@importer.process!
@tag.reload
assert_equal(Tag.categories.value_for("artist"), @tag.category)
end
end
context "#affected_tags" do
setup do
FactoryBot.create(:post, tag_string: "aaa")
FactoryBot.create(:post, tag_string: "bbb")
FactoryBot.create(:post, tag_string: "ccc")
FactoryBot.create(:post, tag_string: "ddd")
FactoryBot.create(:post, tag_string: "eee")
@script = "create alias aaa -> 000\n" +
"create implication bbb -> 111\n" +
"remove alias ccc -> 222\n" +
"remove implication ddd -> 333\n" +
"mass update eee -> 444\n"
end
subject { AliasAndImplicationImporter.new(@script, nil) }
should "return the correct tags" do
assert_equal(%w(aaa 000 bbb 111 ccc 222 ddd 333 eee 444), subject.affected_tags)
end
end
context "given a valid list" do
setup do
@list = "create alias abc -> def\ncreate implication aaa -> bbb\n"
@importer = AliasAndImplicationImporter.new(@list, nil)
end
should "process it" do
@importer.process!
assert(TagAlias.exists?(antecedent_name: "abc", consequent_name: "def"))
assert(TagImplication.exists?(antecedent_name: "aaa", consequent_name: "bbb"))
end
end
context "given a list with an invalid command" do
setup do
@list = "zzzz abc -> def\n"
@importer = AliasAndImplicationImporter.new(@list, nil)
end
should "throw an exception" do
assert_raises(RuntimeError) do
@importer.process!
end
end
end
context "given a list with a logic error" do
setup do
@list = "remove alias zzz -> yyy\n"
@importer = AliasAndImplicationImporter.new(@list, nil)
end
should "throw an exception" do
assert_raises(RuntimeError) do
@importer.process!
end
end
end
should "rename an aliased tag's artist entry and wiki page" do
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", "")
@importer.process!
perform_enqueued_jobs
assert_equal("bbb", artist.reload.name)
assert_equal("bbb", wiki.reload.title)
end
context "remove alias and remove implication commands" do
setup do
@ta = FactoryBot.create(:tag_alias, antecedent_name: "a", consequent_name: "b", status: "active")
@ti = FactoryBot.create(:tag_implication, antecedent_name: "c", consequent_name: "d", status: "active")
@script = %{
remove alias a -> b
remove implication c -> d
}
@importer = AliasAndImplicationImporter.new(@script, nil)
end
should "set aliases and implications as deleted" do
@importer.process!
assert_equal("deleted", @ta.reload.status)
assert_equal("deleted", @ti.reload.status)
end
should "create modactions for each removal" do
assert_difference(-> { ModAction.count }, 2) do
@importer.process!
end
end
should "only remove active aliases and implications" do
@ta2 = FactoryBot.create(:tag_alias, antecedent_name: "a", consequent_name: "b", status: "pending")
@ti2 = FactoryBot.create(:tag_implication, antecedent_name: "c", consequent_name: "d", status: "pending")
@importer.process!
assert_equal("pending", @ta2.reload.status)
assert_equal("pending", @ti2.reload.status)
end
end
end
end