cleanup tests

This commit is contained in:
Paul Chavard 2021-05-06 01:14:57 +02:00 committed by kara Diaby
parent 468e9e849a
commit e043645a88
3 changed files with 65 additions and 33 deletions

View file

@ -31,13 +31,6 @@ class DossierSearchService
.uniq .uniq
end end
def self.id_compatible?(number)
ActiveRecord::Type::Integer.new.serialize(number)
true
rescue ActiveModel::RangeError
false
end
def self.dossier_by_full_text_for_user(search_terms, dossiers) def self.dossier_by_full_text_for_user(search_terms, dossiers)
ts_vector = "to_tsvector('french', search_terms)" ts_vector = "to_tsvector('french', search_terms)"
ts_query = "to_tsquery('french', #{Dossier.connection.quote(to_tsquery(search_terms))})" ts_query = "to_tsquery('french', #{Dossier.connection.quote(to_tsquery(search_terms))})"
@ -56,15 +49,11 @@ class DossierSearchService
end end
end end
def self.dossier_by_full_text_for_current_user(search_terms, user) def self.id_compatible?(number)
ts_vector = "to_tsvector('french', search_terms || private_search_terms)" ActiveRecord::Type::Integer.new.serialize(number)
ts_query = "to_tsquery('french', #{Dossier.connection.quote(to_tsquery(search_terms))})" true
rescue ActiveModel::RangeError
user false
.dossiers
.state_not_brouillon
.where("#{ts_vector} @@ #{ts_query}")
.order(Arel.sql("COALESCE(ts_rank(#{ts_vector}, #{ts_query}), 0) DESC"))
end end
def self.to_tsquery(search_terms) def self.to_tsquery(search_terms)

View file

@ -1,32 +1,47 @@
describe RechercheController, type: :controller do describe RechercheController, type: :controller do
let(:dossier) { create(:dossier, :en_construction) } let(:dossier) { create(:dossier, :en_construction, :with_all_annotations) }
let(:dossier2) { create(:dossier, :en_construction, procedure: dossier.procedure) } let(:dossier2) { create(:dossier, :en_construction, procedure: dossier.procedure) }
let(:instructeur) { create(:instructeur) } let(:instructeur) { create(:instructeur) }
let(:dossier_with_expert) { avis.dossier }
let(:avis) { create(:avis, dossier: create(:dossier, :en_construction, :with_all_annotations)) }
let(:user) { instructeur.user }
before do before do
#instructeur.groupe_instructeurs << dossier2.procedure.defaut_groupe_instructeur instructeur.assign_to_procedure(dossier.procedure)
instructeur.groupe_instructeurs << dossier.procedure.defaut_groupe_instructeur
end end
describe 'GET #index' do describe 'GET #index' do
before { sign_in(instructeur.user) } before { sign_in(user) }
subject { get :index, params: { q: query } } subject { get :index, params: { q: query } }
describe 'by id' do describe 'by id' do
context 'when instructeur own the dossier' do context 'when instructeur own the dossier' do
before do
subject
end
let(:query) { dossier.id } let(:query) { dossier.id }
before { subject }
it { is_expected.to have_http_status(200) } it { is_expected.to have_http_status(200) }
it 'returns the expected dossier' do it 'returns the expected dossier' do
expect(assigns(:dossiers).count).to eq(1) expect(assigns(:projected_dossiers).count).to eq(1)
expect(assigns(:dossiers).first.id).to eq(dossier.id) expect(assigns(:projected_dossiers).first.dossier_id).to eq(dossier.id)
end
end
context 'when expert own the dossier' do
let(:user) { avis.experts_procedure.expert.user }
let(:query) { dossier_with_expert.id }
before { subject }
it { is_expected.to have_http_status(200) }
it 'returns the expected dossier' do
expect(assigns(:projected_dossiers).count).to eq(1)
expect(assigns(:projected_dossiers).first.dossier_id).to eq(dossier_with_expert.id)
end end
end end
@ -38,7 +53,7 @@ describe RechercheController, type: :controller do
it 'does not return the dossier' do it 'does not return the dossier' do
subject subject
expect(assigns(:dossiers).count).to eq(0) expect(assigns(:projected_dossiers).count).to eq(0)
end end
end end
@ -49,7 +64,35 @@ describe RechercheController, type: :controller do
it 'does not return the dossier' do it 'does not return the dossier' do
subject subject
expect(assigns(:dossiers).count).to eq(0) expect(assigns(:projected_dossiers).count).to eq(0)
end
end
end
describe 'by private annotations' do
context 'when instructeur search by private annotations' do
let(:query) { dossier.private_search_terms }
before { subject }
it { is_expected.to have_http_status(200) }
it 'returns the expected dossier' do
expect(assigns(:projected_dossiers).count).to eq(1)
expect(assigns(:projected_dossiers).first.dossier_id).to eq(dossier.id)
end
end
context 'when expert search by private annotations' do
let(:user) { avis.experts_procedure.expert.user }
let(:query) { dossier_with_expert.private_search_terms }
before { subject }
it { is_expected.to have_http_status(200) }
it 'returns 0 dossiers' do
expect(assigns(:projected_dossiers).count).to eq(0)
end end
end end
end end
@ -61,7 +104,7 @@ describe RechercheController, type: :controller do
it 'returns 0 dossier' do it 'returns 0 dossier' do
subject subject
expect(assigns(:dossiers).count).to eq(0) expect(assigns(:projected_dossiers).count).to eq(0)
end end
end end
end end

View file

@ -1,9 +1,9 @@
describe DossierSearchService do describe DossierSearchService do
describe '#matching_dossiers_for_current_user' do describe '#matching_dossiers' do
subject { liste_dossiers } subject { liste_dossiers }
let(:liste_dossiers) do let(:liste_dossiers) do
described_class.matching_dossiers_for_current_user(terms, instructeur_1.user) described_class.matching_dossiers(instructeur_1.dossiers, terms)
end end
let(:administrateur_1) { create(:administrateur) } let(:administrateur_1) { create(:administrateur) }