Skip to content

Commit

Permalink
spec: test champs_id_row_index
Browse files Browse the repository at this point in the history
  • Loading branch information
LeSim committed May 23, 2024
1 parent 512b6b5 commit 4f2355d
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 5 deletions.
28 changes: 23 additions & 5 deletions app/services/pieces_justificatives_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -141,11 +141,7 @@ def pjs_for_champs(dossiers)
champs = champs.reject(&:private?)
end

champs_id_row_index = champs.filter { _1.row_id.present? }.group_by(&:dossier_id).values.each_with_object({}) do |champs_for_dossier, hash|
champs_for_dossier.group_by(&:stable_id).values.each do |champs_for_stable_id|
champs_for_stable_id.sort_by(&:row_id).each.with_index { |c, index| hash[c.id] = index }
end
end
champs_id_row_index = compute_champ_id_row_index(champs)

champs.flat_map do |champ|
champ.piece_justificative_file_attachments.filter { |a| safe_attachment(a) }.map.with_index do |attachment, index|
Expand Down Expand Up @@ -301,4 +297,26 @@ def safe_attachment(attachment)
.blob
.virus_scan_result == ActiveStorage::VirusScanner::SAFE
end

# given
# repet_0 (stable_id: r0)
# # row_0
# # # pj_champ_0 (stable_id: 0)
# # row_1
# # # pj_champ_1 (stable_id: 0)
# repet_1 (stable_id: r1)
# # row_0
# # # pj_champ_2 (stable_id: 1)
# # # pj_champ_3 (stable_id: 2)
# # row_1
# # # pj_champ_4 (stable_id: 1)
# # # pj_champ_5 (stable_id: 2)
# it returns { pj_0.id => 0, pj_1.id => 1, pj_2.id => 0, pj_3.id => 0, pj_4.id => 1, pj_5.id => 1 }
def compute_champ_id_row_index(champs)
champs.filter(&:child?).group_by(&:dossier_id).values.each_with_object({}) do |children_for_dossier, hash|
children_for_dossier.group_by(&:stable_id).values.each do |champs_for_stable_id|
champs_for_stable_id.sort_by(&:row_id).each.with_index { |c, index| hash[c.id] = index }
end
end
end
end
63 changes: 63 additions & 0 deletions spec/services/pieces_justificatives_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,69 @@ def attachments(champ) = champ.piece_justificative_file.attachments
end
end

describe '#compute_champ_id_row_index' do
let(:user_profile) { build(:administrateur) }
let(:types_de_champ_public) do
[
{ type: :repetition, children: [{ type: :piece_justificative }] },
{ type: :repetition, children: [{ type: :piece_justificative }, { type: :piece_justificative }] }
]
end

let(:procedure) { create(:procedure, types_de_champ_public:) }
let(:dossier_1) { create(:dossier, procedure:) }
let(:champs) { dossier_1.champs }

def pj_champ(d) = d.champs_public.find_by(type: 'Champs::PieceJustificativeChamp')
def repetition(d, index:) = d.champs_public.filter(&:repetition?)[index]

subject { PiecesJustificativesService.new(user_profile:, export_template: nil).send(:compute_champ_id_row_index, champs) }

before do
pj_champ(dossier_1)

# repet_0 (stable_id: r0)
# # row_0
# # # pj_champ_0 (stable_id: 0)
# # row_1
# # # pj_champ_1 (stable_id: 0)
# repet_1 (stable_id: r1)
# # row_0
# # # pj_champ_2 (stable_id: 1)
# # # pj_champ_3 (stable_id: 2)
# # row_1
# # # pj_champ_4 (stable_id: 1)
# # # pj_champ_5 (stable_id: 2)

repet_0 = repetition(dossier_1, index: 0)
repet_1 = repetition(dossier_1, index: 1)

repet_0.add_row(dossier_1.revision)
repet_0.add_row(dossier_1.revision)

repet_1.add_row(dossier_1.revision)
repet_1.add_row(dossier_1.revision)
end

it do
champs = dossier_1.champs_public
repet_0 = champs[0]
pj_0 = repet_0.rows.first.first
pj_1 = repet_0.rows.second.first

repet_1 = champs[1]
pj_2 = repet_1.rows.first.first
pj_3 = repet_1.rows.first.second

pj_4 = repet_1.rows.second.first
pj_5 = repet_1.rows.second.second

binding.irb

is_expected.to eq({ pj_0.id => 0, pj_1.id => 1, pj_2.id => 0, pj_3.id => 0, pj_4.id => 1, pj_5.id => 1 })
end
end

def attach_file_to_champ(champ, safe = true)
attach_file(champ.piece_justificative_file, safe)
end
Expand Down

0 comments on commit 4f2355d

Please sign in to comment.