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