1 # frozen_string_literal: true
 
   7     class RelationsControllerTest < ActionDispatch::IntegrationTest
 
   9       # test all routes which lead to this controller
 
  12           { :path => "/api/0.6/way/1/relations", :method => :get },
 
  13           { :controller => "api/ways/relations", :action => "index", :way_id => "1" }
 
  16           { :path => "/api/0.6/way/1/relations.json", :method => :get },
 
  17           { :controller => "api/ways/relations", :action => "index", :way_id => "1", :format => "json" }
 
  23         # should include relations with that way as a member
 
  24         relation_with_way = create(:relation_member, :member => way).relation
 
  25         # should ignore relations without that way as a member
 
  26         _relation_without_way = create(:relation_member).relation
 
  27         # should ignore relations with the way involved indirectly, via a relation
 
  28         second_relation = create(:relation_member, :member => way).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 => way, :relation => relation_with_way)
 
  32         # should not include deleted relations
 
  33         deleted_relation = create(:relation, :deleted)
 
  34         create(:relation_member, :member => way, :relation => deleted_relation)
 
  36         get api_way_relations_path(way)
 
  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_way, 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='way'][ref='#{way.id}']"
 
  57         containing_relation = create(:relation_member, :member => way).relation
 
  59         get api_way_relations_path(way, :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"]