1 # frozen_string_literal: true
 
   7     class RelationsControllerTest < ActionDispatch::IntegrationTest
 
   9       # test all routes which lead to this controller
 
  12           { :path => "/api/0.6/relation/1/relations", :method => :get },
 
  13           { :controller => "api/relations/relations", :action => "index", :relation_id => "1" }
 
  16           { :path => "/api/0.6/relation/1/relations.json", :method => :get },
 
  17           { :controller => "api/relations/relations", :action => "index", :relation_id => "1", :format => "json" }
 
  22         relation = create(:relation)
 
  23         # should include relations with that relation as a member
 
  24         relation_with_relation = create(:relation_member, :member => relation).relation
 
  25         # should ignore any relation without that relation as a member
 
  26         _relation_without_relation = create(:relation_member).relation
 
  27         # should ignore relations with the relation involved indirectly, via a relation
 
  28         second_relation = create(:relation_member, :member => relation).relation
 
  29         _super_relation = create(:relation_member, :member => second_relation).relation
 
  30         # should combine multiple relation_member references into just one relation entry
 
  31         create(:relation_member, :member => relation, :relation => relation_with_relation)
 
  32         # should not include deleted relations
 
  33         deleted_relation = create(:relation, :deleted)
 
  34         create(:relation_member, :member => relation, :relation => deleted_relation)
 
  36         get api_relation_relations_path(relation)
 
  38         assert_response :success
 
  40         # count one osm element
 
  41         assert_select "osm[version='#{Settings.api_version}'][generator='#{Settings.generator}']", 1
 
  43         # we should have only the expected number of relations
 
  44         expected_relations = [relation_with_relation, second_relation]
 
  45         assert_select "osm>relation", expected_relations.size
 
  47         # and each of them should contain the element we originally searched for
 
  48         expected_relations.each do |containing_relation|
 
  49           # The relation should appear once, but the element could appear multiple times
 
  50           assert_select "osm>relation[id='#{containing_relation.id}']", 1
 
  51           assert_select "osm>relation[id='#{containing_relation.id}']>member[type='relation'][ref='#{relation.id}']"
 
  56         relation = create(:relation)
 
  57         containing_relation = create(:relation_member, :member => relation).relation
 
  59         get api_relation_relations_path(relation, :format => "json")
 
  61         assert_response :success
 
  62         js = ActiveSupport::JSON.decode(@response.body)
 
  64         assert_equal 1, js["elements"].count
 
  65         js_relations = js["elements"].filter { |e| e["type"] == "relation" }
 
  66         assert_equal 1, js_relations.count
 
  67         assert_equal containing_relation.id, js_relations[0]["id"]