Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JSON output nodes, ways, relations, map #2485

Merged
merged 10 commits into from
Feb 26, 2020
Prev Previous commit
Next Next commit
Added unit tests
  • Loading branch information
mmd-osm committed Jan 8, 2020
commit 91b31f03de0143c4e207fb3a001b32859f560388
21 changes: 21 additions & 0 deletions test/controllers/api/nodes_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ def test_routes
{ :path => "/api/0.6/node/1", :method => :get },
{ :controller => "api/nodes", :action => "show", :id => "1" }
)
assert_routing(
{ :path => "/api/0.6/node/1.json", :method => :get },
{ :controller => "api/nodes", :action => "show", :id => "1", :format => "json" }
)
assert_routing(
{ :path => "/api/0.6/node/1", :method => :put },
{ :controller => "api/nodes", :action => "update", :id => "1" }
Expand All @@ -25,6 +29,10 @@ def test_routes
{ :path => "/api/0.6/nodes", :method => :get },
{ :controller => "api/nodes", :action => "index" }
)
assert_routing(
{ :path => "/api/0.6/nodes.json", :method => :get },
{ :controller => "api/nodes", :action => "index", :format => "json" }
)
end

def test_create
Expand Down Expand Up @@ -464,6 +472,19 @@ def test_index
assert_select "node[id='#{node5.id}'][visible='false']", :count => 1
end

# test a working call with json format
get :index, :params => { :nodes => "#{node1.id},#{node2.id},#{node3.id},#{node4.id},#{node5.id}", :format => "json" }

js = ActiveSupport::JSON.decode(@response.body)
assert_not_nil js
assert_equal 5, js["elements"].count
assert_equal 5, (js["elements"].count { |a| a["type"] == "node" })
assert_equal 1, (js["elements"].count { |a| a["id"] == node1.id && a["visible"].nil? })
assert_equal 1, (js["elements"].count { |a| a["id"] == node2.id && a["visible"] == false })
assert_equal 1, (js["elements"].count { |a| a["id"] == node3.id && a["visible"].nil? })
assert_equal 1, (js["elements"].count { |a| a["id"] == node4.id && a["visible"].nil? })
assert_equal 1, (js["elements"].count { |a| a["id"] == node5.id && a["visible"] == false })

# check error when a non-existent node is included
get :index, :params => { :nodes => "#{node1.id},#{node2.id},#{node3.id},#{node4.id},#{node5.id},0" }
assert_response :not_found
Expand Down
8 changes: 8 additions & 0 deletions test/controllers/api/old_nodes_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@ def test_routes
{ :path => "/api/0.6/node/1/2", :method => :get },
{ :controller => "api/old_nodes", :action => "version", :id => "1", :version => "2" }
)
assert_routing(
{ :path => "/api/0.6/node/1/history.json", :method => :get },
{ :controller => "api/old_nodes", :action => "history", :id => "1", :format => "json" }
)
assert_routing(
{ :path => "/api/0.6/node/1/2.json", :method => :get },
{ :controller => "api/old_nodes", :action => "version", :id => "1", :version => "2", :format => "json" }
)
assert_routing(
{ :path => "/api/0.6/node/1/2/redact", :method => :post },
{ :controller => "api/old_nodes", :action => "redact", :id => "1", :version => "2" }
Expand Down
8 changes: 8 additions & 0 deletions test/controllers/api/old_relations_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@ def test_routes
{ :path => "/api/0.6/relation/1/2", :method => :get },
{ :controller => "api/old_relations", :action => "version", :id => "1", :version => "2" }
)
assert_routing(
{ :path => "/api/0.6/relation/1/history.json", :method => :get },
{ :controller => "api/old_relations", :action => "history", :id => "1", :format => "json" }
)
assert_routing(
{ :path => "/api/0.6/relation/1/2.json", :method => :get },
{ :controller => "api/old_relations", :action => "version", :id => "1", :version => "2", :format => "json" }
)
assert_routing(
{ :path => "/api/0.6/relation/1/2/redact", :method => :post },
{ :controller => "api/old_relations", :action => "redact", :id => "1", :version => "2" }
Expand Down
8 changes: 8 additions & 0 deletions test/controllers/api/old_ways_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@ def test_routes
{ :path => "/api/0.6/way/1/2", :method => :get },
{ :controller => "api/old_ways", :action => "version", :id => "1", :version => "2" }
)
assert_routing(
{ :path => "/api/0.6/way/1/history.json", :method => :get },
{ :controller => "api/old_ways", :action => "history", :id => "1", :format => "json" }
)
assert_routing(
{ :path => "/api/0.6/way/1/2.json", :method => :get },
{ :controller => "api/old_ways", :action => "version", :id => "1", :version => "2", :format => "json" }
)
assert_routing(
{ :path => "/api/0.6/way/1/2/redact", :method => :post },
{ :controller => "api/old_ways", :action => "redact", :id => "1", :version => "2" }
Expand Down
36 changes: 36 additions & 0 deletions test/controllers/api/relations_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,18 @@ def test_routes
{ :path => "/api/0.6/relation/1/full", :method => :get },
{ :controller => "api/relations", :action => "full", :id => "1" }
)
assert_routing(
{ :path => "/api/0.6/relation/1/full.json", :method => :get },
{ :controller => "api/relations", :action => "full", :id => "1", :format => "json" }
)
assert_routing(
{ :path => "/api/0.6/relation/1", :method => :get },
{ :controller => "api/relations", :action => "show", :id => "1" }
)
assert_routing(
{ :path => "/api/0.6/relation/1.json", :method => :get },
{ :controller => "api/relations", :action => "show", :id => "1", :format => "json" }
)
assert_routing(
{ :path => "/api/0.6/relation/1", :method => :put },
{ :controller => "api/relations", :action => "update", :id => "1" }
Expand All @@ -29,6 +37,10 @@ def test_routes
{ :path => "/api/0.6/relations", :method => :get },
{ :controller => "api/relations", :action => "index" }
)
assert_routing(
{ :path => "/api/0.6/relations.json", :method => :get },
{ :controller => "api/relations", :action => "index", :format => "json" }
)

