Merge pull request #7264 from betagouv/refactor_procedure_revision_spec

refactor(procedure_revision_spec)
This commit is contained in:
LeSim 2022-05-06 11:29:00 +02:00 committed by GitHub
commit 09590cbfac
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1,42 +1,43 @@
describe ProcedureRevision do
let(:procedure) { create(:procedure, :with_type_de_champ, :with_type_de_champ_private, :with_repetition) }
let(:revision) { procedure.active_revision }
let(:type_de_champ) { revision.types_de_champ_public.first }
let(:type_de_champ_private) { revision.types_de_champ_private.first }
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
type_de_champ = revision.types_de_champ_public.repetition.first
type_de_champ.update(stable_id: 3333)
type_de_champ
repetition = draft.types_de_champ_public.repetition.first
repetition.update(stable_id: 3333)
repetition
end
describe '#add_type_de_champ' do
let(:procedure) { create(:procedure, :with_type_de_champ, :with_type_de_champ_private, :with_repetition) }
it 'type_de_champ' do
expect(revision.types_de_champ_public.size).to eq(2)
new_type_de_champ = revision.add_type_de_champ({
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"
})
revision.reload
expect(revision.types_de_champ_public.size).to eq(3)
expect(revision.types_de_champ_public.last).to eq(new_type_de_champ)
expect(revision.revision_types_de_champ_public.last.position).to eq(2)
expect(revision.revision_types_de_champ_public.last.type_de_champ).to eq(new_type_de_champ)
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
expect(revision.types_de_champ_private.size).to eq(1)
revision.add_type_de_champ({
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
})
revision.reload
expect(revision.types_de_champ_private.size).to eq(2)
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)
revision.add_type_de_champ({
draft.add_type_de_champ({
type_champ: TypeDeChamp.type_champs.fetch(:text),
libelle: "Un champ text",
parent_id: type_de_champ_repetition.stable_id
@ -47,131 +48,139 @@ describe ProcedureRevision do
describe '#move_type_de_champ' do
let(:procedure) { create(:procedure, :with_type_de_champ, types_de_champ_count: 4) }
let(:last_type_de_champ) { revision.types_de_champ_public.last }
let(:last_type_de_champ) { draft.types_de_champ_public.last }
context 'with 4 types de champ publiques' do
it 'move down' do
expect(revision.types_de_champ_public.index(type_de_champ)).to eq(0)
type_de_champ.update(order_place: nil)
revision.move_type_de_champ(type_de_champ.stable_id, 2)
revision.reload
expect(revision.types_de_champ_public.index(type_de_champ)).to eq(2)
expect(revision.procedure.types_de_champ.index(type_de_champ)).to eq(2)
expect(revision.procedure.types_de_champ_for_procedure_presentation.not_repetition.index(type_de_champ)).to eq(2)
expect(draft.types_de_champ_public.index(type_de_champ_public)).to eq(0)
type_de_champ_public.update(order_place: nil)
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)
end
it 'move up' do
expect(revision.types_de_champ_public.index(last_type_de_champ)).to eq(3)
expect(draft.types_de_champ_public.index(last_type_de_champ)).to eq(3)
last_type_de_champ.update(order_place: nil)
revision.move_type_de_champ(last_type_de_champ.stable_id, 0)
revision.reload
expect(revision.types_de_champ_public.index(last_type_de_champ)).to eq(0)
expect(revision.procedure.types_de_champ.index(last_type_de_champ)).to eq(0)
expect(revision.procedure.types_de_champ_for_procedure_presentation.not_repetition.index(last_type_de_champ)).to eq(0)
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)
end
end
context 'repetition' do
context 'with a champ repetition repetition' do
let(:procedure) { create(:procedure, :with_repetition) }
let(:type_de_champ) { type_de_champ_repetition.types_de_champ.first }
let(:last_type_de_champ) { type_de_champ_repetition.types_de_champ.last }
before do
revision.add_type_de_champ({
let!(:second_child) do
draft.add_type_de_champ({
type_champ: TypeDeChamp.type_champs.fetch(:text),
libelle: "Un champ text",
libelle: "second child",
parent_id: type_de_champ_repetition.stable_id
})
revision.add_type_de_champ({
end
let!(:last_child) do
draft.add_type_de_champ({
type_champ: TypeDeChamp.type_champs.fetch(:text),
libelle: "Un champ text",
libelle: "last child",
parent_id: type_de_champ_repetition.stable_id
})
type_de_champ_repetition.reload
end
it 'move down' do
expect(type_de_champ_repetition.types_de_champ.index(type_de_champ)).to eq(0)
revision.move_type_de_champ(type_de_champ.stable_id, 2)
expect(type_de_champ_repetition.types_de_champ.index(second_child)).to eq(1)
draft.move_type_de_champ(second_child.stable_id, 2)
type_de_champ_repetition.reload
expect(type_de_champ_repetition.types_de_champ.index(type_de_champ)).to eq(2)
expect(type_de_champ_repetition.types_de_champ.index(second_child)).to eq(2)
end
it 'move up' do
expect(type_de_champ_repetition.types_de_champ.index(last_type_de_champ)).to eq(2)
revision.move_type_de_champ(last_type_de_champ.stable_id, 0)
expect(type_de_champ_repetition.types_de_champ.index(last_child)).to eq(2)
draft.move_type_de_champ(last_child.stable_id, 0)
type_de_champ_repetition.reload
expect(type_de_champ_repetition.types_de_champ.index(last_type_de_champ)).to eq(0)
expect(type_de_champ_repetition.types_de_champ.index(last_child)).to eq(0)
end
end
end
describe '#remove_type_de_champ' do
context 'for a classic tdc' do
let(:procedure) { create(:procedure, :with_type_de_champ, :with_type_de_champ_private) }
it 'type_de_champ' do
expect(revision.types_de_champ_public.size).to eq(2)
revision.remove_type_de_champ(type_de_champ.stable_id)
procedure.reload
expect(revision.types_de_champ_public.size).to eq(1)
draft.remove_type_de_champ(type_de_champ_public.stable_id)
expect(draft.types_de_champ_public).to be_empty
end
it 'type_de_champ_private' do
expect(revision.types_de_champ_private.size).to eq(1)
revision.remove_type_de_champ(type_de_champ_private.stable_id)
expect(revision.types_de_champ_private.size).to eq(0)
draft.remove_type_de_champ(type_de_champ_private.stable_id)
expect(draft.types_de_champ_private).to be_empty
end
end
it 'type_de_champ_repetition' do
expect(type_de_champ_repetition.types_de_champ.size).to eq(1)
expect(revision.types_de_champ_public.size).to eq(2)
revision.remove_type_de_champ(type_de_champ_repetition.types_de_champ.first.stable_id)
type_de_champ_repetition.reload
expect(type_de_champ_repetition.types_de_champ.size).to eq(0)
expect(revision.types_de_champ_public.size).to eq(2)
context 'for a type_de_champ_repetition' do
let(:procedure) { create(:procedure, :with_repetition) }
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
describe '#create_new_revision' do
let(:new_revision) { procedure.create_new_revision }
let(:procedure) { create(:procedure, :with_type_de_champ, :with_type_de_champ_private, :with_repetition) }
let(:new_draft) { procedure.create_new_revision }
before { new_revision.save }
before { new_draft.save }
it 'should be part of procedure' do
expect(new_revision.procedure).to eq(revision.procedure)
expect(new_draft.procedure).to eq(draft.procedure)
expect(procedure.revisions.count).to eq(2)
expect(procedure.revisions).to eq([revision, new_revision])
expect(procedure.revisions).to eq([draft, new_draft])
end
it 'should have types_de_champ' do
expect(new_revision.types_de_champ_public.count).to eq(2)
expect(new_revision.types_de_champ_private.count).to eq(1)
expect(new_revision.types_de_champ_public).to eq(revision.types_de_champ_public)
expect(new_revision.types_de_champ_private).to eq(revision.types_de_champ_private)
expect(new_draft.types_de_champ_public.count).to eq(2)
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_revision.revision_types_de_champ_public.count).to eq(2)
expect(new_revision.revision_types_de_champ_private.count).to eq(1)
expect(new_revision.revision_types_de_champ_public.count).to eq(revision.revision_types_de_champ_public.count)
expect(new_revision.revision_types_de_champ_private.count).to eq(revision.revision_types_de_champ_private.count)
expect(new_revision.revision_types_de_champ_public).not_to eq(revision.revision_types_de_champ_public)
expect(new_revision.revision_types_de_champ_private).not_to eq(revision.revision_types_de_champ_private)
expect(new_draft.revision_types_de_champ_public.count).to eq(2)
expect(new_draft.revision_types_de_champ_private.count).to eq(1)
expect(new_draft.revision_types_de_champ_public.count).to eq(draft.revision_types_de_champ_public.count)
expect(new_draft.revision_types_de_champ_private.count).to eq(draft.revision_types_de_champ_private.count)
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
describe '#compare' do
let(:type_de_champ_first) { revision.types_de_champ_public.first }
let(:type_de_champ_second) { revision.types_de_champ_public.second }
let(:type_de_champ_first) { draft.types_de_champ_public.first }
let(:type_de_champ_second) { draft.types_de_champ_public.second }
it 'type_de_champ' do
expect(new_revision.types_de_champ_public.size).to eq(2)
new_type_de_champ = new_revision.add_type_de_champ({
expect(new_draft.types_de_champ_public.size).to eq(2)
new_type_de_champ = new_draft.add_type_de_champ({
type_champ: TypeDeChamp.type_champs.fetch(:text),
libelle: "Un champ text"
})
revision.reload
new_revision.reload
expect(new_revision.types_de_champ_public.size).to eq(3)
expect(new_revision.types_de_champ_public.last).to eq(new_type_de_champ)
expect(new_revision.revision_types_de_champ_public.last.position).to eq(2)
expect(new_revision.revision_types_de_champ_public.last.type_de_champ).to eq(new_type_de_champ)
expect(new_revision.revision_types_de_champ_public.last.type_de_champ.revision).to eq(new_revision)
expect(procedure.active_revision.different_from?(new_revision)).to be_truthy
expect(procedure.active_revision.compare(new_revision)).to eq([
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([
{
model: :type_de_champ,
op: :add,
@ -181,8 +190,8 @@ describe ProcedureRevision do
}
])
new_revision.find_or_clone_type_de_champ(new_revision.types_de_champ_public.first.stable_id).update(libelle: 'modifier le libelle')
expect(procedure.active_revision.compare(new_revision.reload)).to eq([
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([
{
model: :type_de_champ,
op: :update,
@ -201,10 +210,10 @@ describe ProcedureRevision do
stable_id: new_type_de_champ.stable_id
}
])
expect(new_revision.types_de_champ_public.first.revision).to eq(new_revision)
expect(new_draft.types_de_champ_public.first.revision).to eq(new_draft)
new_revision.move_type_de_champ(new_revision.types_de_champ_public.second.stable_id, 2)
expect(procedure.active_revision.compare(new_revision.reload)).to eq([
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([
{
model: :type_de_champ,
op: :update,
@ -232,10 +241,10 @@ describe ProcedureRevision do
stable_id: type_de_champ_second.stable_id
}
])
expect(new_revision.types_de_champ_public.last.revision).to eq(revision)
expect(new_draft.types_de_champ_public.last.revision).to eq(draft)
new_revision.remove_type_de_champ(new_revision.types_de_champ_public.first.stable_id)
expect(procedure.active_revision.compare(new_revision.reload)).to eq([
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([
{
model: :type_de_champ,
op: :remove,
@ -252,9 +261,9 @@ describe ProcedureRevision do
}
])
new_revision.find_or_clone_type_de_champ(new_revision.types_de_champ_public.last.stable_id).update(description: 'une description')
new_revision.find_or_clone_type_de_champ(new_revision.types_de_champ_public.last.stable_id).update(mandatory: true)
expect(procedure.active_revision.compare(new_revision.reload)).to eq([
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([
{
model: :type_de_champ,
op: :remove,
@ -291,9 +300,9 @@ describe ProcedureRevision do
}
])
new_revision.find_or_clone_type_de_champ(new_revision.types_de_champ_public.last.types_de_champ.first.stable_id).update(type_champ: :drop_down_list)
new_revision.find_or_clone_type_de_champ(new_revision.types_de_champ_public.last.types_de_champ.first.stable_id).update(drop_down_options: ['one', 'two'])
expect(procedure.active_revision.compare(new_revision.reload)).to eq([
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([
{
model: :type_de_champ,
op: :remove,
@ -336,7 +345,7 @@ describe ProcedureRevision do
private: false,
from: "text",
to: "drop_down_list",
stable_id: new_revision.types_de_champ_public.last.types_de_champ.first.stable_id
stable_id: new_draft.types_de_champ_public.last.types_de_champ.first.stable_id
},
{
model: :type_de_champ,
@ -346,13 +355,13 @@ describe ProcedureRevision do
private: false,
from: [],
to: ["one", "two"],
stable_id: new_revision.types_de_champ_public.last.types_de_champ.first.stable_id
stable_id: new_draft.types_de_champ_public.last.types_de_champ.first.stable_id
}
])
new_revision.find_or_clone_type_de_champ(new_revision.types_de_champ_public.last.types_de_champ.first.stable_id).update(type_champ: :carte)
new_revision.find_or_clone_type_de_champ(new_revision.types_de_champ_public.last.types_de_champ.first.stable_id).update(options: { cadastres: true, znieff: true })
expect(procedure.active_revision.compare(new_revision.reload)).to eq([
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([
{
model: :type_de_champ,
op: :remove,
@ -395,7 +404,7 @@ describe ProcedureRevision do
private: false,
from: "text",
to: "carte",
stable_id: new_revision.types_de_champ_public.last.types_de_champ.first.stable_id
stable_id: new_draft.types_de_champ_public.last.types_de_champ.first.stable_id
},
{
model: :type_de_champ,
@ -405,7 +414,7 @@ describe ProcedureRevision do
private: false,
from: [],
to: [:cadastres, :znieff],
stable_id: new_revision.types_de_champ_public.last.types_de_champ.first.stable_id
stable_id: new_draft.types_de_champ_public.last.types_de_champ.first.stable_id
}
])
end