diff --git a/app/models/wage.rb b/app/models/wage.rb index 6485e0ff3..2524aecc5 100644 --- a/app/models/wage.rb +++ b/app/models/wage.rb @@ -9,7 +9,6 @@ class Wage < ApplicationRecord validates :daily_rate, :yearly_cap, numericality: { only_integer: true, greater_than: 0 } validates :mefstat4, uniqueness: { - scope: [:ministry, :daily_rate, :yearly_cap, :school_year_id], - message: 'already exists for this ministry, rates and school year' + scope: %i[ministry daily_rate yearly_cap school_year_id] } end diff --git a/db/wage_seeder.rb b/db/wage_seeder.rb index 15c77d374..fcd94116e 100644 --- a/db/wage_seeder.rb +++ b/db/wage_seeder.rb @@ -1,5 +1,5 @@ # frozen_string_literal: true - +# rubocop:disable all require "csv" class WageSeeder @@ -13,7 +13,7 @@ class WageSeeder def self.seed(file_paths = nil) @@logger = ActiveSupport::TaggedLogging.new(Logger.new($stdout)) - paths = file_paths || Dir.glob(Rails.root.join("data/wages/*.csv")) + paths = file_paths || Rails.root.glob("data/wages/*.csv") Wage.transaction do paths.each do |file_path| @@ -24,8 +24,6 @@ def self.seed(file_paths = nil) @@logger.info "[seeds] upserted #{Wage.count} total wages" end - private - def self.process_file(file_path) file_name = File.basename(file_path, ".csv") start_year = file_name.split("_").first.to_i @@ -49,7 +47,7 @@ def self.process_file(file_path) Wage.upsert_all( wages, - unique_by: [:mefstat4, :ministry, :daily_rate, :yearly_cap, :school_year_id, :mef_codes] + unique_by: %i[mefstat4 ministry daily_rate yearly_cap school_year_id mef_codes] ) @@logger.info "[seeds] upserted wages for school year #{school_year.start_year}-#{school_year.start_year + 1}" diff --git a/spec/db/wage_seeder_spec.rb b/spec/db/wage_seeder_spec.rb index 10b497d53..c74cd3903 100644 --- a/spec/db/wage_seeder_spec.rb +++ b/spec/db/wage_seeder_spec.rb @@ -1,23 +1,27 @@ +# frozen_string_literal: true + # spec/lib/wage_seeder_spec.rb -require 'rails_helper' -require_relative '../../db/wage_seeder' +require "rails_helper" +require_relative "../../db/wage_seeder" # rubocop:disable RSpec/ExampleLength, RSpec/MultipleExpectations RSpec.describe WageSeeder do - describe '.seed' do + describe ".seed" do let(:school_year) { SchoolYear.find_by!(start_year: 2023) } - let(:csv_path) { ['dummy.csv'] } + let(:csv_path) { ["dummy.csv"] } - context 'when seeding wages' do + context "when seeding wages" do let(:csv_data) do [ CSV::Row.new( - ['MEF', 'MEF_STAT_11', 'MEF_STAT_4', 'DISPOSITIF_FORMATION', 'LIBELLE_LONG', 'BOP', 'FORFAIT JOURNALIER', 'PLAFOND MAX'], - ['2402214411', '23110022144', '2311', '240', '1CAP1 CHARCUTERIE-TRAITEUR', 'MENJ', '15', '525'] + ["MEF", "MEF_STAT_11", "MEF_STAT_4", "DISPOSITIF_FORMATION", "LIBELLE_LONG", "BOP", "FORFAIT JOURNALIER", + "PLAFOND MAX"], + ["2402214411", "23110022144", "2311", "240", "1CAP1 CHARCUTERIE-TRAITEUR", "MENJ", "15", "525"] ), CSV::Row.new( - ['MEF', 'MEF_STAT_11', 'MEF_STAT_4', 'DISPOSITIF_FORMATION', 'LIBELLE_LONG', 'BOP', 'FORFAIT JOURNALIER', 'PLAFOND MAX'], - ['2402214511', '23110022145', '2311', '240', '1CAP1 CHOCOLATERIE-CONFISERIE', 'MENJ', '15', '525'] + ["MEF", "MEF_STAT_11", "MEF_STAT_4", "DISPOSITIF_FORMATION", "LIBELLE_LONG", "BOP", "FORFAIT JOURNALIER", + "PLAFOND MAX"], + ["2402214511", "23110022145", "2311", "240", "1CAP1 CHOCOLATERIE-CONFISERIE", "MENJ", "15", "525"] ) ] end @@ -26,30 +30,31 @@ allow(CSV).to receive(:read).with(csv_path.first, headers: true).and_return(csv_data) end - it 'is idempotent' do - WageSeeder.seed(csv_path) + it "is idempotent" do + described_class.seed(csv_path) wage = Wage.last expect(wage).to have_attributes( - mefstat4: '2311', - ministry: 'menj', + mefstat4: "2311", + ministry: "menj", daily_rate: 15, yearly_cap: 525, - mef_codes: contain_exactly('2402214411', '2402214511'), + mef_codes: contain_exactly("2402214411", "2402214511"), school_year: school_year ) - expect { WageSeeder.seed(csv_path) }.not_to change(Wage, :count) + expect { described_class.seed(csv_path) }.not_to change(Wage, :count) expect(Wage.last.attributes).to eq(wage.attributes) end end - context 'when adding new data' do + context "when adding new data" do let(:csv_data) do [ CSV::Row.new( - ['MEF', 'MEF_STAT_11', 'MEF_STAT_4', 'DISPOSITIF_FORMATION', 'LIBELLE_LONG', 'BOP', 'FORFAIT JOURNALIER', 'PLAFOND MAX'], - ['2402214411', '23110022144', '2311', '240', '1CAP1 CHARCUTERIE-TRAITEUR', 'MENJ', '15', '525'] + ["MEF", "MEF_STAT_11", "MEF_STAT_4", "DISPOSITIF_FORMATION", "LIBELLE_LONG", "BOP", "FORFAIT JOURNALIER", + "PLAFOND MAX"], + ["2402214411", "23110022144", "2311", "240", "1CAP1 CHARCUTERIE-TRAITEUR", "MENJ", "15", "525"] ) ] end @@ -57,12 +62,14 @@ let(:updated_csv_data) do [ CSV::Row.new( - ['MEF', 'MEF_STAT_11', 'MEF_STAT_4', 'DISPOSITIF_FORMATION', 'LIBELLE_LONG', 'BOP', 'FORFAIT JOURNALIER', 'PLAFOND MAX'], - ['2402214411', '23110022144', '2311', '240', '1CAP1 CHARCUTERIE-TRAITEUR', 'MENJ', '15', '525'] + ["MEF", "MEF_STAT_11", "MEF_STAT_4", "DISPOSITIF_FORMATION", "LIBELLE_LONG", "BOP", "FORFAIT JOURNALIER", + "PLAFOND MAX"], + ["2402214411", "23110022144", "2311", "240", "1CAP1 CHARCUTERIE-TRAITEUR", "MENJ", "15", "525"] ), CSV::Row.new( - ['MEF', 'MEF_STAT_11', 'MEF_STAT_4', 'DISPOSITIF_FORMATION', 'LIBELLE_LONG', 'BOP', 'FORFAIT JOURNALIER', 'PLAFOND MAX'], - ['2402413211', '23110024132', '2311', '240', '1CAP1 FLEURISTE DE MODE', 'MENJ', '15', '525'] + ["MEF", "MEF_STAT_11", "MEF_STAT_4", "DISPOSITIF_FORMATION", "LIBELLE_LONG", "BOP", "FORFAIT JOURNALIER", + "PLAFOND MAX"], + ["2402413211", "23110024132", "2311", "240", "1CAP1 FLEURISTE DE MODE", "MENJ", "15", "525"] ) ] end @@ -71,13 +78,13 @@ allow(CSV).to receive(:read).with(csv_path.first, headers: true).and_return(csv_data) end - it 'handles updates correctly' do - WageSeeder.seed(csv_path) - expect(Wage.last.mef_codes).to contain_exactly('2402214411') + it "handles updates correctly" do + described_class.seed(csv_path) + expect(Wage.last.mef_codes).to contain_exactly("2402214411") allow(CSV).to receive(:read).with(csv_path.first, headers: true).and_return(updated_csv_data) - expect { WageSeeder.seed(csv_path) }.not_to change(Wage, :count) - expect(Wage.last.mef_codes).to contain_exactly('2402214411', '2402413211') + expect { described_class.seed(csv_path) }.not_to change(Wage, :count) + expect(Wage.last.mef_codes).to contain_exactly("2402214411", "2402413211") end end end