ninoxe 1.1.4 → 1.1.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (74) hide show
  1. checksums.yaml +5 -13
  2. data/app/models/chouette/footnote.rb +7 -0
  3. data/app/models/chouette/line.rb +6 -1
  4. data/app/models/chouette/vehicle_journey.rb +7 -4
  5. data/db/migrate/20150115153453_create_footnotes.rb +11 -0
  6. data/db/migrate/20150119160029_create_vehicle_journey_footnotes.rb +8 -0
  7. data/lib/factories/chouette_footnotes.rb +6 -0
  8. data/lib/ninoxe/version.rb +1 -1
  9. data/spec/dummy/README.rdoc +261 -0
  10. data/spec/dummy/Rakefile +6 -0
  11. data/spec/dummy/app/assets/javascripts/application.js +15 -0
  12. data/spec/dummy/app/assets/stylesheets/application.css +13 -0
  13. data/spec/dummy/app/controllers/application_controller.rb +3 -0
  14. data/spec/dummy/app/helpers/application_helper.rb +2 -0
  15. data/spec/dummy/app/mailers/.gitkeep +0 -0
  16. data/spec/dummy/app/models/.gitkeep +0 -0
  17. data/spec/dummy/app/views/layouts/application.html.erb +14 -0
  18. data/spec/dummy/config/application.rb +56 -0
  19. data/spec/dummy/config/boot.rb +10 -0
  20. data/{config/database.yml.me → spec/dummy/config/database.yml} +2 -2
  21. data/spec/dummy/config/environment.rb +7 -0
  22. data/spec/dummy/config/environments/development.rb +37 -0
  23. data/spec/dummy/config/environments/production.rb +67 -0
  24. data/spec/dummy/config/environments/test.rb +37 -0
  25. data/spec/dummy/config/initializers/active_record.rb +2 -0
  26. data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
  27. data/spec/dummy/config/initializers/inflections.rb +15 -0
  28. data/spec/dummy/config/initializers/mime_types.rb +5 -0
  29. data/spec/dummy/config/initializers/secret_token.rb +7 -0
  30. data/spec/dummy/config/initializers/session_store.rb +8 -0
  31. data/spec/dummy/config/initializers/wrap_parameters.rb +14 -0
  32. data/spec/dummy/config/locales/en.yml +5 -0
  33. data/spec/dummy/config/routes.rb +4 -0
  34. data/spec/dummy/config.ru +4 -0
  35. data/spec/dummy/db/schema.rb +485 -0
  36. data/spec/dummy/db/test.sqlite3 +0 -0
  37. data/spec/dummy/lib/assets/.gitkeep +0 -0
  38. data/spec/dummy/log/.gitkeep +0 -0
  39. data/spec/dummy/public/404.html +26 -0
  40. data/spec/dummy/public/422.html +26 -0
  41. data/spec/dummy/public/500.html +25 -0
  42. data/spec/dummy/public/favicon.ico +0 -0
  43. data/spec/dummy/script/rails +6 -0
  44. data/spec/models/chouette/access_link_spec.rb +78 -0
  45. data/spec/models/chouette/access_point_spec.rb +266 -0
  46. data/spec/models/chouette/active_record_spec.rb +121 -0
  47. data/spec/models/chouette/area_type_spec.rb +53 -0
  48. data/spec/models/chouette/company_spec.rb +51 -0
  49. data/spec/models/chouette/connection_link_spec.rb +56 -0
  50. data/spec/models/chouette/direction_spec.rb +60 -0
  51. data/spec/models/chouette/exporter_spec.rb +28 -0
  52. data/spec/models/chouette/file_validator_spec.rb +28 -0
  53. data/spec/models/chouette/footnote_spec.rb +9 -0
  54. data/spec/models/chouette/group_of_line_spec.rb +31 -0
  55. data/spec/models/chouette/journey_pattern_spec.rb +62 -0
  56. data/spec/models/chouette/line_spec.rb +119 -0
  57. data/spec/models/chouette/loader_spec.rb +69 -0
  58. data/spec/models/chouette/network_spec.rb +22 -0
  59. data/spec/models/chouette/object_id_spec.rb +146 -0
  60. data/spec/models/chouette/route_spec.rb +234 -0
  61. data/spec/models/chouette/stop_area_spec.rb +440 -0
  62. data/spec/models/chouette/stop_point_spec.rb +38 -0
  63. data/spec/models/chouette/time_table_period_spec.rb +66 -0
  64. data/spec/models/chouette/time_table_spec.rb +1218 -0
  65. data/spec/models/chouette/transport_mode_spec.rb +64 -0
  66. data/spec/models/chouette/trident_active_record_spec.rb +115 -0
  67. data/spec/models/chouette/vehicle_journey_at_stop_spec.rb +46 -0
  68. data/spec/models/chouette/vehicle_journey_spec.rb +223 -0
  69. data/spec/presenters/chouette/geometry/general_presenter.rb +1 -0
  70. data/spec/presenters/chouette/geometry/line_presenter_spec.rb +13 -0
  71. data/spec/spec_helper.rb +45 -0
  72. metadata +160 -33
  73. data/app/models/chouette/time_table_vehicle_journey.rb +0 -5
  74. data/lib/ninoxe.rb~ +0 -7
