]> git.openstreetmap.org Git - rails.git/blob - test/controllers/api/relations/relations_controller_test.rb
Declare api relation relations as nested resources
[rails.git] / test / controllers / api / relations / relations_controller_test.rb
1 require "test_helper"
2
3 module Api
4   module Relations
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/relation/1/relations", :method => :get },
11           { :controller => "api/relations/relations", :action => "index", :relation_id => "1" }
12         )
13         assert_routing(
14           { :path => "/api/0.6/relation/1/relations.json", :method => :get },
15           { :controller => "api/relations/relations", :action => "index", :relation_id => "1", :format => "json" }
16         )
17       end
18
19       def test_index
20         relation = create(:relation)
21         # should include relations with that relation as a member
22         relation_with_relation = create(:relation_member, :member => relation).relation
23         # should ignore any relation without that relation as a member
24         _relation_without_relation = create(:relation_member).relation
25         # should ignore relations with the relation involved indirectly, via a relation
26         second_relation = create(:relation_member, :member => relation).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 => relation, :relation => relation_with_relation)
30         # should not include deleted relations
31         deleted_relation = create(:relation, :deleted)
32         create(:relation_member, :member => relation, :relation => deleted_relation)
33
34         get api_relation_relations_path(relation)
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_relation, 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='relation'][ref='#{relation.id}']"
50         end
51       end
52
53       def test_index_json
54         relation = create(:relation)
55         containing_relation = create(:relation_member, :member => relation).relation
56
57         get api_relation_relations_path(relation, :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