3 class WayTest < ActiveSupport::TestCase
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
9 assert_equal 7, Way.count
14 visible_way = create(:way)
15 create(:way_node, :way => visible_way, :node => node)
16 invisible_way = create(:way)
17 create(:way_node, :way => invisible_way, :node => node)
18 used_way = create(:way)
19 create(:way_node, :way => used_way, :node => node)
20 create(:relation_member, :member => used_way)
22 [visible_way, invisible_way, used_way].each do |way|
23 assert_equal node.bbox.min_lon, way.bbox.min_lon, "min_lon"
24 assert_equal node.bbox.min_lat, way.bbox.min_lat, "min_lat"
25 assert_equal node.bbox.max_lon, way.bbox.max_lon, "max_lon"
26 assert_equal node.bbox.max_lat, way.bbox.max_lat, "max_lat"
30 # Check that the preconditions fail when you are over the defined limit of
31 # the maximum number of nodes in each way.
32 def test_max_nodes_per_way_limit
33 node_a = create(:node)
34 node_b = create(:node)
35 node_c = create(:node)
36 way = create(:way_with_nodes, :nodes_count => 1)
37 # Take one of the current ways and add nodes to it until we are near the limit
38 way = Way.find(current_ways(:visible_way).id)
40 # it already has 1 node
41 1.upto(MAX_NUMBER_OF_WAY_NODES / 2) do
42 way.add_nd_num(node_a.id)
43 way.add_nd_num(node_b.id)
48 way.add_nd_num(node_c.id)
52 def test_from_xml_no_id
53 noid = "<osm><way version='12' changeset='23' /></osm>"
54 assert_nothing_raised(OSM::APIBadXMLError) do
55 Way.from_xml(noid, true)
57 message = assert_raise(OSM::APIBadXMLError) do
58 Way.from_xml(noid, false)
60 assert_match /ID is required when updating/, message.message
63 def test_from_xml_no_changeset_id
64 nocs = "<osm><way id='123' version='23' /></osm>"
65 message_create = assert_raise(OSM::APIBadXMLError) do
66 Way.from_xml(nocs, true)
68 assert_match /Changeset id is missing/, message_create.message
69 message_update = assert_raise(OSM::APIBadXMLError) do
70 Way.from_xml(nocs, false)
72 assert_match /Changeset id is missing/, message_update.message
75 def test_from_xml_no_version
76 no_version = "<osm><way id='123' changeset='23' /></osm>"
77 assert_nothing_raised(OSM::APIBadXMLError) do
78 Way.from_xml(no_version, true)
80 message_update = assert_raise(OSM::APIBadXMLError) do
81 Way.from_xml(no_version, false)
83 assert_match /Version is required when updating/, message_update.message
86 def test_from_xml_id_zero
87 id_list = ["", "0", "00", "0.0", "a"]
89 zero_id = "<osm><way id='#{id}' changeset='33' version='23' /></osm>"
90 assert_nothing_raised(OSM::APIBadUserInput) do
91 Way.from_xml(zero_id, true)
93 message_update = assert_raise(OSM::APIBadUserInput) do
94 Way.from_xml(zero_id, false)
96 assert_match /ID of way cannot be zero when updating/, message_update.message
100 def test_from_xml_no_text
102 message_create = assert_raise(OSM::APIBadXMLError) do
103 Way.from_xml(no_text, true)
105 assert_match /Must specify a string with one or more characters/, message_create.message
106 message_update = assert_raise(OSM::APIBadXMLError) do
107 Way.from_xml(no_text, false)
109 assert_match /Must specify a string with one or more characters/, message_update.message
112 def test_from_xml_no_k_v
113 nokv = "<osm><way id='23' changeset='23' version='23'><tag /></way></osm>"
114 message_create = assert_raise(OSM::APIBadXMLError) do
115 Way.from_xml(nokv, true)
117 assert_match /tag is missing key/, message_create.message
118 message_update = assert_raise(OSM::APIBadXMLError) do
119 Way.from_xml(nokv, false)
121 assert_match /tag is missing key/, message_update.message
124 def test_from_xml_no_v
125 no_v = "<osm><way id='23' changeset='23' version='23'><tag k='key' /></way></osm>"
126 message_create = assert_raise(OSM::APIBadXMLError) do
127 Way.from_xml(no_v, true)
129 assert_match /tag is missing value/, message_create.message
130 message_update = assert_raise(OSM::APIBadXMLError) do
131 Way.from_xml(no_v, false)
133 assert_match /tag is missing value/, message_update.message
136 def test_from_xml_duplicate_k
137 dupk = "<osm><way id='23' changeset='23' version='23'><tag k='dup' v='test' /><tag k='dup' v='tester' /></way></osm>"
138 message_create = assert_raise(OSM::APIDuplicateTagsError) do
139 Way.from_xml(dupk, true)
141 assert_equal "Element way/ has duplicate tags with key dup", message_create.message
142 message_update = assert_raise(OSM::APIDuplicateTagsError) do
143 Way.from_xml(dupk, false)
145 assert_equal "Element way/23 has duplicate tags with key dup", message_update.message
149 way = current_ways(:way_with_multiple_nodes)
150 nodes = Way.find(way.id).way_nodes
151 assert_equal 3, nodes.count
152 assert_equal 4, nodes[0].node_id
153 assert_equal 15, nodes[1].node_id
154 assert_equal 11, nodes[2].node_id
158 way = current_ways(:way_with_multiple_nodes)
159 nodes = Way.find(way.id).nodes
160 assert_equal 3, nodes.count
161 assert_equal 4, nodes[0].id
162 assert_equal 15, nodes[1].id
163 assert_equal 11, nodes[2].id
167 way = current_ways(:way_with_multiple_nodes)
168 nodes = Way.find(way.id).nds
169 assert_equal 3, nodes.count
170 assert_equal 4, nodes[0]
171 assert_equal 15, nodes[1]
172 assert_equal 11, nodes[2]
176 way = current_ways(:way_with_versions)
177 taglist = create_list(:way_tag, 2, :way => way)
178 tags = Way.find(way.id).way_tags.order(:k)
179 assert_equal taglist.count, tags.count
180 taglist.sort_by!(&:k).each_index do |i|
181 assert_equal taglist[i].k, tags[i].k
182 assert_equal taglist[i].v, tags[i].v
187 way = current_ways(:way_with_versions)
188 taglist = create_list(:way_tag, 2, :way => way)
189 tags = Way.find(way.id).tags
190 assert_equal taglist.count, tags.count
191 taglist.each do |tag|
192 assert_equal tag.v, tags[tag.k]
196 def test_containing_relation_members
197 way = current_ways(:used_way)
198 crm = Way.find(way.id).containing_relation_members.order(:relation_id)
199 # assert_equal 1, crm.size
200 assert_equal 1, crm.first.relation_id
201 assert_equal "Way", crm.first.member_type
202 assert_equal way.id, crm.first.member_id
203 assert_equal 1, crm.first.relation.id
206 def test_containing_relations
207 way = current_ways(:used_way)
208 cr = Way.find(way.id).containing_relations.order(:id)
209 assert_equal 1, cr.size
210 assert_equal 1, cr.first.id