source: subversion/sites/rails_port/test/unit/node_test.rb @ 15798

Last change on this file since 15798 was 15798, checked in by Shaun McDonald, 10 years ago

More Node.from_xml unit tests

File size: 9.8 KB
Line 
1require File.dirname(__FILE__) + '/../test_helper'
2
3class NodeTest < ActiveSupport::TestCase
4  api_fixtures
5 
6  def test_node_count
7    assert_equal 16, 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/SCALE
53    assert_equal dbnode.lon, node.longitude.to_f/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/SCALE
69    assert_equal dbnode.lon, node.longitude.to_f/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(:latitude => 12.3456,
81                             :longitude => 65.4321,
82                             :changeset_id => changesets(:normal_user_first_change).id,
83                             :visible => 1, 
84                             :version => 1)
85    assert node_template.create_with_history(users(:normal_user))
86
87    node = Node.find(node_template.id)
88    assert_not_nil node
89    assert_equal node_template.latitude, node.latitude
90    assert_equal node_template.longitude, node.longitude
91    assert_equal node_template.changeset_id, node.changeset_id
92    assert_equal node_template.visible, node.visible
93    assert_equal node_template.timestamp.to_i, node.timestamp.to_i
94
95    assert_equal OldNode.find(:all, :conditions => [ "id = ?", node_template.id ]).length, 1
96    old_node = OldNode.find(:first, :conditions => [ "id = ?", node_template.id ])
97    assert_not_nil old_node
98    assert_equal node_template.latitude, old_node.latitude
99    assert_equal node_template.longitude, old_node.longitude
100    assert_equal node_template.changeset_id, old_node.changeset_id
101    assert_equal node_template.visible, old_node.visible
102    assert_equal node_template.tags, old_node.tags
103    assert_equal node_template.timestamp.to_i, old_node.timestamp.to_i
104  end
105
106  def test_update
107    node_template = Node.find(current_nodes(:visible_node).id)
108    assert_not_nil node_template
109
110    assert_equal OldNode.find(:all, :conditions => [ "id = ?", node_template.id ]).length, 1
111    old_node_template = OldNode.find(:first, :conditions => [ "id = ?", node_template.id ])
112    assert_not_nil old_node_template
113
114    node_template.latitude = 12.3456
115    node_template.longitude = 65.4321
116    #node_template.tags = "updated=yes"
117    assert node_template.update_from(old_node_template, users(:normal_user))
118
119    node = Node.find(node_template.id)
120    assert_not_nil node
121    assert_equal node_template.latitude, node.latitude
122    assert_equal node_template.longitude, node.longitude
123    assert_equal node_template.changeset_id, node.changeset_id
124    assert_equal node_template.visible, node.visible
125    #assert_equal node_template.tags, node.tags
126    assert_equal node_template.timestamp.to_i, node.timestamp.to_i
127
128    assert_equal OldNode.find(:all, :conditions => [ "id = ?", node_template.id ]).length, 2
129    assert_equal OldNode.find(:all, :conditions => [ "id = ? and timestamp = ?", node_template.id, node_template.timestamp ]).length, 1
130    old_node = OldNode.find(:first, :conditions => [ "id = ? and timestamp = ?", node_template.id, node_template.timestamp ])
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    assert_equal node_template.timestamp.to_i, old_node.timestamp.to_i
138  end
139
140  def test_delete
141    node_template = Node.find(current_nodes(:visible_node))
142    assert_not_nil node_template
143
144    assert_equal OldNode.find(:all, :conditions => [ "id = ?", node_template.id ]).length, 1
145    old_node_template = OldNode.find(:first, :conditions => [ "id = ?", node_template.id ])
146    assert_not_nil old_node_template
147
148    assert node_template.delete_with_history!(old_node_template, users(:normal_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 node_template.visible, node.visible
156    #assert_equal node_template.tags, node.tags
157    assert_equal node_template.timestamp.to_i, node.timestamp.to_i
158
159    assert_equal OldNode.find(:all, :conditions => [ "id = ?", node_template.id ]).length, 2
160    assert_equal OldNode.find(:all, :conditions => [ "id = ? and timestamp = ?", node_template.id, node_template.timestamp ]).length, 1
161    old_node = OldNode.find(:first, :conditions => [ "id = ? and timestamp = ?", node_template.id, node_template.timestamp ])
162    assert_not_nil old_node
163    assert_equal node_template.latitude, old_node.latitude
164    assert_equal node_template.longitude, old_node.longitude
165    assert_equal node_template.changeset_id, old_node.changeset_id
166    assert_equal node_template.visible, old_node.visible
167    #assert_equal node_template.tags, old_node.tags
168    assert_equal node_template.timestamp.to_i, old_node.timestamp.to_i
169  end
170 
171  def test_from_xml_no_id
172    lat = 56.7
173    lon = -2.3
174    changeset = 2
175    version = 1
176    noid = "<osm><node lat='#{lat}' lon='#{lon}' changeset='#{changeset}' version='#{version}' /></osm>"
177    # First try a create which doesn't need the id
178    assert_nothing_raised(OSM::APIBadXMLError) {
179      Node.from_xml(noid, true)
180    }
181    # Now try an update with no id, and make sure that it gives the appropriate exception
182    message = assert_raise(OSM::APIBadXMLError) {
183      Node.from_xml(noid, false)
184    }
185    assert_match /ID is required when updating./, message.message
186  end
187 
188  def test_from_xml_no_lat
189    nolat = "<osm><node id='1' lon='23.3' changeset='2' version='23' /></osm>"
190    message_create = assert_raise(OSM::APIBadXMLError) {
191      Node.from_xml(nolat, true)
192    }
193    assert_match /lat missing/, message_create.message
194    message_update = assert_raise(OSM::APIBadXMLError) {
195      Node.from_xml(nolat, false)
196    }
197    assert_match /lat missing/, message_update.message
198  end
199 
200  def test_from_xml_no_lon
201    nolon = "<osm><node id='1' lat='23.1' changeset='2' version='23' /></osm>"
202    message_create = assert_raise(OSM::APIBadXMLError) {
203      Node.from_xml(nolon, true)
204    }
205    assert_match /lon missing/, message_create.message
206    message_update = assert_raise(OSM::APIBadXMLError) {
207      Node.from_xml(nolon, false)
208    }
209    assert_match /lon missing/, message_update.message
210  end
211
212  def test_from_xml_no_changeset_id
213    nocs = "<osm><node id='123' lon='23.23' lat='23.1' version='23' /></osm>"
214    message_create = assert_raise(OSM::APIBadXMLError) {
215      Node.from_xml(nocs, true)
216    }
217    assert_match /changeset id missing/, message_create.message
218    message_update = assert_raise(OSM::APIBadXMLError) {
219      Node.from_xml(nocs, false)
220    }
221    assert_match /changeset id missing/, message_update.message
222  end
223 
224  def test_from_xml_no_version
225    no_version = "<osm><node id='123' lat='23' lon='23' changeset='23' /></osm>"
226    assert_nothing_raised(OSM::APIBadXMLError) {
227      Node.from_xml(no_version, true)
228    }
229    message_update = assert_raise(OSM::APIBadXMLError) {
230      Node.from_xml(no_version, false)
231    }
232    assert_match /Version is required when updating/, message_update.message
233  end
234 
235  def test_from_xml_double_lat
236    nocs = "<osm><node id='123' lon='23.23' lat='23.1' lat='12' changeset='23' version='23' /></osm>"
237    message_create = assert_raise(OSM::APIBadXMLError) {
238      Node.from_xml(nocs, true)
239    } 
240    assert_match /Fatal error: Attribute lat redefined at/, message_create.message
241    message_update = assert_raise(OSM::APIBadXMLError) {
242      Node.from_xml(nocs, false)
243    }
244    assert_match /Fatal error: Attribute lat redefined at/, message_update.message
245  end
246 
247  def test_from_xml_no_text
248    no_text = ""
249    message_create = assert_raise(OSM::APIBadXMLError) {
250      Node.from_xml(no_text, true)
251    }
252    assert_match /Must specify a string with one or more characters/, message_create.message
253    message_update = assert_raise(OSM::APIBadXMLError) {
254      Node.from_xml(no_text, false)
255    }
256    assert_match /Must specify a string with one or more characters/, message_create.message
257  end
258end
Note: See TracBrowser for help on using the repository browser.