demarches-normaliennes/spec/models/procedure_revision_spec.rb

428 lines
16 KiB
Ruby
Raw Normal View History

describe ProcedureRevision do
2022-05-05 14:27:10 +02:00
let(:draft) { procedure.draft_revision }
let(:type_de_champ_public) { draft.types_de_champ_public.first }
let(:type_de_champ_private) { draft.types_de_champ_private.first }
let(:type_de_champ_repetition) do
2022-05-05 14:27:10 +02:00
repetition = draft.types_de_champ_public.repetition.first
2022-05-05 12:04:54 +02:00
repetition.update(stable_id: 3333)
repetition
end
describe '#add_type_de_champ' do
2022-05-05 14:13:03 +02:00
let(:procedure) { create(:procedure, :with_type_de_champ, :with_type_de_champ_private, :with_repetition) }
it 'type_de_champ' do
2022-05-05 14:27:10 +02:00
expect(draft.types_de_champ_public.size).to eq(2)
new_type_de_champ = draft.add_type_de_champ({
type_champ: TypeDeChamp.type_champs.fetch(:text),
libelle: "Un champ text"
})
2022-05-05 14:27:10 +02:00
draft.reload
expect(draft.types_de_champ_public.size).to eq(3)
expect(draft.types_de_champ_public.last).to eq(new_type_de_champ)
expect(draft.revision_types_de_champ_public.last.position).to eq(2)
expect(draft.revision_types_de_champ_public.last.type_de_champ).to eq(new_type_de_champ)
end
it 'type_de_champ_private' do
2022-05-05 14:27:10 +02:00
expect(draft.types_de_champ_private.size).to eq(1)
draft.add_type_de_champ({
type_champ: TypeDeChamp.type_champs.fetch(:text),
libelle: "Un champ text",
private: true
})
2022-05-05 14:27:10 +02:00
draft.reload
expect(draft.types_de_champ_private.size).to eq(2)
end
it 'type_de_champ_repetition' do
expect(type_de_champ_repetition.types_de_champ.size).to eq(1)
2022-05-05 14:27:10 +02:00
draft.add_type_de_champ({
type_champ: TypeDeChamp.type_champs.fetch(:text),
libelle: "Un champ text",
parent_id: type_de_champ_repetition.stable_id
})
expect(type_de_champ_repetition.types_de_champ.size).to eq(2)
end
end
describe '#move_type_de_champ' do
let(:procedure) { create(:procedure, :with_type_de_champ, types_de_champ_count: 4) }
2022-05-05 14:27:10 +02:00
let(:last_type_de_champ) { draft.types_de_champ_public.last }
2022-05-05 14:13:03 +02:00
context 'with 4 types de champ publiques' do
it 'move down' do
2022-05-05 14:27:10 +02:00
expect(draft.types_de_champ_public.index(type_de_champ_public)).to eq(0)
2022-05-05 14:13:03 +02:00
type_de_champ_public.update(order_place: nil)
2022-05-05 14:27:10 +02:00
draft.move_type_de_champ(type_de_champ_public.stable_id, 2)
draft.reload
expect(draft.types_de_champ_public.index(type_de_champ_public)).to eq(2)
expect(draft.procedure.types_de_champ.index(type_de_champ_public)).to eq(2)
expect(draft.procedure.types_de_champ_for_procedure_presentation.not_repetition.index(type_de_champ_public)).to eq(2)
2022-05-05 14:13:03 +02:00
end
2022-05-05 14:13:03 +02:00
it 'move up' do
2022-05-05 14:27:10 +02:00
expect(draft.types_de_champ_public.index(last_type_de_champ)).to eq(3)
2022-05-05 14:13:03 +02:00
last_type_de_champ.update(order_place: nil)
2022-05-05 14:27:10 +02:00
draft.move_type_de_champ(last_type_de_champ.stable_id, 0)
draft.reload
expect(draft.types_de_champ_public.index(last_type_de_champ)).to eq(0)
expect(draft.procedure.types_de_champ.index(last_type_de_champ)).to eq(0)
expect(draft.procedure.types_de_champ_for_procedure_presentation.not_repetition.index(last_type_de_champ)).to eq(0)
2022-05-05 14:13:03 +02:00
end
end
2022-05-05 14:13:03 +02:00
context 'with a champ repetition repetition' do
let(:procedure) { create(:procedure, :with_repetition) }
2022-05-05 12:04:54 +02:00
let!(:second_child) do
2022-05-05 14:27:10 +02:00
draft.add_type_de_champ({
type_champ: TypeDeChamp.type_champs.fetch(:text),
2022-05-05 12:04:54 +02:00
libelle: "second child",
parent_id: type_de_champ_repetition.stable_id
})
2022-05-05 12:04:54 +02:00
end
let!(:last_child) do
2022-05-05 14:27:10 +02:00
draft.add_type_de_champ({
type_champ: TypeDeChamp.type_champs.fetch(:text),
2022-05-05 12:04:54 +02:00
libelle: "last child",
parent_id: type_de_champ_repetition.stable_id
})
end
it 'move down' do
2022-05-05 12:04:54 +02:00
expect(type_de_champ_repetition.types_de_champ.index(second_child)).to eq(1)
2022-05-05 14:27:10 +02:00
draft.move_type_de_champ(second_child.stable_id, 2)
type_de_champ_repetition.reload
2022-05-05 12:04:54 +02:00
expect(type_de_champ_repetition.types_de_champ.index(second_child)).to eq(2)
end
it 'move up' do
2022-05-05 12:04:54 +02:00
expect(type_de_champ_repetition.types_de_champ.index(last_child)).to eq(2)
2022-05-05 14:27:10 +02:00
draft.move_type_de_champ(last_child.stable_id, 0)
type_de_champ_repetition.reload
2022-05-05 12:04:54 +02:00
expect(type_de_champ_repetition.types_de_champ.index(last_child)).to eq(0)
end
end
end
describe '#remove_type_de_champ' do
2022-05-06 09:26:21 +02:00
context 'for a classic tdc' do
let(:procedure) { create(:procedure, :with_type_de_champ, :with_type_de_champ_private) }
2022-05-05 14:13:03 +02:00
2022-05-06 09:26:21 +02:00
it 'type_de_champ' do
draft.remove_type_de_champ(type_de_champ_public.stable_id)
2022-05-05 12:04:54 +02:00
2022-05-06 09:26:21 +02:00
expect(draft.types_de_champ_public).to be_empty
end
2022-05-06 09:26:21 +02:00
it 'type_de_champ_private' do
draft.remove_type_de_champ(type_de_champ_private.stable_id)
2022-05-05 12:04:54 +02:00
2022-05-06 09:26:21 +02:00
expect(draft.types_de_champ_private).to be_empty
end
end
2022-05-06 09:26:21 +02:00
context 'for a type_de_champ_repetition' do
let(:procedure) { create(:procedure, :with_repetition) }
2022-05-05 12:04:54 +02:00
2022-05-06 09:26:21 +02:00
it 'can remove its children' do
draft.remove_type_de_champ(type_de_champ_repetition.types_de_champ.first.stable_id)
expect(type_de_champ_repetition.types_de_champ).to be_empty
expect(draft.types_de_champ_public.size).to eq(1)
end
end
end
2020-07-08 19:11:03 +02:00
describe '#create_new_revision' do
2022-05-05 14:27:10 +02:00
let(:new_draft) { procedure.create_new_revision }
2020-07-08 19:11:03 +02:00
2022-05-06 11:14:29 +02:00
context 'from a simple procedure' do
let(:procedure) { create(:procedure) }
2020-07-08 19:11:03 +02:00
2022-05-06 11:14:29 +02:00
it 'should be part of procedure' do
expect(new_draft.procedure).to eq(draft.procedure)
expect(procedure.revisions.count).to eq(2)
expect(procedure.revisions).to eq([draft, new_draft])
end
2020-07-08 19:11:03 +02:00
end
2022-05-06 11:14:29 +02:00
context 'with simple tdc' do
let(:procedure) { create(:procedure, :with_type_de_champ, :with_type_de_champ_private) }
it 'should have the same tdcs with different links' do
expect(new_draft.types_de_champ_public.count).to eq(1)
expect(new_draft.types_de_champ_private.count).to eq(1)
expect(new_draft.types_de_champ_public).to eq(draft.types_de_champ_public)
expect(new_draft.types_de_champ_private).to eq(draft.types_de_champ_private)
expect(new_draft.revision_types_de_champ_public.count).to eq(1)
expect(new_draft.revision_types_de_champ_private.count).to eq(1)
expect(new_draft.revision_types_de_champ_public).not_to eq(draft.revision_types_de_champ_public)
expect(new_draft.revision_types_de_champ_private).not_to eq(draft.revision_types_de_champ_private)
end
2020-07-08 19:11:03 +02:00
end
2021-01-21 11:32:01 +01:00
describe '#compare' do
2022-05-06 11:14:29 +02:00
let(:procedure) { create(:procedure, :with_type_de_champ, :with_type_de_champ_private, :with_repetition) }
2022-05-05 14:27:10 +02:00
let(:type_de_champ_first) { draft.types_de_champ_public.first }
let(:type_de_champ_second) { draft.types_de_champ_public.second }
2021-01-21 11:32:01 +01:00
it 'type_de_champ' do
2022-05-05 14:27:10 +02:00
expect(new_draft.types_de_champ_public.size).to eq(2)
new_type_de_champ = new_draft.add_type_de_champ({
2021-01-21 11:32:01 +01:00
type_champ: TypeDeChamp.type_champs.fetch(:text),
libelle: "Un champ text"
})
2022-05-05 14:27:10 +02:00
draft.reload
new_draft.reload
expect(new_draft.types_de_champ_public.size).to eq(3)
expect(new_draft.types_de_champ_public.last).to eq(new_type_de_champ)
expect(new_draft.revision_types_de_champ_public.last.position).to eq(2)
expect(new_draft.revision_types_de_champ_public.last.type_de_champ).to eq(new_type_de_champ)
expect(new_draft.revision_types_de_champ_public.last.type_de_champ.revision).to eq(new_draft)
expect(procedure.active_revision.different_from?(new_draft)).to be_truthy
expect(procedure.active_revision.compare(new_draft)).to eq([
2021-01-21 11:32:01 +01:00
{
model: :type_de_champ,
2021-01-21 11:32:01 +01:00
op: :add,
label: "Un champ text",
private: false,
stable_id: new_type_de_champ.stable_id
2021-01-21 11:32:01 +01:00
}
])
2022-05-05 14:27:10 +02:00
new_draft.find_or_clone_type_de_champ(new_draft.types_de_champ_public.first.stable_id).update(libelle: 'modifier le libelle')
expect(procedure.active_revision.compare(new_draft.reload)).to eq([
2021-01-21 11:32:01 +01:00
{
model: :type_de_champ,
2021-01-21 11:32:01 +01:00
op: :update,
attribute: :libelle,
label: type_de_champ_first.libelle,
private: false,
2021-01-21 11:32:01 +01:00
from: type_de_champ_first.libelle,
to: "modifier le libelle",
stable_id: type_de_champ_first.stable_id
2021-01-21 11:32:01 +01:00
},
{
model: :type_de_champ,
2021-01-21 11:32:01 +01:00
op: :add,
label: "Un champ text",
private: false,
stable_id: new_type_de_champ.stable_id
2021-01-21 11:32:01 +01:00
}
])
2022-05-05 14:27:10 +02:00
expect(new_draft.types_de_champ_public.first.revision).to eq(new_draft)
2021-01-21 11:32:01 +01:00
2022-05-05 14:27:10 +02:00
new_draft.move_type_de_champ(new_draft.types_de_champ_public.second.stable_id, 2)
expect(procedure.active_revision.compare(new_draft.reload)).to eq([
2021-01-21 11:32:01 +01:00
{
model: :type_de_champ,
2021-01-21 11:32:01 +01:00
op: :update,
attribute: :libelle,
label: type_de_champ_first.libelle,
private: false,
2021-01-21 11:32:01 +01:00
from: type_de_champ_first.libelle,
to: "modifier le libelle",
stable_id: type_de_champ_first.stable_id
2021-01-21 11:32:01 +01:00
},
{
model: :type_de_champ,
2021-01-21 11:32:01 +01:00
op: :add,
label: "Un champ text",
private: false,
stable_id: new_type_de_champ.stable_id
2021-01-21 11:32:01 +01:00
},
{
model: :type_de_champ,
2021-01-21 11:32:01 +01:00
op: :move,
label: type_de_champ_second.libelle,
private: false,
2021-01-21 11:32:01 +01:00
from: 1,
to: 2,
stable_id: type_de_champ_second.stable_id
2021-01-21 11:32:01 +01:00
}
])
2022-05-05 14:27:10 +02:00
expect(new_draft.types_de_champ_public.last.revision).to eq(draft)
2021-01-21 11:32:01 +01:00
2022-05-05 14:27:10 +02:00
new_draft.remove_type_de_champ(new_draft.types_de_champ_public.first.stable_id)
expect(procedure.active_revision.compare(new_draft.reload)).to eq([
2021-01-21 11:32:01 +01:00
{
model: :type_de_champ,
2021-01-21 11:32:01 +01:00
op: :remove,
label: type_de_champ_first.libelle,
private: false,
stable_id: type_de_champ_first.stable_id
2021-01-21 11:32:01 +01:00
},
{
model: :type_de_champ,
2021-01-21 11:32:01 +01:00
op: :add,
label: "Un champ text",
private: false,
stable_id: new_type_de_champ.stable_id
2021-01-21 11:32:01 +01:00
}
])
2022-05-05 14:27:10 +02:00
new_draft.find_or_clone_type_de_champ(new_draft.types_de_champ_public.last.stable_id).update(description: 'une description')
new_draft.find_or_clone_type_de_champ(new_draft.types_de_champ_public.last.stable_id).update(mandatory: true)
expect(procedure.active_revision.compare(new_draft.reload)).to eq([
2021-01-21 11:32:01 +01:00
{
model: :type_de_champ,
2021-01-21 11:32:01 +01:00
op: :remove,
label: type_de_champ_first.libelle,
private: false,
stable_id: type_de_champ_first.stable_id
2021-01-21 11:32:01 +01:00
},
{
model: :type_de_champ,
2021-01-21 11:32:01 +01:00
op: :add,
label: "Un champ text",
private: false,
stable_id: new_type_de_champ.stable_id
2021-01-21 11:32:01 +01:00
},
{
model: :type_de_champ,
2021-01-21 11:32:01 +01:00
op: :update,
attribute: :description,
label: type_de_champ_second.libelle,
private: false,
2021-01-21 11:32:01 +01:00
from: type_de_champ_second.description,
to: "une description",
stable_id: type_de_champ_second.stable_id
2021-01-21 11:32:01 +01:00
},
{
model: :type_de_champ,
2021-01-21 11:32:01 +01:00
op: :update,
attribute: :mandatory,
label: type_de_champ_second.libelle,
private: false,
2021-01-21 11:32:01 +01:00
from: false,
to: true,
stable_id: type_de_champ_second.stable_id
2021-01-21 11:32:01 +01:00
}
])
2022-05-05 14:27:10 +02:00
new_draft.find_or_clone_type_de_champ(new_draft.types_de_champ_public.last.types_de_champ.first.stable_id).update(type_champ: :drop_down_list)
new_draft.find_or_clone_type_de_champ(new_draft.types_de_champ_public.last.types_de_champ.first.stable_id).update(drop_down_options: ['one', 'two'])
expect(procedure.active_revision.compare(new_draft.reload)).to eq([
2021-01-21 11:32:01 +01:00
{
model: :type_de_champ,
2021-01-21 11:32:01 +01:00
op: :remove,
label: type_de_champ_first.libelle,
private: false,
stable_id: type_de_champ_first.stable_id
2021-01-21 11:32:01 +01:00
},
{
model: :type_de_champ,
2021-01-21 11:32:01 +01:00
op: :add,
label: "Un champ text",
private: false,
stable_id: new_type_de_champ.stable_id
2021-01-21 11:32:01 +01:00
},
{
model: :type_de_champ,
2021-01-21 11:32:01 +01:00
op: :update,
attribute: :description,
label: type_de_champ_second.libelle,
private: false,
2021-01-21 11:32:01 +01:00
from: type_de_champ_second.description,
to: "une description",
stable_id: type_de_champ_second.stable_id
2021-01-21 11:32:01 +01:00
},
{
model: :type_de_champ,
2021-01-21 11:32:01 +01:00
op: :update,
attribute: :mandatory,
label: type_de_champ_second.libelle,
private: false,
2021-01-21 11:32:01 +01:00
from: false,
to: true,
stable_id: type_de_champ_second.stable_id
2021-01-21 11:32:01 +01:00
},
{
model: :type_de_champ,
2021-01-21 11:32:01 +01:00
op: :update,
attribute: :type_champ,
label: "sub type de champ",
private: false,
2021-01-21 11:32:01 +01:00
from: "text",
to: "drop_down_list",
2022-05-05 14:27:10 +02:00
stable_id: new_draft.types_de_champ_public.last.types_de_champ.first.stable_id
2021-01-21 11:32:01 +01:00
},
{
model: :type_de_champ,
2021-01-21 11:32:01 +01:00
op: :update,
attribute: :drop_down_options,
label: "sub type de champ",
private: false,
2021-01-21 11:32:01 +01:00
from: [],
to: ["one", "two"],
2022-05-05 14:27:10 +02:00
stable_id: new_draft.types_de_champ_public.last.types_de_champ.first.stable_id
2021-01-21 11:32:01 +01:00
}
])
2021-06-23 15:54:12 +02:00
2022-05-05 14:27:10 +02:00
new_draft.find_or_clone_type_de_champ(new_draft.types_de_champ_public.last.types_de_champ.first.stable_id).update(type_champ: :carte)
new_draft.find_or_clone_type_de_champ(new_draft.types_de_champ_public.last.types_de_champ.first.stable_id).update(options: { cadastres: true, znieff: true })
expect(procedure.active_revision.compare(new_draft.reload)).to eq([
2021-06-23 15:54:12 +02:00
{
model: :type_de_champ,
2021-06-23 15:54:12 +02:00
op: :remove,
label: type_de_champ_first.libelle,
private: false,
stable_id: type_de_champ_first.stable_id
2021-06-23 15:54:12 +02:00
},
{
model: :type_de_champ,
2021-06-23 15:54:12 +02:00
op: :add,
label: "Un champ text",
private: false,
stable_id: new_type_de_champ.stable_id
2021-06-23 15:54:12 +02:00
},
{
model: :type_de_champ,
2021-06-23 15:54:12 +02:00
op: :update,
attribute: :description,
label: type_de_champ_second.libelle,
private: false,
from: type_de_champ_second.description,
to: "une description",
stable_id: type_de_champ_second.stable_id
2021-06-23 15:54:12 +02:00
},
{
model: :type_de_champ,
2021-06-23 15:54:12 +02:00
op: :update,
attribute: :mandatory,
label: type_de_champ_second.libelle,
private: false,
from: false,
to: true,
stable_id: type_de_champ_second.stable_id
2021-06-23 15:54:12 +02:00
},
{
model: :type_de_champ,
2021-06-23 15:54:12 +02:00
op: :update,
attribute: :type_champ,
label: "sub type de champ",
private: false,
from: "text",
to: "carte",
2022-05-05 14:27:10 +02:00
stable_id: new_draft.types_de_champ_public.last.types_de_champ.first.stable_id
2021-06-23 15:54:12 +02:00
},
{
model: :type_de_champ,
2021-06-23 15:54:12 +02:00
op: :update,
attribute: :carte_layers,
label: "sub type de champ",
private: false,
from: [],
to: [:cadastres, :znieff],
2022-05-05 14:27:10 +02:00
stable_id: new_draft.types_de_champ_public.last.types_de_champ.first.stable_id
2021-06-23 15:54:12 +02:00
}
])
2021-01-21 11:32:01 +01:00
end
end
2020-07-08 19:11:03 +02:00
end
end