]> git.openstreetmap.org Git - rails.git/blob - test/controllers/api/ways/relations_controller_test.rb
Add frozen_string_literal comments to ruby files
[rails.git] / test / controllers / api / ways / relations_controller_test.rb
1 # frozen_string_literal: true
2
3 require "test_helper"
4
5 module Api
6   module Ways
7     class RelationsControllerTest < ActionDispatch::IntegrationTest
8       ##
9       # test all routes which lead to this controller
10       def test_routes
11         assert_routing(
12           { :path => "/api/0.6/way/1/relations", :method => :get },
13           { :controller => "api/ways/relations", :action => "index", :way_id => "1" }
14         )
15         assert_routing(
16           { :path => "/api/0.6/way/1/relations.json", :method => :get },
17           { :controller => "api/ways/relations", :action => "index", :way_id => "1", :format => "json" }
18         )
19       end
20
21       def test_index
22         way = create(:way)
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)
35
36         get api_way_relations_path(way)
37
38         assert_response :success
39
40         # count one osm element
41         assert_select "osm[version='#{Settings.api_version}'][generator='#{Settings.generator}']", 1
42
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
46
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}']"
52         end
53       end
54
55       def test_index_json
56         way = create(:way)
57         containing_relation = create(:relation_member, :member => way).relation
58
59         get api_way_relations_path(way, :format => "json")
60
61         assert_response :success
62         js = ActiveSupport::JSON.decode(@response.body)
63         assert_not_nil js
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"]
68       end
69     end
70   end
71 end