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