]> git.openstreetmap.org Git - rails.git/blob - test/models/node_test.rb
Merge remote-tracking branch 'upstream/pull/2596'
[rails.git] / test / models / node_test.rb
1 require "test_helper"
2
3 class NodeTest < ActiveSupport::TestCase
4   def test_node_too_far_north
5     node = build(:node, :latitude => 90.01 * OldNode::SCALE)
6     node.validate
7     assert_includes node.errors.full_messages, "Node is not in the world"
8   end
9
10   def test_node_north_limit
11     node = build(:node, :latitude => 90 * OldNode::SCALE)
12     node.validate
13     assert_not_includes node.errors.full_messages, "Node is not in the world"
14   end
15
16   def test_node_too_far_south
17     node = build(:node, :latitude => -90.01 * OldNode::SCALE)
18     node.validate
19     assert_includes node.errors.full_messages, "Node is not in the world"
20   end
21
22   def test_node_south_limit
23     node = build(:node, :latitude => -90 * OldNode::SCALE)
24     node.validate
25     assert_not_includes node.errors.full_messages, "Node is not in the world"
26   end
27
28   def test_node_too_far_west
29     node = build(:node, :longitude => -180.01 * OldNode::SCALE)
30     node.validate
31     assert_includes node.errors.full_messages, "Node is not in the world"
32   end
33
34   def test_node_west_limit
35     node = build(:node, :longitude => -180 * OldNode::SCALE)
36     node.validate
37     assert_not_includes node.errors.full_messages, "Node is not in the world"
38   end
39
40   def test_node_too_far_east
41     node = build(:node, :longitude => 180.01 * OldNode::SCALE)
42     node.validate
43     assert_includes node.errors.full_messages, "Node is not in the world"
44   end
45
46   def test_node_east_limit
47     node = build(:node, :longitude => 180 * OldNode::SCALE)
48     node.validate
49     assert_not_includes node.errors.full_messages, "Node is not in the world"
50   end
51
52   def test_totally_wrong
53     node = build(:node, :latitude => 200 * OldNode::SCALE, :longitude => 200 * OldNode::SCALE)
54     node.validate
55     assert_includes node.errors.full_messages, "Node is not in the world"
56   end
57
58   def test_lat_lon
59     node = build(:node, :latitude => 12.345 * OldNode::SCALE, :longitude => 34.567 * OldNode::SCALE)
60
61     assert_in_delta 12.345, node.lat, 0.0000001
62     assert_in_delta 34.567, node.lon, 0.0000001
63
64     node.lat = 54.321
65     node.lon = 76.543
66
67     assert_in_delta 54.321 * OldNode::SCALE, node.latitude, 0.000001
68     assert_in_delta 76.543 * OldNode::SCALE, node.longitude, 0.000001
69   end
70
71   # Check that you can create a node and store it
72   def test_create
73     changeset = create(:changeset)
74     node_template = Node.new(
75       :lat => 12.3456,
76       :lon => 65.4321,
77       :changeset_id => changeset.id,
78       :visible => 1,
79       :version => 1
80     )
81     assert node_template.create_with_history(changeset.user)
82
83     node = Node.find(node_template.id)
84     assert_not_nil node
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
90
91     assert_equal OldNode.where(:node_id => node_template.id).count, 1
92     old_node = OldNode.where(:node_id => node_template.id).first
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
100   end
101
102   def test_update
103     node = create(:node)
104     create(:old_node, :node_id => node.id, :version => 1)
105     node_template = Node.find(node.id)
106
107     assert_not_nil node_template
108     assert_equal OldNode.where(:node_id => node_template.id).count, 1
109     assert_not_nil node
110
111     node_template.lat = 12.3456
112     node_template.lon = 65.4321
113     # node_template.tags = "updated=yes"
114     assert node.update_from(node_template, node.changeset.user)
115
116     node = Node.find(node_template.id)
117     assert_not_nil node
118     assert_equal node_template.latitude, node.latitude
119     assert_equal node_template.longitude, node.longitude
120     assert_equal node_template.changeset_id, node.changeset_id
121     assert_equal node_template.visible, node.visible
122     # assert_equal node_template.tags, node.tags
123
124     assert_equal OldNode.where(:node_id => node_template.id).count, 2
125     old_node = OldNode.where(:node_id => node_template.id, :version => 2).first
126     assert_not_nil old_node
127     assert_equal node_template.latitude, old_node.latitude
128     assert_equal node_template.longitude, old_node.longitude
129     assert_equal node_template.changeset_id, old_node.changeset_id
130     assert_equal node_template.visible, old_node.visible
131     # assert_equal node_template.tags, old_node.tags
132   end
133
134   def test_delete
135     node = create(:node)
136     create(:old_node, :node_id => node.id, :version => 1)
137     node_template = Node.find(node.id)
138
139     assert_not_nil node_template
140     assert_equal OldNode.where(:node_id => node_template.id).count, 1
141     assert_not_nil node
142
143     assert node.delete_with_history!(node_template, node.changeset.user)
144
145     node = Node.find(node_template.id)
146     assert_not_nil node
147     assert_equal node_template.latitude, node.latitude
148     assert_equal node_template.longitude, node.longitude
149     assert_equal node_template.changeset_id, node.changeset_id
150     assert_not node.visible
151     # assert_equal node_template.tags, node.tags
152
153     assert_equal OldNode.where(:node_id => node_template.id).count, 2
154     old_node = OldNode.where(:node_id => node_template.id, :version => 2).first
155     assert_not_nil old_node
156     assert_equal node_template.latitude, old_node.latitude
157     assert_equal node_template.longitude, old_node.longitude
158     assert_equal node_template.changeset_id, old_node.changeset_id
159     assert_not old_node.visible
160     # assert_equal node_template.tags, old_node.tags
161   end
162
163   def test_from_xml_no_id
164     lat = 56.7
165     lon = -2.3
166     changeset = 2
167     version = 1
168     noid = "<osm><node lat='#{lat}' lon='#{lon}' changeset='#{changeset}' version='#{version}' /></osm>"
169     # First try a create which doesn't need the id
170     assert_nothing_raised do
171       Node.from_xml(noid, true)
172     end
173     # Now try an update with no id, and make sure that it gives the appropriate exception
174     message = assert_raise(OSM::APIBadXMLError) do
175       Node.from_xml(noid, false)
176     end
177     assert_match(/ID is required when updating./, message.message)
178   end
179
180   def test_from_xml_no_lat
181     nolat = "<osm><node id='1' lon='23.3' changeset='2' version='23' /></osm>"
182     message_create = assert_raise(OSM::APIBadXMLError) do
183       Node.from_xml(nolat, true)
184     end
185     assert_match(/lat missing/, message_create.message)
186     message_update = assert_raise(OSM::APIBadXMLError) do
187       Node.from_xml(nolat, false)
188     end
189     assert_match(/lat missing/, message_update.message)
190   end
191
192   def test_from_xml_no_lon
193     nolon = "<osm><node id='1' lat='23.1' changeset='2' version='23' /></osm>"
194     message_create = assert_raise(OSM::APIBadXMLError) do
195       Node.from_xml(nolon, true)
196     end
197     assert_match(/lon missing/, message_create.message)
198     message_update = assert_raise(OSM::APIBadXMLError) do
199       Node.from_xml(nolon, false)
200     end
201     assert_match(/lon missing/, message_update.message)
202   end
203
204   def test_from_xml_no_changeset_id
205     nocs = "<osm><node id='123' lon='23.23' lat='23.1' version='23' /></osm>"
206     message_create = assert_raise(OSM::APIBadXMLError) do
207       Node.from_xml(nocs, true)
208     end
209     assert_match(/Changeset id is missing/, message_create.message)
210     message_update = assert_raise(OSM::APIBadXMLError) do
211       Node.from_xml(nocs, false)
212     end
213     assert_match(/Changeset id is missing/, message_update.message)
214   end
215
216   def test_from_xml_no_version
217     no_version = "<osm><node id='123' lat='23' lon='23' changeset='23' /></osm>"
218     assert_nothing_raised do
219       Node.from_xml(no_version, true)
220     end
221     message_update = assert_raise(OSM::APIBadXMLError) do
222       Node.from_xml(no_version, false)
223     end
224     assert_match(/Version is required when updating/, message_update.message)
225   end
226
227   def test_from_xml_double_lat
228     nocs = "<osm><node id='123' lon='23.23' lat='23.1' lat='12' changeset='23' version='23' /></osm>"
229     message_create = assert_raise(OSM::APIBadXMLError) do
230       Node.from_xml(nocs, true)
231     end
232     assert_match(/Fatal error: Attribute lat redefined at/, message_create.message)
233     message_update = assert_raise(OSM::APIBadXMLError) do
234       Node.from_xml(nocs, false)
235     end
236     assert_match(/Fatal error: Attribute lat redefined at/, message_update.message)
237   end
238
239   def test_from_xml_id_zero
240     id_list = ["", "0", "00", "0.0", "a"]
241     id_list.each do |id|
242       zero_id = "<osm><node id='#{id}' lat='12.3' lon='12.3' changeset='33' version='23' /></osm>"
243       assert_nothing_raised do
244         Node.from_xml(zero_id, true)
245       end
246       message_update = assert_raise(OSM::APIBadUserInput) do
247         Node.from_xml(zero_id, false)
248       end
249       assert_match(/ID of node cannot be zero when updating/, message_update.message)
250     end
251   end
252
253   def test_from_xml_no_text
254     no_text = ""
255     message_create = assert_raise(OSM::APIBadXMLError) do
256       Node.from_xml(no_text, true)
257     end
258     assert_match(/Must specify a string with one or more characters/, message_create.message)
259     message_update = assert_raise(OSM::APIBadXMLError) do
260       Node.from_xml(no_text, false)
261     end
262     assert_match(/Must specify a string with one or more characters/, message_update.message)
263   end
264
265   def test_from_xml_no_node
266     no_node = "<osm></osm>"
267     message_create = assert_raise(OSM::APIBadXMLError) do
268       Node.from_xml(no_node, true)
269     end
270     assert_match %r{XML doesn't contain an osm/node element}, message_create.message
271     message_update = assert_raise(OSM::APIBadXMLError) do
272       Node.from_xml(no_node, false)
273     end
274     assert_match %r{XML doesn't contain an osm/node element}, message_update.message
275   end
276
277   def test_from_xml_no_k_v
278     nokv = "<osm><node id='23' lat='12.3' lon='23.4' changeset='12' version='23'><tag /></node></osm>"
279     message_create = assert_raise(OSM::APIBadXMLError) do
280       Node.from_xml(nokv, true)
281     end
282     assert_match(/tag is missing key/, message_create.message)
283     message_update = assert_raise(OSM::APIBadXMLError) do
284       Node.from_xml(nokv, false)
285     end
286     assert_match(/tag is missing key/, message_update.message)
287   end
288
289   def test_from_xml_no_v
290     no_v = "<osm><node id='23' lat='23.43' lon='23.32' changeset='23' version='32'><tag k='key' /></node></osm>"
291     message_create = assert_raise(OSM::APIBadXMLError) do
292       Node.from_xml(no_v, true)
293     end
294     assert_match(/tag is missing value/, message_create.message)
295     message_update = assert_raise(OSM::APIBadXMLError) do
296       Node.from_xml(no_v, false)
297     end
298     assert_match(/tag is missing value/, message_update.message)
299   end
300
301   def test_from_xml_duplicate_k
302     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>"
303     message_create = assert_raise(OSM::APIDuplicateTagsError) do
304       Node.from_xml(dupk, true)
305     end
306     assert_equal "Element node/ has duplicate tags with key dup", message_create.message
307     message_update = assert_raise(OSM::APIDuplicateTagsError) do
308       Node.from_xml(dupk, false)
309     end
310     assert_equal "Element node/23 has duplicate tags with key dup", message_update.message
311   end
312
313   def test_node_tags
314     node = create(:node)
315     taglist = create_list(:node_tag, 2, :node => node)
316     tags = Node.find(node.id).node_tags.order(:k)
317     assert_equal taglist.count, tags.count
318     taglist.sort_by!(&:k).each_index do |i|
319       assert_equal taglist[i].k, tags[i].k
320       assert_equal taglist[i].v, tags[i].v
321     end
322   end
323
324   def test_tags
325     node = create(:node)
326     taglist = create_list(:node_tag, 2, :node => node)
327     tags = Node.find(node.id).tags
328     assert_equal taglist.count, tags.count
329     taglist.each do |tag|
330       assert_equal tag.v, tags[tag.k]
331     end
332   end
333
334   def test_containing_relation_members
335     node = create(:node)
336     relation_member1 = create(:relation_member, :member => node)
337     relation_member2 = create(:relation_member, :member => node)
338     relation_member3 = create(:relation_member, :member => node)
339     crm = Node.find(node.id).containing_relation_members.order(:relation_id)
340     #    assert_equal 3, crm.size
341     assert_equal relation_member1.relation_id, crm.first.relation_id
342     assert_equal "Node", crm.first.member_type
343     assert_equal node.id, crm.first.member_id
344     assert_equal relation_member1.relation_id, crm.first.relation.id
345     assert_equal relation_member2.relation_id, crm.second.relation_id
346     assert_equal "Node", crm.second.member_type
347     assert_equal node.id, crm.second.member_id
348     assert_equal relation_member2.relation_id, crm.second.relation.id
349     assert_equal relation_member3.relation_id, crm.third.relation_id
350     assert_equal "Node", crm.third.member_type
351     assert_equal node.id, crm.third.member_id
352     assert_equal relation_member3.relation_id, crm.third.relation.id
353   end
354
355   def test_containing_relations
356     node = create(:node)
357     relation_member1 = create(:relation_member, :member => node)
358     relation_member2 = create(:relation_member, :member => node)
359     relation_member3 = create(:relation_member, :member => node)
360     cr = Node.find(node.id).containing_relations.order(:id)
361
362     assert_equal 3, cr.size
363     assert_equal relation_member1.relation.id, cr.first.id
364     assert_equal relation_member2.relation.id, cr.second.id
365     assert_equal relation_member3.relation.id, cr.third.id
366   end
367 end