]> git.openstreetmap.org Git - rails.git/blobdiff - test/controllers/api/map_controller_test.rb
API JSON output
[rails.git] / test / controllers / api / map_controller_test.rb
index 5ee5a9e6ccaffb63eb65e941f532e79a502c6f9d..5df2dc6a3cabb30c1e58d13e6463a933566566ca 100644 (file)
@@ -23,6 +23,10 @@ module Api
         { :path => "/api/0.6/map", :method => :get },
         { :controller => "api/map", :action => "index" }
       )
+      assert_routing(
+        { :path => "/api/0.6/map.json", :method => :get },
+        { :controller => "api/map", :action => "index", :format => "json" }
+      )
     end
 
     # -------------------------------------
@@ -49,7 +53,7 @@ module Api
         print @response.body
       end
       assert_response :success, "Expected scucess with the map call"
-      assert_select "osm[version='#{API_VERSION}'][generator='#{GENERATOR}']", :count => 1 do
+      assert_select "osm[version='#{Settings.api_version}'][generator='#{Settings.generator}']", :count => 1 do
         assert_select "bounds[minlon='#{format('%.7f', minlon)}'][minlat='#{format('%.7f', minlat)}'][maxlon='#{format('%.7f', maxlon)}'][maxlat='#{format('%.7f', maxlat)}']", :count => 1
         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
           # This should really be more generic
@@ -63,6 +67,54 @@ module Api
       end
     end
 
+    def test_map_json
+      node = create(:node, :lat => 7, :lon => 7)
+      tag = create(:node_tag, :node => node)
+      way1 = create(:way_node, :node => node).way
+      way2 = create(:way_node, :node => node).way
+      relation = create(:relation_member, :member => node).relation
+
+      # Need to split the min/max lat/lon out into their own variables here
+      # so that we can test they are returned later.
+      minlon = node.lon - 0.1
+      minlat = node.lat - 0.1
+      maxlon = node.lon + 0.1
+      maxlat = node.lat + 0.1
+      bbox = "#{minlon},#{minlat},#{maxlon},#{maxlat}"
+      get :index, :params => { :bbox => bbox, :format => "json" }
+      if $VERBOSE
+        print @request.to_yaml
+        print @response.body
+      end
+      assert_response :success, "Expected success with the map call"
+      js = ActiveSupport::JSON.decode(@response.body)
+      assert_not_nil js
+
+      assert_equal Settings.api_version, js["version"]
+      assert_equal Settings.generator, js["generator"]
+      assert_equal "#{format('%.7f', minlon)}", js["bounds"]["minlon"]
+      assert_equal "#{format('%.7f', minlat)}", js["bounds"]["minlat"]
+      assert_equal "#{format('%.7f', maxlon)}", js["bounds"]["maxlon"]
+      assert_equal "#{format('%.7f', maxlat)}", js["bounds"]["maxlat"]
+
+      result_nodes = js["elements"].select { |a| a["type"] == "node" }
+                                   .select { |a| a["id"] == node.id }
+                                   .select { |a| a["lat"] == "#{format('%.7f', node.lat)}" }
+                                   .select { |a| a["lon"] == "#{format('%.7f', node.lon)}" }
+                                   .select { |a| a["version"] == node.version }
+                                   .select { |a| a["changeset"] == node.changeset_id }
+                                   .select { |a| a["timestamp"] == node.timestamp.xmlschema }
+      assert_equal result_nodes.count, 1
+      result_node = result_nodes.first
+
+      assert_equal result_node["tags"], tag.k => tag.v
+      assert_equal 2, (js["elements"].count { |a| a["type"] == "way" })
+      assert_equal 1, (js["elements"].count { |a| a["type"] == "way" && a["id"] == way1.id })
+      assert_equal 1, (js["elements"].count { |a| a["type"] == "way" && a["id"] == way2.id })
+      assert_equal 1, (js["elements"].count { |a| a["type"] == "relation" })
+      assert_equal 1, (js["elements"].count { |a| a["type"] == "relation" && a["id"] == relation.id })
+    end
+
     # This differs from the above test in that we are making the bbox exactly
     # the same as the node we are looking at
     def test_map_inclusive
@@ -75,7 +127,7 @@ module Api
       bbox = "#{node.lon},#{node.lat},#{node.lon},#{node.lat}"
       get :index, :params => { :bbox => bbox }
       assert_response :success, "The map call should have succeeded"
-      assert_select "osm[version='#{API_VERSION}'][generator='#{GENERATOR}']", :count => 1 do
+      assert_select "osm[version='#{Settings.api_version}'][generator='#{Settings.generator}']", :count => 1 do
         assert_select "bounds[minlon='#{node.lon}'][minlat='#{node.lat}'][maxlon='#{node.lon}'][maxlat='#{node.lat}']", :count => 1
         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
           # This should really be more generic
@@ -103,7 +155,7 @@ module Api
       bbox = "#{node.lon},#{node.lat},#{node.lon},#{node.lat}"
       get :index, :params => { :bbox => bbox }
       assert_response :success, "The map call should have succeeded"
-      assert_select "osm[version='#{API_VERSION}'][generator='#{GENERATOR}']", :count => 1 do
+      assert_select "osm[version='#{Settings.api_version}'][generator='#{Settings.generator}']", :count => 1 do
         assert_select "bounds[minlon='#{node.lon}'][minlat='#{node.lat}'][maxlon='#{node.lon}'][maxlat='#{node.lat}']", :count => 1
         assert_select "node", :count => 3
         assert_select "node[id='#{node.id}']", :count => 1
@@ -120,7 +172,7 @@ module Api
     def test_map_empty
       get :index, :params => { :bbox => "179.998,89.998,179.999.1,89.999" }
       assert_response :success, "The map call should have succeeded"
-      assert_select "osm[version='#{API_VERSION}'][generator='#{GENERATOR}']", :count => 1 do
+      assert_select "osm[version='#{Settings.api_version}'][generator='#{Settings.generator}']", :count => 1 do
         assert_select "bounds[minlon='179.9980000'][minlat='89.9980000'][maxlon='179.9990000'][maxlat='89.9990000']", :count => 1
         assert_select "node", :count => 0
         assert_select "way", :count => 0
@@ -138,7 +190,7 @@ module Api
       @badbigbbox.each do |bbox|
         get :index, :params => { :bbox => bbox }
         assert_response :bad_request, "The bbox:#{bbox} was expected to be too big"
-        assert_equal "The maximum bbox size is #{MAX_REQUEST_AREA}, and your request was too large. Either request a smaller area, or use planet.osm", @response.body, "bbox: #{bbox}"
+        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}"
       end
     end