Skip to content

Commit

Permalink
Fix WageSeeder + test WageSeeder + minor fixes (#1385)
Browse files Browse the repository at this point in the history
  • Loading branch information
pskl authored Feb 13, 2025
1 parent e11c722 commit 6ea4b86
Show file tree
Hide file tree
Showing 12 changed files with 126 additions and 20 deletions.
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ GEM
puma (6.6.0)
nio4r (~> 2.0)
racc (1.8.1)
rack (3.1.9)
rack (3.1.10)
rack-mini-profiler (3.3.1)
rack (>= 1.2.0)
rack-oauth2 (2.2.1)
Expand Down
8 changes: 6 additions & 2 deletions app/controllers/validations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,13 @@ def show
infer_page_title(name: @classe.label)

@pfmps = current_establishment.validatable_pfmps
.includes(schooling: :attributive_decision_attachment)
.includes(
schooling: [
:attributive_decision_attachment,
{ student: :ribs }
]
)
.where(schoolings: { classe: @classe })
.includes(student: :ribs)
.order(:"students.last_name", :"pfmps.start_date")

@total_amount = @pfmps.sum(&:amount)
Expand Down
2 changes: 1 addition & 1 deletion app/models/pfmp.rb
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ def amounts_yearly_cap

pfmps = all_pfmps_for_mef
cap = mef.wage.yearly_cap
total = pfmps.sum(:amount)
total = pfmps.to_a.map { |pfmp| pfmp.amount || 0 }.sum
return unless total > cap

errors.add(:amount,
Expand Down
4 changes: 4 additions & 0 deletions app/models/wage.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,8 @@ class Wage < ApplicationRecord

validates :mefstat4, :ministry, :daily_rate, :yearly_cap, presence: true
validates :daily_rate, :yearly_cap, numericality: { only_integer: true, greater_than: 0 }

validates :mefstat4, uniqueness: {
scope: %i[ministry daily_rate yearly_cap school_year_id]
}
end
2 changes: 1 addition & 1 deletion config/initializers/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module Aplypro
VERSION = "2.2.1"
VERSION = "2.2.2"
end
1 change: 0 additions & 1 deletion data/mefs/2024_2025.csv
Original file line number Diff line number Diff line change
Expand Up @@ -1317,7 +1317,6 @@ MEF,DISPOSITIF_FORMATION,FORMATION_DIPLOME,DUREE_DISPOSITIF,ANNEE_DISPOSITIF,LIB
2533420111,253,1034201,1,1,MC 34201,MC ENTRET.COLLECT. PATRIMOINE (MC3),1/9/08,,3,O,23910034201,2391,4/26/23,mc entretien des collections du patrimoine (mc3),RS 2023 : modification du niveau du diplome,MENJ
2533440111,253,1034401,1,1,MC 34401,MC SURETE ESPAC.OUVERTS PUBLIC(MC3),1/9/01,8/31/24,3,O,23910034401,2391,4/26/23,Mc surete des espaces ouverts au public (mc3),RS 2023 : modification du niveau du diplome,MENJ
2533440211,253,1034402,1,1,MC 34402,MC SECU.CIVILE ET D'ENTREPRISE (MC3),1/9/02,8/31/24,3,O,23910034402,2391,4/26/23,Mc securite civile et d'entreprise (mc3),RS 2023 : modification du niveau du diplome,MENJ
7429990111,742,38199901,1,1,FCND3 99901,FORM COMP NON DIP HORS MLDS NIV 3,9/1/12,,3,O,44310099901,443100999,4/18/14,Formation complementaire non diplomante hors MLDS de niveau 3,,MENJ
7419990111,741,48199901,1,1,FCND4 99901,FORM COMP NON DIP HORS MLDS NIV 4,9/1/12,,3,O,44210099901,4421,4/18/14,Formation complementaire non diplomante hors MLDS de niveau 4,,MENJ
7409990111,740,58199901,1,1,FCND5 99901,FORM COMP NON DIP HORS MLDS NIV 5,9/1/12,,3,O,44110099901,4411,4/18/14,Formation complementaire non diplomante hors MLDS de niveau 5,,MENJ
2502231311,250,40122313,1,1,1BMA1 22313,1BMA1 ARMURERIE,1/9/95,,3,O,23B10022313,23B1,5/11/15,1bma1 armurerie,,MENJ
Expand Down
1 change: 0 additions & 1 deletion data/wages/2024_2025.csv
Original file line number Diff line number Diff line change
Expand Up @@ -1317,7 +1317,6 @@ MEF,MEF_STAT_11,MEF_STAT_4,DISPOSITIF_FORMATION,LIBELLE_LONG,BOP,FORFAIT JOURNAL
2533420111,23910034201,2391,253,MC ENTRET.COLLECT. PATRIMOINE (MC3),MENJ,20,1800
2533440111,23910034401,2391,253,MC SURETE ESPAC.OUVERTS PUBLIC(MC3),MENJ,20,1800
2533440211,23910034402,2391,253,MC SECU.CIVILE ET D'ENTREPRISE (MC3),MENJ,20,1800
7429990111,44110099901,4431,742,FORM COMP NON DIP HORS MLDS NIV 3,MENJ,0,0
7419990111,44210099901,4421,741,FORM COMP NON DIP HORS MLDS NIV 4,MENJ,20,1800
7409990111,44110099901,4411,740,FORM COMP NON DIP HORS MLDS NIV 5,MENJ,15,1350
2502231311,23B10022313,23B1,250,1BMA1 ARMURERIE,MENJ,20,800
Expand Down
7 changes: 7 additions & 0 deletions db/migrate/20250212163347_add_unique_constraint_to_wages.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class AddUniqueConstraintToWages < ActiveRecord::Migration[8.0]
def change
add_index :wages, [:mefstat4, :ministry, :daily_rate, :yearly_cap, :school_year_id],
unique: true,
name: 'index_wages_on_core_attributes'
end
end
3 changes: 2 additions & 1 deletion db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 7 additions & 6 deletions db/wage_seeder.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

# rubocop:disable all
require "csv"

class WageSeeder
Expand All @@ -10,20 +10,20 @@ class WageSeeder
ministry: "BOP"
}.freeze

def self.seed
def self.seed(file_paths = nil)
@@logger = ActiveSupport::TaggedLogging.new(Logger.new($stdout))

paths = file_paths || Rails.root.glob("data/wages/*.csv")

Wage.transaction do
Dir.glob(Rails.root.join("data/wages/*.csv")).each do |file_path|
paths.each do |file_path|
process_file(file_path)
end
end

@@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
Expand All @@ -46,7 +46,8 @@ def self.process_file(file_path)
end

Wage.upsert_all(
wages
wages,
unique_by: %i[mefstat4 ministry daily_rate yearly_cap school_year_id]
)

@@logger.info "[seeds] upserted wages for school year #{school_year.start_year}-#{school_year.start_year + 1}"
Expand Down
92 changes: 92 additions & 0 deletions spec/db/wage_seeder_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
# frozen_string_literal: true

# spec/lib/wage_seeder_spec.rb
require "rails_helper"
require_relative "../../db/wage_seeder"

# rubocop:disable RSpec/ExampleLength, RSpec/MultipleExpectations
RSpec.describe WageSeeder do
describe ".seed" do
let!(:school_year) { SchoolYear.create!(start_year: 2022) }
let(:csv_path) { ["2022_2023.csv"] }

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"]
),
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"]
)
]
end