assert_routing(
{ :path => "/api/0.6/node/1/relations", :method => :get },
Expand All @@ -42,6 +54,18 @@ def test_routes
{ :path => "/api/0.6/relation/1/relations", :method => :get },
{ :controller => "api/relations", :action => "relations_for_relation", :id => "1" }
)
assert_routing(
{ :path => "/api/0.6/node/1/relations.json", :method => :get },
{ :controller => "api/relations", :action => "relations_for_node", :id => "1", :format => "json" }
)
assert_routing(
{ :path => "/api/0.6/way/1/relations.json", :method => :get },
{ :controller => "api/relations", :action => "relations_for_way", :id => "1", :format => "json" }
)
assert_routing(
{ :path => "/api/0.6/relation/1/relations.json", :method => :get },
{ :controller => "api/relations", :action => "relations_for_relation", :id => "1", :format => "json" }
)
end

# -------------------------------------
Expand Down Expand Up @@ -187,6 +211,18 @@ def test_index
assert_select "relation[id='#{relation4.id}'][visible='true']", :count => 1
end

# test a working call with json format
get :index, :params => { :relations => "#{relation1.id},#{relation2.id},#{relation3.id},#{relation4.id}", :format => "json" }

js = ActiveSupport::JSON.decode(@response.body)
assert_not_nil js
assert_equal 4, js["elements"].count
assert_equal 4, (js["elements"].count { |a| a["type"] == "relation" })
assert_equal 1, (js["elements"].count { |a| a["id"] == relation1.id && a["visible"].nil? })
assert_equal 1, (js["elements"].count { |a| a["id"] == relation2.id && a["visible"] == false })
assert_equal 1, (js["elements"].count { |a| a["id"] == relation3.id && a["visible"].nil? })
assert_equal 1, (js["elements"].count { |a| a["id"] == relation4.id && a["visible"].nil? })

# check error when a non-existent relation is included
get :index, :params => { :relations => "#{relation1.id},#{relation2.id},#{relation3.id},#{relation4.id},0" }
assert_response :not_found
Expand Down
24 changes: 24 additions & 0 deletions test/controllers/api/ways_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,18 @@ def test_routes
{ :path => "/api/0.6/way/1/full", :method => :get },
{ :controller => "api/ways", :action => "full", :id => "1" }
)
assert_routing(
{ :path => "/api/0.6/way/1/full.json", :method => :get },
{ :controller => "api/ways", :action => "full", :id => "1", :format => "json" }
)
assert_routing(
{ :path => "/api/0.6/way/1", :method => :get },
{ :controller => "api/ways", :action => "show", :id => "1" }
)
assert_routing(
{ :path => "/api/0.6/way/1.json", :method => :get },
{ :controller => "api/ways", :action => "show", :id => "1", :format => "json" }
)
assert_routing(
{ :path => "/api/0.6/way/1", :method => :put },
{ :controller => "api/ways", :action => "update", :id => "1" }
Expand All @@ -29,6 +37,10 @@ def test_routes
{ :path => "/api/0.6/ways", :method => :get },
{ :controller => "api/ways", :action => "index" }
)
assert_routing(
{ :path => "/api/0.6/ways.json", :method => :get },
{ :controller => "api/ways", :action => "index", :format => "json" }
)
end

# -------------------------------------
Expand Down Expand Up @@ -104,6 +116,18 @@ def test_index
assert_select "way[id='#{way4.id}'][visible='true']", :count => 1
end

# test a working call with json format
get :index, :params => { :ways => "#{way1.id},#{way2.id},#{way3.id},#{way4.id}", :format => "json" }

js = ActiveSupport::JSON.decode(@response.body)
assert_not_nil js
assert_equal 4, js["elements"].count
assert_equal 4, (js["elements"].count { |a| a["type"] == "way" })
assert_equal 1, (js["elements"].count { |a| a["id"] == way1.id && a["visible"].nil? })
assert_equal 1, (js["elements"].count { |a| a["id"] == way2.id && a["visible"] == false })
assert_equal 1, (js["elements"].count { |a| a["id"] == way3.id && a["visible"].nil? })
assert_equal 1, (js["elements"].count { |a| a["id"] == way4.id && a["visible"].nil? })

# check error when a non-existent way is included
get :index, :params => { :ways => "#{way1.id},#{way2.id},#{way3.id},#{way4.id},0" }
assert_response :not_found
Expand Down