2024-04-29 00:17:15 +02:00
# frozen_string_literal: true
2017-10-02 17:03:38 +02:00
describe ProcedurePresentation do
2023-06-08 18:58:47 +02:00
include ActiveSupport :: Testing :: TimeHelpers
2024-07-22 14:58:16 +02:00
let ( :procedure ) { create ( :procedure , :published , types_de_champ_public : , types_de_champ_private : [ { } ] ) }
let ( :types_de_champ_public ) { [ { } ] }
2020-07-20 16:13:51 +02:00
let ( :instructeur ) { create ( :instructeur ) }
let ( :assign_to ) { create ( :assign_to , procedure : procedure , instructeur : instructeur ) }
2022-11-16 12:46:33 +01:00
let ( :first_type_de_champ ) { assign_to . procedure . active_revision . types_de_champ_public . first }
2020-10-30 15:01:13 +01:00
let ( :first_type_de_champ_id ) { first_type_de_champ . stable_id . to_s }
2020-06-08 14:33:58 +02:00
let ( :procedure_presentation ) {
2020-07-20 16:13:51 +02:00
create ( :procedure_presentation ,
2018-10-02 17:04:23 +02:00
assign_to : assign_to ,
2017-09-28 11:04:18 +02:00
displayed_fields : [
2024-07-19 11:16:40 +02:00
{ label : " test1 " , table : " user " , column : " email " } ,
2024-09-09 16:23:32 +02:00
{ label : " test2 " , table : " type_de_champ " , column : first_type_de_champ_id , virtual : false }
2018-01-15 19:17:01 +01:00
] ,
2024-07-19 11:16:40 +02:00
sort : { table : " user " , column : " email " , " order " = > " asc " } ,
2020-07-20 16:59:27 +02:00
filters : filters )
2017-09-28 11:04:18 +02:00
}
2020-06-08 14:33:58 +02:00
let ( :procedure_presentation_id ) { procedure_presentation . id }
let ( :filters ) { { " a-suivre " = > [ ] , " suivis " = > [ { " label " = > " label1 " , " table " = > " self " , " column " = > " created_at " } ] } }
2017-10-02 17:03:38 +02:00
describe " # displayed_fields " do
2024-09-09 16:23:32 +02:00
it { expect ( procedure_presentation . displayed_fields ) . to eq ( [ { " label " = > " test1 " , " table " = > " user " , " column " = > " email " } , { " label " = > " test2 " , " table " = > " type_de_champ " , " column " = > first_type_de_champ_id , " virtual " = > false } ] ) }
end
describe " # displayed_fields_for_headers " do
it { expect ( procedure_presentation . displayed_fields_for_headers . map ( & :label ) ) . to eq ( [ " Nº dossier " , " test1 " , " test2 " , " Statut " ] ) }
2017-10-02 17:03:38 +02:00
end
2017-09-27 15:16:07 +02:00
describe " # sort " do
2018-12-24 17:37:57 +01:00
it { expect ( procedure_presentation . sort ) . to eq ( { " table " = > " user " , " column " = > " email " , " order " = > " asc " } ) }
2017-09-27 15:16:07 +02:00
end
2017-09-28 11:04:18 +02:00
describe " # filters " do
2018-10-02 17:04:23 +02:00
it { expect ( procedure_presentation . filters ) . to eq ( { " a-suivre " = > [ ] , " suivis " = > [ { " label " = > " label1 " , " table " = > " self " , " column " = > " created_at " } ] } ) }
end
describe 'validation' do
it { expect ( build ( :procedure_presentation ) ) . to be_valid }
context 'of displayed fields' do
2024-07-19 11:16:40 +02:00
it { expect ( build ( :procedure_presentation , displayed_fields : [ { table : " user " , column : " reset_password_token " , " order " = > " asc " } ] ) ) . to be_invalid }
2018-10-02 17:04:23 +02:00
end
context 'of sort' do
2024-07-19 11:16:40 +02:00
it { expect ( build ( :procedure_presentation , sort : { table : " notifications " , column : " notifications " , " order " = > " asc " } ) ) . to be_valid }
it { expect ( build ( :procedure_presentation , sort : { table : " self " , column : " id " , " order " = > " asc " } ) ) . to be_valid }
it { expect ( build ( :procedure_presentation , sort : { table : " self " , column : " state " , " order " = > " asc " } ) ) . to be_valid }
it { expect ( build ( :procedure_presentation , sort : { table : " user " , column : " reset_password_token " , " order " = > " asc " } ) ) . to be_invalid }
2018-10-02 17:04:23 +02:00
end
context 'of filters' do
2024-07-19 11:16:40 +02:00
it { expect ( build ( :procedure_presentation , filters : { " suivis " = > [ { table : " user " , column : " reset_password_token " , " order " = > " asc " } ] } ) ) . to be_invalid }
it { expect ( build ( :procedure_presentation , filters : { " suivis " = > [ { table : " user " , column : " email " , " value " = > " exceedingly long filter value " * 10 } ] } ) ) . to be_invalid }
2018-10-02 17:04:23 +02:00
end
2017-09-28 11:04:18 +02:00
end
2018-10-03 14:46:12 +02:00
2018-10-05 10:08:39 +02:00
describe '#sorted_ids' do
2019-08-06 11:02:54 +02:00
let ( :instructeur ) { create ( :instructeur ) }
let ( :assign_to ) { create ( :assign_to , procedure : procedure , instructeur : instructeur ) }
2018-10-05 10:08:39 +02:00
let ( :sort ) { { 'table' = > table , 'column' = > column , 'order' = > order } }
2020-07-20 16:13:51 +02:00
let ( :procedure_presentation ) { create ( :procedure_presentation , assign_to : assign_to , sort : sort ) }
2018-10-05 10:08:39 +02:00
2024-07-19 18:13:26 +02:00
subject { procedure_presentation . send ( :sorted_ids , procedure . dossiers , procedure . dossiers . count ) }
2018-10-05 10:08:39 +02:00
context 'for notifications table' do
let ( :table ) { 'notifications' }
let ( :column ) { 'notifications' }
let! ( :notified_dossier ) { create ( :dossier , :en_construction , procedure : procedure ) }
let! ( :recent_dossier ) { create ( :dossier , :en_construction , procedure : procedure ) }
let! ( :older_dossier ) { create ( :dossier , :en_construction , procedure : procedure ) }
before do
2020-07-30 11:05:39 +02:00
notified_dossier . update! ( last_champ_updated_at : Time . zone . local ( 2018 , 9 , 20 ) )
2019-08-06 11:02:54 +02:00
create ( :follow , instructeur : instructeur , dossier : notified_dossier , demande_seen_at : Time . zone . local ( 2018 , 9 , 10 ) )
2018-10-25 15:19:29 +02:00
notified_dossier . touch ( time : Time . zone . local ( 2018 , 9 , 20 ) )
recent_dossier . touch ( time : Time . zone . local ( 2018 , 9 , 25 ) )
older_dossier . touch ( time : Time . zone . local ( 2018 , 5 , 13 ) )
2018-10-05 10:08:39 +02:00
end
context 'in ascending order' do
let ( :order ) { 'asc' }
it { is_expected . to eq ( [ older_dossier , recent_dossier , notified_dossier ] . map ( & :id ) ) }
end
context 'in descending order' do
let ( :order ) { 'desc' }
it { is_expected . to eq ( [ notified_dossier , recent_dossier , older_dossier ] . map ( & :id ) ) }
end
2018-10-23 18:48:05 +02:00
context 'with a dossier terminé' do
let! ( :notified_dossier ) { create ( :dossier , :accepte , procedure : procedure ) }
let ( :order ) { 'desc' }
it { is_expected . to eq ( [ notified_dossier , recent_dossier , older_dossier ] . map ( & :id ) ) }
end
2018-10-05 10:08:39 +02:00
end
context 'for self table' do
let ( :table ) { 'self' }
let ( :order ) { 'asc' } # Desc works the same, no extra test required
2018-10-17 12:10:23 +02:00
context 'for created_at column' do
let ( :column ) { 'created_at' }
2018-10-25 15:19:29 +02:00
let! ( :recent_dossier ) { Timecop . freeze ( Time . zone . local ( 2018 , 10 , 17 ) ) { create ( :dossier , procedure : procedure ) } }
let! ( :older_dossier ) { Timecop . freeze ( Time . zone . local ( 2003 , 11 , 11 ) ) { create ( :dossier , procedure : procedure ) } }
2018-10-05 10:08:39 +02:00
2018-10-17 12:10:23 +02:00
it { is_expected . to eq ( [ older_dossier , recent_dossier ] . map ( & :id ) ) }
2018-10-05 10:08:39 +02:00
end
2018-10-17 12:10:23 +02:00
context 'for en_construction_at column' do
let ( :column ) { 'en_construction_at' }
2018-10-25 15:19:29 +02:00
let! ( :recent_dossier ) { create ( :dossier , :en_construction , procedure : procedure , en_construction_at : Time . zone . local ( 2018 , 10 , 17 ) ) }
let! ( :older_dossier ) { create ( :dossier , :en_construction , procedure : procedure , en_construction_at : Time . zone . local ( 2013 , 1 , 1 ) ) }
2018-10-17 12:10:23 +02:00
it { is_expected . to eq ( [ older_dossier , recent_dossier ] . map ( & :id ) ) }
end
context 'for updated_at column' do
let ( :column ) { 'updated_at' }
let ( :recent_dossier ) { create ( :dossier , procedure : procedure ) }
let ( :older_dossier ) { create ( :dossier , procedure : procedure ) }
before do
2018-10-25 15:19:29 +02:00
recent_dossier . touch ( time : Time . zone . local ( 2018 , 9 , 25 ) )
older_dossier . touch ( time : Time . zone . local ( 2018 , 5 , 13 ) )
2018-10-17 12:10:23 +02:00
end
it { is_expected . to eq ( [ older_dossier , recent_dossier ] . map ( & :id ) ) }
end
2018-10-05 10:08:39 +02:00
end
context 'for type_de_champ table' do
2021-07-22 18:16:04 +02:00
context 'with no revisions' do
let ( :table ) { 'type_de_champ' }
2022-11-16 12:46:33 +01:00
let ( :column ) { procedure . active_revision . types_de_champ_public . first . stable_id . to_s }
2018-10-05 10:08:39 +02:00
2021-07-22 18:16:04 +02:00
let ( :beurre_dossier ) { create ( :dossier , procedure : procedure ) }
let ( :tartine_dossier ) { create ( :dossier , procedure : procedure ) }
2018-10-05 10:08:39 +02:00
2021-07-22 18:16:04 +02:00
before do
2022-11-10 22:21:14 +01:00
beurre_dossier . champs_public . first . update ( value : 'beurre' )
tartine_dossier . champs_public . first . update ( value : 'tartine' )
2021-07-22 18:16:04 +02:00
end
context 'asc' do
let ( :order ) { 'asc' }
it { is_expected . to eq ( [ beurre_dossier , tartine_dossier ] . map ( & :id ) ) }
end
context 'desc' do
let ( :order ) { 'desc' }
it { is_expected . to eq ( [ tartine_dossier , beurre_dossier ] . map ( & :id ) ) }
end
2018-10-05 10:08:39 +02:00
end
2021-07-22 18:16:04 +02:00
context 'with a revision adding a new type_de_champ' do
let! ( :tdc ) { { type_champ : :text , libelle : 'nouveau champ' } }
let ( :table ) { 'type_de_champ' }
2022-11-16 12:46:33 +01:00
let ( :column ) { procedure . active_revision . types_de_champ_public . last . stable_id . to_s }
2021-07-22 18:16:04 +02:00
let ( :nothing_dossier ) { create ( :dossier , procedure : procedure ) }
let ( :beurre_dossier ) { create ( :dossier , procedure : procedure ) }
let ( :tartine_dossier ) { create ( :dossier , procedure : procedure ) }
before do
nothing_dossier
procedure . draft_revision . add_type_de_champ ( tdc )
procedure . publish_revision!
2022-11-10 22:21:14 +01:00
beurre_dossier . champs_public . last . update ( value : 'beurre' )
tartine_dossier . champs_public . last . update ( value : 'tartine' )
2021-07-22 18:16:04 +02:00
end
context 'asc' do
let ( :order ) { 'asc' }
it { is_expected . to eq ( [ beurre_dossier , tartine_dossier , nothing_dossier ] . map ( & :id ) ) }
end
context 'desc' do
let ( :order ) { 'desc' }
it { is_expected . to eq ( [ nothing_dossier , tartine_dossier , beurre_dossier ] . map ( & :id ) ) }
end
end
2018-10-05 10:08:39 +02:00
end
context 'for type_de_champ_private table' do
2021-07-22 18:16:04 +02:00
context 'with no revisions' do
2024-07-19 16:53:22 +02:00
let ( :table ) { 'type_de_champ' }
2022-11-16 12:46:33 +01:00
let ( :column ) { procedure . active_revision . types_de_champ_private . first . stable_id . to_s }
2018-10-05 10:08:39 +02:00
2021-07-22 18:16:04 +02:00
let ( :biere_dossier ) { create ( :dossier , procedure : procedure ) }
let ( :vin_dossier ) { create ( :dossier , procedure : procedure ) }
2018-10-05 10:08:39 +02:00
2021-07-22 18:16:04 +02:00
before do
biere_dossier . champs_private . first . update ( value : 'biere' )
vin_dossier . champs_private . first . update ( value : 'vin' )
end
context 'asc' do
let ( :order ) { 'asc' }
it { is_expected . to eq ( [ biere_dossier , vin_dossier ] . map ( & :id ) ) }
end
context 'desc' do
let ( :order ) { 'desc' }
it { is_expected . to eq ( [ vin_dossier , biere_dossier ] . map ( & :id ) ) }
end
2018-10-05 10:08:39 +02:00
end
2021-07-22 18:16:04 +02:00
context 'with a revision adding a new type_de_champ' do
let! ( :tdc ) { { type_champ : :text , private : true , libelle : 'nouveau champ' } }
2024-07-19 16:53:22 +02:00
let ( :table ) { 'type_de_champ' }
2022-11-16 12:46:33 +01:00
let ( :column ) { procedure . active_revision . types_de_champ_private . last . stable_id . to_s }
2021-07-22 18:16:04 +02:00
let ( :nothing_dossier ) { create ( :dossier , procedure : procedure ) }
let ( :biere_dossier ) { create ( :dossier , procedure : procedure ) }
let ( :vin_dossier ) { create ( :dossier , procedure : procedure ) }
before do
nothing_dossier
procedure . draft_revision . add_type_de_champ ( tdc )
procedure . publish_revision!
biere_dossier . champs_private . last . update ( value : 'biere' )
vin_dossier . champs_private . last . update ( value : 'vin' )
end
context 'asc' do
let ( :order ) { 'asc' }
it { is_expected . to eq ( [ biere_dossier , vin_dossier , nothing_dossier ] . map ( & :id ) ) }
end
context 'desc' do
let ( :order ) { 'desc' }
it { is_expected . to eq ( [ nothing_dossier , vin_dossier , biere_dossier ] . map ( & :id ) ) }
end
end
2018-10-05 10:08:39 +02:00
end
2018-10-11 11:28:51 +02:00
context 'for individual table' do
let ( :table ) { 'individual' }
let ( :order ) { 'asc' } # Desc works the same, no extra test required
let ( :procedure ) { create ( :procedure , :for_individual ) }
2021-11-19 13:48:52 +01:00
let! ( :first_dossier ) { create ( :dossier , procedure : procedure , individual : build ( :individual , gender : 'M' , prenom : 'Alain' , nom : 'Antonelli' ) ) }
let! ( :last_dossier ) { create ( :dossier , procedure : procedure , individual : build ( :individual , gender : 'Mme' , prenom : 'Zora' , nom : 'Zemmour' ) ) }
2018-10-11 11:28:51 +02:00
context 'for gender column' do
let ( :column ) { 'gender' }
it { is_expected . to eq ( [ first_dossier , last_dossier ] . map ( & :id ) ) }
end
context 'for prenom column' do
let ( :column ) { 'prenom' }
it { is_expected . to eq ( [ first_dossier , last_dossier ] . map ( & :id ) ) }
end
context 'for nom column' do
let ( :column ) { 'nom' }
it { is_expected . to eq ( [ first_dossier , last_dossier ] . map ( & :id ) ) }
end
end
2019-10-16 13:02:42 +02:00
context 'for followers_instructeurs table' do
let ( :table ) { 'followers_instructeurs' }
let ( :order ) { 'asc' } # Desc works the same, no extra test required
let! ( :dossier_z ) { create ( :dossier , :en_construction , procedure : procedure ) }
2022-03-16 18:23:08 +01:00
let! ( :dossier_a ) { create ( :dossier , :en_construction , procedure : procedure ) }
2019-10-16 13:02:42 +02:00
let! ( :dossier_without_instructeur ) { create ( :dossier , :en_construction , procedure : procedure ) }
before do
2022-03-16 18:23:08 +01:00
create ( :follow , dossier : dossier_z , instructeur : create ( :instructeur , email : 'zythum@exemple.fr' ) )
2019-10-16 13:02:42 +02:00
create ( :follow , dossier : dossier_a , instructeur : create ( :instructeur , email : 'abaca@exemple.fr' ) )
2021-05-04 15:37:29 +02:00
create ( :follow , dossier : dossier_a , instructeur : create ( :instructeur , email : 'abaca2@exemple.fr' ) )
2019-10-16 13:02:42 +02:00
end
context 'for email column' do
let ( :column ) { 'email' }
it { is_expected . to eq ( [ dossier_a , dossier_z , dossier_without_instructeur ] . map ( & :id ) ) }
end
end
2023-06-02 17:05:19 +02:00
context 'for avis table' do
let ( :table ) { 'avis' }
2023-08-29 17:52:14 +02:00
let ( :column ) { 'question_answer' }
2023-06-02 17:05:19 +02:00
let ( :order ) { 'asc' }
let! ( :dossier_yes ) { create ( :dossier , procedure : ) }
let! ( :dossier_no ) { create ( :dossier , procedure : ) }
before do
create_list ( :avis , 2 , dossier : dossier_yes , question_answer : true )
create ( :avis , dossier : dossier_no , question_answer : true )
create ( :avis , dossier : dossier_no , question_answer : false )
end
2023-08-29 17:52:14 +02:00
it { is_expected . to eq ( [ dossier_no , dossier_yes ] . map ( & :id ) ) }
2023-06-02 17:05:19 +02:00
end
2018-10-05 10:08:39 +02:00
context 'for other tables' do
# All other columns and tables work the same so it’ s ok to test only one
let ( :table ) { 'etablissement' }
let ( :column ) { 'code_postal' }
let ( :order ) { 'asc' } # Desc works the same, no extra test required
let! ( :huitieme_dossier ) { create ( :dossier , procedure : procedure , etablissement : create ( :etablissement , code_postal : '75008' ) ) }
let! ( :vingtieme_dossier ) { create ( :dossier , procedure : procedure , etablissement : create ( :etablissement , code_postal : '75020' ) ) }
it { is_expected . to eq ( [ huitieme_dossier , vingtieme_dossier ] . map ( & :id ) ) }
end
end
2018-10-03 19:17:22 +02:00
describe '#filtered_ids' do
2020-07-20 16:13:51 +02:00
let ( :procedure_presentation ) { create ( :procedure_presentation , assign_to : assign_to , filters : { " suivis " = > filter } ) }
2018-10-03 19:17:22 +02:00
2024-07-19 18:13:26 +02:00
subject { procedure_presentation . send ( :filtered_ids , procedure . dossiers . joins ( :user ) , 'suivis' ) }
2018-10-03 19:17:22 +02:00
2018-10-10 18:18:28 +02:00
context 'for self table' do
context 'for created_at column' do
2019-02-19 13:10:24 +01:00
let ( :filter ) { [ { 'table' = > 'self' , 'column' = > 'created_at' , 'value' = > '18/9/2018' } ] }
2018-10-25 15:19:29 +02:00
let! ( :kept_dossier ) { create ( :dossier , procedure : procedure , created_at : Time . zone . local ( 2018 , 9 , 18 , 14 , 28 ) ) }
let! ( :discarded_dossier ) { create ( :dossier , procedure : procedure , created_at : Time . zone . local ( 2018 , 9 , 17 , 23 , 59 ) ) }
2018-10-10 18:18:28 +02:00
it { is_expected . to contain_exactly ( kept_dossier . id ) }
end
2018-10-17 12:16:04 +02:00
context 'for en_construction_at column' do
2019-02-19 13:10:24 +01:00
let ( :filter ) { [ { 'table' = > 'self' , 'column' = > 'en_construction_at' , 'value' = > '17/10/2018' } ] }
2018-10-25 15:19:29 +02:00
let! ( :kept_dossier ) { create ( :dossier , :en_construction , procedure : procedure , en_construction_at : Time . zone . local ( 2018 , 10 , 17 ) ) }
let! ( :discarded_dossier ) { create ( :dossier , :en_construction , procedure : procedure , en_construction_at : Time . zone . local ( 2013 , 1 , 1 ) ) }
2018-10-17 12:16:04 +02:00
it { is_expected . to contain_exactly ( kept_dossier . id ) }
end
2018-10-10 18:18:28 +02:00
context 'for updated_at column' do
2019-02-19 13:10:24 +01:00
let ( :filter ) { [ { 'table' = > 'self' , 'column' = > 'updated_at' , 'value' = > '18/9/2018' } ] }
2018-10-10 18:18:28 +02:00
let ( :kept_dossier ) { create ( :dossier , procedure : procedure ) }
let ( :discarded_dossier ) { create ( :dossier , procedure : procedure ) }
before do
2018-10-25 15:19:29 +02:00
kept_dossier . touch ( time : Time . zone . local ( 2018 , 9 , 18 , 14 , 28 ) )
discarded_dossier . touch ( time : Time . zone . local ( 2018 , 9 , 17 , 23 , 59 ) )
2018-10-10 18:18:28 +02:00
end
it { is_expected . to contain_exactly ( kept_dossier . id ) }
end
2018-10-26 11:02:14 +02:00
2022-06-09 14:01:22 +02:00
context 'for updated_since column' do
let ( :filter ) { [ { 'table' = > 'self' , 'column' = > 'updated_since' , 'value' = > '18/9/2018' } ] }
let ( :kept_dossier ) { create ( :dossier , procedure : procedure ) }
let ( :later_dossier ) { create ( :dossier , procedure : procedure ) }
let ( :discarded_dossier ) { create ( :dossier , procedure : procedure ) }
before do
kept_dossier . touch ( time : Time . zone . local ( 2018 , 9 , 18 , 14 , 28 ) )
later_dossier . touch ( time : Time . zone . local ( 2018 , 9 , 19 , 14 , 28 ) )
discarded_dossier . touch ( time : Time . zone . local ( 2018 , 9 , 17 , 14 , 28 ) )
end
it { is_expected . to match_array ( [ kept_dossier . id , later_dossier . id ] ) }
end
2023-06-08 18:58:47 +02:00
context 'for sva_svr_decision_before column' do
before do
travel_to Time . zone . local ( 2023 , 6 , 10 , 10 )
end
let ( :procedure ) { create ( :procedure , :published , :sva , types_de_champ_public : [ { } ] , types_de_champ_private : [ { } ] ) }
let ( :filter ) { [ { 'table' = > 'self' , 'column' = > 'sva_svr_decision_before' , 'value' = > '15/06/2023' } ] }
let! ( :kept_dossier ) { create ( :dossier , :en_instruction , procedure : , sva_svr_decision_on : Date . current ) }
let! ( :later_dossier ) { create ( :dossier , :en_instruction , procedure : , sva_svr_decision_on : Date . current + 2 . days ) }
let! ( :discarded_dossier ) { create ( :dossier , :en_instruction , procedure : , sva_svr_decision_on : Date . current + 10 . days ) }
let! ( :en_construction_dossier ) { create ( :dossier , :en_construction , procedure : , sva_svr_decision_on : Date . current + 2 . days ) }
let! ( :accepte_dossier ) { create ( :dossier , :accepte , procedure : , sva_svr_decision_on : Date . current + 2 . days ) }
it { is_expected . to match_array ( [ kept_dossier . id , later_dossier . id , en_construction_dossier . id ] ) }
end
2022-06-09 14:01:22 +02:00
2018-10-26 15:10:42 +02:00
context 'ignore time of day' do
2019-02-19 13:10:24 +01:00
let ( :filter ) { [ { 'table' = > 'self' , 'column' = > 'en_construction_at' , 'value' = > '17/10/2018 19:30' } ] }
2018-10-26 15:10:42 +02:00
let! ( :kept_dossier ) { create ( :dossier , :en_construction , procedure : procedure , en_construction_at : Time . zone . local ( 2018 , 10 , 17 , 15 , 56 ) ) }
let! ( :discarded_dossier ) { create ( :dossier , :en_construction , procedure : procedure , en_construction_at : Time . zone . local ( 2018 , 10 , 18 , 5 , 42 ) ) }
it { is_expected . to contain_exactly ( kept_dossier . id ) }
end
2018-10-26 11:02:14 +02:00
context 'for a malformed date' do
2018-10-26 14:23:11 +02:00
context 'when its a string' do
let ( :filter ) { [ { 'table' = > 'self' , 'column' = > 'updated_at' , 'value' = > 'malformed date' } ] }
2019-02-19 13:10:24 +01:00
2018-10-26 14:23:11 +02:00
it { is_expected . to match ( [ ] ) }
end
context 'when its a number' do
let ( :filter ) { [ { 'table' = > 'self' , 'column' = > 'updated_at' , 'value' = > '177500' } ] }
it { is_expected . to match ( [ ] ) }
end
2018-10-26 11:02:14 +02:00
end
2019-02-19 13:11:00 +01:00
context 'with multiple search values' do
let ( :filter ) do
[
{ 'table' = > 'self' , 'column' = > 'en_construction_at' , 'value' = > '17/10/2018' } ,
{ 'table' = > 'self' , 'column' = > 'en_construction_at' , 'value' = > '19/10/2018' }
]
end
let! ( :kept_dossier ) { create ( :dossier , :en_construction , procedure : procedure , en_construction_at : Time . zone . local ( 2018 , 10 , 17 ) ) }
let! ( :other_kept_dossier ) { create ( :dossier , :en_construction , procedure : procedure , en_construction_at : Time . zone . local ( 2018 , 10 , 19 ) ) }
let! ( :discarded_dossier ) { create ( :dossier , :en_construction , procedure : procedure , en_construction_at : Time . zone . local ( 2013 , 1 , 1 ) ) }
2022-10-03 15:18:46 +02:00
it 'returns every dossier that matches any of the search criteria for a given column' do
is_expected . to contain_exactly ( kept_dossier . id , other_kept_dossier . id )
end
end
context 'with multiple state filters' do
let ( :filter ) do
[
{ 'table' = > 'self' , 'column' = > 'state' , 'value' = > 'en_construction' } ,
{ 'table' = > 'self' , 'column' = > 'state' , 'value' = > 'en_instruction' }
]
end
let! ( :kept_dossier ) { create ( :dossier , :en_construction , procedure : procedure ) }
let! ( :other_kept_dossier ) { create ( :dossier , :en_instruction , procedure : procedure ) }
let! ( :discarded_dossier ) { create ( :dossier , :accepte , procedure : procedure ) }
2019-02-19 13:11:00 +01:00
it 'returns every dossier that matches any of the search criteria for a given column' do
is_expected . to contain_exactly ( kept_dossier . id , other_kept_dossier . id )
end
end
2023-09-26 16:36:39 +02:00
context 'with en_construction state filters' do
let ( :filter ) do
[
{ 'table' = > 'self' , 'column' = > 'state' , 'value' = > 'en_construction' }
]
end
let! ( :en_construction ) { create ( :dossier , :en_construction , procedure : procedure ) }
let! ( :en_construction_with_correction ) { create ( :dossier , :en_construction , procedure : procedure ) }
let! ( :correction ) { create ( :dossier_correction , dossier : en_construction_with_correction ) }
it 'excludes dossier en construction with pending correction' do
is_expected . to contain_exactly ( en_construction . id )
end
end
2018-10-10 18:18:28 +02:00
end
2018-10-03 19:17:22 +02:00
context 'for type_de_champ table' do
2020-10-30 15:01:13 +01:00
let ( :filter ) { [ { 'table' = > 'type_de_champ' , 'column' = > type_de_champ . stable_id . to_s , 'value' = > 'keep' } ] }
2019-02-19 13:10:24 +01:00
2018-10-03 19:17:22 +02:00
let ( :kept_dossier ) { create ( :dossier , procedure : procedure ) }
let ( :discarded_dossier ) { create ( :dossier , procedure : procedure ) }
2022-11-16 12:46:33 +01:00
let ( :type_de_champ ) { procedure . active_revision . types_de_champ_public . first }
2018-10-03 19:17:22 +02:00
2020-06-08 14:33:58 +02:00
context 'with single value' do
before do
2024-07-01 15:31:32 +02:00
kept_dossier . champs . find_by ( stable_id : type_de_champ . stable_id ) . update ( value : 'keep me' )
discarded_dossier . champs . find_by ( stable_id : type_de_champ . stable_id ) . update ( value : 'discard me' )
2020-06-08 14:33:58 +02:00
end
2018-10-03 19:17:22 +02:00
2020-06-08 14:33:58 +02:00
it { is_expected . to contain_exactly ( kept_dossier . id ) }
end
2019-02-19 13:11:00 +01:00
context 'with multiple search values' do
let ( :filter ) do
[
2020-10-30 15:01:13 +01:00
{ 'table' = > 'type_de_champ' , 'column' = > type_de_champ . stable_id . to_s , 'value' = > 'keep' } ,
{ 'table' = > 'type_de_champ' , 'column' = > type_de_champ . stable_id . to_s , 'value' = > 'and' }
2019-02-19 13:11:00 +01:00
]
end
let ( :other_kept_dossier ) { create ( :dossier , procedure : procedure ) }
before do
2024-07-01 15:31:32 +02:00
kept_dossier . champs . find_by ( stable_id : type_de_champ . stable_id ) . update ( value : 'keep me' )
discarded_dossier . champs . find_by ( stable_id : type_de_champ . stable_id ) . update ( value : 'discard me' )
other_kept_dossier . champs . find_by ( stable_id : type_de_champ . stable_id ) . update ( value : 'and me too' )
2019-02-19 13:11:00 +01:00
end
it 'returns every dossier that matches any of the search criteria for a given column' do
is_expected . to contain_exactly ( kept_dossier . id , other_kept_dossier . id )
end
end
2020-06-08 14:33:58 +02:00
context 'with yes_no type_de_champ' do
2020-10-30 15:01:13 +01:00
let ( :filter ) { [ { 'table' = > 'type_de_champ' , 'column' = > type_de_champ . stable_id . to_s , 'value' = > 'true' } ] }
2023-06-20 16:11:18 +02:00
let ( :types_de_champ_public ) { [ { type : :yes_no } ] }
2020-06-08 14:33:58 +02:00
before do
2024-07-01 15:31:32 +02:00
kept_dossier . champs . find_by ( stable_id : type_de_champ . stable_id ) . update ( value : 'true' )
discarded_dossier . champs . find_by ( stable_id : type_de_champ . stable_id ) . update ( value : 'false' )
2020-06-08 14:33:58 +02:00
end
it { is_expected . to contain_exactly ( kept_dossier . id ) }
end
2023-06-20 16:11:18 +02:00
context 'with departement type_de_champ' do
let ( :filter ) { [ { 'table' = > 'type_de_champ' , 'column' = > type_de_champ . stable_id . to_s , 'value_column' = > :external_id , 'value' = > '13' } ] }
let ( :types_de_champ_public ) { [ { type : :departements } ] }
before do
2024-07-01 15:31:32 +02:00
kept_dossier . champs . find_by ( stable_id : type_de_champ . stable_id ) . update ( external_id : '13' )
discarded_dossier . champs . find_by ( stable_id : type_de_champ . stable_id ) . update ( external_id : '69' )
2023-06-20 16:11:18 +02:00
end
it { is_expected . to contain_exactly ( kept_dossier . id ) }
end
2018-10-03 19:17:22 +02:00
end
context 'for type_de_champ_private table' do
2024-07-19 16:53:22 +02:00
let ( :filter ) { [ { 'table' = > 'type_de_champ' , 'column' = > type_de_champ_private . stable_id . to_s , 'value' = > 'keep' } ] }
2019-02-19 13:10:24 +01:00
2018-10-03 19:17:22 +02:00
let ( :kept_dossier ) { create ( :dossier , procedure : procedure ) }
let ( :discarded_dossier ) { create ( :dossier , procedure : procedure ) }
2022-11-16 12:46:33 +01:00
let ( :type_de_champ_private ) { procedure . active_revision . types_de_champ_private . first }
2018-10-03 19:17:22 +02:00
before do
2024-07-01 15:31:32 +02:00
kept_dossier . champs . find_by ( stable_id : type_de_champ_private . stable_id ) . update ( value : 'keep me' )
discarded_dossier . champs . find_by ( stable_id : type_de_champ_private . stable_id ) . update ( value : 'discard me' )
2018-10-03 19:17:22 +02:00
end
it { is_expected . to contain_exactly ( kept_dossier . id ) }
2019-02-19 13:11:00 +01:00
context 'with multiple search values' do
let ( :filter ) do
[
2024-07-19 16:53:22 +02:00
{ 'table' = > 'type_de_champ' , 'column' = > type_de_champ_private . stable_id . to_s , 'value' = > 'keep' } ,
{ 'table' = > 'type_de_champ' , 'column' = > type_de_champ_private . stable_id . to_s , 'value' = > 'and' }
2019-02-19 13:11:00 +01:00
]
end
let ( :other_kept_dossier ) { create ( :dossier , procedure : procedure ) }
before do
2024-07-01 15:31:32 +02:00
other_kept_dossier . champs . find_by ( stable_id : type_de_champ_private . stable_id ) . update ( value : 'and me too' )
2019-02-19 13:11:00 +01:00
end
it 'returns every dossier that matches any of the search criteria for a given column' do
is_expected . to contain_exactly ( kept_dossier . id , other_kept_dossier . id )
end
end
2018-10-03 19:17:22 +02:00
end
2024-07-22 14:58:16 +02:00
context 'for type_de_champ using AddressableColumnConcern' do
let ( :types_de_champ_public ) { [ { type : :rna , stable_id : 1 } ] }
let ( :type_de_champ ) { procedure . active_revision . types_de_champ . first }
let ( :available_columns ) { type_de_champ . dynamic_type . columns ( table : 'type_de_champ' ) }
let ( :column ) { available_columns . find { _1 . value_column == value_column_searched } }
let ( :filter ) { [ column . to_json . merge ( { " value " = > value } ) ] }
let ( :kept_dossier ) { create ( :dossier , procedure : procedure ) }
context " when searching by postal_code (text) " do
let ( :value ) { " 60580 " }
let ( :value_column_searched ) { [ 'postal_code' ] }
before do
kept_dossier . champs_public . find_by ( stable_id : 1 ) . update ( value_json : { " postal_code " = > value } )
create ( :dossier , procedure : procedure ) . champs_public . find_by ( stable_id : 1 ) . update ( value_json : { " postal_code " = > " unknown " } )
end
it { is_expected . to contain_exactly ( kept_dossier . id ) }
it 'describes column' do
expect ( column . type ) . to eq ( :text )
expect ( column . options_for_select ) . to eq ( [ ] )
end
end
context " when searching by departement_code (enum) " do
let ( :value ) { " 99 " }
let ( :value_column_searched ) { [ 'departement_code' ] }
before do
kept_dossier . champs_public . find_by ( stable_id : 1 ) . update ( value_json : { " departement_code " = > value } )
create ( :dossier , procedure : procedure ) . champs_public . find_by ( stable_id : 1 ) . update ( value_json : { " departement_code " = > " unknown " } )
end
it { is_expected . to contain_exactly ( kept_dossier . id ) }
it 'describes column' do
expect ( column . type ) . to eq ( :enum )
expect ( column . options_for_select . first ) . to eq ( [ " 99 – Etranger " , " 99 " ] )
end
end
context " when searching by region_name " do
let ( :value ) { " 60 " }
let ( :value_column_searched ) { [ 'region_name' ] }
before do
kept_dossier . champs_public . find_by ( stable_id : 1 ) . update ( value_json : { " region_name " = > value } )
create ( :dossier , procedure : procedure ) . champs_public . find_by ( stable_id : 1 ) . update ( value_json : { " region_name " = > " unknown " } )
end
it { is_expected . to contain_exactly ( kept_dossier . id ) }
it 'describes column' do
expect ( column . type ) . to eq ( :enum )
expect ( column . options_for_select . first ) . to eq ( [ " Auvergne-Rhône-Alpes " , " Auvergne-Rhône-Alpes " ] )
end
end
end
2018-10-03 19:17:22 +02:00
context 'for etablissement table' do
context 'for entreprise_date_creation column' do
2019-02-19 13:10:24 +01:00
let ( :filter ) { [ { 'table' = > 'etablissement' , 'column' = > 'entreprise_date_creation' , 'value' = > '21/6/2018' } ] }
2018-10-25 15:19:29 +02:00
let! ( :kept_dossier ) { create ( :dossier , procedure : procedure , etablissement : create ( :etablissement , entreprise_date_creation : Time . zone . local ( 2018 , 6 , 21 ) ) ) }
let! ( :discarded_dossier ) { create ( :dossier , procedure : procedure , etablissement : create ( :etablissement , entreprise_date_creation : Time . zone . local ( 2008 , 6 , 21 ) ) ) }
2018-10-03 19:17:22 +02:00
it { is_expected . to contain_exactly ( kept_dossier . id ) }
2019-02-19 13:11:00 +01:00
context 'with multiple search values' do
let ( :filter ) do
[
{ 'table' = > 'etablissement' , 'column' = > 'entreprise_date_creation' , 'value' = > '21/6/2016' } ,
{ 'table' = > 'etablissement' , 'column' = > 'entreprise_date_creation' , 'value' = > '21/6/2018' }
]
end
let! ( :other_kept_dossier ) { create ( :dossier , procedure : procedure , etablissement : create ( :etablissement , entreprise_date_creation : Time . zone . local ( 2016 , 6 , 21 ) ) ) }
it 'returns every dossier that matches any of the search criteria for a given column' do
is_expected . to contain_exactly ( kept_dossier . id , other_kept_dossier . id )
end
end
2018-10-03 19:17:22 +02:00
end
context 'for code_postal column' do
# All columns except entreprise_date_creation work exacly the same, just testing one
2019-02-19 13:10:24 +01:00
let ( :filter ) { [ { 'table' = > 'etablissement' , 'column' = > 'code_postal' , 'value' = > '75017' } ] }
2018-10-03 19:17:22 +02:00
let! ( :kept_dossier ) { create ( :dossier , procedure : procedure , etablissement : create ( :etablissement , code_postal : '75017' ) ) }
let! ( :discarded_dossier ) { create ( :dossier , procedure : procedure , etablissement : create ( :etablissement , code_postal : '25000' ) ) }
it { is_expected . to contain_exactly ( kept_dossier . id ) }
2019-02-19 13:11:00 +01:00
context 'with multiple search values' do
let ( :filter ) do
[
{ 'table' = > 'etablissement' , 'column' = > 'code_postal' , 'value' = > '75017' } ,
{ 'table' = > 'etablissement' , 'column' = > 'code_postal' , 'value' = > '88100' }
]
end
let! ( :other_kept_dossier ) { create ( :dossier , procedure : procedure , etablissement : create ( :etablissement , code_postal : '88100' ) ) }
it 'returns every dossier that matches any of the search criteria for a given column' do
is_expected . to contain_exactly ( kept_dossier . id , other_kept_dossier . id )
end
end
2018-10-03 19:17:22 +02:00
end
end
context 'for user table' do
2019-02-19 13:10:24 +01:00
let ( :filter ) { [ { 'table' = > 'user' , 'column' = > 'email' , 'value' = > 'keepmail' } ] }
2018-10-03 19:17:22 +02:00
let! ( :kept_dossier ) { create ( :dossier , procedure : procedure , user : create ( :user , email : 'me@keepmail.com' ) ) }
let! ( :discarded_dossier ) { create ( :dossier , procedure : procedure , user : create ( :user , email : 'me@discard.com' ) ) }
it { is_expected . to contain_exactly ( kept_dossier . id ) }
2019-02-19 13:11:00 +01:00
context 'with multiple search values' do
let ( :filter ) do
[
{ 'table' = > 'user' , 'column' = > 'email' , 'value' = > 'keepmail' } ,
{ 'table' = > 'user' , 'column' = > 'email' , 'value' = > 'beta.gouv.fr' }
]
end
let! ( :other_kept_dossier ) { create ( :dossier , procedure : procedure , user : create ( :user , email : 'bazinga@beta.gouv.fr' ) ) }
it 'returns every dossier that matches any of the search criteria for a given column' do
is_expected . to contain_exactly ( kept_dossier . id , other_kept_dossier . id )
end
end
2018-10-03 19:17:22 +02:00
end
2018-10-11 11:17:58 +02:00
context 'for individual table' do
let ( :procedure ) { create ( :procedure , :for_individual ) }
2021-11-19 13:48:52 +01:00
let! ( :kept_dossier ) { create ( :dossier , procedure : procedure , individual : build ( :individual , gender : 'Mme' , prenom : 'Josephine' , nom : 'Baker' ) ) }
let! ( :discarded_dossier ) { create ( :dossier , procedure : procedure , individual : build ( :individual , gender : 'M' , prenom : 'Jean' , nom : 'Tremblay' ) ) }
2018-10-11 11:17:58 +02:00
context 'for gender column' do
let ( :filter ) { [ { 'table' = > 'individual' , 'column' = > 'gender' , 'value' = > 'Mme' } ] }
it { is_expected . to contain_exactly ( kept_dossier . id ) }
end
context 'for prenom column' do
let ( :filter ) { [ { 'table' = > 'individual' , 'column' = > 'prenom' , 'value' = > 'Josephine' } ] }
it { is_expected . to contain_exactly ( kept_dossier . id ) }
end
context 'for nom column' do
let ( :filter ) { [ { 'table' = > 'individual' , 'column' = > 'nom' , 'value' = > 'Baker' } ] }
it { is_expected . to contain_exactly ( kept_dossier . id ) }
end
2019-02-19 13:11:00 +01:00
context 'with multiple search values' do
let ( :filter ) do
[
{ 'table' = > 'individual' , 'column' = > 'prenom' , 'value' = > 'Josephine' } ,
{ 'table' = > 'individual' , 'column' = > 'prenom' , 'value' = > 'Romuald' }
]
end
2021-11-19 13:48:52 +01:00
let! ( :other_kept_dossier ) { create ( :dossier , procedure : procedure , individual : build ( :individual , gender : 'M' , prenom : 'Romuald' , nom : 'Pistis' ) ) }
2019-02-19 13:11:00 +01:00
it 'returns every dossier that matches any of the search criteria for a given column' do
is_expected . to contain_exactly ( kept_dossier . id , other_kept_dossier . id )
end
end
2018-10-11 11:17:58 +02:00
end
2019-04-09 14:32:29 +02:00
2019-08-06 11:02:54 +02:00
context 'for followers_instructeurs table' do
let ( :filter ) { [ { 'table' = > 'followers_instructeurs' , 'column' = > 'email' , 'value' = > 'keepmail' } ] }
2019-04-09 14:32:29 +02:00
let! ( :kept_dossier ) { create ( :dossier , procedure : procedure ) }
let! ( :discarded_dossier ) { create ( :dossier , procedure : procedure ) }
before do
2019-08-06 11:02:54 +02:00
create ( :follow , dossier : kept_dossier , instructeur : create ( :instructeur , email : 'me@keepmail.com' ) )
create ( :follow , dossier : discarded_dossier , instructeur : create ( :instructeur , email : 'me@discard.com' ) )
2019-04-09 14:32:29 +02:00
end
it { is_expected . to contain_exactly ( kept_dossier . id ) }
context 'with multiple search values' do
let ( :filter ) do
[
2019-08-06 11:02:54 +02:00
{ 'table' = > 'followers_instructeurs' , 'column' = > 'email' , 'value' = > 'keepmail' } ,
{ 'table' = > 'followers_instructeurs' , 'column' = > 'email' , 'value' = > 'beta.gouv.fr' }
2019-04-09 14:32:29 +02:00
]
end
let ( :other_kept_dossier ) { create ( :dossier , procedure : procedure ) }
before do
2019-08-06 11:02:54 +02:00
create ( :follow , dossier : other_kept_dossier , instructeur : create ( :instructeur , email : 'bazinga@beta.gouv.fr' ) )
2019-04-09 14:32:29 +02:00
end
it 'returns every dossier that matches any of the search criteria for a given column' do
is_expected . to contain_exactly ( kept_dossier . id , other_kept_dossier . id )
end
end
end
2019-09-18 15:25:05 +02:00
context 'for groupe_instructeur table' do
2023-01-13 16:40:48 +01:00
let ( :filter ) { [ { 'table' = > 'groupe_instructeur' , 'column' = > 'id' , 'value' = > procedure . defaut_groupe_instructeur . id . to_s } ] }
2019-09-18 15:25:05 +02:00
2023-06-29 11:45:20 +02:00
let! ( :gi_2 ) { create ( :groupe_instructeur , label : 'gi2' , procedure : procedure ) }
let! ( :gi_3 ) { create ( :groupe_instructeur , label : 'gi3' , procedure : procedure ) }
2019-09-18 15:25:05 +02:00
2021-03-09 11:21:30 +01:00
let! ( :kept_dossier ) { create ( :dossier , :en_construction , procedure : procedure ) }
let! ( :discarded_dossier ) { create ( :dossier , :en_construction , procedure : procedure , groupe_instructeur : gi_2 ) }
2019-09-18 15:25:05 +02:00
it { is_expected . to contain_exactly ( kept_dossier . id ) }
context 'with multiple search values' do
let ( :filter ) do
[
2023-01-13 16:40:48 +01:00
{ 'table' = > 'groupe_instructeur' , 'column' = > 'id' , 'value' = > procedure . defaut_groupe_instructeur . id . to_s } ,
{ 'table' = > 'groupe_instructeur' , 'column' = > 'id' , 'value' = > gi_3 . id . to_s }
2019-09-18 15:25:05 +02:00
]
end
2020-09-17 11:15:21 +02:00
let! ( :other_kept_dossier ) { create ( :dossier , procedure : procedure , groupe_instructeur : gi_3 ) }
2019-09-18 15:25:05 +02:00
it 'returns every dossier that matches any of the search criteria for a given column' do
is_expected . to contain_exactly ( kept_dossier . id , other_kept_dossier . id )
end
end
end
2018-10-03 19:17:22 +02:00
end
2019-03-04 17:01:31 +01:00
2020-06-08 14:33:58 +02:00
describe " # human_value_for_filter " do
2024-07-19 11:16:40 +02:00
let ( :filters ) { { " suivis " = > [ { label : " label1 " , table : " type_de_champ " , column : first_type_de_champ_id , " value " = > " true " } ] } }
2020-06-08 14:33:58 +02:00
subject { procedure_presentation . human_value_for_filter ( procedure_presentation . filters [ " suivis " ] . first ) }
context 'when type_de_champ text' do
it 'should passthrough value' do
expect ( subject ) . to eq ( " true " )
end
end
context 'when type_de_champ yes_no' do
2024-01-23 09:52:20 +01:00
let ( :procedure ) { create ( :procedure , types_de_champ_public : [ { type : :yes_no } ] ) }
2020-06-08 14:33:58 +02:00
it 'should transform value' do
expect ( subject ) . to eq ( " oui " )
end
end
2022-12-14 23:13:54 +01:00
context 'when filter is state' do
2024-07-19 11:16:40 +02:00
let ( :filters ) { { " suivis " = > [ { table : " self " , column : " state " , " value " = > " en_construction " } ] } }
2022-12-14 23:13:54 +01:00
it 'should get i18n value' do
2023-04-18 13:09:22 +02:00
expect ( subject ) . to eq ( " En construction " )
2022-12-14 23:13:54 +01:00
end
end
2023-06-08 19:01:23 +02:00
context 'when filter is a date' do
2024-07-19 11:16:40 +02:00
let ( :filters ) { { " suivis " = > [ { table : " self " , column : " en_instruction_at " , " value " = > " 15/06/2023 " } ] } }
2023-06-08 19:01:23 +02:00
it 'should get formatted value' do
expect ( subject ) . to eq ( " 15/06/2023 " )
end
end
2020-06-08 14:33:58 +02:00
end
describe " # add_filter " do
let ( :filters ) { { " suivis " = > [ ] } }
context 'when type_de_champ yes_no' do
2024-01-23 09:52:20 +01:00
let ( :procedure ) { create ( :procedure , types_de_champ_public : [ { type : :yes_no } ] ) }
2020-06-08 14:33:58 +02:00
it 'should downcase and transform value' do
2024-05-13 16:38:37 +02:00
procedure_presentation . add_filter ( " suivis " , " type_de_champ/ #{ first_type_de_champ_id } " , + " Oui " )
2020-06-08 14:33:58 +02:00
2021-02-16 15:05:04 +01:00
expect ( procedure_presentation . filters ) . to eq ( {
" suivis " = >
[
2023-06-20 16:11:18 +02:00
{ " label " = > first_type_de_champ . libelle , " table " = > " type_de_champ " , " column " = > first_type_de_champ_id , " value " = > " true " , " value_column " = > " value " }
2021-02-16 15:05:04 +01:00
]
2020-06-08 14:33:58 +02:00
} )
end
end
context 'when type_de_champ text' do
let ( :filters ) { { " suivis " = > [ ] } }
2020-06-10 15:39:01 +02:00
it 'should passthrough value' do
2020-06-08 14:33:58 +02:00
procedure_presentation . add_filter ( " suivis " , " type_de_champ/ #{ first_type_de_champ_id } " , " Oui " )
2021-02-16 15:05:04 +01:00
expect ( procedure_presentation . filters ) . to eq ( {
" suivis " = > [
2023-06-20 16:11:18 +02:00
{ " label " = > first_type_de_champ . libelle , " table " = > " type_de_champ " , " column " = > first_type_de_champ_id , " value " = > " Oui " , " value_column " = > " value " }
]
} )
end
end
context 'when type_de_champ departements' do
let ( :procedure ) { create ( :procedure , types_de_champ_public : [ { type : :departements } ] ) }
let ( :filters ) { { " suivis " = > [ ] } }
it 'should set value_column' do
procedure_presentation . add_filter ( " suivis " , " type_de_champ/ #{ first_type_de_champ_id } " , " 13 " )
expect ( procedure_presentation . filters ) . to eq ( {
" suivis " = > [
{ " label " = > first_type_de_champ . libelle , " table " = > " type_de_champ " , " column " = > first_type_de_champ_id , " value " = > " 13 " , " value_column " = > " external_id " }
2021-02-16 15:05:04 +01:00
]
2020-06-08 14:33:58 +02:00
} )
end
end
end
2022-04-15 12:13:32 +02:00
describe '#filtered_sorted_ids' do
2023-05-04 18:24:18 +02:00
let ( :procedure_presentation ) { create ( :procedure_presentation , assign_to : ) }
2022-04-15 12:13:32 +02:00
let ( :dossier_1 ) { create ( :dossier ) }
let ( :dossier_2 ) { create ( :dossier ) }
let ( :dossier_3 ) { create ( :dossier ) }
let ( :dossiers ) { Dossier . where ( id : [ dossier_1 , dossier_2 , dossier_3 ] . map ( & :id ) ) }
let ( :sorted_ids ) { [ dossier_2 , dossier_3 , dossier_1 ] . map ( & :id ) }
2023-05-04 18:24:18 +02:00
let ( :statut ) { 'tous' }
2022-04-15 12:13:32 +02:00
2023-05-04 18:24:18 +02:00
subject { procedure_presentation . filtered_sorted_ids ( dossiers , statut ) }
2022-04-15 12:13:32 +02:00
2023-05-04 18:24:18 +02:00
context 'with no filters' do
let ( :statut ) { 'suivis' }
let ( :dossiers ) { procedure . dossiers }
before do
create ( :follow , dossier : en_construction_dossier , instructeur : procedure_presentation . instructeur )
create ( :follow , dossier : accepte_dossier , instructeur : procedure_presentation . instructeur )
end
2022-04-15 12:13:32 +02:00
2023-05-04 18:24:18 +02:00
let ( :en_construction_dossier ) { create ( :dossier , :en_construction , procedure : ) }
let ( :accepte_dossier ) { create ( :dossier , :accepte , procedure : ) }
2022-04-15 12:13:32 +02:00
2023-05-04 18:24:18 +02:00
it { is_expected . to contain_exactly ( en_construction_dossier . id ) }
end
2022-04-15 12:13:32 +02:00
2023-05-04 18:24:18 +02:00
context 'with mocked sorted_ids' do
2022-04-15 12:13:32 +02:00
before do
2023-05-04 18:24:18 +02:00
expect ( procedure_presentation ) . to receive ( :sorted_ids ) . and_return ( sorted_ids )
2022-04-15 12:13:32 +02:00
end
it { is_expected . to eq ( sorted_ids ) }
2023-05-04 18:24:18 +02:00
context 'when a filter is present' do
let ( :filtered_ids ) { [ dossier_1 , dossier_2 , dossier_3 ] . map ( & :id ) }
before do
procedure_presentation . filters [ 'tous' ] = 'some_filter'
expect ( procedure_presentation ) . to receive ( :filtered_ids ) . and_return ( filtered_ids )
end
it { is_expected . to eq ( sorted_ids ) }
end
2022-04-15 12:13:32 +02:00
end
end
2017-10-02 17:03:38 +02:00
end