|
|
|
@@ -13,13 +13,23 @@ RSpec.describe Tag, type: :model do |
|
|
|
context 'when merging a simple source tag' do |
|
|
|
let!(:source_post_tag) { PostTag.create!(post: post_record, tag: source_tag) } |
|
|
|
|
|
|
|
it 'moves the post_tag, deletes the source tag, and aliases the source tag_name' do |
|
|
|
it 'discards the source post_tag, creates an active target post_tag, ' + |
|
|
|
'discards the source tag, and aliases the source tag_name' do |
|
|
|
described_class.merge_tags!(target_tag, [source_tag]) |
|
|
|
|
|
|
|
expect(source_post_tag.reload.tag_id).to eq(target_tag.id) |
|
|
|
expect(Tag.exists?(source_tag.id)).to be(false) |
|
|
|
expect(source_tag_name.reload.canonical_id).to eq(target_tag.tag_name_id) |
|
|
|
expect(target_tag.reload.post_count).to eq(1) |
|
|
|
|
|
|
|
source_pt = PostTag.with_discarded.find(source_post_tag.id) |
|
|
|
active_target = PostTag.kept.find_by(post_id: post_record.id, tag_id: target_tag.id) |
|
|
|
|
|
|
|
expect(source_pt.discarded_at).to be_present |
|
|
|
expect(source_pt.tag_id).to eq(source_tag.id) |
|
|
|
expect(active_target).to be_present |
|
|
|
|
|
|
|
expect(Tag.with_discarded.find(source_tag.id)).to be_discarded |
|
|
|
expect(source_tag.tag_name.reload.canonical_id).to eq(target_tag.tag_name_id) |
|
|
|
end |
|
|
|
end |
|
|
|
|
|
|
|
@@ -27,31 +37,48 @@ RSpec.describe Tag, type: :model do |
|
|
|
let!(:target_post_tag) { PostTag.create!(post: post_record, tag: target_tag) } |
|
|
|
let!(:source_post_tag) { PostTag.create!(post: post_record, tag: source_tag) } |
|
|
|
|
|
|
|
it 'discards the duplicate source post_tag and keeps one active target post_tag' do |
|
|
|
it 'discards the source post_tag, keeps one active target post_tag, ' + |
|
|
|
'and aliases the source tag_name' do |
|
|
|
described_class.merge_tags!(target_tag, [source_tag]) |
|
|
|
|
|
|
|
source_pt = PostTag.with_discarded.find(source_post_tag.id) |
|
|
|
active = PostTag.kept.where(post_id: post_record.id, tag_id: target_tag.id) |
|
|
|
discarded_source = PostTag.with_discarded.find(source_post_tag.id) |
|
|
|
|
|
|
|
expect(source_pt.discarded_at).to be_present |
|
|
|
expect(source_pt.tag_id).to eq(source_tag.id) |
|
|
|
expect(active.count).to eq(1) |
|
|
|
expect(discarded_source.discarded_at).to be_present |
|
|
|
expect(discarded_source.tag_id).to eq(target_tag.id) |
|
|
|
|
|
|
|
expect(source_pt.discarded_at).to be_present |
|
|
|
expect(source_pt.tag_id).to eq(source_tag.id) |
|
|
|
expect(Tag.exists?(source_tag.id)).to be(false) |
|
|
|
expect(source_tag_name.reload.canonical_id).to eq(target_tag.tag_name_id) |
|
|
|
expect(target_tag.reload.post_count).to eq(1) |
|
|
|
|
|
|
|
expect(active.first.id).to eq(target_post_tag.id) |
|
|
|
|
|
|
|
expect(Tag.with_discarded.find(source_tag.id)).to be_discarded |
|
|
|
expect(source_tag.tag_name.reload.canonical_id).to eq(target_tag.tag_name_id) |
|
|
|
end |
|
|
|
end |
|
|
|
|
|
|
|
context 'when source_tags includes the target itself' do |
|
|
|
let!(:source_post_tag) { PostTag.create!(post: post_record, tag: source_tag) } |
|
|
|
|
|
|
|
it 'ignores the target tag in source_tags' do |
|
|
|
it 'ignores the target in source_tags while still merging the source tag' do |
|
|
|
described_class.merge_tags!(target_tag, [source_tag, target_tag]) |
|
|
|
|
|
|
|
expect(Tag.exists?(target_tag.id)).to be(true) |
|
|
|
expect(Tag.exists?(source_tag.id)).to be(false) |
|
|
|
expect(source_post_tag.reload.tag_id).to eq(target_tag.id) |
|
|
|
expect(target_tag.reload.post_count).to eq(1) |
|
|
|
|
|
|
|
source_pt = PostTag.with_discarded.find(source_post_tag.id) |
|
|
|
active_target = PostTag.kept.find_by(post_id: post_record.id, tag_id: target_tag.id) |
|
|
|
|
|
|
|
expect(Tag.find(target_tag.id)).to be_present |
|
|
|
expect(Tag.with_discarded.find(source_tag.id)).to be_discarded |
|
|
|
expect(source_pt.discarded_at).to be_present |
|
|
|
expect(source_pt.tag_id).to eq(source_tag.id) |
|
|
|
expect(active_target).to be_present |
|
|
|
end |
|
|
|
end |
|
|
|
|
|
|
|
@@ -59,19 +86,16 @@ RSpec.describe Tag, type: :model do |
|
|
|
let!(:source_post_tag) { PostTag.create!(post: post_record, tag: source_tag) } |
|
|
|
let!(:wiki_page) do |
|
|
|
WikiPage.create!( |
|
|
|
tag_name: source_tag_name, |
|
|
|
tag_name: source_tag.tag_name, |
|
|
|
created_user: create_admin_user!, |
|
|
|
updated_user: create_admin_user! |
|
|
|
) |
|
|
|
end |
|
|
|
|
|
|
|
it 'still merges by bypassing validations' do |
|
|
|
described_class.merge_tags!(target_tag, [source_tag]) |
|
|
|
|
|
|
|
expect(Tag.exists?(source_tag.id)).to be(false) |
|
|
|
expect(source_post_tag.reload.tag_id).to eq(target_tag.id) |
|
|
|
expect(source_tag_name.reload.canonical_id).to eq(target_tag.tag_name_id) |
|
|
|
expect(source_tag_name.reload).not_to be_valid |
|
|
|
expect { |
|
|
|
described_class.merge_tags!(target_tag, [source_tag]) |
|
|
|
}.to raise_error(ActiveRecord::RecordInvalid) |
|
|
|
end |
|
|
|
end |
|
|
|
|
|
|
|
@@ -86,6 +110,10 @@ RSpec.describe Tag, type: :model do |
|
|
|
expect(Tag.exists?(source_tag.id)).to be(false) |
|
|
|
expect(TagName.exists?(source_tag_name_id)).to be(false) |
|
|
|
expect(target_tag.reload.post_count).to eq(0) |
|
|
|
|
|
|
|
expect(Tag.with_discarded.find(source_tag.id)).not_to be_discarded |
|
|
|
expect(PostTag.kept.find(source_post_tag.id).tag_id).to eq(source_tag.id) |
|
|
|
expect(source_tag.tag_name.reload.canonical_id).to be_nil |
|
|
|
end |
|
|
|
end |
|
|
|
end |
|
|
|
|