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