aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorteddywing2017-11-06 12:36:17 +0100
committerGitHub2017-11-06 12:36:17 +0100
commit1b70d5584f59fb3afd47030904bc16daa9f200bc (patch)
treef90611dec75bf50a2a54e5ce37b469bac61220a2
parent94d240c3ae5511591452aa1a43c90811b0c571dd (diff)
parentea1cbc874aa6609c6217514640eb1eca20665d41 (diff)
downloadchouette-core-1b70d5584f59fb3afd47030904bc16daa9f200bc.tar.bz2
Merge pull request #109 from af83/4802-referential-model-normalisation
4802 referential model normalisation
-rw-r--r--app/models/referential.rb12
-rw-r--r--app/models/referential_suite.rb13
-rw-r--r--config/locales/referential_suites.en.yml5
-rw-r--r--config/locales/referential_suites.fr.yml5
-rw-r--r--config/locales/referentials.en.yml1
-rw-r--r--config/locales/referentials.fr.yml7
-rw-r--r--config/locales/workbenches.en.yml6
-rw-r--r--config/locales/workbenches.fr.yml6
-rw-r--r--spec/factories/referentials.rb10
-rw-r--r--spec/features/workbenches_spec.rb12
-rw-r--r--spec/helpers/table_builder_helper_spec.rb2
-rw-r--r--spec/models/referential/referential_org_through_workbench_spec.rb44
-rw-r--r--spec/models/referential_spec.rb2
-rw-r--r--spec/models/referential_suite/referential_suite_current_new_spec.rb64
-rw-r--r--spec/models/referential_suite/referential_suite_spec.rb (renamed from spec/models/referential_suite_spec.rb)0
-rw-r--r--spec/requests/api/v1/netex_import_spec.rb2
16 files changed, 166 insertions, 25 deletions
diff --git a/app/models/referential.rb b/app/models/referential.rb
index c7b52ddf8..ed13cd077 100644
--- a/app/models/referential.rb
+++ b/app/models/referential.rb
@@ -1,4 +1,3 @@
-# coding: utf-8
class Referential < ActiveRecord::Base
include DataFormatEnumerations
@@ -28,6 +27,15 @@ class Referential < ActiveRecord::Base
belongs_to :organisation
validates_presence_of :organisation
+ validate def validate_consistent_organisation
+ return true if workbench_id.nil?
+ ids = [workbench.organisation_id, organisation_id]
+ return true if ids.first == ids.last
+ errors.add(:inconsistent_organisation,
+ I18n.t('referentials.errors.inconsistent_organisation',
+ indirect_name: workbench.organisation.name,
+ direct_name: organisation.name))
+ end
belongs_to :line_referential
validates_presence_of :line_referential
@@ -290,7 +298,7 @@ class Referential < ActiveRecord::Base
end
def assign_prefix
- self.prefix = self.organisation.name.parameterize.gsub('-', '_')
+ self.prefix = organisation.name.parameterize.gsub('-', '_')
end
def assign_line_and_stop_area_referential
diff --git a/app/models/referential_suite.rb b/app/models/referential_suite.rb
index 9fd25ef3f..93c2c3f36 100644
--- a/app/models/referential_suite.rb
+++ b/app/models/referential_suite.rb
@@ -1,6 +1,19 @@
class ReferentialSuite < ActiveRecord::Base
belongs_to :new, class_name: 'Referential'
+ validate def validate_consistent_new
+ return true if new_id.nil?
+ return true if new.referential_suite_id == id
+ errors.add(:inconsistent_new,
+ I18n.t('referential_suites.errors.inconsistent_new', name: new.name))
+ end
+
belongs_to :current, class_name: 'Referential'
+ validate def validate_consistent_current
+ return true if current_id.nil?
+ return true if current.referential_suite_id == id
+ errors.add(:inconsistent_current,
+ I18n.t('referential_suites.errors.inconsistent_current', name: current.name))
+ end
has_many :referentials
end
diff --git a/config/locales/referential_suites.en.yml b/config/locales/referential_suites.en.yml
index 1a77d9026..aa6fef0e2 100644
--- a/config/locales/referential_suites.en.yml
+++ b/config/locales/referential_suites.en.yml
@@ -1,2 +1,5 @@
-en:
+fr:
referential_suites:
+ errors:
+ inconsistent_current: "The current referential (%{name}) does not belong to this referential suite"
+ inconsistent_new: "The new referential (%{name}) does not belong to this referential suite"
diff --git a/config/locales/referential_suites.fr.yml b/config/locales/referential_suites.fr.yml
index c87b32fc7..9cb1e33d3 100644
--- a/config/locales/referential_suites.fr.yml
+++ b/config/locales/referential_suites.fr.yml
@@ -1,2 +1,5 @@
-fr:
+en:
referential_suites:
+ errors:
+ inconsistent_current: "Le current referential (%{name}) n'appartient pas à cette referential suite"
+ inconsistent_new: "Le new referential (%{name}) n'appartient pas à cette referential suite"
diff --git a/config/locales/referentials.en.yml b/config/locales/referentials.en.yml
index 14b4dbe66..7923ba5f2 100644
--- a/config/locales/referentials.en.yml
+++ b/config/locales/referentials.en.yml
@@ -38,6 +38,7 @@ en:
edit: "Edit this data space"
clone: "Clone this data space"
errors:
+ inconsistent_organisation: "Organisation of asscociated workbench is (%{indirect_name}), while directly associated organisation is (%{direct_name}), they need to be equal"
validity_period: "Invalid validity periode"
pg_excluded: "can't begins with pg_"
public_excluded: "public is a reserved value"
diff --git a/config/locales/referentials.fr.yml b/config/locales/referentials.fr.yml
index b90f6e5b6..6aa81b33d 100644
--- a/config/locales/referentials.fr.yml
+++ b/config/locales/referentials.fr.yml
@@ -2,7 +2,7 @@ fr:
referentials:
filters:
name_or_number_or_objectid: 'Indiquez un nom de ligne, nom court ou objectid'
- name: 'Indiquez un nom de référentiel...'
+ name: 'Indiquez un nom de jeu de données...'
line: 'Indiquez une ligne...'
search_no_results: 'Aucun jeu de données ne correspond à votre recherche'
error_period_filter: "Le filtre par période doit contenir une date de début et de fin valides"
@@ -38,6 +38,7 @@ fr:
edit: "Editer ce jeu de données"
clone: "Cloner ce jeu de données"
errors:
+ inconsistent_organisation: "L'organisation asscociée par espace de travail est (%{indirect_name}), mais l'organisation associée directement est (%{direct_name}), elles doivent être identiques."
validity_period: "Période de validité invalide"
pg_excluded: "ne peut pas commencer par pg_ (valeurs réservées)"
public_excluded: "public est une valeur réservée"
@@ -102,8 +103,8 @@ fr:
referential:
slug: "caractères autorisés : alphanumériques minuscules et 'souligné' et doit commencer par une lettre"
prefix: "caractères autorisés : alphanumériques et 'souligné'"
- upper_corner: "latitude,longitude dans le référentiel WGS84, le séparateur de décimales est 'point'"
- lower_corner: "latitude,longitude dans le référentiel WGS84, le séparateur de décimales est 'point'"
+ upper_corner: "latitude,longitude dans le jeu de données WGS84, le séparateur de décimales est 'point'"
+ lower_corner: "latitude,longitude dans le jeu de données WGS84, le séparateur de décimales est 'point'"
simple_form:
labels:
diff --git a/config/locales/workbenches.en.yml b/config/locales/workbenches.en.yml
index 051ef1c96..3a2fcd598 100644
--- a/config/locales/workbenches.en.yml
+++ b/config/locales/workbenches.en.yml
@@ -11,9 +11,9 @@ en:
see: "See the list"
no_content: "No content yet."
referential_count:
- zero: "Currently, there is no referential in your workbench"
- one: "Currently, there is one referential in your workbench"
- other: "Currently, there are #{count} referentials in your workbench"
+ zero: "There is no referential in your workbench"
+ one: "There is one referential in your workbench"
+ other: "There are #{count} referentials in your workbench"
activerecord:
models:
workbench:
diff --git a/config/locales/workbenches.fr.yml b/config/locales/workbenches.fr.yml
index 7a614efc5..0f8a516e3 100644
--- a/config/locales/workbenches.fr.yml
+++ b/config/locales/workbenches.fr.yml
@@ -1,9 +1,9 @@
fr:
workbenches:
referential_count:
- zero: "Aucun jeu de données à l'heure actuelle"
- one: "1 jeu de données à l'heure actuelle"
- other: "#{count} jeux de données à l'heure actuelle"
+ zero: "Aucun jeu de données dans cet espace de travail"
+ one: "1 jeu de données dans cet espace de travail"
+ other: "#{count} jeux de données dans cet espace de travail"
activerecord:
models:
workbench:
diff --git a/spec/factories/referentials.rb b/spec/factories/referentials.rb
index 5348ad96b..a4155d181 100644
--- a/spec/factories/referentials.rb
+++ b/spec/factories/referentials.rb
@@ -3,11 +3,17 @@ FactoryGirl.define do
sequence(:name) { |n| "Test #{n}" }
sequence(:slug) { |n| "test_#{n}" }
sequence(:prefix) { |n| "test_#{n}" }
- association :organisation
- association :workbench
association :line_referential
association :stop_area_referential
+ association :organisation
time_zone "Europe/Paris"
ready { true }
+
+ factory :workbench_referential do
+ association :workbench
+ before :create do | ref |
+ ref.workbench.organisation = ref.organisation
+ end
+ end
end
end
diff --git a/spec/features/workbenches_spec.rb b/spec/features/workbenches_spec.rb
index e33f8c134..f1151a67b 100644
--- a/spec/features/workbenches_spec.rb
+++ b/spec/features/workbenches_spec.rb
@@ -1,6 +1,4 @@
-# coding: utf-8
-
-describe 'Workbenches', type: :feature do
+RSpec.describe 'Workbenches', type: :feature do
login_user
let(:line_ref) { create :line_referential }
@@ -8,7 +6,7 @@ describe 'Workbenches', type: :feature do
let(:ref_metadata) { create(:referential_metadata, lines: [line]) }
let!(:workbench) { create(:workbench, line_referential: line_ref, organisation: @user.organisation) }
- let!(:referential) { create :referential, workbench: workbench, metadatas: [ref_metadata], organisation: @user.organisation }
+ let!(:referential) { create :workbench_referential, workbench: workbench, metadatas: [ref_metadata], organisation: @user.organisation }
describe 'show' do
context 'ready' do
@@ -25,10 +23,10 @@ describe 'Workbenches', type: :feature do
end
context 'filtering' do
- let(:another_organisation) { create :organisation }
+ let!(:another_organisation) { create :organisation }
let(:another_line) { create :line, line_referential: line_ref }
let(:another_ref_metadata) { create(:referential_metadata, lines: [another_line]) }
- let!(:other_referential) { create :referential, workbench: workbench, metadatas: [another_ref_metadata], organisation: another_organisation}
+ let!(:other_referential) { create :workbench_referential, workbench: workbench, metadatas: [another_ref_metadata] }
before(:each) do
visit workbench_path(workbench)
@@ -53,7 +51,7 @@ describe 'Workbenches', type: :feature do
it 'should be possible to filter by multiple organisation' do
find("#q_organisation_name_eq_any_#{@user.organisation.name.parameterize.underscore}").set(true)
- find("#q_organisation_name_eq_any_#{another_organisation.name.parameterize.underscore}").set(true)
+ find("#q_organisation_name_eq_any_#{other_referential.organisation.name.parameterize.underscore}").set(true)
click_button I18n.t('actions.filter')
expect(page).to have_content(referential.name)
diff --git a/spec/helpers/table_builder_helper_spec.rb b/spec/helpers/table_builder_helper_spec.rb
index e17196a19..4d9808044 100644
--- a/spec/helpers/table_builder_helper_spec.rb
+++ b/spec/helpers/table_builder_helper_spec.rb
@@ -7,7 +7,7 @@ end
describe TableBuilderHelper, type: :helper do
describe "#table_builder_2" do
it "builds a table" do
- referential = build_stubbed(:referential)
+ referential = build_stubbed(:workbench_referential)
workbench = referential.workbench
user_context = UserContext.new(
diff --git a/spec/models/referential/referential_org_through_workbench_spec.rb b/spec/models/referential/referential_org_through_workbench_spec.rb
new file mode 100644
index 000000000..358a9e5e6
--- /dev/null
+++ b/spec/models/referential/referential_org_through_workbench_spec.rb
@@ -0,0 +1,44 @@
+RSpec.describe Referential do
+
+ describe 'Normalisation between Workbench and Organisation' do
+
+ context 'no workbench' do
+ subject{ create( :referential, workbench: nil ) }
+ it do
+ expect_it.to be_valid
+ end
+ end
+ context 'workbench with same organisation' do
+ let( :workbench ){ create :workbench}
+ subject do
+ create( :referential,
+ workbench: workbench,
+ organisation: workbench.organisation )
+ end
+ it do
+ expect_it.to be_valid
+ end
+ end
+
+ context 'workbench with different organisation' do
+ let( :workbench ){ create :workbench}
+ subject do
+ build( :referential, workbench: workbench)
+ end
+ before do
+ subject.organisation = build_stubbed(:organisation)
+ end
+ it 'is not valid' do
+ expect_it.not_to be_valid
+ end
+ it 'has correct error message' do
+ subject.valid?
+ errors = subject.errors.messages[:inconsistent_organisation]
+ expect(errors.grep(%r<#{subject.organisation.name}>)).not_to be_empty
+ expect(errors.grep(%r<#{workbench.organisation.name}>)).not_to be_empty
+ end
+ end
+
+ end
+
+end
diff --git a/spec/models/referential_spec.rb b/spec/models/referential_spec.rb
index bb8fabb2e..ad9c43010 100644
--- a/spec/models/referential_spec.rb
+++ b/spec/models/referential_spec.rb
@@ -1,7 +1,7 @@
require 'spec_helper'
describe Referential, :type => :model do
- let(:ref) { create :referential, metadatas: [create(:referential_metadata)] }
+ let(:ref) { create :workbench_referential, metadatas: [create(:referential_metadata)] }
# it "create a rule_parameter_set" do
# referential = create(:referential)
diff --git a/spec/models/referential_suite/referential_suite_current_new_spec.rb b/spec/models/referential_suite/referential_suite_current_new_spec.rb
new file mode 100644
index 000000000..a910a12da
--- /dev/null
+++ b/spec/models/referential_suite/referential_suite_current_new_spec.rb
@@ -0,0 +1,64 @@
+RSpec.describe ReferentialSuite do
+
+ describe 'Normalisation of current and new towards a proper Referential' do
+
+ subject { create :referential_suite, new: nil, current: nil }
+
+ describe 'valid' do
+ context 'current and new nil' do
+ it do
+ expect_it.to be_valid
+ end
+ end
+
+ context 'current nil and new pointing correctly back' do
+ let( :child ){ create :referential, referential_suite: subject }
+ it do
+ subject.new_id = child.id
+ expect_it.to be_valid
+ end
+ end
+
+ context 'new nil and current pointing correctly back' do
+ let( :child ){ create :referential, referential_suite: subject }
+ it do
+ subject.current_id = child.id
+ expect_it.to be_valid
+ end
+ end
+
+ context 'new and current pointing correctly back' do
+ let( :child ){ create :referential, referential_suite: subject }
+ let( :sibbling ){ create :referential, referential_suite: subject }
+ it do
+ subject.current_id = child.id
+ subject.new_id = sibbling.id
+ expect_it.to be_valid
+ end
+ end
+ end
+
+ describe 'invalid' do
+ context 'current points to incorrect referential(not a child), new is nil' do
+ let( :current ){ create :referential }
+ it do
+ subject.current_id = current.id
+ expect_it.not_to be_valid
+ expect( subject.errors.messages[:inconsistent_new] ).to be_nil
+ expect( subject.errors.messages[:inconsistent_current].first ).to match(%r<#{current.name}>)
+ end
+ end
+ context 'current points to correct referential, new to incorrect referential(not a child)' do
+ let( :current ){ create :referential, referential_suite: subject }
+ let( :new ){ create :referential, referential_suite: create( :referential_suite ) }
+ it do
+ subject.current_id = current.id
+ subject.new_id = new.id
+ expect_it.not_to be_valid
+ expect( subject.errors.messages[:inconsistent_current] ).to be_nil
+ expect( subject.errors.messages[:inconsistent_new].first ).to match(%r<#{new.name}>)
+ end
+ end
+ end
+ end
+end
diff --git a/spec/models/referential_suite_spec.rb b/spec/models/referential_suite/referential_suite_spec.rb
index 771187b55..771187b55 100644
--- a/spec/models/referential_suite_spec.rb
+++ b/spec/models/referential_suite/referential_suite_spec.rb
diff --git a/spec/requests/api/v1/netex_import_spec.rb b/spec/requests/api/v1/netex_import_spec.rb
index b6728168e..a90e51e5b 100644
--- a/spec/requests/api/v1/netex_import_spec.rb
+++ b/spec/requests/api/v1/netex_import_spec.rb
@@ -2,7 +2,7 @@ RSpec.describe "NetexImport", type: :request do
describe 'POST netex_imports' do
- let( :referential ){ create :referential }
+ let( :referential ){ create :workbench_referential }
let( :workbench ){ referential.workbench }
let( :workbench_import ){ create :workbench_import }