]> git.openstreetmap.org Git - rails.git/blob - test/controllers/api/changes_controller_test.rb
Merge remote-tracking branch 'upstream/pull/2225'
[rails.git] / test / controllers / api / changes_controller_test.rb
1 require "test_helper"
2
3 module Api
4   class ChangesControllerTest < ActionController::TestCase
5     ##
6     # test all routes which lead to this controller
7     def test_routes
8       assert_routing(
9         { :path => "/api/0.6/changes", :method => :get },
10         { :controller => "api/changes", :action => "index" }
11       )
12     end
13
14     # MySQL and Postgres require that the C based functions are installed for
15     # this test to work. More information is available from:
16     # http://wiki.openstreetmap.org/wiki/Rails#Installing_the_quadtile_functions
17     # or by looking at the readme in db/README
18     def test_changes_simple
19       # create a selection of nodes
20       (1..5).each do |n|
21         create(:node, :timestamp => Time.utc(2007, 1, 1, 0, 0, 0), :lat => n, :lon => n)
22       end
23       # deleted nodes should also be counted
24       create(:node, :deleted, :timestamp => Time.utc(2007, 1, 1, 0, 0, 0), :lat => 6, :lon => 6)
25       # nodes in the same tile won't change the total
26       create(:node, :timestamp => Time.utc(2007, 1, 1, 0, 0, 0), :lat => 6, :lon => 6)
27       # nodes with a different timestamp should be ignored
28       create(:node, :timestamp => Time.utc(2008, 1, 1, 0, 0, 0), :lat => 7, :lon => 7)
29
30       travel_to Time.utc(2010, 4, 3, 10, 55, 0) do
31         get :index
32         assert_response :success
33         now = Time.now.getutc
34         hourago = now - 1.hour
35         assert_select "osm[version='#{Settings.api_version}'][generator='#{Settings.generator}']", :count => 1 do
36           assert_select "changes[starttime='#{hourago.xmlschema}'][endtime='#{now.xmlschema}']", :count => 1 do
37             assert_select "tile", :count => 0
38           end
39         end
40       end
41
42       travel_to Time.utc(2007, 1, 1, 0, 30, 0) do
43         get :index
44         assert_response :success
45         # print @response.body
46         # As we have loaded the fixtures, we can assume that there are some
47         # changes at the time we have frozen at
48         now = Time.now.getutc
49         hourago = now - 1.hour
50         assert_select "osm[version='#{Settings.api_version}'][generator='#{Settings.generator}']", :count => 1 do
51           assert_select "changes[starttime='#{hourago.xmlschema}'][endtime='#{now.xmlschema}']", :count => 1 do
52             assert_select "tile", :count => 6
53           end
54         end
55       end
56     end
57
58     def test_changes_zoom_invalid
59       zoom_to_test = %w[p -1 0 17 one two]
60       zoom_to_test.each do |zoom|
61         get :index, :params => { :zoom => zoom }
62         assert_response :bad_request
63         assert_equal @response.body, "Requested zoom is invalid, or the supplied start is after the end time, or the start duration is more than 24 hours"
64       end
65     end
66
67     def test_changes_zoom_valid
68       1.upto(16) do |zoom|
69         get :index, :params => { :zoom => zoom }
70         assert_response :success
71         # NOTE: there was a test here for the timing, but it was too sensitive to be a good test
72         # and it was annoying.
73         assert_select "osm[version='#{Settings.api_version}'][generator='#{Settings.generator}']", :count => 1 do
74           assert_select "changes", :count => 1
75         end
76       end
77     end
78
79     def test_changes_hours_invalid
80       invalid = %w[-21 335 -1 0 25 26 100 one two three ping pong :]
81       invalid.each do |hour|
82         get :index, :params => { :hours => hour }
83         assert_response :bad_request, "Problem with the hour: #{hour}"
84         assert_equal @response.body, "Requested zoom is invalid, or the supplied start is after the end time, or the start duration is more than 24 hours", "Problem with the hour: #{hour}."
85       end
86     end
87
88     def test_changes_hours_valid
89       1.upto(24) do |hour|
90         get :index, :params => { :hours => hour }
91         assert_response :success
92       end
93     end
94
95     def test_changes_start_end_invalid
96       get :index, :params => { :start => "2010-04-03 10:55:00", :end => "2010-04-03 09:55:00" }
97       assert_response :bad_request
98       assert_equal @response.body, "Requested zoom is invalid, or the supplied start is after the end time, or the start duration is more than 24 hours"
99     end
100
101     def test_changes_start_end_valid
102       get :index, :params => { :start => "2010-04-03 09:55:00", :end => "2010-04-03 10:55:00" }
103       assert_response :success
104     end
105   end
106 end