before do
allow(CSV).to receive(:read).with(csv_path.first, headers: true).and_return(csv_data)
end

it "is idempotent" do
described_class.seed(csv_path)

wage = Wage.last
expect(wage).to have_attributes(
mefstat4: "2311",
ministry: "menj",
daily_rate: 15,
yearly_cap: 525,
mef_codes: contain_exactly("2402214411", "2402214511"),
school_year: school_year
)

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
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"]
)
]
end

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"]
),
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"]
)
]
end

before do
allow(CSV).to receive(:read).with(csv_path.first, headers: true).and_return(csv_data)
end

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 { described_class.seed(csv_path) }.not_to change(Wage, :count)
expect(Wage.last.mef_codes).to contain_exactly("2402214411", "2402413211")
end
end
end
end
# rubocop:enable RSpec/ExampleLength, RSpec/MultipleExpectations
11 changes: 5 additions & 6 deletions spec/models/mef_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,13 @@
end

context "when there are several wages with same mefstat4 & ministry" do
let!(:correct_wage) { create(:wage, mefstat4: mef.mefstat4, ministry: mef.ministry, mef_codes: [mef.code]) }

before do
create(:wage, mefstat4: mef.mefstat4, ministry: mef.ministry, mef_codes: %w[many codes])
end
before { create(:wage, mefstat4: mef.mefstat4, ministry: mef.ministry, mef_codes: [mef.code]) }

it "returns the correct wage" do
expect(mef.wage).to eq correct_wage
expect do
create(:wage, mefstat4: mef.mefstat4, ministry: mef.ministry,
mef_codes: %w[many codes])
end.to raise_error ActiveRecord::RecordInvalid
end
end
end
Expand Down

0 comments on commit 6ea4b86

Please sign in to comment.