4 class MapControllerTest < ActionController::TestCase
7 @badbigbbox = %w[-0.1,-0.1,1.1,1.1 10,10,11,11]
8 @badmalformedbbox = %w[-0.1 hello
10 @badlatmixedbbox = %w[0,0.1,0.1,0 -0.1,80,0.1,70 0.24,54.34,0.25,54.33]
11 @badlonmixedbbox = %w[80,-0.1,70,0.1 54.34,0.24,54.33,0.25]
12 # @badlatlonoutboundsbbox = %w{ 191,-0.1,193,0.1 -190.1,89.9,-190,90 }
13 @goodbbox = %w[-0.1,-0.1,0.1,0.1 51.1,-0.1,51.2,0
14 -0.1,%20-0.1,%200.1,%200.1 -0.1edcd,-0.1d,0.1,0.1 -0.1E,-0.1E,0.1S,0.1N S0.1,W0.1,N0.1,E0.1]
15 # That last item in the goodbbox really shouldn't be there, as the API should
16 # reall reject it, however this is to test to see if the api changes.
20 # test all routes which lead to this controller
23 { :path => "/api/0.6/map", :method => :get },
24 { :controller => "api/map", :action => "index" }
27 { :path => "/api/0.6/map.json", :method => :get },
28 { :controller => "api/map", :action => "index", :format => "json" }
32 # -------------------------------------
33 # Test reading a bounding box.
34 # -------------------------------------
37 node = create(:node, :lat => 7, :lon => 7)
38 tag = create(:node_tag, :node => node)
39 way1 = create(:way_node, :node => node).way
40 way2 = create(:way_node, :node => node).way
41 relation = create(:relation_member, :member => node).relation
43 # Need to split the min/max lat/lon out into their own variables here
44 # so that we can test they are returned later.
45 minlon = node.lon - 0.1
46 minlat = node.lat - 0.1
47 maxlon = node.lon + 0.1
48 maxlat = node.lat + 0.1
49 bbox = "#{minlon},#{minlat},#{maxlon},#{maxlat}"
50 get :index, :params => { :bbox => bbox }
52 print @request.to_yaml
55 assert_response :success, "Expected scucess with the map call"
56 assert_select "osm[version='#{Settings.api_version}'][generator='#{Settings.generator}']", :count => 1 do
57 assert_select "bounds[minlon='#{format('%.7f', minlon)}'][minlat='#{format('%.7f', minlat)}'][maxlon='#{format('%.7f', maxlon)}'][maxlat='#{format('%.7f', maxlat)}']", :count => 1
58 assert_select "node[id='#{node.id}'][lat='#{format('%.7f', node.lat)}'][lon='#{format('%.7f', node.lon)}'][version='#{node.version}'][changeset='#{node.changeset_id}'][visible='#{node.visible}'][timestamp='#{node.timestamp.xmlschema}']", :count => 1 do
59 # This should really be more generic
60 assert_select "tag[k='#{tag.k}'][v='#{tag.v}']"
62 assert_select "way", :count => 2
63 assert_select "way[id='#{way1.id}']", :count => 1
64 assert_select "way[id='#{way2.id}']", :count => 1
65 assert_select "relation", :count => 1
66 assert_select "relation[id='#{relation.id}']", :count => 1
71 node = create(:node, :lat => 7, :lon => 7)
72 tag = create(:node_tag, :node => node)
73 way1 = create(:way_node, :node => node).way
74 way2 = create(:way_node, :node => node).way
75 relation = create(:relation_member, :member => node).relation
77 # Need to split the min/max lat/lon out into their own variables here
78 # so that we can test they are returned later.
79 minlon = node.lon - 0.1
80 minlat = node.lat - 0.1
81 maxlon = node.lon + 0.1
82 maxlat = node.lat + 0.1
83 bbox = "#{minlon},#{minlat},#{maxlon},#{maxlat}"
84 get :index, :params => { :bbox => bbox, :format => "json" }
86 print @request.to_yaml
89 assert_response :success, "Expected success with the map call"
90 js = ActiveSupport::JSON.decode(@response.body)
93 assert_equal Settings.api_version, js["version"]
94 assert_equal Settings.generator, js["generator"]
95 assert_equal "#{format('%.7f', minlon)}", js["bounds"]["minlon"]
96 assert_equal "#{format('%.7f', minlat)}", js["bounds"]["minlat"]
97 assert_equal "#{format('%.7f', maxlon)}", js["bounds"]["maxlon"]
98 assert_equal "#{format('%.7f', maxlat)}", js["bounds"]["maxlat"]
100 result_nodes = js["elements"].select { |a| a["type"] == "node" }
101 .select { |a| a["id"] == node.id }
102 .select { |a| a["lat"] == "#{format('%.7f', node.lat)}" }
103 .select { |a| a["lon"] == "#{format('%.7f', node.lon)}" }
104 .select { |a| a["version"] == node.version }
105 .select { |a| a["changeset"] == node.changeset_id }
106 .select { |a| a["timestamp"] == node.timestamp.xmlschema }
107 assert_equal result_nodes.count, 1
108 result_node = result_nodes.first
110 assert_equal result_node["tags"], tag.k => tag.v
111 assert_equal 2, (js["elements"].count { |a| a["type"] == "way" })
112 assert_equal 1, (js["elements"].count { |a| a["type"] == "way" && a["id"] == way1.id })
113 assert_equal 1, (js["elements"].count { |a| a["type"] == "way" && a["id"] == way2.id })
114 assert_equal 1, (js["elements"].count { |a| a["type"] == "relation" })
115 assert_equal 1, (js["elements"].count { |a| a["type"] == "relation" && a["id"] == relation.id })
118 # This differs from the above test in that we are making the bbox exactly
119 # the same as the node we are looking at
120 def test_map_inclusive
121 node = create(:node, :lat => 7, :lon => 7)
122 tag = create(:node_tag, :node => node)
123 way1 = create(:way_node, :node => node).way
124 way2 = create(:way_node, :node => node).way
125 relation = create(:relation_member, :member => node).relation
127 bbox = "#{node.lon},#{node.lat},#{node.lon},#{node.lat}"
128 get :index, :params => { :bbox => bbox }
129 assert_response :success, "The map call should have succeeded"
130 assert_select "osm[version='#{Settings.api_version}'][generator='#{Settings.generator}']", :count => 1 do
131 assert_select "bounds[minlon='#{node.lon}'][minlat='#{node.lat}'][maxlon='#{node.lon}'][maxlat='#{node.lat}']", :count => 1
132 assert_select "node[id='#{node.id}'][lat='#{format('%.7f', node.lat)}'][lon='#{format('%.7f', node.lon)}'][version='#{node.version}'][changeset='#{node.changeset_id}'][visible='#{node.visible}'][timestamp='#{node.timestamp.xmlschema}']", :count => 1 do
133 # This should really be more generic
134 assert_select "tag[k='#{tag.k}'][v='#{tag.v}']"
136 assert_select "way", :count => 2
137 assert_select "way[id='#{way1.id}']", :count => 1
138 assert_select "way[id='#{way2.id}']", :count => 1
139 assert_select "relation", :count => 1
140 assert_select "relation[id='#{relation.id}']", :count => 1
144 def test_map_complete_way
145 node = create(:node, :lat => 7, :lon => 7)
146 # create a couple of nodes well outside of the bbox
147 node2 = create(:node, :lat => 45, :lon => 45)
148 node3 = create(:node, :lat => 10, :lon => 10)
149 way1 = create(:way_node, :node => node).way
150 create(:way_node, :way => way1, :node => node2, :sequence_id => 2)
151 way2 = create(:way_node, :node => node).way
152 create(:way_node, :way => way2, :node => node3, :sequence_id => 2)
153 relation = create(:relation_member, :member => way1).relation
155 bbox = "#{node.lon},#{node.lat},#{node.lon},#{node.lat}"
156 get :index, :params => { :bbox => bbox }
157 assert_response :success, "The map call should have succeeded"
158 assert_select "osm[version='#{Settings.api_version}'][generator='#{Settings.generator}']", :count => 1 do
159 assert_select "bounds[minlon='#{node.lon}'][minlat='#{node.lat}'][maxlon='#{node.lon}'][maxlat='#{node.lat}']", :count => 1
160 assert_select "node", :count => 3
161 assert_select "node[id='#{node.id}']", :count => 1
162 assert_select "node[id='#{node2.id}']", :count => 1
163 assert_select "node[id='#{node3.id}']", :count => 1
164 assert_select "way", :count => 2
165 assert_select "way[id='#{way1.id}']", :count => 1
166 assert_select "way[id='#{way2.id}']", :count => 1
167 assert_select "relation", :count => 1
168 assert_select "relation[id='#{relation.id}']", :count => 1
173 get :index, :params => { :bbox => "179.998,89.998,179.999.1,89.999" }
174 assert_response :success, "The map call should have succeeded"
175 assert_select "osm[version='#{Settings.api_version}'][generator='#{Settings.generator}']", :count => 1 do
176 assert_select "bounds[minlon='179.9980000'][minlat='89.9980000'][maxlon='179.9990000'][maxlat='89.9990000']", :count => 1
177 assert_select "node", :count => 0
178 assert_select "way", :count => 0
179 assert_select "relation", :count => 0
183 def test_map_without_bbox
185 assert_response :bad_request
186 assert_equal "The parameter bbox is required, and must be of the form min_lon,min_lat,max_lon,max_lat", @response.body, "A bbox param was expected"
189 def test_bbox_too_big
190 @badbigbbox.each do |bbox|
191 get :index, :params => { :bbox => bbox }
192 assert_response :bad_request, "The bbox:#{bbox} was expected to be too big"
193 assert_equal "The maximum bbox size is #{Settings.max_request_area}, and your request was too large. Either request a smaller area, or use planet.osm", @response.body, "bbox: #{bbox}"
197 def test_bbox_malformed
198 @badmalformedbbox.each do |bbox|
199 get :index, :params => { :bbox => bbox }
200 assert_response :bad_request, "The bbox:#{bbox} was expected to be malformed"
201 assert_equal "The parameter bbox is required, and must be of the form min_lon,min_lat,max_lon,max_lat", @response.body, "bbox: #{bbox}"
205 def test_bbox_lon_mixedup
206 @badlonmixedbbox.each do |bbox|
207 get :index, :params => { :bbox => bbox }
208 assert_response :bad_request, "The bbox:#{bbox} was expected to have the longitude mixed up"
209 assert_equal "The minimum longitude must be less than the maximum longitude, but it wasn't", @response.body, "bbox: #{bbox}"
213 def test_bbox_lat_mixedup
214 @badlatmixedbbox.each do |bbox|
215 get :index, :params => { :bbox => bbox }
216 assert_response :bad_request, "The bbox:#{bbox} was expected to have the latitude mixed up"
217 assert_equal "The minimum latitude must be less than the maximum latitude, but it wasn't", @response.body, "bbox: #{bbox}"
221 # We can't actually get an out of bounds error, as the bbox is sanitised.
222 # def test_latlon_outofbounds
223 # @badlatlonoutboundsbbox.each do |bbox|
224 # [ "trackpoints", "map" ].each do |tq|
225 # get tq, :bbox => bbox
226 # #print @request.to_yaml
227 # assert_response :bad_request, "The bbox #{bbox} was expected to be out of range"
228 # assert_equal "The latitudes must be between -90 an 90, and longitudes between -180 and 180", @response.body, "bbox: #{bbox}"