]> git.openstreetmap.org Git - rails.git/blob - test/controllers/api/ways/relations_controller_test.rb
Merge remote-tracking branch 'upstream/pull/5637'
[rails.git] / test / controllers / api / ways / relations_controller_test.rb
1 require "test_helper"
2
3 module Api
4   module Ways
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/way/1/relations", :method => :get },
11           { :controller => "api/ways/relations", :action => "index", :way_id => "1" }
12         )
13         assert_routing(
14           { :path => "/api/0.6/way/1/relations.json", :method => :get },
15           { :controller => "api/ways/relations", :action => "index", :way_id => "1", :format => "json" }
16         )
17       end
18
19       def test_index
20         way = create(:way)
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)
33
34         get api_way_relations_path(way)
35
36         assert_response :success
37
38         # count one osm element
39         assert_select "osm[version='#{Settings.api_version}'][generator='#{Settings.generator}']", 1
40
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
44
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}']"
50         end
51       end
52
53       def test_index_json
54         way = create(:way)
55         containing_relation = create(:relation_member, :member => way).relation
56
57         get api_way_relations_path(way, :format => "json")
58
59         assert_response :success
60         js = ActiveSupport::JSON.decode(@response.body)
61         assert_not_nil js
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"]
66       end
67     end
68   end
69 end