]> git.openstreetmap.org Git - rails.git/blob - test/models/node_test.rb
Sprockets 3 seems to be OK now
[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 18, 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     node_template = Node.new(
81       :latitude => 12.3456,
82       :longitude => 65.4321,
83       :changeset_id => changesets(:normal_user_first_change).id,
84       :visible => 1,
85       :version => 1
86     )
87     assert node_template.create_with_history(users(:normal_user))
88
89     node = Node.find(node_template.id)
90     assert_not_nil node
91     assert_equal node_template.latitude, node.latitude
92     assert_equal node_template.longitude, node.longitude
93     assert_equal node_template.changeset_id, node.changeset_id
94     assert_equal node_template.visible, node.visible
95     assert_equal node_template.timestamp.to_i, node.timestamp.to_i
96
97     assert_equal OldNode.where(:node_id => node_template.id).count, 1
98     old_node = OldNode.where(:node_id => node_template.id).first
99     assert_not_nil old_node
100     assert_equal node_template.latitude, old_node.latitude
101     assert_equal node_template.longitude, old_node.longitude
102     assert_equal node_template.changeset_id, old_node.changeset_id
103     assert_equal node_template.visible, old_node.visible
104     assert_equal node_template.tags, old_node.tags
105     assert_equal node_template.timestamp.to_i, old_node.timestamp.to_i
106   end
107
108   def test_update
109     node_template = Node.find(current_nodes(:visible_node).id)
110     assert_not_nil node_template
111
112     assert_equal OldNode.where(:node_id => node_template.id).count, 1
113     node = Node.find(node_template.id)
114     assert_not_nil node
115
116     node_template.latitude = 12.3456
117     node_template.longitude = 65.4321
118     # node_template.tags = "updated=yes"
119     assert node.update_from(node_template, users(:normal_user))
120
121     node = Node.find(node_template.id)
122     assert_not_nil node
123     assert_equal node_template.latitude, node.latitude
124     assert_equal node_template.longitude, node.longitude
125     assert_equal node_template.changeset_id, node.changeset_id
126     assert_equal node_template.visible, node.visible
127     # assert_equal node_template.tags, node.tags
128
129     assert_equal OldNode.where(:node_id => node_template.id).count, 2
130     old_node = OldNode.where(:node_id => node_template.id, :version => 2).first
131     assert_not_nil old_node
132     assert_equal node_template.latitude, old_node.latitude
133     assert_equal node_template.longitude, old_node.longitude
134     assert_equal node_template.changeset_id, old_node.changeset_id
135     assert_equal node_template.visible, old_node.visible
136     # assert_equal node_template.tags, old_node.tags
137   end
138
139   def test_delete
140     node_template = Node.find(current_nodes(:visible_node).id)
141     assert_not_nil node_template
142
143     assert_equal OldNode.where(:node_id => node_template.id).count, 1
144     node = Node.find(node_template.id)
145     assert_not_nil node
146
147     assert node.delete_with_history!(node_template, users(:normal_user))
148
149     node = Node.find(node_template.id)
150     assert_not_nil node
151     assert_equal node_template.latitude, node.latitude
152     assert_equal node_template.longitude, node.longitude
153     assert_equal node_template.changeset_id, node.changeset_id
154     assert_equal false, node.visible
155     # assert_equal node_template.tags, node.tags
156
157     assert_equal OldNode.where(:node_id => node_template.id).count, 2
158     old_node = OldNode.where(:node_id => node_template.id, :version => 2).first
159     assert_not_nil old_node
160     assert_equal node_template.latitude, old_node.latitude
161     assert_equal node_template.longitude, old_node.longitude
162     assert_equal node_template.changeset_id, old_node.changeset_id
163     assert_equal false, old_node.visible
164     # assert_equal node_template.tags, old_node.tags
165   end
166
167   def test_from_xml_no_id
168     lat = 56.7
169     lon = -2.3
170     changeset = 2
171     version = 1
172     noid = "<osm><node lat='#{lat}' lon='#{lon}' changeset='#{changeset}' version='#{version}' /></osm>"
173     # First try a create which doesn't need the id
174     assert_nothing_raised(OSM::APIBadXMLError) do
175       Node.from_xml(noid, true)
176     end
177     # Now try an update with no id, and make sure that it gives the appropriate exception
178     message = assert_raise(OSM::APIBadXMLError) do
179       Node.from_xml(noid, false)
180     end
181     assert_match /ID is required when updating./, message.message
182   end
183
184   def test_from_xml_no_lat
185     nolat = "<osm><node id='1' lon='23.3' changeset='2' version='23' /></osm>"
186     message_create = assert_raise(OSM::APIBadXMLError) do
187       Node.from_xml(nolat, true)
188     end
189     assert_match /lat missing/, message_create.message
190     message_update = assert_raise(OSM::APIBadXMLError) do
191       Node.from_xml(nolat, false)
192     end
193     assert_match /lat missing/, message_update.message
194   end
195
196   def test_from_xml_no_lon
197     nolon = "<osm><node id='1' lat='23.1' changeset='2' version='23' /></osm>"
198     message_create = assert_raise(OSM::APIBadXMLError) do
199       Node.from_xml(nolon, true)
200     end
201     assert_match /lon missing/, message_create.message
202     message_update = assert_raise(OSM::APIBadXMLError) do
203       Node.from_xml(nolon, false)
204     end
205     assert_match /lon missing/, message_update.message
206   end
207
208   def test_from_xml_no_changeset_id
209     nocs = "<osm><node id='123' lon='23.23' lat='23.1' version='23' /></osm>"
210     message_create = assert_raise(OSM::APIBadXMLError) do
211       Node.from_xml(nocs, true)
212     end
213     assert_match /Changeset id is missing/, message_create.message
214     message_update = assert_raise(OSM::APIBadXMLError) do
215       Node.from_xml(nocs, false)
216     end
217     assert_match /Changeset id is missing/, message_update.message
218   end
219
220   def test_from_xml_no_version
221     no_version = "<osm><node id='123' lat='23' lon='23' changeset='23' /></osm>"
222     assert_nothing_raised(OSM::APIBadXMLError) do
223       Node.from_xml(no_version, true)
224     end
225     message_update = assert_raise(OSM::APIBadXMLError) do
226       Node.from_xml(no_version, false)
227     end
228     assert_match /Version is required when updating/, message_update.message
229   end
230
231   def test_from_xml_double_lat
232     nocs = "<osm><node id='123' lon='23.23' lat='23.1' lat='12' changeset='23' version='23' /></osm>"
233     message_create = assert_raise(OSM::APIBadXMLError) do
234       Node.from_xml(nocs, true)
235     end
236     assert_match /Fatal error: Attribute lat redefined at/, message_create.message
237     message_update = assert_raise(OSM::APIBadXMLError) do
238       Node.from_xml(nocs, false)
239     end
240     assert_match /Fatal error: Attribute lat redefined at/, message_update.message
241   end
242
243   def test_from_xml_id_zero
244     id_list = ["", "0", "00", "0.0", "a"]
245     id_list.each do |id|
246       zero_id = "<osm><node id='#{id}' lat='12.3' lon='12.3' changeset='33' version='23' /></osm>"
247       assert_nothing_raised(OSM::APIBadUserInput) do
248         Node.from_xml(zero_id, true)
249       end
250       message_update = assert_raise(OSM::APIBadUserInput) do
251         Node.from_xml(zero_id, false)
252       end
253       assert_match /ID of node cannot be zero when updating/, message_update.message
254     end
255   end
256
257   def test_from_xml_no_text
258     no_text = ""
259     message_create = assert_raise(OSM::APIBadXMLError) do
260       Node.from_xml(no_text, true)
261     end
262     assert_match /Must specify a string with one or more characters/, message_create.message
263     message_update = assert_raise(OSM::APIBadXMLError) do
264       Node.from_xml(no_text, false)
265     end
266     assert_match /Must specify a string with one or more characters/, message_update.message
267   end
268
269   def test_from_xml_no_node
270     no_node = "<osm></osm>"
271     message_create = assert_raise(OSM::APIBadXMLError) do
272       Node.from_xml(no_node, true)
273     end
274     assert_match %r{XML doesn't contain an osm/node element}, message_create.message
275     message_update = assert_raise(OSM::APIBadXMLError) do
276       Node.from_xml(no_node, false)
277     end
278     assert_match %r{XML doesn't contain an osm/node element}, message_update.message
279   end
280
281   def test_from_xml_no_k_v
282     nokv = "<osm><node id='23' lat='12.3' lon='23.4' changeset='12' version='23'><tag /></node></osm>"
283     message_create = assert_raise(OSM::APIBadXMLError) do
284       Node.from_xml(nokv, true)
285     end
286     assert_match /tag is missing key/, message_create.message
287     message_update = assert_raise(OSM::APIBadXMLError) do
288       Node.from_xml(nokv, false)
289     end
290     assert_match /tag is missing key/, message_update.message
291   end
292
293   def test_from_xml_no_v
294     no_v = "<osm><node id='23' lat='23.43' lon='23.32' changeset='23' version='32'><tag k='key' /></node></osm>"
295     message_create = assert_raise(OSM::APIBadXMLError) do
296       Node.from_xml(no_v, true)
297     end
298     assert_match /tag is missing value/, message_create.message
299     message_update = assert_raise(OSM::APIBadXMLError) do
300       Node.from_xml(no_v, false)
301     end
302     assert_match /tag is missing value/, message_update.message
303   end
304
305   def test_from_xml_duplicate_k
306     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>"
307     message_create = assert_raise(OSM::APIDuplicateTagsError) do
308       Node.from_xml(dupk, true)
309     end
310     assert_equal "Element node/ has duplicate tags with key dup", message_create.message
311     message_update = assert_raise(OSM::APIDuplicateTagsError) do
312       Node.from_xml(dupk, false)
313     end
314     assert_equal "Element node/23 has duplicate tags with key dup", message_update.message
315   end
316
317   def test_node_tags
318     node = current_nodes(:node_with_versions)
319     tags = Node.find(node.id).node_tags.order(:k)
320     assert_equal 2, tags.count
321     assert_equal "testing", tags[0].k
322     assert_equal "added in node version 3", tags[0].v
323     assert_equal "testing two", tags[1].k
324     assert_equal "modified in node version 4", tags[1].v
325   end
326
327   def test_tags
328     node = current_nodes(:node_with_versions)
329     tags = Node.find(node.id).tags
330     assert_equal 2, tags.size
331     assert_equal "added in node version 3", tags["testing"]
332     assert_equal "modified in node version 4", tags["testing two"]
333   end
334
335   def test_containing_relation_members
336     node = current_nodes(:node_used_by_relationship)
337     crm = Node.find(node.id).containing_relation_members.order(:relation_id)
338     #    assert_equal 3, crm.size
339     assert_equal 1, crm.first.relation_id
340     assert_equal "Node", crm.first.member_type
341     assert_equal node.id, crm.first.member_id
342     assert_equal 1, crm.first.relation.id
343     assert_equal 2, crm.second.relation_id
344     assert_equal "Node", crm.second.member_type
345     assert_equal node.id, crm.second.member_id
346     assert_equal 2, crm.second.relation.id
347     assert_equal 3, crm.third.relation_id
348     assert_equal "Node", crm.third.member_type
349     assert_equal node.id, crm.third.member_id
350     assert_equal 3, crm.third.relation.id
351   end
352
353   def test_containing_relations
354     node = current_nodes(:node_used_by_relationship)
355     cr = Node.find(node.id).containing_relations.order(:id)
356     assert_equal 3, cr.size
357     assert_equal 1, cr.first.id
358     assert_equal 2, cr.second.id
359     assert_equal 3, cr.third.id
360   end
361 end