]> git.openstreetmap.org Git - rails.git/blob - test/controllers/api/nodes/relations_controller_test.rb
Add frozen_string_literal comments to ruby files
[rails.git] / test / controllers / api / nodes / relations_controller_test.rb
1 # frozen_string_literal: true
2
3 require "test_helper"
4
5 module Api
6   module Nodes
7     class RelationsControllerTest < 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/relations", :method => :get },
13           { :controller => "api/nodes/relations", :action => "index", :node_id => "1" }
14         )
15         assert_routing(
16           { :path => "/api/0.6/node/1/relations.json", :method => :get },
17           { :controller => "api/nodes/relations", :action => "index", :node_id => "1", :format => "json" }
18         )
19       end
20
21       ##
22       # check that all relations containing a particular node, and no extra
23       # relations, are returned.
24       def test_index
25         node = create(:node)
26         # should include relations with that node as a member
27         relation_with_node = create(:relation_member, :member => node).relation
28         # should ignore relations without that node as a member
29         _relation_without_node = create(:relation_member).relation
30         # should ignore relations with the node involved indirectly, via a way
31         way = create(:way_node, :node => node).way
32         _relation_with_way = create(:relation_member, :member => way).relation
33         # should ignore relations with the node involved indirectly, via a relation
34         second_relation = create(:relation_member, :member => node).relation
35         _super_relation = create(:relation_member, :member => second_relation).relation
36         # should combine multiple relation_member references into just one relation entry
37         create(:relation_member, :member => node, :relation => relation_with_node)
38         # should not include deleted relations
39         deleted_relation = create(:relation, :deleted)
40         create(:relation_member, :member => node, :relation => deleted_relation)
41
42         get api_node_relations_path(node)
43
44         assert_response :success
45
46         # count one osm element
47         assert_select "osm[version='#{Settings.api_version}'][generator='#{Settings.generator}']", 1
48
49         # we should have only the expected number of relations
50         expected_relations = [relation_with_node, second_relation]
51         assert_select "osm>relation", expected_relations.size
52
53         # and each of them should contain the element we originally searched for
54         expected_relations.each do |containing_relation|
55           # The relation should appear once, but the element could appear multiple times
56           assert_select "osm>relation[id='#{containing_relation.id}']", 1
57           assert_select "osm>relation[id='#{containing_relation.id}']>member[type='node'][ref='#{node.id}']"
58         end
59       end
60
61       def test_index_json
62         node = create(:node)
63         containing_relation = create(:relation_member, :member => node).relation
64
65         get api_node_relations_path(node, :format => "json")
66
67         assert_response :success
68         js = ActiveSupport::JSON.decode(@response.body)
69         assert_not_nil js
70         assert_equal 1, js["elements"].count
71         js_relations = js["elements"].filter { |e| e["type"] == "relation" }
72         assert_equal 1, js_relations.count
73         assert_equal containing_relation.id, js_relations[0]["id"]
74       end
75     end
76   end
77 end