3 class NodeTest < ActiveSupport::TestCase
7 assert_equal 19, Node.count
10 def test_node_too_far_north
11 invalid_node_test(:node_too_far_north)
14 def test_node_north_limit
15 valid_node_test(:node_north_limit)
18 def test_node_too_far_south
19 invalid_node_test(:node_too_far_south)
22 def test_node_south_limit
23 valid_node_test(:node_south_limit)
26 def test_node_too_far_west
27 invalid_node_test(:node_too_far_west)
30 def test_node_west_limit
31 valid_node_test(:node_west_limit)
34 def test_node_too_far_east
35 invalid_node_test(:node_too_far_east)
38 def test_node_east_limit
39 valid_node_test(:node_east_limit)
42 def test_totally_wrong
43 invalid_node_test(:node_totally_wrong)
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
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 / Node::SCALE
53 assert_equal dbnode.lon, node.longitude.to_f / Node::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)
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
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 / Node::SCALE
69 assert_equal dbnode.lon, node.longitude.to_f / Node::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?
78 # Check that you can create a node and store it
80 changeset = create(:changeset)
81 node_template = Node.new(
83 :longitude => 65.4321,
84 :changeset_id => changeset.id,
88 assert node_template.create_with_history(changeset.user)
90 node = Node.find(node_template.id)
92 assert_equal node_template.latitude, node.latitude
93 assert_equal node_template.longitude, node.longitude
94 assert_equal node_template.changeset_id, node.changeset_id
95 assert_equal node_template.visible, node.visible
96 assert_equal node_template.timestamp.to_i, node.timestamp.to_i
98 assert_equal OldNode.where(:node_id => node_template.id).count, 1
99 old_node = OldNode.where(:node_id => node_template.id).first
100 assert_not_nil old_node
101 assert_equal node_template.latitude, old_node.latitude
102 assert_equal node_template.longitude, old_node.longitude
103 assert_equal node_template.changeset_id, old_node.changeset_id
104 assert_equal node_template.visible, old_node.visible
105 assert_equal node_template.tags, old_node.tags
106 assert_equal node_template.timestamp.to_i, old_node.timestamp.to_i
110 node_template = Node.find(current_nodes(:visible_node).id)
111 assert_not_nil node_template
113 assert_equal OldNode.where(:node_id => node_template.id).count, 1
114 node = Node.find(node_template.id)
117 node_template.latitude = 12.3456
118 node_template.longitude = 65.4321
119 # node_template.tags = "updated=yes"
120 assert node.update_from(node_template, current_nodes(:visible_node).changeset.user)
122 node = Node.find(node_template.id)
124 assert_equal node_template.latitude, node.latitude
125 assert_equal node_template.longitude, node.longitude
126 assert_equal node_template.changeset_id, node.changeset_id
127 assert_equal node_template.visible, node.visible
128 # assert_equal node_template.tags, node.tags
130 assert_equal OldNode.where(:node_id => node_template.id).count, 2
131 old_node = OldNode.where(:node_id => node_template.id, :version => 2).first
132 assert_not_nil old_node
133 assert_equal node_template.latitude, old_node.latitude
134 assert_equal node_template.longitude, old_node.longitude
135 assert_equal node_template.changeset_id, old_node.changeset_id
136 assert_equal node_template.visible, old_node.visible
137 # assert_equal node_template.tags, old_node.tags
141 node_template = Node.find(current_nodes(:visible_node).id)
142 assert_not_nil node_template
144 assert_equal OldNode.where(:node_id => node_template.id).count, 1
145 node = Node.find(node_template.id)
148 assert node.delete_with_history!(node_template, current_nodes(:visible_node).changeset.user)
150 node = Node.find(node_template.id)
152 assert_equal node_template.latitude, node.latitude
153 assert_equal node_template.longitude, node.longitude
154 assert_equal node_template.changeset_id, node.changeset_id
155 assert_equal false, node.visible
156 # assert_equal node_template.tags, node.tags
158 assert_equal OldNode.where(:node_id => node_template.id).count, 2
159 old_node = OldNode.where(:node_id => node_template.id, :version => 2).first
160 assert_not_nil old_node
161 assert_equal node_template.latitude, old_node.latitude
162 assert_equal node_template.longitude, old_node.longitude
163 assert_equal node_template.changeset_id, old_node.changeset_id
164 assert_equal false, old_node.visible
165 # assert_equal node_template.tags, old_node.tags
168 def test_from_xml_no_id
173 noid = "<osm><node lat='#{lat}' lon='#{lon}' changeset='#{changeset}' version='#{version}' /></osm>"
174 # First try a create which doesn't need the id
175 assert_nothing_raised(OSM::APIBadXMLError) do
176 Node.from_xml(noid, true)
178 # Now try an update with no id, and make sure that it gives the appropriate exception
179 message = assert_raise(OSM::APIBadXMLError) do
180 Node.from_xml(noid, false)
182 assert_match /ID is required when updating./, message.message
185 def test_from_xml_no_lat
186 nolat = "<osm><node id='1' lon='23.3' changeset='2' version='23' /></osm>"
187 message_create = assert_raise(OSM::APIBadXMLError) do
188 Node.from_xml(nolat, true)
190 assert_match /lat missing/, message_create.message
191 message_update = assert_raise(OSM::APIBadXMLError) do
192 Node.from_xml(nolat, false)
194 assert_match /lat missing/, message_update.message
197 def test_from_xml_no_lon
198 nolon = "<osm><node id='1' lat='23.1' changeset='2' version='23' /></osm>"
199 message_create = assert_raise(OSM::APIBadXMLError) do
200 Node.from_xml(nolon, true)
202 assert_match /lon missing/, message_create.message
203 message_update = assert_raise(OSM::APIBadXMLError) do
204 Node.from_xml(nolon, false)
206 assert_match /lon missing/, message_update.message
209 def test_from_xml_no_changeset_id
210 nocs = "<osm><node id='123' lon='23.23' lat='23.1' version='23' /></osm>"
211 message_create = assert_raise(OSM::APIBadXMLError) do
212 Node.from_xml(nocs, true)
214 assert_match /Changeset id is missing/, message_create.message
215 message_update = assert_raise(OSM::APIBadXMLError) do
216 Node.from_xml(nocs, false)
218 assert_match /Changeset id is missing/, message_update.message
221 def test_from_xml_no_version
222 no_version = "<osm><node id='123' lat='23' lon='23' changeset='23' /></osm>"
223 assert_nothing_raised(OSM::APIBadXMLError) do
224 Node.from_xml(no_version, true)
226 message_update = assert_raise(OSM::APIBadXMLError) do
227 Node.from_xml(no_version, false)
229 assert_match /Version is required when updating/, message_update.message
232 def test_from_xml_double_lat
233 nocs = "<osm><node id='123' lon='23.23' lat='23.1' lat='12' changeset='23' version='23' /></osm>"
234 message_create = assert_raise(OSM::APIBadXMLError) do
235 Node.from_xml(nocs, true)
237 assert_match /Fatal error: Attribute lat redefined at/, message_create.message
238 message_update = assert_raise(OSM::APIBadXMLError) do
239 Node.from_xml(nocs, false)
241 assert_match /Fatal error: Attribute lat redefined at/, message_update.message
244 def test_from_xml_id_zero
245 id_list = ["", "0", "00", "0.0", "a"]
247 zero_id = "<osm><node id='#{id}' lat='12.3' lon='12.3' changeset='33' version='23' /></osm>"
248 assert_nothing_raised(OSM::APIBadUserInput) do
249 Node.from_xml(zero_id, true)
251 message_update = assert_raise(OSM::APIBadUserInput) do
252 Node.from_xml(zero_id, false)
254 assert_match /ID of node cannot be zero when updating/, message_update.message
258 def test_from_xml_no_text
260 message_create = assert_raise(OSM::APIBadXMLError) do
261 Node.from_xml(no_text, true)
263 assert_match /Must specify a string with one or more characters/, message_create.message
264 message_update = assert_raise(OSM::APIBadXMLError) do
265 Node.from_xml(no_text, false)
267 assert_match /Must specify a string with one or more characters/, message_update.message
270 def test_from_xml_no_node
271 no_node = "<osm></osm>"
272 message_create = assert_raise(OSM::APIBadXMLError) do
273 Node.from_xml(no_node, true)
275 assert_match %r{XML doesn't contain an osm/node element}, message_create.message
276 message_update = assert_raise(OSM::APIBadXMLError) do
277 Node.from_xml(no_node, false)
279 assert_match %r{XML doesn't contain an osm/node element}, message_update.message
282 def test_from_xml_no_k_v
283 nokv = "<osm><node id='23' lat='12.3' lon='23.4' changeset='12' version='23'><tag /></node></osm>"
284 message_create = assert_raise(OSM::APIBadXMLError) do
285 Node.from_xml(nokv, true)
287 assert_match /tag is missing key/, message_create.message
288 message_update = assert_raise(OSM::APIBadXMLError) do
289 Node.from_xml(nokv, false)
291 assert_match /tag is missing key/, message_update.message
294 def test_from_xml_no_v
295 no_v = "<osm><node id='23' lat='23.43' lon='23.32' changeset='23' version='32'><tag k='key' /></node></osm>"
296 message_create = assert_raise(OSM::APIBadXMLError) do
297 Node.from_xml(no_v, true)
299 assert_match /tag is missing value/, message_create.message
300 message_update = assert_raise(OSM::APIBadXMLError) do
301 Node.from_xml(no_v, false)
303 assert_match /tag is missing value/, message_update.message
306 def test_from_xml_duplicate_k
307 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>"
308 message_create = assert_raise(OSM::APIDuplicateTagsError) do
309 Node.from_xml(dupk, true)
311 assert_equal "Element node/ has duplicate tags with key dup", message_create.message
312 message_update = assert_raise(OSM::APIDuplicateTagsError) do
313 Node.from_xml(dupk, false)
315 assert_equal "Element node/23 has duplicate tags with key dup", message_update.message
319 node = current_nodes(:node_with_versions)
320 taglist = create_list(:node_tag, 2, :node => node)
321 tags = Node.find(node.id).node_tags.order(:k)
322 assert_equal taglist.count, tags.count
323 taglist.sort_by!(&:k).each_index do |i|
324 assert_equal taglist[i].k, tags[i].k
325 assert_equal taglist[i].v, tags[i].v
330 node = current_nodes(:node_with_versions)
331 taglist = create_list(:node_tag, 2, :node => node)
332 tags = Node.find(node.id).tags
333 assert_equal taglist.count, tags.count
334 taglist.each do |tag|
335 assert_equal tag.v, tags[tag.k]
339 def test_containing_relation_members
340 node = current_nodes(:node_used_by_relationship)
341 crm = Node.find(node.id).containing_relation_members.order(:relation_id)
342 # assert_equal 3, crm.size
343 assert_equal 1, crm.first.relation_id
344 assert_equal "Node", crm.first.member_type
345 assert_equal node.id, crm.first.member_id
346 assert_equal 1, crm.first.relation.id
347 assert_equal 2, crm.second.relation_id
348 assert_equal "Node", crm.second.member_type
349 assert_equal node.id, crm.second.member_id
350 assert_equal 2, crm.second.relation.id
351 assert_equal 3, crm.third.relation_id
352 assert_equal "Node", crm.third.member_type
353 assert_equal node.id, crm.third.member_id
354 assert_equal 3, crm.third.relation.id
357 def test_containing_relations
358 node = current_nodes(:node_used_by_relationship)
359 cr = Node.find(node.id).containing_relations.order(:id)
360 assert_equal 3, cr.size
361 assert_equal 1, cr.first.id
362 assert_equal 2, cr.second.id
363 assert_equal 3, cr.third.id