Skip to content

Commit

Permalink
Cop
Browse files Browse the repository at this point in the history
  • Loading branch information
pskl committed Feb 13, 2025
1 parent b7753da commit 249f985
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 34 deletions.
3 changes: 1 addition & 2 deletions app/models/wage.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
8 changes: 3 additions & 5 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 @@ -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|
Expand All @@ -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
Expand All @@ -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}"
Expand Down
61 changes: 34 additions & 27 deletions spec/db/wage_seeder_spec.rb
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -26,43 +30,46 @@
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

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
Expand All @@ -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
Expand Down

0 comments on commit 249f985

Please sign in to comment.