]> git.openstreetmap.org Git - rails.git/blob - test/controllers/api/nodes/ways_controller_test.rb
Merge remote-tracking branch 'upstream/pull/5637'
[rails.git] / test / controllers / api / nodes / ways_controller_test.rb
1 require "test_helper"
2
3 module Api
4   module Nodes
5     class WaysControllerTest < ActionDispatch::IntegrationTest
6       ##
7       # test all routes which lead to this controller
8       def test_routes
9         assert_routing(
10           { :path => "/api/0.6/node/1/ways", :method => :get },
11           { :controller => "api/nodes/ways", :action => "index", :node_id => "1" }
12         )
13         assert_routing(
14           { :path => "/api/0.6/node/1/ways.json", :method => :get },
15           { :controller => "api/nodes/ways", :action => "index", :node_id => "1", :format => "json" }
16         )
17       end
18
19       ##
20       # test that a call to ways_for_node returns all ways that contain the node
21       # and none that don't.
22       def test_index
23         node = create(:node)
24         way1 = create(:way)
25         way2 = create(:way)
26         create(:way_node, :way => way1, :node => node)
27         create(:way_node, :way => way2, :node => node)
28         # create an unrelated way
29         create(:way_with_nodes, :nodes_count => 2)
30         # create a way which used to use the node
31         way3_v1 = create(:old_way, :version => 1)
32         _way3_v2 = create(:old_way, :current_way => way3_v1.current_way, :version => 2)
33         create(:old_way_node, :old_way => way3_v1, :node => node)
34
35         get api_node_ways_path(node)
36         assert_response :success
37         ways_xml = XML::Parser.string(@response.body).parse
38         assert_not_nil ways_xml, "failed to parse ways_for_node response"
39
40         # check that the set of IDs match expectations
41         expected_way_ids = [way1.id,
42                             way2.id]
43         found_way_ids = ways_xml.find("//osm/way").collect { |w| w["id"].to_i }
44         assert_equal expected_way_ids.sort, found_way_ids.sort,
45                      "expected ways for node #{node.id} did not match found"
46
47         # check the full ways to ensure we're not missing anything
48         expected_way_ids.each do |id|
49           way_xml = ways_xml.find("//osm/way[@id='#{id}']").first
50           assert_ways_are_equal(Way.find(id),
51                                 Way.from_xml_node(way_xml))
52         end
53       end
54
55       def test_index_json
56         node = create(:node)
57         way = create(:way)
58         create(:way_node, :way => way, :node => node)
59
60         get api_node_ways_path(node, :format => "json")
61
62         assert_response :success
63         js = ActiveSupport::JSON.decode(@response.body)
64         assert_not_nil js
65         assert_equal 1, js["elements"].count
66         js_ways = js["elements"].filter { |e| e["type"] == "way" }
67         assert_equal 1, js_ways.count
68         assert_equal way.id, js_ways[0]["id"]
69       end
70     end
71   end
72 end