]> git.openstreetmap.org Git - rails.git/blob - test/controllers/api/relations/relations_controller_test.rb
Add frozen_string_literal comments to ruby files
[rails.git] / test / controllers / api / relations / relations_controller_test.rb
1 # frozen_string_literal: true
2
3 require "test_helper"
4
5 module Api
6   module Relations
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/relation/1/relations", :method => :get },
13           { :controller => "api/relations/relations", :action => "index", :relation_id => "1" }
14         )
15         assert_routing(
16           { :path => "/api/0.6/relation/1/relations.json", :method => :get },
17           { :controller => "api/relations/relations", :action => "index", :relation_id => "1", :format => "json" }
18         )
19       end
20
21       def test_index
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)
35
36         get api_relation_relations_path(relation)
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_relation, 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='relation'][ref='#{relation.id}']"
52         end
53       end
54
55       def test_index_json
56         relation = create(:relation)
57         containing_relation = create(:relation_member, :member => relation).relation
58
59         get api_relation_relations_path(relation, :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