#281 テストまだ通ってないので要確認

このコミットが含まれているのは:
2026-03-09 21:06:58 +09:00
コミット 1e788de9a0
9個のファイルの変更232行の追加12行の削除
+63
ファイルの表示
@@ -0,0 +1,63 @@
require 'rails_helper'
RSpec.describe TagNameSanitisationRule, type: :model do
describe '.sanitise' do
before do
described_class.create!(priority: 10, source_pattern: '_', replacement: '')
described_class.create!(priority: 20, source_pattern: 'ABC', replacement: 'abc')
end
it 'applies rules in priority order' do
expect(described_class.sanitise('A_B_C')).to eq('abc')
end
it 'does not fail when a rule does not match' do
expect { described_class.sanitise('xyz') }.not_to raise_error
expect(described_class.sanitise('xyz')).to eq('xyz')
end
end
describe 'validations' do
it 'is invalid when source_pattern is not a valid regexp' do
rule = described_class.new(priority: 10, source_pattern: '[', replacement: '')
expect(rule).to be_invalid
expect(rule.errors[:source_pattern]).to be_present
end
end
describe '.apply!' do
before do
described_class.create!(priority: 10, source_pattern: '_', replacement: '')
end
context 'when no conflicting tag_name exists' do
let!(:tag_name) { TagName.create!(name: 'foo_bar') }
it 'renames the tag_name' do
described_class.apply!
expect(tag_name.reload.name).to eq('foobar')
end
end
context 'when a conflicting canonical tag_name exists' do
let!(:existing) { TagName.create!(name: 'foobar') }
let!(:source) { TagName.create!(name: 'foo_bar') }
it 'deletes the source tag_name' do
described_class.apply!
expect(TagName.exists?(source.id)).to be(false)
expect(existing.reload.name).to eq('foobar')
end
end
context 'when the source tag_name has a tag and the existing one has no tag' do
let!(:existing) { TagName.create!(name: 'foobar') }
let!(:source_tag) { create(:tag, name: 'foo_bar', category: :general) }
it 'moves the tag to the existing tag_name' do
described_class.apply!
expect(source_tag.reload.tag_name_id).to eq(existing.id)
end
end
end
end
+28
ファイルの表示
@@ -0,0 +1,28 @@
require 'rails_helper'
RSpec.describe TagName, type: :model do
describe 'before_validation :sanitise_name' do
before do
TagNameSanitisationRule.create!(priority: 10, source_pattern: '_', replacement: '')
end
it 'sanitises name on create' do
tag_name = TagName.create!(name: 'foo_bar')
expect(tag_name.reload.name).to eq('foobar')
end
it 'sanitises name on update' do
tag_name = TagName.create!(name: 'foobar')
tag_name.update!(name: 'foo_bar')
expect(tag_name.reload.name).to eq('foobar')
end
it 'becomes invalid when sanitised name conflicts with another tag_name' do
TagName.create!(name: 'foobar')
tag_name = TagName.new(name: 'foo_bar')
expect(tag_name).to be_invalid
expect(tag_name.errors[:name]).to be_present
end
end
end
+30 -9
ファイルの表示
@@ -2,10 +2,13 @@ require 'rails_helper'
RSpec.describe Tag, type: :model do
describe '.merge_tags!' do
let!(:target_tag) { create(:tag) }
let!(:source_tag) { create(:tag) }
let!(:target_tag) { create(:tag, category: :general) }
let!(:source_tag) { create(:tag, category: :general) }
let!(:source_tag_name) { source_tag.tag_name }
let!(:post_record) { Post.create!(url: 'https://example.com/posts/1', title: 'test post') }
let!(:post_record) do
Post.create!(url: 'https://example.com/posts/1', title: 'test post')
end
context 'when merging a simple source tag' do
let!(:source_post_tag) { PostTag.create!(post: post_record, tag: source_tag) }
@@ -15,7 +18,8 @@ RSpec.describe Tag, type: :model do
expect(source_post_tag.reload.tag_id).to eq(target_tag.id)
expect(Tag.exists?(source_tag.id)).to be(false)
expect(source_tag.tag_name.reload.canonical_id).to eq(target_tag.tag_name_id)
expect(source_tag_name.reload.canonical_id).to eq(target_tag.tag_name_id)
expect(target_tag.reload.post_count).to eq(1)
end
end
@@ -31,8 +35,10 @@ RSpec.describe Tag, type: :model do
expect(active.count).to eq(1)
expect(discarded_source.discarded_at).to be_present
expect(discarded_source.tag_id).to eq(source_tag.id)
expect(Tag.exists?(source_tag.id)).to be(false)
expect(source_tag.tag_name.reload.canonical_id).to eq(target_tag.tag_name_id)
expect(source_tag_name.reload.canonical_id).to eq(target_tag.tag_name_id)
expect(target_tag.reload.post_count).to eq(1)
end
end
@@ -45,6 +51,7 @@ RSpec.describe Tag, type: :model do
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)
end
end
@@ -52,9 +59,10 @@ 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.tag_name,
created_user: create_admin_user!,
updated_user: create_admin_user!)
tag_name: source_tag_name,
created_user: create_admin_user!,
updated_user: create_admin_user!
)
end
it 'rolls back the transaction' do
@@ -64,7 +72,20 @@ RSpec.describe Tag, type: :model do
expect(Tag.exists?(source_tag.id)).to be(true)
expect(source_post_tag.reload.tag_id).to eq(source_tag.id)
expect(source_tag.tag_name.reload.canonical_id).to be_nil
expect(source_tag_name.reload.canonical_id).to be_nil
end
end
context 'when merging a nico source tag' do
let!(:target_tag) { create(:tag, category: :nico, name: 'nico:foo') }
let!(:source_tag) { create(:tag, category: :nico, name: 'nico:bar') }
let!(:source_tag_name_id) { source_tag.tag_name_id }
it 'deletes the source tag_name instead of aliasing it' do
described_class.merge_tags!(target_tag, [source_tag])
expect(Tag.exists?(source_tag.id)).to be(false)
expect(TagName.exists?(source_tag_name_id)).to be(false)
end
end
end