3 class NodeTest < ActiveSupport::TestCase
4 def test_node_too_far_north
5 node = build(:node, :latitude => 90.01 * OldNode::SCALE)
7 assert_includes node.errors.full_messages, "Node is not in the world"
10 def test_node_north_limit
11 node = build(:node, :latitude => 90 * OldNode::SCALE)
13 assert_not_includes node.errors.full_messages, "Node is not in the world"
16 def test_node_too_far_south
17 node = build(:node, :latitude => -90.01 * OldNode::SCALE)
19 assert_includes node.errors.full_messages, "Node is not in the world"
22 def test_node_south_limit
23 node = build(:node, :latitude => -90 * OldNode::SCALE)
25 assert_not_includes node.errors.full_messages, "Node is not in the world"
28 def test_node_too_far_west
29 node = build(:node, :longitude => -180.01 * OldNode::SCALE)
31 assert_includes node.errors.full_messages, "Node is not in the world"
34 def test_node_west_limit
35 node = build(:node, :longitude => -180 * OldNode::SCALE)
37 assert_not_includes node.errors.full_messages, "Node is not in the world"
40 def test_node_too_far_east
41 node = build(:node, :longitude => 180.01 * OldNode::SCALE)
43 assert_includes node.errors.full_messages, "Node is not in the world"
46 def test_node_east_limit
47 node = build(:node, :longitude => 180 * OldNode::SCALE)
49 assert_not_includes node.errors.full_messages, "Node is not in the world"
52 def test_totally_wrong
53 node = build(:node, :latitude => 200 * OldNode::SCALE, :longitude => 200 * OldNode::SCALE)
55 assert_includes node.errors.full_messages, "Node is not in the world"
59 node = build(:node, :latitude => 12.345 * OldNode::SCALE, :longitude => 34.567 * OldNode::SCALE)
61 assert_in_delta 12.345, node.lat, 0.0000001
62 assert_in_delta 34.567, node.lon, 0.0000001
67 assert_in_delta 54.321 * OldNode::SCALE, node.latitude, 0.000001
68 assert_in_delta 76.543 * OldNode::SCALE, node.longitude, 0.000001
71 # Check that you can create a node and store it
73 changeset = create(:changeset)
74 node_template = build(:node, :lat => 12.3456,
76 :changeset_id => changeset.id,
79 assert node_template.create_with_history(changeset.user)
81 node = Node.find(node_template.id)
83 assert_equal node_template.latitude, node.latitude
84 assert_equal node_template.longitude, node.longitude
85 assert_equal node_template.changeset_id, node.changeset_id
86 assert_equal node_template.visible, node.visible
87 assert_equal node_template.timestamp.to_i, node.timestamp.to_i
89 assert_equal(1, OldNode.where(:node_id => node_template.id).count)
90 old_node = OldNode.find_by(:node_id => node_template.id, :version => 1)
91 assert_not_nil old_node
92 assert_equal node_template.latitude, old_node.latitude
93 assert_equal node_template.longitude, old_node.longitude
94 assert_equal node_template.changeset_id, old_node.changeset_id
95 assert_equal node_template.visible, old_node.visible
96 assert_equal node_template.tags, old_node.tags
97 assert_equal node_template.timestamp.to_i, old_node.timestamp.to_i
102 create(:old_node, :node_id => node.id, :version => 1)
103 node_template = Node.find(node.id)
105 assert_not_nil node_template
106 assert_equal(1, OldNode.where(:node_id => node_template.id).count)
109 node_template.lat = 12.3456
110 node_template.lon = 65.4321
111 # node_template.tags = "updated=yes"
112 assert node.update_from(node_template, node.changeset.user)
114 node = Node.find(node_template.id)
116 assert_equal node_template.latitude, node.latitude
117 assert_equal node_template.longitude, node.longitude
118 assert_equal node_template.changeset_id, node.changeset_id
119 assert_equal node_template.visible, node.visible
120 # assert_equal node_template.tags, node.tags
122 assert_equal(2, OldNode.where(:node_id => node_template.id).count)
123 old_node = OldNode.find_by(:node_id => node_template.id, :version => 2)
124 assert_not_nil old_node
125 assert_equal node_template.latitude, old_node.latitude
126 assert_equal node_template.longitude, old_node.longitude
127 assert_equal node_template.changeset_id, old_node.changeset_id
128 assert_equal node_template.visible, old_node.visible
129 # assert_equal node_template.tags, old_node.tags
134 create(:old_node, :node_id => node.id, :version => 1)
135 node_template = Node.find(node.id)
137 assert_not_nil node_template
138 assert_equal(1, OldNode.where(:node_id => node_template.id).count)
141 assert node.delete_with_history!(node_template, node.changeset.user)
143 node = Node.find(node_template.id)
145 assert_equal node_template.latitude, node.latitude
146 assert_equal node_template.longitude, node.longitude
147 assert_equal node_template.changeset_id, node.changeset_id
148 assert_not node.visible
149 # assert_equal node_template.tags, node.tags
151 assert_equal(2, OldNode.where(:node_id => node_template.id).count)
152 old_node = OldNode.find_by(:node_id => node_template.id, :version => 2)
153 assert_not_nil old_node
154 assert_equal node_template.latitude, old_node.latitude
155 assert_equal node_template.longitude, old_node.longitude
156 assert_equal node_template.changeset_id, old_node.changeset_id
157 assert_not old_node.visible
158 # assert_equal node_template.tags, old_node.tags
161 def test_from_xml_no_id
166 noid = "<osm><node lat='#{lat}' lon='#{lon}' changeset='#{changeset}' version='#{version}' /></osm>"
167 # First try a create which doesn't need the id
168 assert_nothing_raised do
169 Node.from_xml(noid, :create => true)
171 # Now try an update with no id, and make sure that it gives the appropriate exception
172 message = assert_raise(OSM::APIBadXMLError) do
173 Node.from_xml(noid, :create => false)
175 assert_match(/ID is required when updating./, message.message)
178 def test_from_xml_no_lat
179 nolat = "<osm><node id='1' lon='23.3' changeset='2' version='23' /></osm>"
180 message_create = assert_raise(OSM::APIBadXMLError) do
181 Node.from_xml(nolat, :create => true)
183 assert_match(/lat missing/, message_create.message)
184 message_update = assert_raise(OSM::APIBadXMLError) do
185 Node.from_xml(nolat, :create => false)
187 assert_match(/lat missing/, message_update.message)
190 def test_from_xml_no_lon
191 nolon = "<osm><node id='1' lat='23.1' changeset='2' version='23' /></osm>"
192 message_create = assert_raise(OSM::APIBadXMLError) do
193 Node.from_xml(nolon, :create => true)
195 assert_match(/lon missing/, message_create.message)
196 message_update = assert_raise(OSM::APIBadXMLError) do
197 Node.from_xml(nolon, :create => false)
199 assert_match(/lon missing/, message_update.message)
202 def test_from_xml_no_changeset_id
203 nocs = "<osm><node id='123' lon='23.23' lat='23.1' version='23' /></osm>"
204 message_create = assert_raise(OSM::APIBadXMLError) do
205 Node.from_xml(nocs, :create => true)
207 assert_match(/Changeset id is missing/, message_create.message)
208 message_update = assert_raise(OSM::APIBadXMLError) do
209 Node.from_xml(nocs, :create => false)
211 assert_match(/Changeset id is missing/, message_update.message)
214 def test_from_xml_no_version
215 no_version = "<osm><node id='123' lat='23' lon='23' changeset='23' /></osm>"
216 assert_nothing_raised do
217 Node.from_xml(no_version, :create => true)
219 message_update = assert_raise(OSM::APIBadXMLError) do
220 Node.from_xml(no_version, :create => false)
222 assert_match(/Version is required when updating/, message_update.message)
225 def test_from_xml_double_lat
226 nocs = "<osm><node id='123' lon='23.23' lat='23.1' lat='12' changeset='23' version='23' /></osm>"
227 message_create = assert_raise(OSM::APIBadXMLError) do
228 Node.from_xml(nocs, :create => true)
230 assert_match(/Fatal error: Attribute lat redefined at/, message_create.message)
231 message_update = assert_raise(OSM::APIBadXMLError) do
232 Node.from_xml(nocs, :create => false)
234 assert_match(/Fatal error: Attribute lat redefined at/, message_update.message)
237 def test_from_xml_id_zero
238 id_list = ["", "0", "00", "0.0", "a"]
240 zero_id = "<osm><node id='#{id}' lat='12.3' lon='12.3' changeset='33' version='23' /></osm>"
241 assert_nothing_raised do
242 Node.from_xml(zero_id, :create => true)
244 message_update = assert_raise(OSM::APIBadUserInput) do
245 Node.from_xml(zero_id, :create => false)
247 assert_match(/ID of node cannot be zero when updating/, message_update.message)
251 def test_from_xml_no_text
253 message_create = assert_raise(OSM::APIBadXMLError) do
254 Node.from_xml(no_text, :create => true)
256 assert_match(/Must specify a string with one or more characters/, message_create.message)
257 message_update = assert_raise(OSM::APIBadXMLError) do
258 Node.from_xml(no_text, :create => false)
260 assert_match(/Must specify a string with one or more characters/, message_update.message)
263 def test_from_xml_no_node
264 no_node = "<osm></osm>"
265 message_create = assert_raise(OSM::APIBadXMLError) do
266 Node.from_xml(no_node, :create => true)
268 assert_match %r{XML doesn't contain an osm/node element}, message_create.message
269 message_update = assert_raise(OSM::APIBadXMLError) do
270 Node.from_xml(no_node, :create => false)
272 assert_match %r{XML doesn't contain an osm/node element}, message_update.message
275 def test_from_xml_no_k_v
276 nokv = "<osm><node id='23' lat='12.3' lon='23.4' changeset='12' version='23'><tag /></node></osm>"
277 message_create = assert_raise(OSM::APIBadXMLError) do
278 Node.from_xml(nokv, :create => true)
280 assert_match(/tag is missing key/, message_create.message)
281 message_update = assert_raise(OSM::APIBadXMLError) do
282 Node.from_xml(nokv, :create => false)
284 assert_match(/tag is missing key/, message_update.message)
287 def test_from_xml_no_v
288 no_v = "<osm><node id='23' lat='23.43' lon='23.32' changeset='23' version='32'><tag k='key' /></node></osm>"
289 message_create = assert_raise(OSM::APIBadXMLError) do
290 Node.from_xml(no_v, :create => true)
292 assert_match(/tag is missing value/, message_create.message)
293 message_update = assert_raise(OSM::APIBadXMLError) do
294 Node.from_xml(no_v, :create => false)
296 assert_match(/tag is missing value/, message_update.message)
299 def test_from_xml_duplicate_k
300 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>"
301 message_create = assert_raise(OSM::APIDuplicateTagsError) do
302 Node.from_xml(dupk, :create => true)
304 assert_equal "Element node/ has duplicate tags with key dup", message_create.message
305 message_update = assert_raise(OSM::APIDuplicateTagsError) do
306 Node.from_xml(dupk, :create => false)
308 assert_equal "Element node/23 has duplicate tags with key dup", message_update.message
313 taglist = create_list(:node_tag, 2, :node => node)
314 tags = Node.find(node.id).node_tags.order(:k)
315 assert_equal taglist.count, tags.count
316 taglist.sort_by!(&:k).each_index do |i|
317 assert_equal taglist[i].k, tags[i].k
318 assert_equal taglist[i].v, tags[i].v
324 taglist = create_list(:node_tag, 2, :node => node)
325 tags = Node.find(node.id).tags
326 assert_equal taglist.count, tags.count
327 taglist.each do |tag|
328 assert_equal tag.v, tags[tag.k]
332 def test_containing_relation_members
334 relation_member1 = create(:relation_member, :member => node)
335 relation_member2 = create(:relation_member, :member => node)
336 relation_member3 = create(:relation_member, :member => node)
337 crm = Node.find(node.id).containing_relation_members.order(:relation_id)
338 # assert_equal 3, crm.size
339 assert_equal relation_member1.relation_id, crm.first.relation_id
340 assert_equal "Node", crm.first.member_type
341 assert_equal node.id, crm.first.member_id
342 assert_equal relation_member1.relation_id, crm.first.relation.id
343 assert_equal relation_member2.relation_id, crm.second.relation_id
344 assert_equal "Node", crm.second.member_type
345 assert_equal node.id, crm.second.member_id
346 assert_equal relation_member2.relation_id, crm.second.relation.id
347 assert_equal relation_member3.relation_id, crm.third.relation_id
348 assert_equal "Node", crm.third.member_type
349 assert_equal node.id, crm.third.member_id
350 assert_equal relation_member3.relation_id, crm.third.relation.id
353 def test_containing_relations
355 relation_member1 = create(:relation_member, :member => node)
356 relation_member2 = create(:relation_member, :member => node)
357 relation_member3 = create(:relation_member, :member => node)
358 cr = Node.find(node.id).containing_relations.order(:id)
360 assert_equal 3, cr.size
361 assert_equal relation_member1.relation.id, cr.first.id
362 assert_equal relation_member2.relation.id, cr.second.id
363 assert_equal relation_member3.relation.id, cr.third.id
366 test "raises missing changeset exception when creating" do
369 assert_raises OSM::APIChangesetMissingError do
370 node.create_with_history(user)
374 test "raises user-changeset mismatch exception when creating" do
376 changeset = create(:changeset)
377 node = Node.new(:changeset => changeset)
378 assert_raises OSM::APIUserChangesetMismatchError do
379 node.create_with_history(user)
383 test "raises already closed changeset exception when creating" do
385 changeset = create(:changeset, :closed, :user => user)
386 node = Node.new(:changeset => changeset)
387 assert_raises OSM::APIChangesetAlreadyClosedError do
388 node.create_with_history(user)
392 test "raises id precondition exception when updating" do
394 node = Node.new(:id => 23)
395 new_node = Node.new(:id => 42)
396 assert_raises OSM::APIPreconditionFailedError do
397 node.update_from(new_node, user)
401 test "raises version mismatch exception when updating" do
403 node = Node.new(:id => 42, :version => 7)
404 new_node = Node.new(:id => 42, :version => 12)
405 assert_raises OSM::APIVersionMismatchError do
406 node.update_from(new_node, user)
410 test "raises missing changeset exception when updating" do
412 node = Node.new(:id => 42, :version => 12)
413 new_node = Node.new(:id => 42, :version => 12)
414 assert_raises OSM::APIChangesetMissingError do
415 node.update_from(new_node, user)
419 test "raises user-changeset mismatch exception when updating" do
421 changeset = create(:changeset)
422 node = Node.new(:id => 42, :version => 12)
423 new_node = Node.new(:id => 42, :version => 12, :changeset => changeset)
424 assert_raises OSM::APIUserChangesetMismatchError do
425 node.update_from(new_node, user)
429 test "raises already closed changeset exception when updating" do
431 changeset = create(:changeset, :closed, :user => user)
432 node = Node.new(:id => 42, :version => 12)
433 new_node = Node.new(:id => 42, :version => 12, :changeset => changeset)
434 assert_raises OSM::APIChangesetAlreadyClosedError do
435 node.update_from(new_node, user)