aboutsummaryrefslogtreecommitdiffstats
path: root/spec/models/chouette/access_point_spec.rb
blob: 9c637cf41fa0aabd66c9253c5f419d0d82f3796b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
require 'spec_helper'

describe Chouette::AccessPoint, :type => :model do
  subject { create(:access_point) }

  describe '#objectid' do
    it "should have the same class as stop_area objectid" do
      expect(subject.objectid.class).to eq(subject.stop_area.objectid.class)
    end
  end

  it { is_expected.to validate_presence_of :name }
  it { is_expected.to validate_numericality_of :latitude }
  it { is_expected.to validate_numericality_of :longitude }
  

  describe ".latitude" do
    it "should accept -90 value" do
      subject = create :access_point
      subject.latitude = -90
      expect(subject.valid?).to be_truthy
    end
    it "should reject < -90 value" do
      subject = create :access_point
      subject.latitude = -90.0001
      expect(subject.valid?).to be_falsey
    end
    it "should accept 90 value" do
      subject = create :access_point
      subject.latitude = 90
      expect(subject.valid?).to be_truthy
    end
    it "should reject > 90 value" do
      subject = create :access_point
      subject.latitude = 90.0001
      expect(subject.valid?).to be_falsey
    end
  end

  describe ".longitude" do
    it "should accept -180 value" do
      subject = create :access_point
      subject.longitude = -180
      expect(subject.valid?).to be_truthy
    end
    it "should reject < -180 value" do
      subject = create :access_point
      subject.longitude = -180.0001
      expect(subject.valid?).to be_falsey
    end
    it "should accept 180 value" do
      subject = create :access_point
      subject.longitude = 180
      expect(subject.valid?).to be_truthy
    end
    it "should reject > 180 value" do
      subject = create :access_point
      subject.longitude = 180.0001
      expect(subject.valid?).to be_falsey
    end
  end

  describe ".long_lat" do
    it "should accept longitude and latitude both as nil" do
      subject = create :access_point
      subject.longitude = nil
      subject.latitude = nil
      expect(subject.valid?).to be_truthy
    end
    it "should accept longitude and latitude both numerical" do
      subject = create :access_point
      subject.longitude = 10
      subject.latitude = 10
      expect(subject.valid?).to be_truthy
    end
    it "should reject longitude nil with latitude numerical" do
      subject = create :access_point
      subject.longitude = nil
      subject.latitude = 10
      expect(subject.valid?).to be_falsey
    end
    it "should reject longitude numerical with latitude nil" do
      subject = create :access_point
      subject.longitude = 10
      subject.latitude = nil
      expect(subject.valid?).to be_falsey
    end
  end

  describe "#access_type" do
    def self.legacy_access_types
      %w{In Out InOut}
    end

    legacy_access_types.each do |access_type|
      context "when access_type is #{access_type}" do
        access_point_type = Chouette::AccessPointType.new(access_type.underscore)
        it "should be #{access_point_type}" do
          subject.access_type = access_type
          expect(subject.access_point_type).to eq(access_point_type)
        end
      end
    end
  end

  describe "#access_point_type=" do
    it "should change access_type with Chouette::AccessPointType#name" do
      subject.access_point_type = "in_out"
      expect(subject.access_type).to eq("InOut")
    end

  end

  describe "#to_lat_lng" do
    it "should return nil if latitude is nil" do
      subject.latitude = nil
      expect(subject.to_lat_lng).to be_nil
    end

    it "should return nil if longitude is nil" do
      subject.longitude = nil
      expect(subject.to_lat_lng).to be_nil
    end

  end

  describe "#geometry" do
    let(:access_point) { create(:access_point) }

    it "should be nil when to_lat_lng is nil" do
      allow(access_point).to receive_messages :longitude => nil
      allow(access_point).to receive_messages :latitude => nil
      expect(access_point.geometry).to be_nil
    end
  end

  describe "#generic_access_link_matrix" do
    it "should have 2 generic_access_links in matrix" do
      stop_place = create :stop_area, :area_type => "gdl"
      commercial_stop_point = create :stop_area, :area_type => "lda" ,:parent => stop_place
      subject = create :access_point, :stop_area => stop_place
      expect(subject.generic_access_link_matrix.size).to eq(2)
    end

    it "should have new generic_access_links in matrix" do
      commercial_stop_point = create :stop_area, :area_type => "lda"
      subject = create :access_point, :stop_area => commercial_stop_point
      subject.generic_access_link_matrix.each do |link|
        expect(link.id).to be_nil
      end
    end
    # it "should have only last generic_access_links as new in matrix" do
    #   commercial_stop_point = create :stop_area, :area_type => "lda"
    #   subject = create :access_point, :stop_area => commercial_stop_point
    #   link = create :access_link, :access_point => subject, :stop_area => commercial_stop_point
    #   subject.generic_access_link_matrix.each do |link|
    #     if link.link_key.start_with?"A_"
    #       expect(link.id).not_to be_nil
    #     else
    #       expect(link.id).to be_nil
    #     end
    #   end
    # end
  end

  describe "#detail_access_link_matrix" do
    # it "should have 4 detail_access_links in matrix" do
    #   stop_place = create :stop_area, :area_type => "zdlp"
    #   commercial_stop_point = create :stop_area, :area_type => "lda" ,:parent => stop_place
    #   zdep1 = create :stop_area, :parent => commercial_stop_point, :area_type => "zdep"
    #   zdep2 = create :stop_area, :parent => commercial_stop_point, :area_type => "zdep"
    #   subject = create :access_point, :stop_area => stop_place
    #   expect(subject.detail_access_link_matrix.size).to eq(4)
    # end

    it "should have new detail_access_links in matrix" do
      commercial_stop_point = create :stop_area, :area_type => "lda"
      zdep = create :stop_area, :parent => commercial_stop_point, :area_type => "zdep"
      subject = create :access_point, :stop_area => commercial_stop_point
      subject.detail_access_link_matrix.each do |link|
        expect(link.id).to be_nil
      end
    end
    it "should have only last detail_access_links as new in matrix" do
      commercial_stop_point = create :stop_area, :area_type => "lda"
      zdep = create :stop_area, :parent => commercial_stop_point, :area_type => "zdep"
      subject = create :access_point, :stop_area => commercial_stop_point
      link = create :access_link, :access_point => subject, :stop_area => zdep
      subject.detail_access_link_matrix.each do |link|
        if link.link_key.start_with?"A_"
          expect(link.id).not_to be_nil
        else
          expect(link.id).to be_nil
        end
      end
    end
  end

  describe "#coordinates" do
    it "should convert coordinates into latitude/longitude" do
     commercial_stop_point = create :stop_area, :area_type => "lda"
     subject = create :access_point, :stop_area => commercial_stop_point, :coordinates => "45.123,120.456"
     expect(subject.longitude).to be_within(0.001).of(120.456)
     expect(subject.latitude).to be_within(0.001).of(45.123)
   end
    it "should set empty coordinates into nil latitude/longitude" do
     commercial_stop_point = create :stop_area, :area_type => "lda"
     subject = create :access_point, :stop_area => commercial_stop_point, :coordinates => "45.123,120.456"
     expect(subject.longitude).to be_within(0.001).of(120.456)
     expect(subject.latitude).to be_within(0.001).of(45.123)
     subject.coordinates = ""
     subject.save
     expect(subject.longitude).to be_nil
     expect(subject.latitude).to be_nil
   end
    it "should convert latitude/longitude into coordinates" do
     commercial_stop_point = create :stop_area, :area_type => "lda"
     subject = create :access_point, :stop_area => commercial_stop_point, :longitude => 120.456, :latitude => 45.123
     expect(subject.coordinates).to eq("45.123,120.456")
   end
    it "should convert nil latitude/longitude into empty coordinates" do
    commercial_stop_point = create :stop_area, :area_type => "lda"
     subject = create :access_point, :stop_area => commercial_stop_point, :longitude => nil, :latitude => nil
     expect(subject.coordinates).to eq("")
   end
    it "should accept valid coordinates" do
     commercial_stop_point = create :stop_area, :area_type => "lda"
     subject = create :access_point, :stop_area => commercial_stop_point, :coordinates => "45.123,120.456"
     expect(subject.valid?).to be_truthy
     subject.coordinates = "45.123, 120.456"
     expect(subject.valid?).to be_truthy
     expect(subject.longitude).to be_within(0.001).of(120.456)
     expect(subject.latitude).to be_within(0.001).of(45.123)
     subject.coordinates = "45.123,  -120.456"
     expect(subject.valid?).to be_truthy
     subject.coordinates = "45.123 ,120.456"
     expect(subject.valid?).to be_truthy
     subject.coordinates = "45.123   ,   120.456"
     expect(subject.valid?).to be_truthy
     subject.coordinates = " 45.123,120.456"
     expect(subject.valid?).to be_truthy
     subject.coordinates = "45.123,120.456  "
     expect(subject.valid?).to be_truthy
    end
    it "should accept valid coordinates on limits" do
     commercial_stop_point = create :stop_area, :area_type => "lda"
     subject = create :access_point, :stop_area => commercial_stop_point, :coordinates => "90,180"
     expect(subject.valid?).to be_truthy
     subject.coordinates = "-90,-180"
     expect(subject.valid?).to be_truthy
     subject.coordinates = "-90.,180."
     expect(subject.valid?).to be_truthy
     subject.coordinates = "-90.0,180.00"
     expect(subject.valid?).to be_truthy
    end
    it "should reject invalid coordinates" do
     commercial_stop_point = create :stop_area, :area_type => "lda"
     subject = create :access_point, :stop_area => commercial_stop_point
     subject.coordinates = ",12"
     expect(subject.valid?).to be_falsey
     subject.coordinates = "-90"
     expect(subject.valid?).to be_falsey
     subject.coordinates = "-90.1,180."
     expect(subject.valid?).to be_falsey
     subject.coordinates = "-90.0,180.1"
     expect(subject.valid?).to be_falsey
     subject.coordinates = "-91.0,18.1"
     expect(subject.valid?).to be_falsey
    end
  end

end