1 # frozen_string_literal: true
5 class NodeTest < ActiveSupport::TestCase
6 def test_node_too_far_north
7 node = build(:node, :latitude => 90.01 * OldNode::SCALE)
9 assert_includes node.errors.full_messages, "Node is not in the world"
12 def test_node_north_limit
13 node = build(:node, :latitude => 90 * OldNode::SCALE)
15 assert_not_includes node.errors.full_messages, "Node is not in the world"
18 def test_node_too_far_south
19 node = build(:node, :latitude => -90.01 * OldNode::SCALE)
21 assert_includes node.errors.full_messages, "Node is not in the world"
24 def test_node_south_limit
25 node = build(:node, :latitude => -90 * OldNode::SCALE)
27 assert_not_includes node.errors.full_messages, "Node is not in the world"
30 def test_node_too_far_west
31 node = build(:node, :longitude => -180.01 * OldNode::SCALE)
33 assert_includes node.errors.full_messages, "Node is not in the world"
36 def test_node_west_limit
37 node = build(:node, :longitude => -180 * OldNode::SCALE)
39 assert_not_includes node.errors.full_messages, "Node is not in the world"
42 def test_node_too_far_east
43 node = build(:node, :longitude => 180.01 * OldNode::SCALE)
45 assert_includes node.errors.full_messages, "Node is not in the world"
48 def test_node_east_limit
49 node = build(:node, :longitude => 180 * OldNode::SCALE)
51 assert_not_includes node.errors.full_messages, "Node is not in the world"
54 def test_totally_wrong
55 node = build(:node, :latitude => 200 * OldNode::SCALE, :longitude => 200 * OldNode::SCALE)
57 assert_includes node.errors.full_messages, "Node is not in the world"
61 node = build(:node, :latitude => 12.345 * OldNode::SCALE, :longitude => 34.567 * OldNode::SCALE)
63 assert_in_delta 12.345, node.lat, 0.0000001
64 assert_in_delta 34.567, node.lon, 0.0000001
69 assert_in_delta 54.321 * OldNode::SCALE, node.latitude, 0.000001
70 assert_in_delta 76.543 * OldNode::SCALE, node.longitude, 0.000001
73 # Check that you can create a node and store it
75 changeset = create(:changeset)
76 node_template = build(:node, :lat => 12.3456,
78 :changeset_id => changeset.id,
81 assert node_template.create_with_history(changeset.user)
83 node = Node.find(node_template.id)
85 assert_equal node_template.latitude, node.latitude
86 assert_equal node_template.longitude, node.longitude
87 assert_equal node_template.changeset_id, node.changeset_id
88 assert_equal node_template.visible, node.visible
89 assert_equal node_template.timestamp.to_i, node.timestamp.to_i
91 assert_equal(1, OldNode.where(:node_id => node_template.id).count)
92 old_node = OldNode.find_by(:node_id => node_template.id, :version => 1)
93 assert_not_nil old_node
94 assert_equal node_template.latitude, old_node.latitude
95 assert_equal node_template.longitude, old_node.longitude
96 assert_equal node_template.changeset_id, old_node.changeset_id
97 assert_equal node_template.visible, old_node.visible
98 assert_equal node_template.tags, old_node.tags
99 assert_equal node_template.timestamp.to_i, old_node.timestamp.to_i
103 node = create(:node, :lat => 12.6543, :lon => 65.1234)
104 create(:old_node, :node_id => node.id, :version => 1, :lat => node.lat, :lon => node.lon)
106 node_template = Node.find(node.id)
108 assert_not_nil node_template
109 assert_equal(1, OldNode.where(:node_id => node_template.id).count)
112 node_template.lat = 12.3456
113 node_template.lon = 65.4321
114 # node_template.tags = "updated=yes"
115 assert node.update_from(node_template, node.changeset.user)
117 node = Node.find(node_template.id)
119 assert_equal node_template.latitude, node.latitude
120 assert_equal node_template.longitude, node.longitude
121 assert_equal node_template.changeset_id, node.changeset_id
122 assert_equal node_template.visible, node.visible
123 # assert_equal node_template.tags, node.tags
125 assert_equal(2, OldNode.where(:node_id => node_template.id).count)
126 old_node = OldNode.find_by(:node_id => node_template.id, :version => 2)
127 assert_not_nil old_node
128 assert_equal node_template.latitude, old_node.latitude
129 assert_equal node_template.longitude, old_node.longitude
130 assert_equal node_template.changeset_id, old_node.changeset_id
131 assert_equal node_template.visible, old_node.visible
132 # assert_equal node_template.tags, old_node.tags
137 create(:old_node, :node_id => node.id, :version => 1)
138 node_template = Node.find(node.id)
140 assert_not_nil node_template
141 assert_equal(1, OldNode.where(:node_id => node_template.id).count)
144 assert node.delete_with_history!(node_template, node.changeset.user)
146 node = Node.find(node_template.id)
148 assert_equal node_template.latitude, node.latitude
149 assert_equal node_template.longitude, node.longitude
150 assert_equal node_template.changeset_id, node.changeset_id
151 assert_not node.visible
152 # assert_equal node_template.tags, node.tags
154 assert_equal(2, OldNode.where(:node_id => node_template.id).count)
155 old_node = OldNode.find_by(:node_id => node_template.id, :version => 2)
156 assert_not_nil old_node
157 assert_equal node_template.latitude, old_node.latitude
158 assert_equal node_template.longitude, old_node.longitude
159 assert_equal node_template.changeset_id, old_node.changeset_id
160 assert_not old_node.visible
161 # assert_equal node_template.tags, old_node.tags
164 def test_from_xml_no_id
169 noid = "<osm><node lat='#{lat}' lon='#{lon}' changeset='#{changeset}' version='#{version}' /></osm>"
170 # First try a create which doesn't need the id
171 assert_nothing_raised do
172 Node.from_xml(noid, :create => true)
174 # Now try an update with no id, and make sure that it gives the appropriate exception
175 message = assert_raise(OSM::APIBadXMLError) do
176 Node.from_xml(noid, :create => false)
178 assert_match(/ID is required when updating./, message.message)
181 def test_from_xml_no_lat
182 nolat = "<osm><node id='1' lon='23.3' changeset='2' version='23' /></osm>"
183 message_create = assert_raise(OSM::APIBadXMLError) do
184 Node.from_xml(nolat, :create => true)
186 assert_match(/lat missing/, message_create.message)
187 message_update = assert_raise(OSM::APIBadXMLError) do
188 Node.from_xml(nolat, :create => false)
190 assert_match(/lat missing/, message_update.message)
193 def test_from_xml_no_lon
194 nolon = "<osm><node id='1' lat='23.1' changeset='2' version='23' /></osm>"
195 message_create = assert_raise(OSM::APIBadXMLError) do
196 Node.from_xml(nolon, :create => true)
198 assert_match(/lon missing/, message_create.message)
199 message_update = assert_raise(OSM::APIBadXMLError) do
200 Node.from_xml(nolon, :create => false)
202 assert_match(/lon missing/, message_update.message)
205 def test_from_xml_no_changeset_id
206 nocs = "<osm><node id='123' lon='23.23' lat='23.1' version='23' /></osm>"
207 message_create = assert_raise(OSM::APIBadXMLError) do
208 Node.from_xml(nocs, :create => true)
210 assert_match(/Changeset id is missing/, message_create.message)
211 message_update = assert_raise(OSM::APIBadXMLError) do
212 Node.from_xml(nocs, :create => false)
214 assert_match(/Changeset id is missing/, message_update.message)
217 def test_from_xml_no_version
218 no_version = "<osm><node id='123' lat='23' lon='23' changeset='23' /></osm>"
219 assert_nothing_raised do
220 Node.from_xml(no_version, :create => true)
222 message_update = assert_raise(OSM::APIBadXMLError) do
223 Node.from_xml(no_version, :create => false)
225 assert_match(/Version is required when updating/, message_update.message)
228 def test_from_xml_double_lat
229 nocs = "<osm><node id='123' lon='23.23' lat='23.1' lat='12' changeset='23' version='23' /></osm>"
230 message_create = assert_raise(OSM::APIBadXMLError) do
231 Node.from_xml(nocs, :create => true)
233 assert_match(/Fatal error: Attribute lat redefined at/, message_create.message)
234 message_update = assert_raise(OSM::APIBadXMLError) do
235 Node.from_xml(nocs, :create => false)
237 assert_match(/Fatal error: Attribute lat redefined at/, message_update.message)
240 def test_from_xml_id_zero
241 id_list = ["", "0", "00", "0.0", "a"]
243 zero_id = "<osm><node id='#{id}' lat='12.3' lon='12.3' changeset='33' version='23' /></osm>"
244 assert_nothing_raised do
245 Node.from_xml(zero_id, :create => true)
247 message_update = assert_raise(OSM::APIBadUserInput) do
248 Node.from_xml(zero_id, :create => false)
250 assert_match(/ID of node cannot be zero when updating/, message_update.message)
254 def test_from_xml_no_text
256 message_create = assert_raise(OSM::APIBadXMLError) do
257 Node.from_xml(no_text, :create => true)
259 assert_match(/Must specify a string with one or more characters/, message_create.message)
260 message_update = assert_raise(OSM::APIBadXMLError) do
261 Node.from_xml(no_text, :create => false)
263 assert_match(/Must specify a string with one or more characters/, message_update.message)
266 def test_from_xml_no_node
267 no_node = "<osm></osm>"
268 message_create = assert_raise(OSM::APIBadXMLError) do
269 Node.from_xml(no_node, :create => true)
271 assert_match %r{XML doesn't contain an osm/node element}, message_create.message
272 message_update = assert_raise(OSM::APIBadXMLError) do
273 Node.from_xml(no_node, :create => false)
275 assert_match %r{XML doesn't contain an osm/node element}, message_update.message
278 def test_from_xml_no_k_v
279 nokv = "<osm><node id='23' lat='12.3' lon='23.4' changeset='12' version='23'><tag /></node></osm>"
280 message_create = assert_raise(OSM::APIBadXMLError) do
281 Node.from_xml(nokv, :create => true)
283 assert_match(/tag is missing key/, message_create.message)
284 message_update = assert_raise(OSM::APIBadXMLError) do
285 Node.from_xml(nokv, :create => false)
287 assert_match(/tag is missing key/, message_update.message)
290 def test_from_xml_no_v
291 no_v = "<osm><node id='23' lat='23.43' lon='23.32' changeset='23' version='32'><tag k='key' /></node></osm>"
292 message_create = assert_raise(OSM::APIBadXMLError) do
293 Node.from_xml(no_v, :create => true)
295 assert_match(/tag is missing value/, message_create.message)
296 message_update = assert_raise(OSM::APIBadXMLError) do
297 Node.from_xml(no_v, :create => false)
299 assert_match(/tag is missing value/, message_update.message)
302 def test_from_xml_duplicate_k
303 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>"
304 message_create = assert_raise(OSM::APIDuplicateTagsError) do
305 Node.from_xml(dupk, :create => true)
307 assert_equal "Element node/ has duplicate tags with key dup", message_create.message
308 message_update = assert_raise(OSM::APIDuplicateTagsError) do
309 Node.from_xml(dupk, :create => false)
311 assert_equal "Element node/23 has duplicate tags with key dup", message_update.message
314 def test_element_tags
316 taglist = create_list(:node_tag, 2, :node => node)
317 tags = Node.find(node.id).element_tags.order(:k)
318 assert_equal taglist.count, tags.count
319 taglist.sort_by!(&:k).each_index do |i|
320 assert_equal taglist[i].k, tags[i].k
321 assert_equal taglist[i].v, tags[i].v
327 taglist = create_list(:node_tag, 2, :node => node)
328 tags = Node.find(node.id).tags
329 assert_equal taglist.count, tags.count
330 taglist.each do |tag|
331 assert_equal tag.v, tags[tag.k]
335 def test_containing_relation_members
337 relation_member1 = create(:relation_member, :member => node)
338 relation_member2 = create(:relation_member, :member => node)
339 relation_member3 = create(:relation_member, :member => node)
340 crm = Node.find(node.id).containing_relation_members.order(:relation_id)
341 # assert_equal 3, crm.size
342 assert_equal relation_member1.relation_id, crm.first.relation_id
343 assert_equal "Node", crm.first.member_type
344 assert_equal node.id, crm.first.member_id
345 assert_equal relation_member1.relation_id, crm.first.relation.id
346 assert_equal relation_member2.relation_id, crm.second.relation_id
347 assert_equal "Node", crm.second.member_type
348 assert_equal node.id, crm.second.member_id
349 assert_equal relation_member2.relation_id, crm.second.relation.id
350 assert_equal relation_member3.relation_id, crm.third.relation_id
351 assert_equal "Node", crm.third.member_type
352 assert_equal node.id, crm.third.member_id
353 assert_equal relation_member3.relation_id, crm.third.relation.id
356 def test_containing_relations
358 relation_member1 = create(:relation_member, :member => node)
359 relation_member2 = create(:relation_member, :member => node)
360 relation_member3 = create(:relation_member, :member => node)
361 cr = Node.find(node.id).containing_relations.order(:id)
363 assert_equal 3, cr.size
364 assert_equal relation_member1.relation.id, cr.first.id
365 assert_equal relation_member2.relation.id, cr.second.id
366 assert_equal relation_member3.relation.id, cr.third.id
369 test "raises missing changeset exception when creating" do
372 assert_raises OSM::APIChangesetMissingError do
373 node.create_with_history(user)
377 test "raises user-changeset mismatch exception when creating" do
379 changeset = create(:changeset)
380 node = Node.new(:changeset => changeset)
381 assert_raises OSM::APIUserChangesetMismatchError do
382 node.create_with_history(user)
386 test "raises already closed changeset exception when creating" do
388 changeset = create(:changeset, :closed, :user => user)
389 node = Node.new(:changeset => changeset)
390 assert_raises OSM::APIChangesetAlreadyClosedError do
391 node.create_with_history(user)
395 test "raises id precondition exception when updating" do
397 node = Node.new(:id => 23)
398 new_node = Node.new(:id => 42)
399 assert_raises OSM::APIPreconditionFailedError do
400 node.update_from(new_node, user)
404 test "raises version mismatch exception when updating" do
406 node = Node.new(:id => 42, :version => 7)
407 new_node = Node.new(:id => 42, :version => 12)
408 assert_raises OSM::APIVersionMismatchError do
409 node.update_from(new_node, user)
413 test "raises missing changeset exception when updating" do
415 node = Node.new(:id => 42, :version => 12)
416 new_node = Node.new(:id => 42, :version => 12)
417 assert_raises OSM::APIChangesetMissingError do
418 node.update_from(new_node, user)
422 test "raises user-changeset mismatch exception when updating" do
424 changeset = create(:changeset)
425 node = Node.new(:id => 42, :version => 12)
426 new_node = Node.new(:id => 42, :version => 12, :changeset => changeset)
427 assert_raises OSM::APIUserChangesetMismatchError do
428 node.update_from(new_node, user)
432 test "raises already closed changeset exception when updating" do
434 changeset = create(:changeset, :closed, :user => user)
435 node = Node.new(:id => 42, :version => 12)
436 new_node = Node.new(:id => 42, :version => 12, :changeset => changeset)
437 assert_raises OSM::APIChangesetAlreadyClosedError do
438 node.update_from(new_node, user)
442 test "raises id precondition exception when deleting" do
444 node = Node.new(:id => 23, :visible => true)
445 new_node = Node.new(:id => 42, :visible => false)
446 assert_raises OSM::APIPreconditionFailedError do
447 node.delete_with_history!(new_node, user)