]> git.openstreetmap.org Git - rails.git/blob - test/unit/way_test.rb
Rename a number of methods in the map_bugs controller
[rails.git] / test / unit / way_test.rb
1 require File.dirname(__FILE__) + '/../test_helper'
2
3 class WayTest < ActiveSupport::TestCase
4   api_fixtures
5
6   # Check that we have the correct number of currnet ways in the db
7   # This will need to updated whenever the current_ways.yml is updated
8   def test_db_count
9     assert_equal 5, Way.count
10   end
11   
12   def test_bbox
13     node = current_nodes(:used_node_1)
14     [ :visible_way,
15       :invisible_way,
16       :used_way ].each do |way_symbol|
17       way = current_ways(way_symbol)
18       assert_equal node.bbox, way.bbox
19     end
20   end
21   
22   # Check that the preconditions fail when you are over the defined limit of 
23   # the maximum number of nodes in each way.
24   def test_max_nodes_per_way_limit
25     # Take one of the current ways and add nodes to it until we are near the limit
26     way = Way.find(current_ways(:visible_way).id)
27     assert way.valid?
28     # it already has 1 node
29     1.upto((MAX_NUMBER_OF_WAY_NODES) / 2) {
30       way.add_nd_num(current_nodes(:used_node_1).id)
31       way.add_nd_num(current_nodes(:used_node_2).id)
32     }
33     way.save
34     #print way.nds.size
35     assert way.valid?
36     way.add_nd_num(current_nodes(:visible_node).id)
37     assert way.valid?
38   end
39   
40   def test_from_xml_no_id
41     noid = "<osm><way version='12' changeset='23' /></osm>"
42     assert_nothing_raised(OSM::APIBadXMLError) {
43       Way.from_xml(noid, true)
44     }
45     message = assert_raise(OSM::APIBadXMLError) {
46       Way.from_xml(noid, false)
47     }
48     assert_match /ID is required when updating/, message.message
49   end
50   
51   def test_from_xml_no_changeset_id
52     nocs = "<osm><way id='123' version='23' /></osm>"
53     message_create = assert_raise(OSM::APIBadXMLError) {
54       Way.from_xml(nocs, true)
55     }
56     assert_match /Changeset id is missing/, message_create.message
57     message_update = assert_raise(OSM::APIBadXMLError) {
58       Way.from_xml(nocs, false)
59     }
60     assert_match /Changeset id is missing/, message_update.message
61   end
62   
63   def test_from_xml_no_version
64     no_version = "<osm><way id='123' changeset='23' /></osm>"
65     assert_nothing_raised(OSM::APIBadXMLError) {
66       Way.from_xml(no_version, true)
67     }
68     message_update = assert_raise(OSM::APIBadXMLError) {
69       Way.from_xml(no_version, false)
70     }
71     assert_match /Version is required when updating/, message_update.message
72   end
73
74   def test_from_xml_id_zero
75     id_list = ["", "0", "00", "0.0", "a"]
76     id_list.each do |id|
77       zero_id = "<osm><way id='#{id}' changeset='33' version='23' /></osm>"
78       assert_nothing_raised(OSM::APIBadUserInput) {
79         Way.from_xml(zero_id, true)
80       }
81       message_update = assert_raise(OSM::APIBadUserInput) {
82         Way.from_xml(zero_id, false)
83       }
84       assert_match /ID of way cannot be zero when updating/, message_update.message
85     end
86   end
87   
88   def test_from_xml_no_text
89     no_text = ""
90     message_create = assert_raise(OSM::APIBadXMLError) {
91       Way.from_xml(no_text, true)
92     }
93     assert_match /Must specify a string with one or more characters/, message_create.message
94     message_update = assert_raise(OSM::APIBadXMLError) {
95       Way.from_xml(no_text, false)
96     }
97     assert_match /Must specify a string with one or more characters/, message_update.message
98   end
99   
100   def test_from_xml_no_k_v
101     nokv = "<osm><way id='23' changeset='23' version='23'><tag /></way></osm>"
102     message_create = assert_raise(OSM::APIBadXMLError) {
103       Way.from_xml(nokv, true)
104     }
105     assert_match /tag is missing key/, message_create.message
106     message_update = assert_raise(OSM::APIBadXMLError) {
107       Way.from_xml(nokv, false)
108     }
109     assert_match /tag is missing key/, message_update.message
110   end
111   
112   def test_from_xml_no_v
113     no_v = "<osm><way id='23' changeset='23' version='23'><tag k='key' /></way></osm>"
114     message_create = assert_raise(OSM::APIBadXMLError) {
115       Way.from_xml(no_v, true)
116     }
117     assert_match /tag is missing value/, message_create.message
118     message_update = assert_raise(OSM::APIBadXMLError) {
119       Way.from_xml(no_v, false)
120     }
121     assert_match /tag is missing value/, message_update.message
122   end
123   
124   def test_from_xml_duplicate_k
125     dupk = "<osm><way id='23' changeset='23' version='23'><tag k='dup' v='test' /><tag k='dup' v='tester' /></way></osm>"
126     message_create = assert_raise(OSM::APIDuplicateTagsError) {
127       Way.from_xml(dupk, true)
128     }
129     assert_equal "Element way/ has duplicate tags with key dup", message_create.message
130     message_update = assert_raise(OSM::APIDuplicateTagsError) {
131       Way.from_xml(dupk, false)
132     }
133     assert_equal "Element way/23 has duplicate tags with key dup", message_update.message
134   end
135 end