From 91b31f03de0143c4e207fb3a001b32859f560388 Mon Sep 17 00:00:00 2001 From: mmd-osm Date: Wed, 8 Jan 2020 18:49:45 +0100 Subject: [PATCH 1/1] Added unit tests --- test/controllers/api/nodes_controller_test.rb | 21 +++++++++++ .../api/old_nodes_controller_test.rb | 8 +++++ .../api/old_relations_controller_test.rb | 8 +++++ .../api/old_ways_controller_test.rb | 8 +++++ .../api/relations_controller_test.rb | 36 +++++++++++++++++++ test/controllers/api/ways_controller_test.rb | 24 +++++++++++++ 6 files changed, 105 insertions(+) diff --git a/test/controllers/api/nodes_controller_test.rb b/test/controllers/api/nodes_controller_test.rb index aee41d515..e2174a126 100644 --- a/test/controllers/api/nodes_controller_test.rb +++ b/test/controllers/api/nodes_controller_test.rb @@ -13,6 +13,10 @@ module Api { :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" } @@ -25,6 +29,10 @@ module Api { :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 @@ -464,6 +472,19 @@ module Api 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 diff --git a/test/controllers/api/old_nodes_controller_test.rb b/test/controllers/api/old_nodes_controller_test.rb index 941787d0a..76e78ffad 100644 --- a/test/controllers/api/old_nodes_controller_test.rb +++ b/test/controllers/api/old_nodes_controller_test.rb @@ -17,6 +17,14 @@ module Api { :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" } diff --git a/test/controllers/api/old_relations_controller_test.rb b/test/controllers/api/old_relations_controller_test.rb index 40a80248c..19e8ae357 100644 --- a/test/controllers/api/old_relations_controller_test.rb +++ b/test/controllers/api/old_relations_controller_test.rb @@ -13,6 +13,14 @@ module Api { :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" } diff --git a/test/controllers/api/old_ways_controller_test.rb b/test/controllers/api/old_ways_controller_test.rb index 73e968f30..cd676a960 100644 --- a/test/controllers/api/old_ways_controller_test.rb +++ b/test/controllers/api/old_ways_controller_test.rb @@ -13,6 +13,14 @@ module Api { :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" } diff --git a/test/controllers/api/relations_controller_test.rb b/test/controllers/api/relations_controller_test.rb index b7c17a176..da18099bd 100644 --- a/test/controllers/api/relations_controller_test.rb +++ b/test/controllers/api/relations_controller_test.rb @@ -13,10 +13,18 @@ module Api { :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" } @@ -29,6 +37,10 @@ module Api { :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 }, @@ -42,6 +54,18 @@ module Api { :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 # ------------------------------------- @@ -187,6 +211,18 @@ module Api 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 diff --git a/test/controllers/api/ways_controller_test.rb b/test/controllers/api/ways_controller_test.rb index 433b43f27..e4b9f3663 100644 --- a/test/controllers/api/ways_controller_test.rb +++ b/test/controllers/api/ways_controller_test.rb @@ -13,10 +13,18 @@ module Api { :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" } @@ -29,6 +37,10 @@ module Api { :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 # ------------------------------------- @@ -104,6 +116,18 @@ module Api 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 -- 2.39.5