2018-04-17 16:11:49 +02:00
|
|
|
describe Service, type: :model do
|
|
|
|
describe 'validation' do
|
2024-06-01 22:50:48 +02:00
|
|
|
let(:administrateur) { administrateurs(:default_admin) }
|
2018-04-17 16:11:49 +02:00
|
|
|
let(:params) do
|
|
|
|
{
|
|
|
|
nom: 'service des jardins',
|
2018-05-10 11:38:11 +02:00
|
|
|
organisme: 'mairie des iles',
|
2019-02-18 16:18:09 +01:00
|
|
|
type_organisme: Service.type_organismes.fetch(:association),
|
2018-05-10 11:38:11 +02:00
|
|
|
email: 'super@email.com',
|
2019-11-14 18:31:14 +01:00
|
|
|
telephone: '012345678',
|
2018-05-10 11:38:11 +02:00
|
|
|
horaires: 'du lundi au vendredi',
|
|
|
|
adresse: '12 rue des schtroumpfs',
|
2022-07-19 12:41:10 +02:00
|
|
|
administrateur_id: administrateur.id,
|
|
|
|
siret: "35600082800018"
|
2018-04-17 16:11:49 +02:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2022-07-19 12:18:41 +02:00
|
|
|
subject { Service.new(params) }
|
|
|
|
|
|
|
|
it { expect(Service.new(params)).to be_valid }
|
2018-04-17 16:11:49 +02:00
|
|
|
|
2019-11-14 18:31:14 +01:00
|
|
|
it 'should forbid invalid phone numbers' do
|
|
|
|
invalid_phone_numbers = ["1", "Néant", "01 60 50 40 30 20"]
|
|
|
|
|
|
|
|
invalid_phone_numbers.each do |tel|
|
2022-07-19 12:18:41 +02:00
|
|
|
subject.telephone = tel
|
|
|
|
expect(subject).not_to be_valid
|
2019-11-14 18:31:14 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should accept no phone numbers' do
|
2022-07-19 12:18:41 +02:00
|
|
|
subject.telephone = nil
|
|
|
|
expect(subject).to be_valid
|
2019-11-14 18:31:14 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should accept valid phone numbers' do
|
|
|
|
valid_phone_numbers = ["3646", "273115", "0160376983", "01 60 50 40 30 ", "+33160504030"]
|
|
|
|
|
|
|
|
valid_phone_numbers.each do |tel|
|
2022-07-19 12:18:41 +02:00
|
|
|
subject.telephone = tel
|
|
|
|
expect(subject).to be_valid
|
2019-11-14 18:31:14 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-07-19 12:41:10 +02:00
|
|
|
describe "siret" do
|
|
|
|
it 'should not be invalid' do
|
|
|
|
subject.siret = "012345678901234"
|
|
|
|
expect(subject).not_to be_valid
|
|
|
|
end
|
|
|
|
|
2022-07-25 18:17:30 +02:00
|
|
|
it 'should be required' do
|
2022-07-19 12:41:10 +02:00
|
|
|
subject.siret = nil
|
2022-07-25 18:17:30 +02:00
|
|
|
expect(subject).not_to be_valid
|
2022-07-19 12:41:10 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-04-17 16:11:49 +02:00
|
|
|
context 'when a first service exists' do
|
|
|
|
before { Service.create(params) }
|
|
|
|
|
|
|
|
context 'checks uniqueness of administrateur, name couple' do
|
2022-07-19 12:18:41 +02:00
|
|
|
it { expect(Service.create(params)).not_to be_valid }
|
2018-04-17 16:11:49 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'of type_organisme' do
|
|
|
|
it 'should be set' do
|
2022-07-19 12:18:41 +02:00
|
|
|
expect(Service.new(params.except(:type_organisme))).not_to be_valid
|
2018-04-17 16:11:49 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'of nom' do
|
|
|
|
it 'should be set' do
|
2022-07-19 12:18:41 +02:00
|
|
|
expect(Service.new(params.except(:nom))).not_to be_valid
|
2018-04-17 16:11:49 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'of administrateur' do
|
|
|
|
it 'should be set' do
|
2022-07-19 12:18:41 +02:00
|
|
|
expect(Service.new(params.except(:administrateur_id))).not_to be_valid
|
2018-04-17 16:11:49 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'of type_organisme' do
|
|
|
|
it 'should belong to the enum' do
|
2018-12-24 17:28:20 +01:00
|
|
|
expect { Service.new(params.merge(type_organisme: 'choucroute')) }.to raise_error(ArgumentError)
|
2018-04-17 16:11:49 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2022-07-25 17:37:02 +02:00
|
|
|
|
|
|
|
describe "etablissement adresse & geo coordinates" do
|
|
|
|
subject { create(:service, etablissement_lat: latitude, etablissement_lng: longitude, etablissement_infos: etablissement_infos) }
|
|
|
|
|
|
|
|
context "when the service has no geo coordinates" do
|
|
|
|
let(:latitude) { nil }
|
|
|
|
let(:longitude) { nil }
|
|
|
|
let(:etablissement_infos) { {} }
|
|
|
|
it "should return nil" do
|
|
|
|
expect(subject.etablissement_lat).to be_nil
|
|
|
|
expect(subject.etablissement_lng).to be_nil
|
|
|
|
expect(subject.etablissement_adresse).to be_nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when the service has geo coordinates" do
|
|
|
|
let(:latitude) { 43.5 }
|
|
|
|
let(:longitude) { 4.7 }
|
|
|
|
let(:adresse) { "174 Chemin du Beurre\n13200\nARLES\nFRANCE" }
|
|
|
|
let(:etablissement_infos) { { adresse: adresse } }
|
|
|
|
|
|
|
|
it "should return nil" do
|
|
|
|
expect(subject.etablissement_lat).to eq(43.5)
|
|
|
|
expect(subject.etablissement_lng).to eq(4.7)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should return etablissement adresse" do
|
|
|
|
expect(subject.etablissement_adresse).to eq(adresse)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2022-07-25 18:06:53 +02:00
|
|
|
|
|
|
|
describe 'etablissement_latlng' do
|
|
|
|
it 'without coordinates' do
|
|
|
|
service = build(:service, etablissement_lat: nil, etablissement_lng: nil)
|
|
|
|
expect(service.etablissement_latlng).to eq([nil, nil])
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'with coordinates' do
|
|
|
|
service = build(:service)
|
|
|
|
expect(service.etablissement_latlng).to eq([48.87, 2.34])
|
|
|
|
end
|
|
|
|
end
|
2018-04-17 16:11:49 +02:00
|
|
|
end
|