@@ -0,0 +1,234 @@
1
+ require 'spec_helper'
2
+
3
+ describe Chouette::Route do
4
+ subject { Factory(:route) }
5
+
6
+ it { should validate_uniqueness_of :objectid }
7
+ its(:objectid) { should be_kind_of(Chouette::ObjectId) }
8
+
9
+ it { should validate_presence_of :name }
10
+ it { should validate_presence_of :line }
11
+ it { should validate_presence_of :wayback_code }
12
+ it { should validate_presence_of :direction_code }
13
+
14
+ context "reordering methods" do
15
+ let( :bad_stop_point_ids){subject.stop_points.map { |sp| sp.id + 1}}
16
+ let( :ident){subject.stop_points.map(&:id)}
17
+ let( :first_last_swap){ [ident.last] + ident[1..-2] + [ident.first]}
18
+
19
+ describe "#reorder!" do
20
+ context "invalid stop_point_ids" do
21
+ let( :new_stop_point_ids) { bad_stop_point_ids}
22
+ it { subject.reorder!( new_stop_point_ids).should be_false}
23
+ end
24
+
25
+ context "swaped last and first stop_point_ids" do
26
+ let!( :new_stop_point_ids) { first_last_swap}
27
+ let!( :old_stop_point_ids) { subject.stop_points.map(&:id) }
28
+ let!( :old_stop_area_ids) { subject.stop_areas.map(&:id) }
29
+
30
+ it "should keep stop_point_ids order unchanged" do
31
+ subject.reorder!( new_stop_point_ids).should be_true
32
+ subject.stop_points.map(&:id).should eq( old_stop_point_ids)
33
+ end
34
+ it "should have changed stop_area_ids order" do
35
+ subject.reorder!( new_stop_point_ids).should be_true
36
+ subject.reload
37
+ subject.stop_areas.map(&:id).should eq( [old_stop_area_ids.last] + old_stop_area_ids[1..-2] + [old_stop_area_ids.first])
38
+ end
39
+ end
40
+ end
41
+
42
+ describe "#stop_point_permutation?" do
43
+ context "invalid stop_point_ids" do
44
+ let( :new_stop_point_ids) { bad_stop_point_ids}
45
+ it { should_not be_stop_point_permutation( new_stop_point_ids)}
46
+ end
47
+ context "unchanged stop_point_ids" do
48
+ let( :new_stop_point_ids) { ident}
49
+ it { should be_stop_point_permutation( new_stop_point_ids)}
50
+ end
51
+ context "swaped last and first stop_point_ids" do
52
+ let( :new_stop_point_ids) { first_last_swap}
53
+ it { should be_stop_point_permutation( new_stop_point_ids)}
54
+ end
55
+ end
56
+ end
57
+
58
+ describe "#stop_points_attributes=" do
59
+ let( :journey_pattern) { Factory( :journey_pattern, :route => subject )}
60
+ let( :vehicle_journey) { Factory( :vehicle_journey, :journey_pattern => journey_pattern)}
61
+ def subject_stop_points_attributes
62
+ {}.tap do |hash|
63
+ subject.stop_points.each_with_index { |sp,index| hash[ index.to_s ] = sp.attributes }
64
+ end
65
+ end
66
+ context "route having swapped a new stop" do
67
+ let( :new_stop_point ){Factory.build( :stop_point, :route => subject)}
68
+ def added_stop_hash
69
+ subject_stop_points_attributes.tap do |h|
70
+ h["4"] = new_stop_point.attributes.merge( "position" => "4", "_destroy" => "" )
71
+ end
72
+ end
73
+ let!( :new_route_size ){ subject.stop_points.size+1 }
74
+
75
+ it "should have added stop_point in route" do
76
+ subject.update_attributes( :stop_points_attributes => added_stop_hash)
77
+ Chouette::Route.find( subject.id ).stop_points.size.should == new_route_size
78
+ end
79
+ it "should have added stop_point in route's journey pattern" do
80
+ subject.update_attributes( :stop_points_attributes => added_stop_hash)
81
+ Chouette::JourneyPattern.find( journey_pattern.id ).stop_points.size.should == new_route_size
82
+ end
83
+ it "should have added stop_point in route's vehicle journey at stop" do
84
+ subject.update_attributes( :stop_points_attributes => added_stop_hash)
85
+ Chouette::VehicleJourney.find( vehicle_journey.id ).vehicle_journey_at_stops.size.should == new_route_size
86
+ end
87
+ end
88
+ context "route having swapped stop" do
89
+ def swapped_stop_hash
90
+ subject_stop_points_attributes.tap do |h|
91
+ h[ "1" ][ "position" ] = "3"
92
+ h[ "3" ][ "position" ] = "1"
93
+ end
94
+ end
95
+ let!( :new_stop_id_list ){ subject.stop_points.map(&:id).tap {|array| array.insert( 1, array.delete_at(3)); array.insert( 3, array.delete_at(2) )}.join(",") }
96
+
97
+ it "should have swap stop_points from route" do
98
+ subject.update_attributes( :stop_points_attributes => swapped_stop_hash)
99
+ Chouette::Route.find( subject.id ).stop_points.map(&:id).join(",").should == new_stop_id_list
100
+ end
101
+ it "should have swap stop_points from route's journey pattern" do
102
+ subject.update_attributes( :stop_points_attributes => swapped_stop_hash)
103
+ Chouette::JourneyPattern.find( journey_pattern.id ).stop_points.map(&:id).join(",").should == new_stop_id_list
104
+ end
105
+ it "should have swap stop_points from route's vehicle journey at stop" do
106
+ subject.update_attributes( :stop_points_attributes => swapped_stop_hash)
107
+ Chouette::VehicleJourney.find( vehicle_journey.id ).vehicle_journey_at_stops.map(&:stop_point_id).join(",").should == new_stop_id_list
108
+ end
109
+ end
110
+ context "route having a deleted stop" do
111
+ def removed_stop_hash
112
+ subject_stop_points_attributes.tap do |h|
113
+ h[ "1" ][ "_destroy" ] = "1"
114
+ end
115
+ end
116
+ let!( :new_stop_id_list ){ subject.stop_points.map(&:id).tap {|array| array.delete_at(1) }.join(",") }
117
+
118
+ it "should ignore deleted stop_point from route" do
119
+ subject.update_attributes( :stop_points_attributes => removed_stop_hash)
120
+ Chouette::Route.find( subject.id ).stop_points.map(&:id).join(",").should == new_stop_id_list
121
+ end
122
+ it "should ignore deleted stop_point from route's journey pattern" do
123
+ subject.update_attributes( :stop_points_attributes => removed_stop_hash)
124
+ Chouette::JourneyPattern.find( journey_pattern.id ).stop_points.map(&:id).join(",").should == new_stop_id_list
125
+ end
126
+ it "should ignore deleted stop_point from route's vehicle journey at stop" do
127
+ subject.update_attributes( :stop_points_attributes => removed_stop_hash)
128
+ Chouette::VehicleJourney.find( vehicle_journey.id ).vehicle_journey_at_stops.map(&:stop_point_id).join(",").should == new_stop_id_list
129
+ end
130
+ end
131
+ end
132
+
133
+ describe "#stop_points" do
134
+ context "#find_by_stop_area" do
135
+ context "when arg is first quay id" do
136
+ let(:first_stop_point) { subject.stop_points.first}
137
+ it "should return first quay" do
138
+ subject.stop_points.find_by_stop_area( first_stop_point.stop_area_id).should eq( first_stop_point)
139
+ end
140
+ end
141
+ end
142
+ end
143
+ describe "#stop_areas" do
144
+ let(:line){ Factory(:line)}
145
+ let(:route_1){ Factory(:route, :line => line)}
146
+ let(:route_2){ Factory(:route, :line => line)}
147
+ it "should retreive all stop_area on route" do
148
+ route_1.stop_areas.each do |sa|
149
+ sa.stop_points.map(&:route_id).uniq.should == [route_1.id]
150
+ end
151
+ end
152
+
153
+ context "when route is looping: last and first stop area are the same" do
154
+ it "should retreive same stop_area one last and first position" do
155
+ route_loop = Factory(:route, :line => line)
156
+ first_stop = Chouette::StopPoint.where( :route_id => route_loop.id, :position => 0).first
157
+ last_stop = Factory(:stop_point, :route => route_loop, :position => 5, :stop_area => first_stop.stop_area)
158
+
159
+ route_loop.stop_areas.size.should == 6
160
+ route_loop.stop_areas.select {|s| s.id == first_stop.stop_area.id}.size.should == 2
161
+ end
162
+ end
163
+ end
164
+
165
+ describe "#direction_code" do
166
+ def self.legacy_directions
167
+ %w{A R ClockWise CounterClockWise North NorthWest West SouthWest
168
+ South SouthEast East NorthEast}
169
+ end
170
+ legacy_directions.each do |direction|
171
+ context "when direction is #{direction}" do
172
+ direction_code = Chouette::Direction.new( Chouette::Route.direction_binding[ direction])
173
+ it "should be #{direction_code}" do
174
+ subject.direction = direction
175
+ subject.direction_code.should == direction_code
176
+ end
177
+ end
178
+ end
179
+ context "when direction is nil" do
180
+ it "should be nil" do
181
+ subject.direction = nil
182
+ subject.direction_code.should be_nil
183
+ end
184
+ end
185
+ end
186
+ describe "#direction_code=" do
187
+ context "when unknown direction is provided" do
188
+ it "should change direction to nil" do
189
+ subject.direction_code = "dummy"
190
+ subject.direction.should be_nil
191
+ end
192
+ end
193
+ context "when an existing direction (west) is provided" do
194
+ it "should change direction Direction.west" do
195
+ subject.direction_code = "west"
196
+ subject.direction.should == "West"
197
+ end
198
+ end
199
+ end
200
+ describe "#wayback_code" do
201
+ def self.legacy_waybacks
202
+ %w{A R}
203
+ end
204
+ legacy_waybacks.each do |wayback|
205
+ context "when wayback is #{wayback}" do
206
+ wayback_code = Chouette::Wayback.new( Chouette::Route.wayback_binding[ wayback])
207
+ it "should be #{wayback_code}" do
208
+ subject.wayback = wayback
209
+ subject.wayback_code.should == wayback_code
210
+ end
211
+ end
212
+ end
213
+ context "when wayback is nil" do
214
+ it "should be nil" do
215
+ subject.wayback = nil
216
+ subject.wayback_code.should be_nil
217
+ end
218
+ end
219
+ end
220
+ describe "#wayback_code=" do
221
+ context "when unknown wayback is provided" do
222
+ it "should change wayback to nil" do
223
+ subject.wayback_code = "dummy"
224
+ subject.wayback.should be_nil
225
+ end
226
+ end
227
+ context "when an existing wayback (straight_forward) is provided" do
228
+ it "should change wayback Wayback.straight_forward" do
229
+ subject.wayback_code = "straight_forward"
230
+ subject.wayback.should == "A"
231
+ end
232
+ end
233
+ end
234
+ end
@@ -0,0 +1,440 @@
1
+ require 'spec_helper'
2
+
3
+ describe Chouette::StopArea do
4
+ let!(:quay) { Factory :stop_area, :area_type => "Quay" }
5
+ let!(:boarding_position) { Factory :stop_area, :area_type => "BoardingPosition" }
6
+ let!(:commercial_stop_point) { Factory :stop_area, :area_type => "CommercialStopPoint" }
7
+ let!(:stop_place) { Factory :stop_area, :area_type => "StopPlace" }
8
+ let!(:itl) { Factory :stop_area, :area_type => "ITL" }
9
+
10
+ its(:objectid) { should be_kind_of(Chouette::ObjectId) }
11
+
12
+ it { should validate_presence_of :name }
13
+ it { should validate_presence_of :area_type }
14
+ it { should validate_numericality_of :latitude }
15
+ it { should validate_numericality_of :longitude }
16
+
17
+
18
+ describe ".latitude" do
19
+ it "should accept -90 value" do
20
+ subject = Factory :stop_area, :area_type => "BoardingPosition"
21
+ subject.latitude = -90
22
+ subject.valid?.should be_true
23
+ end
24
+ it "should reject < -90 value" do
25
+ subject = Factory :stop_area, :area_type => "BoardingPosition"
26
+ subject.latitude = -90.0001
27
+ subject.valid?.should be_false
28
+ end
29
+ it "should accept 90 value" do
30
+ subject = Factory :stop_area, :area_type => "BoardingPosition"
31
+ subject.latitude = 90
32
+ subject.valid?.should be_true
33
+ end
34
+ it "should reject > 90 value" do
35
+ subject = Factory :stop_area, :area_type => "BoardingPosition"
36
+ subject.latitude = 90.0001
37
+ subject.valid?.should be_false
38
+ end
39
+ end
40
+
41
+ describe ".longitude" do
42
+ it "should accept -180 value" do
43
+ subject = Factory :stop_area, :area_type => "BoardingPosition"
44
+ subject.longitude = -180
45
+ subject.valid?.should be_true
46
+ end
47
+ it "should reject < -180 value" do
48
+ subject = Factory :stop_area, :area_type => "BoardingPosition"
49
+ subject.longitude = -180.0001
50
+ subject.valid?.should be_false
51
+ end
52
+ it "should accept 180 value" do
53
+ subject = Factory :stop_area, :area_type => "BoardingPosition"
54
+ subject.longitude = 180
55
+ subject.valid?.should be_true
56
+ end
57
+ it "should reject > 180 value" do
58
+ subject = Factory :stop_area, :area_type => "BoardingPosition"
59
+ subject.longitude = 180.0001
60
+ subject.valid?.should be_false
61
+ end
62
+ end
63
+
64
+ describe ".long_lat" do
65
+ it "should accept longitude and latitude both as nil" do
66
+ subject = Factory :stop_area, :area_type => "BoardingPosition"
67
+ subject.longitude = nil
68
+ subject.latitude = nil
69
+ subject.valid?.should be_true
70
+ end
71
+ it "should accept longitude and latitude both numerical" do
72
+ subject = Factory :stop_area, :area_type => "BoardingPosition"
73
+ subject.longitude = 10
74
+ subject.latitude = 10
75
+ subject.valid?.should be_true
76
+ end
77
+ it "should reject longitude nil with latitude numerical" do
78
+ subject = Factory :stop_area, :area_type => "BoardingPosition"
79
+ subject.longitude = nil
80
+ subject.latitude = 10
81
+ subject.valid?.should be_false
82
+ end
83
+ it "should reject longitude numerical with latitude nil" do
84
+ subject = Factory :stop_area, :area_type => "BoardingPosition"
85
+ subject.longitude = 10
86
+ subject.latitude = nil
87
+ subject.valid?.should be_false
88
+ end
89
+ end
90
+
91
+
92
+ describe ".children_in_depth" do
93
+ it "should return all the deepest children from stop area" do
94
+ subject = Factory :stop_area, :area_type => "StopPlace"
95
+ commercial_stop_point = Factory :stop_area, :area_type => "CommercialStopPoint", :parent => subject
96
+ commercial_stop_point2 = Factory :stop_area, :area_type => "CommercialStopPoint", :parent => commercial_stop_point
97
+ quay = Factory :stop_area, :parent => commercial_stop_point, :area_type => "Quay"
98
+ subject.children_in_depth.should =~ [commercial_stop_point, commercial_stop_point2, quay]
99
+ end
100
+ it "should return only the deepest children from stop area" do
101
+ subject = Factory :stop_area, :area_type => "StopPlace"
102
+ commercial_stop_point = Factory :stop_area, :area_type => "CommercialStopPoint", :parent => subject
103
+ commercial_stop_point2 = Factory :stop_area, :area_type => "CommercialStopPoint", :parent => commercial_stop_point
104
+ quay = Factory :stop_area, :parent => commercial_stop_point, :area_type => "Quay"
105
+ subject.children_at_base.should =~ [quay]
106
+ end
107
+ end
108
+
109
+ describe ".stop_area_type" do
110
+ it "should have area_type of BoardingPosition when stop_area_type is set to boarding_position" do
111
+ subject = Factory :stop_area, :stop_area_type => "boarding_position"
112
+ subject.area_type.should == "BoardingPosition"
113
+ end
114
+ it "should have area_type of Quay when stop_area_type is set to quay" do
115
+ subject = Factory :stop_area, :stop_area_type => "quay"
116
+ subject.area_type.should == "Quay"
117
+ end
118
+ it "should have area_type of CommercialStopPoint when stop_area_type is set to commercial_stop_point" do
119
+ subject = Factory :stop_area, :stop_area_type => "commercial_stop_point"
120
+ subject.area_type.should == "CommercialStopPoint"
121
+ end
122
+ it "should have area_type of StopPlace when stop_area_type is set to stop_place" do
123
+ subject = Factory :stop_area, :stop_area_type => "stop_place"
124
+ subject.area_type.should == "StopPlace"
125
+ end
126
+ it "should have area_type of ITL when stop_area_type is set to itl" do
127
+ subject = Factory :stop_area, :stop_area_type => "itl"
128
+ subject.area_type.should == "ITL"
129
+ end
130
+ end
131
+
132
+ describe ".parent" do
133
+ it "should check if parent method exists" do
134
+ subject = Factory :stop_area, :parent_id => commercial_stop_point.id
135
+ subject.parent.should == commercial_stop_point
136
+ end
137
+ end
138
+
139
+ describe ".possible_children" do
140
+
141
+ it "should find no possible descendant for stop area type quay" do
142
+ subject = Factory :stop_area, :area_type => "Quay"
143
+ subject.possible_children.should == []
144
+ end
145
+
146
+ it "should find no possible descendant for stop area type boarding position" do
147
+ subject = Factory :stop_area, :area_type => "BoardingPosition"
148
+ subject.possible_children.should == []
149
+ end
150
+
151
+ it "should find descendant of type quay or boarding position for stop area type commercial stop point" do
152
+ subject = Factory :stop_area, :area_type => "CommercialStopPoint"
153
+ subject.possible_children.should =~ [quay, boarding_position]
154
+ end
155
+
156
+ it "should find no children of type stop place or commercial stop point for stop area type stop place" do
157
+ subject = Factory :stop_area, :area_type => "StopPlace"
158
+ subject.possible_children.should =~ [stop_place, commercial_stop_point]
159
+ end
160
+
161
+ it "should find no children of type ITL for stop area type ITL" do
162
+ subject = Factory :stop_area, :area_type => "ITL"
163
+ subject.possible_children.should =~ [stop_place, commercial_stop_point, quay, boarding_position]
164
+ end
165
+
166
+ end
167
+
168
+ describe ".possible_parents" do
169
+
170
+ it "should find parent type commercial stop point for stop area type boarding position" do
171
+ subject = Factory :stop_area, :area_type => "BoardingPosition"
172
+ subject.possible_parents.should == [commercial_stop_point]
173
+ end
174
+
175
+ it "should find parent type commercial stop point for stop area type quay" do
176
+ subject = Factory :stop_area, :area_type => "Quay"
177
+ subject.possible_parents.should == [commercial_stop_point]
178
+ end
179
+
180
+ it "should find parent type stop place for stop area type commercial stop point" do
181
+ subject = Factory :stop_area, :area_type => "CommercialStopPoint"
182
+ subject.possible_parents.should == [stop_place]
183
+ end
184
+
185
+ it "should find parent type stop place for stop area type stop place" do
186
+ subject = Factory :stop_area, :area_type => "StopPlace"
187
+ subject.possible_parents.should == [stop_place]
188
+ end
189
+
190
+ end
191
+
192
+
193
+ describe ".near" do
194
+
195
+ let(:stop_area) { Factory :stop_area, :latitude => 1, :longitude => 1 }
196
+ let(:stop_area2) { Factory :stop_area, :latitude => 1, :longitude => 1 }
197
+
198
+ it "should find a StopArea at 300m from given origin" do
199
+ Chouette::StopArea.near(stop_area.to_lat_lng.endpoint(0, 0.250, :units => :kms)).should == [stop_area]
200
+ end
201
+
202
+ it "should not find a StopArea at more than 300m from given origin" do
203
+ Chouette::StopArea.near(stop_area2.to_lat_lng.endpoint(0, 0.350, :units => :kms)).should be_empty
204
+ end
205
+
206
+ end
207
+
208
+ describe "#to_lat_lng" do
209
+
210
+ it "should return nil if latitude is nil" do
211
+ subject.latitude = nil
212
+ subject.to_lat_lng.should be_nil
213
+ end
214
+
215
+ it "should return nil if longitude is nil" do
216
+ subject.longitude = nil
217
+ subject.to_lat_lng.should be_nil
218
+ end
219
+
220
+ end
221
+
222
+ describe "#geometry" do
223
+
224
+ it "should be nil when to_lat_lng is nil" do
225
+ subject.stub :to_lat_lng => nil
226
+ subject.geometry.should be_nil
227
+ end
228
+
229
+ end
230
+
231
+ describe ".bounds" do
232
+
233
+ it "should return transform coordinates in floats" do
234
+ Chouette::StopArea.connection.stub :select_rows => [["113.5292500000000000", "22.1127580000000000", "113.5819330000000000", "22.2157050000000000"]]
235
+ GeoRuby::SimpleFeatures::Envelope.should_receive(:from_coordinates).with([[113.5292500000000000, 22.1127580000000000], [113.5819330000000000, 22.2157050000000000]])
236
+ Chouette::StopArea.bounds
237
+ end
238
+
239
+ end
240
+
241
+ describe "#default_position" do
242
+
243
+ it "should return nil when StopArea.bounds is nil" do
244
+ Chouette::StopArea.stub :bounds => nil
245
+ subject.default_position.should be_nil
246
+ end
247
+
248
+ it "should return StopArea.bounds center" do
249
+ Chouette::StopArea.stub :bounds => double(:center => "center")
250
+ subject.default_position.should == Chouette::StopArea.bounds.center
251
+ end
252
+
253
+ end
254
+
255
+ describe "#children_at_base" do
256
+ it "should have 2 children_at_base" do
257
+ subject = Factory :stop_area, :area_type => "StopPlace"
258
+ commercial_stop_point = Factory :stop_area, :area_type => "CommercialStopPoint" ,:parent => subject
259
+ quay1 = Factory :stop_area, :parent => commercial_stop_point, :area_type => "Quay"
260
+ quay2 = Factory :stop_area, :parent => commercial_stop_point, :area_type => "Quay"
261
+ subject.children_at_base.size.should == 2
262
+ end
263
+ end
264
+
265
+
266
+ describe "#generic_access_link_matrix" do
267
+ it "should have no access_links in matrix with no access_point" do
268
+ subject = Factory :stop_area, :area_type => "StopPlace"
269
+ commercial_stop_point = Factory :stop_area, :area_type => "CommercialStopPoint" ,:parent => subject
270
+ subject.generic_access_link_matrix.size.should == 0
271
+ end
272
+ it "should have 4 generic_access_links in matrix with 2 access_points" do
273
+ subject = Factory :stop_area, :area_type => "StopPlace"
274
+ commercial_stop_point = Factory :stop_area, :area_type => "CommercialStopPoint" ,:parent => subject
275
+ access_point1 = Factory :access_point, :stop_area => subject
276
+ access_point2 = Factory :access_point, :stop_area => subject
277
+ subject.generic_access_link_matrix.size.should == 4
278
+ end
279
+ end
280
+ describe "#detail_access_link_matrix" do
281
+ it "should have no access_links in matrix with no access_point" do
282
+ subject = Factory :stop_area, :area_type => "StopPlace"
283
+ commercial_stop_point = Factory :stop_area, :area_type => "CommercialStopPoint" ,:parent => subject
284
+ quay1 = Factory :stop_area, :parent => commercial_stop_point, :area_type => "Quay"
285
+ quay2 = Factory :stop_area, :parent => commercial_stop_point, :area_type => "Quay"
286
+ subject.detail_access_link_matrix.size.should == 0
287
+ end
288
+ it "should have 8 detail_access_links in matrix with 2 children_at_base and 2 access_points" do
289
+ subject = Factory :stop_area, :area_type => "StopPlace"
290
+ commercial_stop_point = Factory :stop_area, :area_type => "CommercialStopPoint" ,:parent => subject
291
+ quay1 = Factory :stop_area, :parent => commercial_stop_point, :area_type => "Quay"
292
+ quay2 = Factory :stop_area, :parent => commercial_stop_point, :area_type => "Quay"
293
+ access_point1 = Factory :access_point, :stop_area => subject
294
+ access_point2 = Factory :access_point, :stop_area => subject
295
+ subject.detail_access_link_matrix.size.should == 8
296
+ end
297
+ end
298
+ describe "#parents" do
299
+ it "should return parent hireachy list" do
300
+ stop_place = Factory :stop_area, :area_type => "StopPlace"
301
+ commercial_stop_point = Factory :stop_area, :area_type => "CommercialStopPoint" ,:parent => stop_place
302
+ subject = Factory :stop_area, :parent => commercial_stop_point, :area_type => "Quay"
303
+ subject.parents.size.should == 2
304
+ end
305
+ it "should return empty parent hireachy list" do
306
+ subject = Factory :stop_area, :area_type => "Quay"
307
+ subject.parents.size.should == 0
308
+ end
309
+ end
310
+
311
+ describe "#clean_invalid_access_links" do
312
+ it "should remove invalid access links" do
313
+ # subject is a CSP with a SP as parent, a quay as child
314
+ # 2 access_points of SP have access_link, one on subject, one on subject child
315
+ # when detaching subject from SP, both access_links must be deleted
316
+ stop_place = Factory :stop_area, :area_type => "StopPlace"
317
+ subject = Factory :stop_area, :area_type => "CommercialStopPoint" ,:parent => stop_place
318
+ access_point1 = Factory :access_point, :stop_area => stop_place
319
+ access_point2 = Factory :access_point, :stop_area => stop_place
320
+ quay = Factory :stop_area, :parent => subject, :area_type => "Quay"
321
+ access_link1 = Factory :access_link, :stop_area => subject, :access_point => access_point1
322
+ access_link2 = Factory :access_link, :stop_area => quay, :access_point => access_point2
323
+ subject.save
324
+ subject.access_links.size.should == 1
325
+ quay.access_links.size.should == 1
326
+ subject.parent=nil
327
+ subject.save
328
+ subject.reload
329
+ subject.access_links.size.should == 0
330
+ quay.access_links.size.should == 0
331
+ end
332
+ it "should not remove still valid access links" do
333
+ # subject is a Q of CSP with a SP as parent
334
+ # 2 access_points, one of SP, one of CSP have access_link on subject
335
+ # when changing subject CSP to another CSP of same SP
336
+ # one access_links must be kept
337
+ stop_place = Factory :stop_area, :area_type => "StopPlace"
338
+ commercial_stop_point1 = Factory :stop_area, :area_type => "CommercialStopPoint" ,:parent => stop_place
339
+ commercial_stop_point2 = Factory :stop_area, :area_type => "CommercialStopPoint" ,:parent => stop_place
340
+ access_point1 = Factory :access_point, :stop_area => stop_place
341
+ access_point2 = Factory :access_point, :stop_area => commercial_stop_point1
342
+ subject = Factory :stop_area, :parent => commercial_stop_point1, :area_type => "Quay"
343
+ access_link1 = Factory :access_link, :stop_area => subject, :access_point => access_point1
344
+ access_link2 = Factory :access_link, :stop_area => subject, :access_point => access_point2
345
+ subject.save
346
+ subject.access_links.size.should == 2
347
+ subject.parent=commercial_stop_point2
348
+ subject.save
349
+ subject.reload
350
+ subject.access_links.size.should == 1
351
+ end
352
+ end
353
+
354
+ describe "#coordinates" do
355
+ it "should convert coordinates into latitude/longitude" do
356
+ subject = Factory :stop_area, :area_type => "BoardingPosition", :coordinates => "45.123,120.456"
357
+ subject.longitude.should be_within(0.001).of(120.456)
358
+ subject.latitude.should be_within(0.001).of(45.123)
359
+ end
360
+ it "should set empty coordinates into nil latitude/longitude" do
361
+ subject = Factory :stop_area, :area_type => "BoardingPosition", :coordinates => "45.123,120.456"
362
+ subject.longitude.should be_within(0.001).of(120.456)
363
+ subject.latitude.should be_within(0.001).of(45.123)
364
+ subject.coordinates = ""
365
+ subject.save
366
+ subject.longitude.should be_nil
367
+ subject.latitude.should be_nil
368
+ end
369
+ it "should convert latitude/longitude into coordinates" do
370
+ subject = Factory :stop_area, :area_type => "BoardingPosition", :longitude => 120.456, :latitude => 45.123
371
+ subject.coordinates.should == "45.123,120.456"
372
+ end
373
+ it "should convert nil latitude/longitude into empty coordinates" do
374
+ subject = Factory :stop_area, :area_type => "BoardingPosition", :longitude => nil, :latitude => nil
375
+ subject.coordinates.should == ""
376
+ end
377
+ it "should accept valid coordinates" do
378
+ subject = Factory :stop_area, :area_type => "BoardingPosition", :coordinates => "45.123,120.456"
379
+ subject.valid?.should be_true
380
+ subject.coordinates = "45.123, 120.456"
381
+ subject.valid?.should be_true
382
+ subject.longitude.should be_within(0.001).of(120.456)
383
+ subject.latitude.should be_within(0.001).of(45.123)
384
+ subject.coordinates = "45.123, -120.456"
385
+ subject.valid?.should be_true
386
+ subject.coordinates = "45.123 ,120.456"
387
+ subject.valid?.should be_true
388
+ subject.coordinates = "45.123 , 120.456"
389
+ subject.valid?.should be_true
390
+ subject.coordinates = " 45.123,120.456"
391
+ subject.valid?.should be_true
392
+ subject.coordinates = "45.123,120.456 "
393
+ subject.valid?.should be_true
394
+ end
395
+ it "should accept valid coordinates on limits" do
396
+ subject = Factory :stop_area, :area_type => "BoardingPosition", :coordinates => "90,180"
397
+ subject.valid?.should be_true
398
+ subject.coordinates = "-90,-180"
399
+ subject.valid?.should be_true
400
+ subject.coordinates = "-90.,180."
401
+ subject.valid?.should be_true
402
+ subject.coordinates = "-90.0,180.00"
403
+ subject.valid?.should be_true
404
+ end
405
+ it "should reject invalid coordinates" do
406
+ subject = Factory :stop_area, :area_type => "BoardingPosition"
407
+ subject.coordinates = ",12"
408
+ subject.valid?.should be_false
409
+ subject.coordinates = "-90"
410
+ subject.valid?.should be_false
411
+ subject.coordinates = "-90.1,180."
412
+ subject.valid?.should be_false
413
+ subject.coordinates = "-90.0,180.1"
414
+ subject.valid?.should be_false
415
+ subject.coordinates = "-91.0,18.1"
416
+ subject.valid?.should be_false
417
+ end
418
+ end
419
+
420
+ describe "#duplicate" do
421
+ it "should be a copy of" do
422
+ stop_place = Factory :stop_area, :area_type => "StopPlace"
423
+ subject = Factory :stop_area, :area_type => "CommercialStopPoint" ,:parent => stop_place, :coordinates => "45.123,120.456"
424
+ access_point1 = Factory :access_point, :stop_area => subject
425
+ access_point2 = Factory :access_point, :stop_area => subject
426
+ quay1 = Factory :stop_area, :parent => subject, :area_type => "Quay"
427
+ target=subject.duplicate
428
+ target.id.should be_nil
429
+ target.name.should == "Copy of "+subject.name
430
+ target.objectid.should == subject.objectid+"_1"
431
+ target.area_type.should == subject.area_type
432
+ target.parent.should be_nil
433
+ target.children.size.should == 0
434
+ target.access_points.size.should == 0
435
+ target.coordinates.should == "45.123,120.456"
436
+ end
437
+ end
438
+
439
+
440
+ end