Skip to content

Commit

Permalink
spec: add specs for migration
Browse files Browse the repository at this point in the history
  • Loading branch information
diegosteiner committed Dec 15, 2023
1 parent 0134db6 commit aae7c5a
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 59 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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
81 changes: 45 additions & 36 deletions spec/migrations/replace_correspondence_language_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit aae7c5a

Please sign in to comment.