aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorXinhui2017-06-19 17:25:23 +0200
committerXinhui2017-06-19 17:25:28 +0200
commit7b11f89ca24ceb4949c27baf279564034f0a5106 (patch)
tree994a64cacf475a381c42bad6efdcc026365984d9
parentaceeb812800c9429631f9c21d3f1b063ee634fef (diff)
downloadchouette-core-7b11f89ca24ceb4949c27baf279564034f0a5106.tar.bz2
JourneyPattern validate minimum stop_points size on update
Refs #3775
-rw-r--r--app/models/chouette/journey_pattern.rb3
-rw-r--r--app/models/concerns/default_attributes_support.rb4
-rw-r--r--spec/models/chouette/journey_pattern_spec.rb82
3 files changed, 83 insertions, 6 deletions
diff --git a/app/models/chouette/journey_pattern.rb b/app/models/chouette/journey_pattern.rb
index 34b0d9345..3c902946d 100644
--- a/app/models/chouette/journey_pattern.rb
+++ b/app/models/chouette/journey_pattern.rb
@@ -14,19 +14,18 @@ class Chouette::JourneyPattern < Chouette::TridentActiveRecord
validates_presence_of :route
validates_presence_of :name
+ validates :stop_points, length: { minimum: 2 }, on: :update
enum section_status: { todo: 0, completed: 1, control: 2 }
attr_accessor :control_checked
after_update :control_route_sections, :unless => "control_checked"
-
def self.state_update route, state
transaction do
state.each do |item|
item.delete('errors')
jp = find_by(objectid: item['object_id']) || state_create_instance(route, item)
next if item['deletable'] && jp.persisted? && jp.destroy
-
# Update attributes and stop_points associations
jp.update_attributes(state_permited_attributes(item))
jp.state_stop_points_update(item) if !jp.errors.any? && jp.persisted?
diff --git a/app/models/concerns/default_attributes_support.rb b/app/models/concerns/default_attributes_support.rb
index ecad26856..7928093e6 100644
--- a/app/models/concerns/default_attributes_support.rb
+++ b/app/models/concerns/default_attributes_support.rb
@@ -63,10 +63,10 @@ module DefaultAttributesSupport
def fix_uniq_objectid
base_objectid = objectid.rpartition(":").first
self.objectid = "#{base_objectid}:#{id}"
- if !valid?
+ if !valid?(:objectid)
base_objectid="#{objectid}_"
cnt=1
- while !valid?
+ while !valid?(:objectid)
self.objectid = "#{base_objectid}#{cnt}"
cnt += 1
end
diff --git a/spec/models/chouette/journey_pattern_spec.rb b/spec/models/chouette/journey_pattern_spec.rb
index 19b5060d2..f7006efc7 100644
--- a/spec/models/chouette/journey_pattern_spec.rb
+++ b/spec/models/chouette/journey_pattern_spec.rb
@@ -2,6 +2,25 @@ require 'spec_helper'
describe Chouette::JourneyPattern, :type => :model do
+ context 'validate minimum stop_points size' do
+ let(:journey_pattern) { create :journey_pattern }
+ let(:stop_points) { journey_pattern.stop_points }
+
+ it 'should be valid if it has at least two sp' do
+ journey_pattern.stop_points.first(stop_points.size - 2).each do |sp|
+ journey_pattern.stop_points.delete(sp)
+ end
+ expect(journey_pattern).to be_valid
+ end
+
+ it 'should not be valid if it has less then two sp' do
+ journey_pattern.stop_points.first(stop_points.size - 1).each do |sp|
+ journey_pattern.stop_points.delete(sp)
+ end
+ expect(journey_pattern).to_not be_valid
+ end
+ end
+
describe "state_update" do
def journey_pattern_to_state jp
jp.attributes.slice('name', 'published_name', 'registration_number').tap do |item|
@@ -24,12 +43,14 @@ describe Chouette::JourneyPattern, :type => :model do
end
it 'should attach checked stop_points' do
- state['stop_points'].each{|sp| sp['checked'] = true}
# Make sure journey_pattern has no stop_points
- journey_pattern.stop_points.delete_all
+ state['stop_points'].each{|sp| sp['checked'] = false}
+ journey_pattern.state_stop_points_update(state)
expect(journey_pattern.reload.stop_points).to be_empty
+ state['stop_points'].each{|sp| sp['checked'] = true}
journey_pattern.state_stop_points_update(state)
+
expect(journey_pattern.reload.stop_points.count).to eq(5)
end
@@ -89,6 +110,63 @@ describe Chouette::JourneyPattern, :type => :model do
expect(collection.first).to_not have_key('object_id')
end
+
+ it 'should create journey_pattern' do
+ new_state = journey_pattern_to_state(build(:journey_pattern, objectid: nil, route: route))
+ Chouette::JourneyPattern.state_create_instance route, new_state
+ expect(new_state['object_id']).to be_truthy
+ expect(new_state['new_record']).to be_truthy
+ end
+
+ it 'should delete journey_pattern' do
+ state['deletable'] = true
+ collection = [state]
+ expect {
+ Chouette::JourneyPattern.state_update route, collection
+ }.to change{Chouette::JourneyPattern.count}.from(1).to(0)
+
+ expect(collection).to be_empty
+ end
+
+ it 'should delete multiple journey_pattern' do
+ collection = 5.times.collect{journey_pattern_to_state(create(:journey_pattern, route: route))}
+ collection.map{|i| i['deletable'] = true}
+
+ expect {
+ Chouette::JourneyPattern.state_update route, collection
+ }.to change{Chouette::JourneyPattern.count}.from(5).to(0)
+ end
+
+ it 'should validate journey_pattern on update' do
+ journey_pattern.name = ''
+ collection = [state]
+ Chouette::JourneyPattern.state_update route, collection
+ expect(collection.first['errors']).to have_key(:name)
+ end
+
+ it 'should validate journey_pattern on create' do
+ new_state = journey_pattern_to_state(build(:journey_pattern, name: '', objectid: nil, route: route))
+ collection = [new_state]
+ expect {
+ Chouette::JourneyPattern.state_update route, collection
+ }.to_not change{Chouette::JourneyPattern.count}
+
+ expect(collection.first['errors']).to have_key(:name)
+ expect(collection.first).to_not have_key('object_id')
+ end
+
+ it 'should not save any journey_pattern of collection if one is invalid' do
+ journey_pattern.name = ''
+ valid_state = journey_pattern_to_state(build(:journey_pattern, objectid: nil, route: route))
+ invalid_state = journey_pattern_to_state(journey_pattern)
+ collection = [valid_state, invalid_state]
+
+ expect {
+ Chouette::JourneyPattern.state_update route, collection
+ }.to_not change{Chouette::JourneyPattern.count}
+
+ expect(collection.first).to_not have_key('object_id')
+ end
end
describe "#stop_point_ids" do