3 class NodeTest < ActiveSupport::TestCase
6 def test_node_too_far_north
7 node = build(:node, :latitude => 90.01 * OldNode::SCALE)
8 assert_equal false, node.valid?
11 def test_node_north_limit
12 node = build(:node, :latitude => 90 * OldNode::SCALE)
16 def test_node_too_far_south
17 node = build(:node, :latitude => -90.01 * OldNode::SCALE)
18 assert_equal false, node.valid?
21 def test_node_south_limit
22 node = build(:node, :latitude => -90 * OldNode::SCALE)
26 def test_node_too_far_west
27 node = build(:node, :longitude => -180.01 * OldNode::SCALE)
28 assert_equal false, node.valid?
31 def test_node_west_limit
32 node = build(:node, :longitude => -180 * OldNode::SCALE)
36 def test_node_too_far_east
37 node = build(:node, :longitude => 180.01 * OldNode::SCALE)
38 assert_equal false, node.valid?
41 def test_node_east_limit
42 node = build(:node, :longitude => 180 * OldNode::SCALE)
46 def test_totally_wrong
47 node = build(:node, :latitude => 200 * OldNode::SCALE, :longitude => 200 * OldNode::SCALE)
48 assert_equal false, node.valid?
51 # Check that you can create a node and store it
53 changeset = create(:changeset)
54 node_template = Node.new(
56 :longitude => 65.4321,
57 :changeset_id => changeset.id,
61 assert node_template.create_with_history(changeset.user)
63 node = Node.find(node_template.id)
65 assert_equal node_template.latitude, node.latitude
66 assert_equal node_template.longitude, node.longitude
67 assert_equal node_template.changeset_id, node.changeset_id
68 assert_equal node_template.visible, node.visible
69 assert_equal node_template.timestamp.to_i, node.timestamp.to_i
71 assert_equal OldNode.where(:node_id => node_template.id).count, 1
72 old_node = OldNode.where(:node_id => node_template.id).first
73 assert_not_nil old_node
74 assert_equal node_template.latitude, old_node.latitude
75 assert_equal node_template.longitude, old_node.longitude
76 assert_equal node_template.changeset_id, old_node.changeset_id
77 assert_equal node_template.visible, old_node.visible
78 assert_equal node_template.tags, old_node.tags
79 assert_equal node_template.timestamp.to_i, old_node.timestamp.to_i
84 create(:old_node, :node_id => node.id, :version => 1)
85 node_template = Node.find(node.id)
87 assert_not_nil node_template
88 assert_equal OldNode.where(:node_id => node_template.id).count, 1
91 node_template.latitude = 12.3456
92 node_template.longitude = 65.4321
93 # node_template.tags = "updated=yes"
94 assert node.update_from(node_template, node.changeset.user)
96 node = Node.find(node_template.id)
98 assert_equal node_template.latitude, node.latitude
99 assert_equal node_template.longitude, node.longitude
100 assert_equal node_template.changeset_id, node.changeset_id
101 assert_equal node_template.visible, node.visible
102 # assert_equal node_template.tags, node.tags
104 assert_equal OldNode.where(:node_id => node_template.id).count, 2
105 old_node = OldNode.where(:node_id => node_template.id, :version => 2).first
106 assert_not_nil old_node
107 assert_equal node_template.latitude, old_node.latitude
108 assert_equal node_template.longitude, old_node.longitude
109 assert_equal node_template.changeset_id, old_node.changeset_id
110 assert_equal node_template.visible, old_node.visible
111 # assert_equal node_template.tags, old_node.tags
116 create(:old_node, :node_id => node.id, :version => 1)
117 node_template = Node.find(node.id)
119 assert_not_nil node_template
120 assert_equal OldNode.where(:node_id => node_template.id).count, 1
123 assert node.delete_with_history!(node_template, node.changeset.user)
125 node = Node.find(node_template.id)
127 assert_equal node_template.latitude, node.latitude
128 assert_equal node_template.longitude, node.longitude
129 assert_equal node_template.changeset_id, node.changeset_id
130 assert_equal false, node.visible
131 # assert_equal node_template.tags, node.tags
133 assert_equal OldNode.where(:node_id => node_template.id).count, 2
134 old_node = OldNode.where(:node_id => node_template.id, :version => 2).first
135 assert_not_nil old_node
136 assert_equal node_template.latitude, old_node.latitude
137 assert_equal node_template.longitude, old_node.longitude
138 assert_equal node_template.changeset_id, old_node.changeset_id
139 assert_equal false, old_node.visible
140 # assert_equal node_template.tags, old_node.tags
143 def test_from_xml_no_id
148 noid = "<osm><node lat='#{lat}' lon='#{lon}' changeset='#{changeset}' version='#{version}' /></osm>"
149 # First try a create which doesn't need the id
150 assert_nothing_raised(OSM::APIBadXMLError) do
151 Node.from_xml(noid, true)
153 # Now try an update with no id, and make sure that it gives the appropriate exception
154 message = assert_raise(OSM::APIBadXMLError) do
155 Node.from_xml(noid, false)
157 assert_match /ID is required when updating./, message.message
160 def test_from_xml_no_lat
161 nolat = "<osm><node id='1' lon='23.3' changeset='2' version='23' /></osm>"
162 message_create = assert_raise(OSM::APIBadXMLError) do
163 Node.from_xml(nolat, true)
165 assert_match /lat missing/, message_create.message
166 message_update = assert_raise(OSM::APIBadXMLError) do
167 Node.from_xml(nolat, false)
169 assert_match /lat missing/, message_update.message
172 def test_from_xml_no_lon
173 nolon = "<osm><node id='1' lat='23.1' changeset='2' version='23' /></osm>"
174 message_create = assert_raise(OSM::APIBadXMLError) do
175 Node.from_xml(nolon, true)
177 assert_match /lon missing/, message_create.message
178 message_update = assert_raise(OSM::APIBadXMLError) do
179 Node.from_xml(nolon, false)
181 assert_match /lon missing/, message_update.message
184 def test_from_xml_no_changeset_id
185 nocs = "<osm><node id='123' lon='23.23' lat='23.1' version='23' /></osm>"
186 message_create = assert_raise(OSM::APIBadXMLError) do
187 Node.from_xml(nocs, true)
189 assert_match /Changeset id is missing/, message_create.message
190 message_update = assert_raise(OSM::APIBadXMLError) do
191 Node.from_xml(nocs, false)
193 assert_match /Changeset id is missing/, message_update.message
196 def test_from_xml_no_version
197 no_version = "<osm><node id='123' lat='23' lon='23' changeset='23' /></osm>"
198 assert_nothing_raised(OSM::APIBadXMLError) do
199 Node.from_xml(no_version, true)
201 message_update = assert_raise(OSM::APIBadXMLError) do
202 Node.from_xml(no_version, false)
204 assert_match /Version is required when updating/, message_update.message
207 def test_from_xml_double_lat
208 nocs = "<osm><node id='123' lon='23.23' lat='23.1' lat='12' changeset='23' version='23' /></osm>"
209 message_create = assert_raise(OSM::APIBadXMLError) do
210 Node.from_xml(nocs, true)
212 assert_match /Fatal error: Attribute lat redefined at/, message_create.message
213 message_update = assert_raise(OSM::APIBadXMLError) do
214 Node.from_xml(nocs, false)
216 assert_match /Fatal error: Attribute lat redefined at/, message_update.message
219 def test_from_xml_id_zero
220 id_list = ["", "0", "00", "0.0", "a"]
222 zero_id = "<osm><node id='#{id}' lat='12.3' lon='12.3' changeset='33' version='23' /></osm>"
223 assert_nothing_raised(OSM::APIBadUserInput) do
224 Node.from_xml(zero_id, true)
226 message_update = assert_raise(OSM::APIBadUserInput) do
227 Node.from_xml(zero_id, false)
229 assert_match /ID of node cannot be zero when updating/, message_update.message
233 def test_from_xml_no_text
235 message_create = assert_raise(OSM::APIBadXMLError) do
236 Node.from_xml(no_text, true)
238 assert_match /Must specify a string with one or more characters/, message_create.message
239 message_update = assert_raise(OSM::APIBadXMLError) do
240 Node.from_xml(no_text, false)
242 assert_match /Must specify a string with one or more characters/, message_update.message
245 def test_from_xml_no_node
246 no_node = "<osm></osm>"
247 message_create = assert_raise(OSM::APIBadXMLError) do
248 Node.from_xml(no_node, true)
250 assert_match %r{XML doesn't contain an osm/node element}, message_create.message
251 message_update = assert_raise(OSM::APIBadXMLError) do
252 Node.from_xml(no_node, false)
254 assert_match %r{XML doesn't contain an osm/node element}, message_update.message
257 def test_from_xml_no_k_v
258 nokv = "<osm><node id='23' lat='12.3' lon='23.4' changeset='12' version='23'><tag /></node></osm>"
259 message_create = assert_raise(OSM::APIBadXMLError) do
260 Node.from_xml(nokv, true)
262 assert_match /tag is missing key/, message_create.message
263 message_update = assert_raise(OSM::APIBadXMLError) do
264 Node.from_xml(nokv, false)
266 assert_match /tag is missing key/, message_update.message
269 def test_from_xml_no_v
270 no_v = "<osm><node id='23' lat='23.43' lon='23.32' changeset='23' version='32'><tag k='key' /></node></osm>"
271 message_create = assert_raise(OSM::APIBadXMLError) do
272 Node.from_xml(no_v, true)
274 assert_match /tag is missing value/, message_create.message
275 message_update = assert_raise(OSM::APIBadXMLError) do
276 Node.from_xml(no_v, false)
278 assert_match /tag is missing value/, message_update.message
281 def test_from_xml_duplicate_k
282 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>"
283 message_create = assert_raise(OSM::APIDuplicateTagsError) do
284 Node.from_xml(dupk, true)
286 assert_equal "Element node/ has duplicate tags with key dup", message_create.message
287 message_update = assert_raise(OSM::APIDuplicateTagsError) do
288 Node.from_xml(dupk, false)
290 assert_equal "Element node/23 has duplicate tags with key dup", message_update.message
295 taglist = create_list(:node_tag, 2, :node => node)
296 tags = Node.find(node.id).node_tags.order(:k)
297 assert_equal taglist.count, tags.count
298 taglist.sort_by!(&:k).each_index do |i|
299 assert_equal taglist[i].k, tags[i].k
300 assert_equal taglist[i].v, tags[i].v
306 taglist = create_list(:node_tag, 2, :node => node)
307 tags = Node.find(node.id).tags
308 assert_equal taglist.count, tags.count
309 taglist.each do |tag|
310 assert_equal tag.v, tags[tag.k]
314 def test_containing_relation_members
315 node = current_nodes(:node_used_by_relationship)
316 crm = Node.find(node.id).containing_relation_members.order(:relation_id)
317 # assert_equal 3, crm.size
318 assert_equal 1, crm.first.relation_id
319 assert_equal "Node", crm.first.member_type
320 assert_equal node.id, crm.first.member_id
321 assert_equal 1, crm.first.relation.id
322 assert_equal 2, crm.second.relation_id
323 assert_equal "Node", crm.second.member_type
324 assert_equal node.id, crm.second.member_id
325 assert_equal 2, crm.second.relation.id
326 assert_equal 3, crm.third.relation_id
327 assert_equal "Node", crm.third.member_type
328 assert_equal node.id, crm.third.member_id
329 assert_equal 3, crm.third.relation.id
332 def test_containing_relations
333 node = current_nodes(:node_used_by_relationship)
334 cr = Node.find(node.id).containing_relations.order(:id)
335 assert_equal 3, cr.size
336 assert_equal 1, cr.first.id
337 assert_equal 2, cr.second.id
338 assert_equal 3, cr.third.id