]> git.openstreetmap.org Git - rails.git/blob - test/functional/old_node_controller_test.rb
Fixed fixtures and added new tests for ways and way_nodes.
[rails.git] / test / functional / old_node_controller_test.rb
1 require File.dirname(__FILE__) + '/../test_helper'
2 require 'old_node_controller'
3
4 # Re-raise errors caught by the controller.
5 class OldNodeController; def rescue_action(e) raise e end; end
6
7 class OldNodeControllerTest < Test::Unit::TestCase
8   api_fixtures
9
10   def setup
11     @controller = OldNodeController.new
12     @request    = ActionController::TestRequest.new
13     @response   = ActionController::TestResponse.new
14   end
15
16   #
17   # TODO: test history
18   #
19
20   ##
21   # test the version call by submitting several revisions of a new node
22   # to the API and ensuring that later calls to version return the 
23   # matching versions of the object.
24   def test_version
25     basic_authorization(users(:normal_user).email, "test")
26     changeset_id = changesets(:normal_user_first_change).id
27
28     # setup a simple XML node
29     xml_doc = current_nodes(:visible_node).to_xml
30     xml_node = xml_doc.find("//osm/node").first
31     nodeid = current_nodes(:visible_node).id
32
33     # keep a hash of the versions => string, as we'll need something
34     # to test against later
35     versions = Hash.new
36
37     # save a version for later checking
38     versions[xml_node['version']] = xml_doc.to_s
39
40     # randomly move the node about
41     20.times do 
42       # move the node somewhere else
43       xml_node['lat'] = precision(rand * 180 -  90).to_s
44       xml_node['lon'] = precision(rand * 360 - 180).to_s
45       with_controller(NodeController.new) do
46         content xml_doc
47         put :update, :id => nodeid
48         assert_response :success
49         xml_node['version'] = @response.body.to_s
50       end
51       # save a version for later checking
52       versions[xml_node['version']] = xml_doc.to_s
53     end
54
55     # add a bunch of random tags
56     30.times do 
57       xml_tag = XML::Node.new("tag")
58       xml_tag['k'] = random_string
59       xml_tag['v'] = random_string
60       xml_node << xml_tag
61       with_controller(NodeController.new) do
62         content xml_doc
63         put :update, :id => nodeid
64         assert_response :success,
65         "couldn't update node #{nodeid} (#{@response.body})"
66         xml_node['version'] = @response.body.to_s
67       end
68       # save a version for later checking
69       versions[xml_node['version']] = xml_doc.to_s
70     end
71
72     # check all the versions
73     versions.keys.each do |key|
74       get :version, :id => nodeid, :version => key.to_i
75
76       assert_response :success,
77          "couldn't get version #{key.to_i} of node #{nodeid}"
78
79       check_node = Node.from_xml(versions[key])
80       api_node = Node.from_xml(@response.body.to_s)
81
82       assert_nodes_are_equal check_node, api_node
83     end
84   end
85
86   ##
87   # Test that getting the current version is identical to picking
88   # that version with the version URI call.
89   def test_current_version
90     check_current_version(current_nodes(:visible_node))
91     check_current_version(current_nodes(:used_node_1))
92     check_current_version(current_nodes(:used_node_2))
93     check_current_version(current_nodes(:node_used_by_relationship))
94     check_current_version(current_nodes(:node_with_versions))
95   end
96   
97   def check_current_version(node_id)
98     # get the current version of the node
99     current_node = with_controller(NodeController.new) do
100       get :read, :id => node_id
101       assert_response :success, "cant get current node #{node_id}" 
102       Node.from_xml(@response.body)
103     end
104     assert_not_nil current_node, "getting node #{node_id} returned nil"
105
106     # get the "old" version of the node from the old_node interface
107     get :version, :id => node_id, :version => current_node.version
108     assert_response :success, "cant get old node #{node_id}, v#{current_node.version}" 
109     old_node = Node.from_xml(@response.body)
110
111     # check the nodes are the same
112     assert_nodes_are_equal current_node, old_node
113   end
114
115   ##
116   # for some reason a==b is false, but there doesn't seem to be any 
117   # difference between the nodes, so i'm checking all the attributes 
118   # manually and blaming it on ActiveRecord
119   def assert_nodes_are_equal(a, b)
120     assert_equal a.id, b.id, "node IDs"
121     assert_equal a.latitude, b.latitude, "latitude on node #{a.id}"
122     assert_equal a.longitude, b.longitude, "longitude on node #{a.id}"
123     assert_equal a.changeset_id, b.changeset_id, "changeset ID on node #{a.id}"
124     assert_equal a.visible, b.visible, "visible on node #{a.id}"
125     assert_equal a.version, b.version, "version on node #{a.id}"
126     assert_equal a.tags, b.tags, "tags on node #{a.id}"
127   end
128
129   ##
130   # returns a 16 character long string with some nasty characters in it.
131   # this ought to stress-test the tag handling as well as the versioning.
132   def random_string
133     letters = [['!','"','$','&',';','@'],
134                ('a'..'z').to_a,
135                ('A'..'Z').to_a,
136                ('0'..'9').to_a].flatten
137     (1..16).map { |i| letters[ rand(letters.length) ] }.join
138   end
139
140   ##
141   # truncate a floating point number to the scale that it is stored in
142   # the database. otherwise rounding errors can produce failing unit
143   # tests when they shouldn't.
144   def precision(f)
145     return (f * GeoRecord::SCALE).round.to_f / GeoRecord::SCALE
146   end
147
148   def basic_authorization(user, pass)
149     @request.env["HTTP_AUTHORIZATION"] = "Basic %s" % Base64.encode64("#{user}:#{pass}")
150   end
151
152   def content(c)
153     @request.env["RAW_POST_DATA"] = c.to_s
154   end
155
156 end