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 node_template = Node.new(
55 :longitude => 65.4321,
56 :changeset_id => changesets(:normal_user_first_change).id,
60 assert node_template.create_with_history(changesets(:normal_user_first_change).user)
62 node = Node.find(node_template.id)
64 assert_equal node_template.latitude, node.latitude
65 assert_equal node_template.longitude, node.longitude
66 assert_equal node_template.changeset_id, node.changeset_id
67 assert_equal node_template.visible, node.visible
68 assert_equal node_template.timestamp.to_i, node.timestamp.to_i
70 assert_equal OldNode.where(:node_id => node_template.id).count, 1
71 old_node = OldNode.where(:node_id => node_template.id).first
72 assert_not_nil old_node
73 assert_equal node_template.latitude, old_node.latitude
74 assert_equal node_template.longitude, old_node.longitude
75 assert_equal node_template.changeset_id, old_node.changeset_id
76 assert_equal node_template.visible, old_node.visible
77 assert_equal node_template.tags, old_node.tags
78 assert_equal node_template.timestamp.to_i, old_node.timestamp.to_i
83 create(:old_node, :node_id => node.id, :version => 1)
84 node_template = Node.find(node.id)
86 assert_not_nil node_template
87 assert_equal OldNode.where(:node_id => node_template.id).count, 1
90 node_template.latitude = 12.3456
91 node_template.longitude = 65.4321
92 # node_template.tags = "updated=yes"
93 assert node.update_from(node_template, node.changeset.user)
95 node = Node.find(node_template.id)
97 assert_equal node_template.latitude, node.latitude
98 assert_equal node_template.longitude, node.longitude
99 assert_equal node_template.changeset_id, node.changeset_id
100 assert_equal node_template.visible, node.visible
101 # assert_equal node_template.tags, node.tags
103 assert_equal OldNode.where(:node_id => node_template.id).count, 2
104 old_node = OldNode.where(:node_id => node_template.id, :version => 2).first
105 assert_not_nil old_node
106 assert_equal node_template.latitude, old_node.latitude
107 assert_equal node_template.longitude, old_node.longitude
108 assert_equal node_template.changeset_id, old_node.changeset_id
109 assert_equal node_template.visible, old_node.visible
110 # assert_equal node_template.tags, old_node.tags
115 create(:old_node, :node_id => node.id, :version => 1)
116 node_template = Node.find(node.id)
118 assert_not_nil node_template
119 assert_equal OldNode.where(:node_id => node_template.id).count, 1
122 assert node.delete_with_history!(node_template, node.changeset.user)
124 node = Node.find(node_template.id)
126 assert_equal node_template.latitude, node.latitude
127 assert_equal node_template.longitude, node.longitude
128 assert_equal node_template.changeset_id, node.changeset_id
129 assert_equal false, node.visible
130 # assert_equal node_template.tags, node.tags
132 assert_equal OldNode.where(:node_id => node_template.id).count, 2
133 old_node = OldNode.where(:node_id => node_template.id, :version => 2).first
134 assert_not_nil old_node
135 assert_equal node_template.latitude, old_node.latitude
136 assert_equal node_template.longitude, old_node.longitude
137 assert_equal node_template.changeset_id, old_node.changeset_id
138 assert_equal false, old_node.visible
139 # assert_equal node_template.tags, old_node.tags
142 def test_from_xml_no_id
147 noid = "<osm><node lat='#{lat}' lon='#{lon}' changeset='#{changeset}' version='#{version}' /></osm>"
148 # First try a create which doesn't need the id
149 assert_nothing_raised(OSM::APIBadXMLError) do
150 Node.from_xml(noid, true)
152 # Now try an update with no id, and make sure that it gives the appropriate exception
153 message = assert_raise(OSM::APIBadXMLError) do
154 Node.from_xml(noid, false)
156 assert_match /ID is required when updating./, message.message
159 def test_from_xml_no_lat
160 nolat = "<osm><node id='1' lon='23.3' changeset='2' version='23' /></osm>"
161 message_create = assert_raise(OSM::APIBadXMLError) do
162 Node.from_xml(nolat, true)
164 assert_match /lat missing/, message_create.message
165 message_update = assert_raise(OSM::APIBadXMLError) do
166 Node.from_xml(nolat, false)
168 assert_match /lat missing/, message_update.message
171 def test_from_xml_no_lon
172 nolon = "<osm><node id='1' lat='23.1' changeset='2' version='23' /></osm>"
173 message_create = assert_raise(OSM::APIBadXMLError) do
174 Node.from_xml(nolon, true)
176 assert_match /lon missing/, message_create.message
177 message_update = assert_raise(OSM::APIBadXMLError) do
178 Node.from_xml(nolon, false)
180 assert_match /lon missing/, message_update.message
183 def test_from_xml_no_changeset_id
184 nocs = "<osm><node id='123' lon='23.23' lat='23.1' version='23' /></osm>"
185 message_create = assert_raise(OSM::APIBadXMLError) do
186 Node.from_xml(nocs, true)
188 assert_match /Changeset id is missing/, message_create.message
189 message_update = assert_raise(OSM::APIBadXMLError) do
190 Node.from_xml(nocs, false)
192 assert_match /Changeset id is missing/, message_update.message
195 def test_from_xml_no_version
196 no_version = "<osm><node id='123' lat='23' lon='23' changeset='23' /></osm>"
197 assert_nothing_raised(OSM::APIBadXMLError) do
198 Node.from_xml(no_version, true)
200 message_update = assert_raise(OSM::APIBadXMLError) do
201 Node.from_xml(no_version, false)
203 assert_match /Version is required when updating/, message_update.message
206 def test_from_xml_double_lat
207 nocs = "<osm><node id='123' lon='23.23' lat='23.1' lat='12' changeset='23' version='23' /></osm>"
208 message_create = assert_raise(OSM::APIBadXMLError) do
209 Node.from_xml(nocs, true)
211 assert_match /Fatal error: Attribute lat redefined at/, message_create.message
212 message_update = assert_raise(OSM::APIBadXMLError) do
213 Node.from_xml(nocs, false)
215 assert_match /Fatal error: Attribute lat redefined at/, message_update.message
218 def test_from_xml_id_zero
219 id_list = ["", "0", "00", "0.0", "a"]
221 zero_id = "<osm><node id='#{id}' lat='12.3' lon='12.3' changeset='33' version='23' /></osm>"
222 assert_nothing_raised(OSM::APIBadUserInput) do
223 Node.from_xml(zero_id, true)
225 message_update = assert_raise(OSM::APIBadUserInput) do
226 Node.from_xml(zero_id, false)
228 assert_match /ID of node cannot be zero when updating/, message_update.message
232 def test_from_xml_no_text
234 message_create = assert_raise(OSM::APIBadXMLError) do
235 Node.from_xml(no_text, true)
237 assert_match /Must specify a string with one or more characters/, message_create.message
238 message_update = assert_raise(OSM::APIBadXMLError) do
239 Node.from_xml(no_text, false)
241 assert_match /Must specify a string with one or more characters/, message_update.message
244 def test_from_xml_no_node
245 no_node = "<osm></osm>"
246 message_create = assert_raise(OSM::APIBadXMLError) do
247 Node.from_xml(no_node, true)
249 assert_match %r{XML doesn't contain an osm/node element}, message_create.message
250 message_update = assert_raise(OSM::APIBadXMLError) do
251 Node.from_xml(no_node, false)
253 assert_match %r{XML doesn't contain an osm/node element}, message_update.message
256 def test_from_xml_no_k_v
257 nokv = "<osm><node id='23' lat='12.3' lon='23.4' changeset='12' version='23'><tag /></node></osm>"
258 message_create = assert_raise(OSM::APIBadXMLError) do
259 Node.from_xml(nokv, true)
261 assert_match /tag is missing key/, message_create.message
262 message_update = assert_raise(OSM::APIBadXMLError) do
263 Node.from_xml(nokv, false)
265 assert_match /tag is missing key/, message_update.message
268 def test_from_xml_no_v
269 no_v = "<osm><node id='23' lat='23.43' lon='23.32' changeset='23' version='32'><tag k='key' /></node></osm>"
270 message_create = assert_raise(OSM::APIBadXMLError) do
271 Node.from_xml(no_v, true)
273 assert_match /tag is missing value/, message_create.message
274 message_update = assert_raise(OSM::APIBadXMLError) do
275 Node.from_xml(no_v, false)
277 assert_match /tag is missing value/, message_update.message
280 def test_from_xml_duplicate_k
281 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>"
282 message_create = assert_raise(OSM::APIDuplicateTagsError) do
283 Node.from_xml(dupk, true)
285 assert_equal "Element node/ has duplicate tags with key dup", message_create.message
286 message_update = assert_raise(OSM::APIDuplicateTagsError) do
287 Node.from_xml(dupk, false)
289 assert_equal "Element node/23 has duplicate tags with key dup", message_update.message
294 taglist = create_list(:node_tag, 2, :node => node)
295 tags = Node.find(node.id).node_tags.order(:k)
296 assert_equal taglist.count, tags.count
297 taglist.sort_by!(&:k).each_index do |i|
298 assert_equal taglist[i].k, tags[i].k
299 assert_equal taglist[i].v, tags[i].v
305 taglist = create_list(:node_tag, 2, :node => node)
306 tags = Node.find(node.id).tags
307 assert_equal taglist.count, tags.count
308 taglist.each do |tag|
309 assert_equal tag.v, tags[tag.k]
313 def test_containing_relation_members
314 node = current_nodes(:node_used_by_relationship)
315 crm = Node.find(node.id).containing_relation_members.order(:relation_id)
316 # assert_equal 3, crm.size
317 assert_equal 1, crm.first.relation_id
318 assert_equal "Node", crm.first.member_type
319 assert_equal node.id, crm.first.member_id
320 assert_equal 1, crm.first.relation.id
321 assert_equal 2, crm.second.relation_id
322 assert_equal "Node", crm.second.member_type
323 assert_equal node.id, crm.second.member_id
324 assert_equal 2, crm.second.relation.id
325 assert_equal 3, crm.third.relation_id
326 assert_equal "Node", crm.third.member_type
327 assert_equal node.id, crm.third.member_id
328 assert_equal 3, crm.third.relation.id
331 def test_containing_relations
332 node = current_nodes(:node_used_by_relationship)
333 cr = Node.find(node.id).containing_relations.order(:id)
334 assert_equal 3, cr.size
335 assert_equal 1, cr.first.id
336 assert_equal 2, cr.second.id
337 assert_equal 3, cr.third.id