1 # frozen_string_literal: true
 
   7     class RelationsControllerTest < ActionDispatch::IntegrationTest
 
   9       # test all routes which lead to this controller
 
  12           { :path => "/api/0.6/node/1/relations", :method => :get },
 
  13           { :controller => "api/nodes/relations", :action => "index", :node_id => "1" }
 
  16           { :path => "/api/0.6/node/1/relations.json", :method => :get },
 
  17           { :controller => "api/nodes/relations", :action => "index", :node_id => "1", :format => "json" }
 
  22       # check that all relations containing a particular node, and no extra
 
  23       # relations, are returned.
 
  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)
 
  42         get api_node_relations_path(node)
 
  44         assert_response :success
 
  46         # count one osm element
 
  47         assert_select "osm[version='#{Settings.api_version}'][generator='#{Settings.generator}']", 1
 
  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
 
  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}']"
 
  63         containing_relation = create(:relation_member, :member => node).relation
 
  65         get api_node_relations_path(node, :format => "json")
 
  67         assert_response :success
 
  68         js = ActiveSupport::JSON.decode(@response.body)
 
  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"]