require 'rails_helper' RSpec.describe Locale, type: :model do include_context 'Ainu locale' def create_ainu_locale! prepare_ainu_reference! described_class.create!(code: 'ain-Kana', language_code: 'ain', script_code: 'Kana', region_code: 392, variants: '', name: 'Ainu') end it 'keeps the active Nipponese locale consistent with its identity tuple' do expect(described_class.where(deprecated_at: nil).pluck(:code)).to eq(['ja']) expect(described_class.nipponese).to have_attributes( code: 'ja', language_code: 'ja', script_code: 'Jpan', region_code: 392, variants: '') end it 'rejects a duplicate locale identity tuple at the database boundary' do expect { described_class.insert_all!([{ code: 'ja-duplicate', language_code: 'ja', script_code: 'Jpan', region_code: 392, variants: '', name: '日本語', created_at: Time.current }]) }.to raise_error(ActiveRecord::RecordNotUnique) end it 'generates a primary name for every existing tag using the name generator' do first = create(:tag, primary_name: 'first_existing_tag') second = create(:tag, primary_name: 'second_existing_tag') allow(TagName).to receive(:generate_name).and_call_original allow(TagName).to receive(:generate_name) .with(kind_of(described_class), first, primary_tag_name_for(first, 'ja')) .and_return('name_from_generator') locale = create_ainu_locale! expect(TagName).to have_received(:generate_name) .with(locale, first, primary_tag_name_for(first, 'ja')) expect(TagName).to have_received(:generate_name) .with(locale, second, primary_tag_name_for(second, 'ja')) expect(first.tag_names.find_by!(language_code: 'ain', primary_flg: true)) .to have_attributes(name: 'name_from_generator', tag_id: first.id, language_code: 'ain', script_code: 'Kana', primary_flg: true, auto_generated: true) expect(second.tag_names.find_by!(language_code: 'ain', primary_flg: true)) .to have_attributes(tag_id: second.id, language_code: 'ain', script_code: 'Kana', primary_flg: true, auto_generated: true) expect(TagName.where(language_code: 'ain', primary_flg: true, tag_id: [first.id, second.id]).count).to eq(2) end it 'does not duplicate an existing primary name in the new language' do prepare_ainu_reference! tag = create(:tag, primary_name: 'already_named') existing = create(:tag_name, name: 'existing_ainu', tag:, language_code: 'ain', script_code: 'Kana') create_ainu_locale! expect(TagName.where(tag_id: tag.id, language_code: 'ain', primary_flg: true)) .to contain_exactly(existing) expect(existing.reload).to have_attributes( tag_id: tag.id, language_code: 'ain', script_code: 'Kana', primary_flg: true, auto_generated: false) end it 'adds the new language primary while preserving existing names and aliases' do tag = create(:tag, primary_name: 'named_tag') create(:tag_name, :alias, name: 'existing_alias', tag:, language_code: 'ja', script_code: 'Jpan') original_names = tag.tag_names.order(:id).map(&:attributes) create_ainu_locale! expect(tag.tag_names.where(language_code: 'ja').order(:id).map(&:attributes)) .to eq(original_names) generated = TagName.find_by!(tag_id: tag.id, language_code: 'ain', primary_flg: true) expect(generated).to have_attributes(name: 'named_tag', tag_id: tag.id, language_code: 'ain', script_code: 'Kana', primary_flg: true, auto_generated: true) expect(TagName.where(tag_id: tag.id, language_code: 'ain', primary_flg: true).count).to eq(1) end it 'rolls back the locale and all generated names when backfill fails' do prepare_ainu_reference! first = create(:tag, primary_name: 'atomic_first') second = create(:tag, primary_name: 'atomic_second') allow(TagName).to receive(:generate_name).and_call_original allow(TagName).to receive(:generate_name) .with(kind_of(described_class), second, primary_tag_name_for(second, 'ja')) .and_raise('backfill failed') expect { create_ainu_locale! }.to raise_error('backfill failed') expect(described_class.exists?(code: 'ain-Kana')).to be(false) expect(TagName.where(language_code: 'ain', tag_id: [first.id, second.id])).to be_empty end it 'rejects a private locale code outside the supported locale set' do prepare_ainu_reference! expect { described_class.create!( code: 'ain-Kana-x-rspec', language_code: 'ain', script_code: 'Kana', region_code: 392, variants: 'x-rspec', name: 'Ainu') }.to raise_error(ArgumentError) end it 'registers the supported Ainu locale tuple' do expect(locale).to have_attributes(code: 'ain-Kana', language_code: 'ain', script_code: 'Kana', region_code: 392, variants: '') end end