From aae7c5a2f3f7b6ff2f2d7152ff424be1f6bc85a3 Mon Sep 17 00:00:00 2001 From: Diego Steiner Date: Fri, 15 Dec 2023 15:28:53 +0100 Subject: [PATCH] spec: add specs for migration --- ...respondance_language_with_core_language.rb | 23 ------ ...respondence_language_with_core_language.rb | 30 +++++++ .../replace_correspondence_language_spec.rb | 81 ++++++++++--------- 3 files changed, 75 insertions(+), 59 deletions(-) delete mode 100644 db/migrate/20230915105810_replace_correspondance_language_with_core_language.rb create mode 100644 db/migrate/20230915105810_replace_correspondence_language_with_core_language.rb diff --git a/db/migrate/20230915105810_replace_correspondance_language_with_core_language.rb b/db/migrate/20230915105810_replace_correspondance_language_with_core_language.rb deleted file mode 100644 index 739f75fde..000000000 --- a/db/migrate/20230915105810_replace_correspondance_language_with_core_language.rb +++ /dev/null @@ -1,23 +0,0 @@ -class ReplaceCorrespondanceLanguageWithCoreLanguage < ActiveRecord::Migration[6.1] - CANTONS_LANGUAGE = { - # de: [:ag, :ai, :ar, :be, :bl, :bs, :gl, :gr, :lu, :nw, :ow, :sg, :sh, :so, :sz, :tg, :ur, :zg, :zh], - fr: [:fr, :ge, :ju, :ne, :vd, :vs], - it: [:ti] - }.freeze - - def up - Person.update_all("language = correspondence_language") - remove_column :people, :correspondence_language, :string, limit: 5 - - Person.where(language: nil).find_each do |person| - person.update!(language: infer_person_language(person)) - end - end - - def infer_person_language(person) - return person.language if person.language.present? - - canton = person.find_kantonalverband&.kantonalverband_cantons&.first&.to_sym - CANTONS_LANGUAGE.find { |language, cantons| cantons.include?(canton) }&.first || :de - end -end diff --git a/db/migrate/20230915105810_replace_correspondence_language_with_core_language.rb b/db/migrate/20230915105810_replace_correspondence_language_with_core_language.rb new file mode 100644 index 000000000..d4c9c5c52 --- /dev/null +++ b/db/migrate/20230915105810_replace_correspondence_language_with_core_language.rb @@ -0,0 +1,30 @@ +class ReplaceCorrespondenceLanguageWithCoreLanguage < ActiveRecord::Migration[6.1] + CANTONS_LANGUAGE = { + # de: [:ag, :ai, :ar, :be, :bl, :bs, :gl, :gr, :lu, :nw, :ow, :sg, :sh, :so, :sz, :tg, :ur, :zg, :zh], + fr: [:fr, :ge, :ju, :ne, :vd, :vs], + it: [:ti] + }.freeze + + def up + Person.where.not(correspondence_language: nil).update_all("language = correspondence_language") + Person.where(correspondence_language: nil).find_each do |person| + person.update!(language: infer_person_language(person)) + end + + remove_column :people, :correspondence_language, :string, limit: 5 + end + + def down + add_column :people, :correspondence_language, :string, limit: 5 + Person.update_all("correspondence_language = language") + end + + def infer_person_language(person) + return person.language if person.language.present? + return person.correspondence_language if person.try(:correspondence_language).present? + + kv = person.send(:find_kantonalverband)&.becomes(Group::Kantonalverband) + canton = kv&.kantonalverband_cantons&.first&.to_s&.downcase&.to_sym + CANTONS_LANGUAGE.find { |language, cantons| cantons.include?(canton) }&.first || :de + end +end diff --git a/spec/migrations/replace_correspondence_language_spec.rb b/spec/migrations/replace_correspondence_language_spec.rb index 668815738..755804496 100644 --- a/spec/migrations/replace_correspondence_language_spec.rb +++ b/spec/migrations/replace_correspondence_language_spec.rb @@ -6,45 +6,54 @@ # https://github.com/hitobito/hitobito_pbs. require 'spec_helper' -migration_file = Dir[Rails.root.join('../hitobito_pbs/db/migrate/20230915105810_replace_correspondance_language_with_core_language.rb')].first -require migration_file - -describe ReplaceCorrespondanceLanguageWithCoreLanguage do - let(:migration_context) { ActiveRecord::MigrationContext.new(ActiveRecord::Migrator.migrations_paths).migrations } - let(:previous_version) { 20230912120642 } - let(:current_version) { 20230915105810 } - subject(:migrate) { ActiveRecord::Migrator.new(:up, migration_context, current_version).migrate } - around do |example| - ActiveRecord::Migration.suppress_messages do - ActiveRecord::Migrator.new(:down, migrations, previous_version).migrate - Person.reset_column_information - example.run - Person.reset_column_information - end - end +require Rails.root.join('../hitobito_pbs/db/migrate/20230915105810_replace_correspondence_language_with_core_language.rb') +describe ReplaceCorrespondenceLanguageWithCoreLanguage do subject(:migration) { described_class.new.tap { |migration| migration.verbose = false } } - let(:kvs) do - { - be: Fabricate(Group::Kantonalverband.name, group: groups(:bund)), - ju: Fabricate(Group::Kantonalverband.name, group: groups(:bund)), - ti: Fabricate(Group::Kantonalverband.name, group: groups(:bund)) - } - end - let!(:kv_cantons) { kvs.map { |kv, group| KantonalverbandCanton.create!(kantonalverband: group, canton: kv) } } - let!(:people) do - { - ti_nil: Fabricate(Group::Kantonalverband::Coach.name, group: kvs[:ti], correspondence_language: nil), - ti_de: Fabricate(Group::Kantonalverband::Coach.name, group: kvs[:ti], correspondence_language: :de), - } - end - describe '#up' do - it '' do - migrate - people.values.each(&:reload) - expect(people[:ti_nil].language).to eq(:it) - expect(people[:ti_de].language).to eq(:de) + describe '#infer_person_language' do + let(:person) { Fabricate(:person) } + let(:language) { nil } + let(:correspondence_language) { nil } + subject(:infer_person_language) { migration.infer_person_language(person) } + + before do + allow(person).to receive(:language).and_return(language) + allow(person).to receive(:correspondence_language).and_return(correspondence_language) + end + + context 'with existing language' do + let(:language) { :test } + it { expect(infer_person_language).to eq(language) } + end + + context 'with existing correspondance_language' do + let(:correspondence_language) { :test } + it { expect(infer_person_language).to eq(:test) } + end + + context 'with language from KV' do + let(:person) { Fabricate(Group::Kantonalverband::Coach.name, group: kv).person } + let(:kv) do + Fabricate(Group::Kantonalverband.name, parent: groups(:bund)).tap do |kv| + kv.kantonalverband_cantons.create!(canton: canton) + end + end + + context 'with KV ti' do + let(:canton) { :ti } + it { expect(infer_person_language).to eq(:it) } + end + + context 'with KV ju' do + let(:canton) { :ju } + it { expect(infer_person_language).to eq(:fr) } + end + + context 'with KV be' do + let(:canton) { :be } + it { expect(infer_person_language).to eq(:de) } + end end end end