5 class RelationsControllerTest < ActionDispatch::IntegrationTest
7 # test all routes which lead to this controller
10 { :path => "/api/0.6/way/1/relations", :method => :get },
11 { :controller => "api/ways/relations", :action => "index", :way_id => "1" }
14 { :path => "/api/0.6/way/1/relations.json", :method => :get },
15 { :controller => "api/ways/relations", :action => "index", :way_id => "1", :format => "json" }
21 # should include relations with that way as a member
22 relation_with_way = create(:relation_member, :member => way).relation
23 # should ignore relations without that way as a member
24 _relation_without_way = create(:relation_member).relation
25 # should ignore relations with the way involved indirectly, via a relation
26 second_relation = create(:relation_member, :member => way).relation
27 _super_relation = create(:relation_member, :member => second_relation).relation
28 # should combine multiple relation_member references into just one relation entry
29 create(:relation_member, :member => way, :relation => relation_with_way)
30 # should not include deleted relations
31 deleted_relation = create(:relation, :deleted)
32 create(:relation_member, :member => way, :relation => deleted_relation)
34 get api_way_relations_path(way)
36 assert_response :success
38 # count one osm element
39 assert_select "osm[version='#{Settings.api_version}'][generator='#{Settings.generator}']", 1
41 # we should have only the expected number of relations
42 expected_relations = [relation_with_way, second_relation]
43 assert_select "osm>relation", expected_relations.size
45 # and each of them should contain the element we originally searched for
46 expected_relations.each do |containing_relation|
47 # The relation should appear once, but the element could appear multiple times
48 assert_select "osm>relation[id='#{containing_relation.id}']", 1
49 assert_select "osm>relation[id='#{containing_relation.id}']>member[type='way'][ref='#{way.id}']"
55 containing_relation = create(:relation_member, :member => way).relation
57 get api_way_relations_path(way, :format => "json")
59 assert_response :success
60 js = ActiveSupport::JSON.decode(@response.body)
62 assert_equal 1, js["elements"].count
63 js_relations = js["elements"].filter { |e| e["type"] == "relation" }
64 assert_equal 1, js_relations.count
65 assert_equal containing_relation.id, js_relations[0]["id"]