]> git.openstreetmap.org Git - rails.git/blob - test/unit/node_test.rb
Fix ruby 1.9 syntax error caused by space before parens on method call
[rails.git] / test / unit / node_test.rb
1 require File.dirname(__FILE__) + '/../test_helper'
2
3 class NodeTest < ActiveSupport::TestCase
4   api_fixtures
5   
6   def test_node_count
7     assert_equal 16, Node.count
8   end
9
10   def test_node_too_far_north
11     invalid_node_test(:node_too_far_north)
12   end
13   
14   def test_node_north_limit
15     valid_node_test(:node_north_limit)
16   end
17   
18   def test_node_too_far_south
19     invalid_node_test(:node_too_far_south)
20   end
21   
22   def test_node_south_limit
23     valid_node_test(:node_south_limit)
24   end
25   
26   def test_node_too_far_west
27     invalid_node_test(:node_too_far_west)
28   end
29   
30   def test_node_west_limit
31     valid_node_test(:node_west_limit)
32   end
33   
34   def test_node_too_far_east
35     invalid_node_test(:node_too_far_east)
36   end
37   
38   def test_node_east_limit
39     valid_node_test(:node_east_limit)
40   end
41   
42   def test_totally_wrong
43     invalid_node_test(:node_totally_wrong)
44   end
45   
46   # This helper method will check to make sure that a node is within the world, and
47   # has the the same lat, lon and timestamp than what was put into the db by 
48   # the fixture
49   def valid_node_test(nod)
50     node = current_nodes(nod)
51     dbnode = Node.find(node.id)
52     assert_equal dbnode.lat, node.latitude.to_f/SCALE
53     assert_equal dbnode.lon, node.longitude.to_f/SCALE
54     assert_equal dbnode.changeset_id, node.changeset_id
55     assert_equal dbnode.timestamp, node.timestamp
56     assert_equal dbnode.version, node.version
57     assert_equal dbnode.visible, node.visible
58     #assert_equal node.tile, QuadTile.tile_for_point(node.lat, node.lon)
59     assert node.valid?
60   end
61   
62   # This helper method will check to make sure that a node is outwith the world, 
63   # and has the same lat, lon and timesamp than what was put into the db by the
64   # fixture
65   def invalid_node_test(nod)
66     node = current_nodes(nod)
67     dbnode = Node.find(node.id)
68     assert_equal dbnode.lat, node.latitude.to_f/SCALE
69     assert_equal dbnode.lon, node.longitude.to_f/SCALE
70     assert_equal dbnode.changeset_id, node.changeset_id
71     assert_equal dbnode.timestamp, node.timestamp
72     assert_equal dbnode.version, node.version
73     assert_equal dbnode.visible, node.visible
74     #assert_equal node.tile, QuadTile.tile_for_point(node.lat, node.lon)
75     assert_equal false, dbnode.valid?
76   end
77   
78   # Check that you can create a node and store it
79   def test_create
80     node_template = Node.new(:latitude => 12.3456,
81                              :longitude => 65.4321,
82                              :changeset_id => changesets(:normal_user_first_change).id,
83                              :visible => 1, 
84                              :version => 1)
85     assert node_template.create_with_history(users(:normal_user))
86
87     node = Node.find(node_template.id)
88     assert_not_nil node
89     assert_equal node_template.latitude, node.latitude
90     assert_equal node_template.longitude, node.longitude
91     assert_equal node_template.changeset_id, node.changeset_id
92     assert_equal node_template.visible, node.visible
93     assert_equal node_template.timestamp.to_i, node.timestamp.to_i
94
95     assert_equal OldNode.where(:node_id => node_template.id).count, 1
96     old_node = OldNode.where(:node_id => node_template.id).first
97     assert_not_nil old_node
98     assert_equal node_template.latitude, old_node.latitude
99     assert_equal node_template.longitude, old_node.longitude
100     assert_equal node_template.changeset_id, old_node.changeset_id
101     assert_equal node_template.visible, old_node.visible
102     assert_equal node_template.tags, old_node.tags
103     assert_equal node_template.timestamp.to_i, old_node.timestamp.to_i
104   end
105
106   def test_update
107     node_template = Node.find(current_nodes(:visible_node).id)
108     assert_not_nil node_template
109
110     assert_equal OldNode.where(:node_id => node_template.id).count, 1
111     node = Node.find(node_template.id)
112     assert_not_nil node
113
114     node_template.latitude = 12.3456
115     node_template.longitude = 65.4321
116     #node_template.tags = "updated=yes"
117     assert node.update_from(node_template, users(:normal_user))
118
119     node = Node.find(node_template.id)
120     assert_not_nil node
121     assert_equal node_template.latitude, node.latitude
122     assert_equal node_template.longitude, node.longitude
123     assert_equal node_template.changeset_id, node.changeset_id
124     assert_equal node_template.visible, node.visible
125     #assert_equal node_template.tags, node.tags
126
127     assert_equal OldNode.where(:node_id => node_template.id).count, 2
128     old_node = OldNode.where(:node_id => node_template.id, :version => 2).first
129     assert_not_nil old_node
130     assert_equal node_template.latitude, old_node.latitude
131     assert_equal node_template.longitude, old_node.longitude
132     assert_equal node_template.changeset_id, old_node.changeset_id
133     assert_equal node_template.visible, old_node.visible
134     #assert_equal node_template.tags, old_node.tags
135   end
136
137   def test_delete
138     node_template = Node.find(current_nodes(:visible_node))
139     assert_not_nil node_template
140
141     assert_equal OldNode.where(:node_id => node_template.id).count, 1
142     node = Node.find(node_template.id)
143     assert_not_nil node
144
145     assert node.delete_with_history!(node_template, users(:normal_user))
146
147     node = Node.find(node_template.id)
148     assert_not_nil node
149     assert_equal node_template.latitude, node.latitude
150     assert_equal node_template.longitude, node.longitude
151     assert_equal node_template.changeset_id, node.changeset_id
152     assert_equal false, node.visible
153     #assert_equal node_template.tags, node.tags
154
155     assert_equal OldNode.where(:node_id => node_template.id).count, 2
156     old_node = OldNode.where(:node_id => node_template.id, :version => 2).first
157     assert_not_nil old_node
158     assert_equal node_template.latitude, old_node.latitude
159     assert_equal node_template.longitude, old_node.longitude
160     assert_equal node_template.changeset_id, old_node.changeset_id
161     assert_equal false, old_node.visible
162     #assert_equal node_template.tags, old_node.tags
163   end
164   
165   def test_from_xml_no_id
166     lat = 56.7
167     lon = -2.3
168     changeset = 2
169     version = 1
170     noid = "<osm><node lat='#{lat}' lon='#{lon}' changeset='#{changeset}' version='#{version}' /></osm>"
171     # First try a create which doesn't need the id
172     assert_nothing_raised(OSM::APIBadXMLError) {
173       Node.from_xml(noid, true)
174     }
175     # Now try an update with no id, and make sure that it gives the appropriate exception
176     message = assert_raise(OSM::APIBadXMLError) {
177       Node.from_xml(noid, false)
178     }
179     assert_match /ID is required when updating./, message.message
180   end
181   
182   def test_from_xml_no_lat
183     nolat = "<osm><node id='1' lon='23.3' changeset='2' version='23' /></osm>"
184     message_create = assert_raise(OSM::APIBadXMLError) {
185       Node.from_xml(nolat, true)
186     }
187     assert_match /lat missing/, message_create.message
188     message_update = assert_raise(OSM::APIBadXMLError) {
189       Node.from_xml(nolat, false)
190     }
191     assert_match /lat missing/, message_update.message
192   end
193   
194   def test_from_xml_no_lon
195     nolon = "<osm><node id='1' lat='23.1' changeset='2' version='23' /></osm>"
196     message_create = assert_raise(OSM::APIBadXMLError) {
197       Node.from_xml(nolon, true)
198     }
199     assert_match /lon missing/, message_create.message
200     message_update = assert_raise(OSM::APIBadXMLError) {
201       Node.from_xml(nolon, false)
202     }
203     assert_match /lon missing/, message_update.message
204   end
205
206   def test_from_xml_no_changeset_id
207     nocs = "<osm><node id='123' lon='23.23' lat='23.1' version='23' /></osm>"
208     message_create = assert_raise(OSM::APIBadXMLError) {
209       Node.from_xml(nocs, true)
210     }
211     assert_match /Changeset id is missing/, message_create.message
212     message_update = assert_raise(OSM::APIBadXMLError) {
213       Node.from_xml(nocs, false)
214     }
215     assert_match /Changeset id is missing/, message_update.message
216   end
217   
218   def test_from_xml_no_version
219     no_version = "<osm><node id='123' lat='23' lon='23' changeset='23' /></osm>"
220     assert_nothing_raised(OSM::APIBadXMLError) {
221       Node.from_xml(no_version, true)
222     }
223     message_update = assert_raise(OSM::APIBadXMLError) {
224       Node.from_xml(no_version, false)
225     }
226     assert_match /Version is required when updating/, message_update.message
227   end
228   
229   def test_from_xml_double_lat
230     nocs = "<osm><node id='123' lon='23.23' lat='23.1' lat='12' changeset='23' version='23' /></osm>"
231     message_create = assert_raise(OSM::APIBadXMLError) {
232       Node.from_xml(nocs, true)
233     } 
234     assert_match /Fatal error: Attribute lat redefined at/, message_create.message
235     message_update = assert_raise(OSM::APIBadXMLError) {
236       Node.from_xml(nocs, false)
237     }
238     assert_match /Fatal error: Attribute lat redefined at/, message_update.message
239   end
240   
241   def test_from_xml_id_zero
242     id_list = ["", "0", "00", "0.0", "a"]
243     id_list.each do |id|
244       zero_id = "<osm><node id='#{id}' lat='12.3' lon='12.3' changeset='33' version='23' /></osm>"
245       assert_nothing_raised(OSM::APIBadUserInput) {
246         Node.from_xml(zero_id, true)
247       }
248       message_update = assert_raise(OSM::APIBadUserInput) {
249         Node.from_xml(zero_id, false)
250       }
251       assert_match /ID of node cannot be zero when updating/, message_update.message
252     end
253   end
254   
255   def test_from_xml_no_text
256     no_text = ""
257     message_create = assert_raise(OSM::APIBadXMLError) {
258       Node.from_xml(no_text, true)
259     }
260     assert_match /Must specify a string with one or more characters/, message_create.message
261     message_update = assert_raise(OSM::APIBadXMLError) {
262       Node.from_xml(no_text, false)
263     }
264     assert_match /Must specify a string with one or more characters/, message_update.message
265   end
266   
267   def test_from_xml_no_node
268     no_node = "<osm></osm>"
269     message_create = assert_raise(OSM::APIBadXMLError) {
270       Node.from_xml(no_node, true)
271     }
272     assert_match /XML doesn't contain an osm\/node element/, message_create.message
273     message_update = assert_raise(OSM::APIBadXMLError) {
274       Node.from_xml(no_node, false)
275     }
276     assert_match /XML doesn't contain an osm\/node element/, message_update.message
277   end
278   
279   def test_from_xml_no_k_v
280     nokv = "<osm><node id='23' lat='12.3' lon='23.4' changeset='12' version='23'><tag /></node></osm>"
281     message_create = assert_raise(OSM::APIBadXMLError) {
282       Node.from_xml(nokv, true)
283     }
284     assert_match /tag is missing key/, message_create.message
285     message_update = assert_raise(OSM::APIBadXMLError) {
286       Node.from_xml(nokv, false)
287     }
288     assert_match /tag is missing key/, message_update.message
289   end
290   
291   def test_from_xml_no_v
292     no_v = "<osm><node id='23' lat='23.43' lon='23.32' changeset='23' version='32'><tag k='key' /></node></osm>"
293     message_create = assert_raise(OSM::APIBadXMLError) {
294       Node.from_xml(no_v, true)
295     }
296     assert_match /tag is missing value/, message_create.message
297     message_update = assert_raise(OSM::APIBadXMLError) {
298       Node.from_xml(no_v, false)
299     }
300     assert_match /tag is missing value/, message_update.message
301   end
302   
303   def test_from_xml_duplicate_k
304     dupk = "<osm><node id='23' lat='23.2' lon='23' changeset='34' version='23'><tag k='dup' v='test' /><tag k='dup' v='tester' /></node></osm>"
305     message_create = assert_raise(OSM::APIDuplicateTagsError) {
306       Node.from_xml(dupk, true)
307     }
308     assert_equal "Element node/ has duplicate tags with key dup", message_create.message
309     message_update = assert_raise(OSM::APIDuplicateTagsError) {
310       Node.from_xml(dupk, false)
311     }
312     assert_equal "Element node/23 has duplicate tags with key dup", message_update.message
313   end
314 end