このコミットが含まれているのは:
@@ -1,5 +1,73 @@
|
||||
require 'rails_helper'
|
||||
|
||||
RSpec.describe PostTag, type: :model do
|
||||
describe 'uniqueness' do
|
||||
it 'rejects duplicate post and tag pairs but allows either to be reused' do
|
||||
post_tag = create(:post_tag)
|
||||
duplicate = build(:post_tag, post: post_tag.post, tag: post_tag.tag)
|
||||
|
||||
expect(duplicate).not_to be_valid
|
||||
expect(duplicate.errors.of_kind?(:post_id, :taken)).to be(true)
|
||||
expect(build(:post_tag, post: post_tag.post, tag: create(:tag))).to be_valid
|
||||
expect(build(:post_tag, post: create(:post), tag: post_tag.tag)).to be_valid
|
||||
end
|
||||
|
||||
it 'enforces uniqueness in the database when validation is bypassed' do
|
||||
post_tag = create(:post_tag)
|
||||
duplicate = build(:post_tag, post: post_tag.post, tag: post_tag.tag)
|
||||
|
||||
expect { duplicate.save!(validate: false) }
|
||||
.to raise_error(ActiveRecord::RecordNotUnique)
|
||||
end
|
||||
end
|
||||
|
||||
describe '#destroy!' do
|
||||
it 'deletes only the selected pair and its sections and updates the counter' do
|
||||
post_tag = create(:post_tag)
|
||||
same_post = create(:post_tag, post: post_tag.post)
|
||||
same_tag = create(:post_tag, tag: post_tag.tag)
|
||||
sections = [post_tag, same_post, same_tag].map do |link|
|
||||
create(:post_tag_section, post: link.post, tag: link.tag,
|
||||
begin_ms: 1000, end_ms: 2000)
|
||||
end
|
||||
|
||||
expect { post_tag.destroy! }.to change(described_class, :count).by(-1)
|
||||
.and change(PostTagSection, :count).by(-1)
|
||||
.and change { post_tag.tag.reload.post_count }.from(2).to(1)
|
||||
|
||||
expect(described_class.exists?(post: post_tag.post, tag: post_tag.tag)).to be(false)
|
||||
expect(same_post.reload).to be_persisted
|
||||
expect(same_tag.reload).to be_persisted
|
||||
expect(PostTagSection.all).to contain_exactly(*sections.drop(1))
|
||||
expect(post_tag.post.reload.tags).to contain_exactly(same_post.tag)
|
||||
expect(post_tag.tag.reload.posts).to contain_exactly(same_tag.post)
|
||||
end
|
||||
|
||||
it 'allows a removed tag to be added again without restoring old sections' do
|
||||
post_tag = create(:post_tag)
|
||||
create(:post_tag_section, post: post_tag.post, tag: post_tag.tag,
|
||||
begin_ms: 1000, end_ms: 2000)
|
||||
post_tag.destroy!
|
||||
|
||||
replacement = create(:post_tag, post: post_tag.post, tag: post_tag.tag)
|
||||
|
||||
expect(replacement.reload.sections).to be_empty
|
||||
expect(replacement.tag.reload.post_count).to eq(1)
|
||||
end
|
||||
end
|
||||
|
||||
describe '#sections' do
|
||||
it 'loads the owning post_tag from a section using both keys' do
|
||||
post_tag = create(:post_tag)
|
||||
create(:post_tag, post: post_tag.post)
|
||||
create(:post_tag, tag: post_tag.tag)
|
||||
section = create(:post_tag_section, post: post_tag.post,
|
||||
tag: post_tag.tag,
|
||||
begin_ms: 1000, end_ms: 2000)
|
||||
|
||||
expect(section.reload.post_tag).to eq(post_tag)
|
||||
end
|
||||
|
||||
it 'loads sections by post_id and tag_id' do
|
||||
post_tag = create(:post_tag)
|
||||
section = create(:post_tag_section,
|
||||
|
||||
@@ -185,15 +185,14 @@ 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 'discards the source post_tag, creates an active target post_tag, discards the source tag, and aliases the source tag_name' do
|
||||
it 'deletes the source link, links the target, and aliases the discarded source tag' do
|
||||
described_class.merge_tags!(target_tag, [source_tag])
|
||||
|
||||
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)
|
||||
target_link = PostTag.find_by(post: post_record, tag: target_tag)
|
||||
|
||||
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(PostTag.exists?(post: post_record, tag: source_tag)).to be(false)
|
||||
expect(target_link).to be_present
|
||||
expect(source_tag.reload.post_count).to eq(0)
|
||||
|
||||
expect(Tag.with_discarded.find(source_tag.id)).to be_discarded
|
||||
expect(TagName.with_discarded.find(source_tag_name.id)).not_to be_discarded
|
||||
@@ -206,16 +205,22 @@ 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 source post_tag, keeps one active target post_tag, discards the source tag, and aliases the source tag_name' do
|
||||
it 'deletes the source link and preserves the existing target link' do
|
||||
create(:post_tag_section, post: post_record, tag: source_tag,
|
||||
begin_ms: 1000, end_ms: 2000)
|
||||
target_section = create(:post_tag_section, post: post_record,
|
||||
tag: target_tag,
|
||||
begin_ms: 3000, end_ms: nil)
|
||||
|
||||
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)
|
||||
target_links = PostTag.where(post: post_record, tag: target_tag)
|
||||
|
||||
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(active.first.id).to eq(target_post_tag.id)
|
||||
expect(PostTag.exists?(post: post_record, tag: source_tag)).to be(false)
|
||||
expect(target_links).to contain_exactly(target_post_tag)
|
||||
expect(source_tag.reload.post_count).to eq(0)
|
||||
expect(PostTagSection.where(post: post_record, tag: source_tag)).to be_empty
|
||||
expect(target_post_tag.reload.sections).to contain_exactly(target_section)
|
||||
|
||||
expect(Tag.with_discarded.find(source_tag.id)).to be_discarded
|
||||
expect(TagName.with_discarded.find(source_tag_name.id)).not_to be_discarded
|
||||
@@ -230,14 +235,12 @@ RSpec.describe Tag, type: :model 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])
|
||||
|
||||
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)
|
||||
target_link = PostTag.find_by(post: post_record, tag: target_tag)
|
||||
|
||||
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
|
||||
expect(PostTag.exists?(post: post_record, tag: source_tag)).to be(false)
|
||||
expect(target_link).to be_present
|
||||
expect(source_tag_name.reload.canonical_id).to eq(target_tag.tag_name_id)
|
||||
expect(target_tag.reload.post_count).to eq(1)
|
||||
end
|
||||
@@ -263,13 +266,10 @@ RSpec.describe Tag, type: :model do
|
||||
it 'still merges, but discards the source tag_name instead of aliasing it' do
|
||||
described_class.merge_tags!(target_tag, [source_tag])
|
||||
|
||||
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)
|
||||
discarded_source_tag_name = TagName.with_discarded.find(source_tag_name.id)
|
||||
target_link = PostTag.find_by(post: post_record, tag: target_tag)
|
||||
|
||||
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(PostTag.exists?(post: post_record, tag: source_tag)).to be(false)
|
||||
expect(target_link).to be_present
|
||||
|
||||
expect(Tag.with_discarded.find(source_tag.id)).to be_discarded
|
||||
expect(target_tag.reload.post_count).to eq(1)
|
||||
@@ -289,14 +289,20 @@ RSpec.describe Tag, type: :model do
|
||||
end
|
||||
|
||||
it 'rolls back the transaction' do
|
||||
source_section = create(:post_tag_section, post: post_record,
|
||||
tag: source_tag,
|
||||
begin_ms: 1000, end_ms: 2000)
|
||||
|
||||
expect {
|
||||
described_class.merge_tags!(target_tag, [source_tag])
|
||||
}.to raise_error(ActiveRecord::RecordInvalid)
|
||||
|
||||
expect(Tag.with_discarded.find(source_tag.id)).not_to be_discarded
|
||||
expect(TagName.with_discarded.find(source_tag_name.id)).not_to be_discarded
|
||||
expect(PostTag.kept.find(source_post_tag.id).tag_id).to eq(source_tag.id)
|
||||
expect(PostTag.kept.find_by(post_id: post_record.id, tag_id: target_tag.id)).to be_nil
|
||||
expect(source_post_tag.reload.tag_id).to eq(source_tag.id)
|
||||
expect(source_post_tag.sections).to contain_exactly(source_section)
|
||||
expect(PostTag.find_by(post: post_record, tag: target_tag)).to be_nil
|
||||
expect(source_tag.reload.post_count).to eq(1)
|
||||
expect(source_tag_name.reload.canonical_id).to be_nil
|
||||
expect(target_tag.reload.post_count).to eq(0)
|
||||
end
|
||||
@@ -365,12 +371,15 @@ RSpec.describe Tag, type: :model do
|
||||
expect(latest.event_type).to eq('update')
|
||||
expect(latest.created_by_user).to be_nil
|
||||
expect(latest.tags).to eq(snapshot_tags(post_record.reload))
|
||||
expect(latest.tags_json.map { |item| item.fetch('id') }).to eq([target_tag.id])
|
||||
expect(affected_versions.first.tags_json.map { |item| item.fetch('id') })
|
||||
.to eq([source_tag.id])
|
||||
|
||||
expect(unaffected_post.reload.post_versions.count).to eq(1)
|
||||
end
|
||||
end
|
||||
|
||||
context 'when the source tag has no active post_tags' do
|
||||
context 'when the source tag has no post_tags' do
|
||||
let!(:another_post) do
|
||||
Post.create!(url: 'https://example.com/posts/3', title: 'another post')
|
||||
end
|
||||
|
||||
新しいイシューから参照
ユーザーをブロックする