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, :deleted)
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
39 # it already has 1 node
40 1.upto(MAX_NUMBER_OF_WAY_NODES / 2) do
41 way.add_nd_num(node_a.id)
42 way.add_nd_num(node_b.id)
47 way.add_nd_num(node_c.id)
51 def test_from_xml_no_id
52 noid = "<osm><way version='12' changeset='23' /></osm>"
53 assert_nothing_raised(OSM::APIBadXMLError) do
54 Way.from_xml(noid, true)
56 message = assert_raise(OSM::APIBadXMLError) do
57 Way.from_xml(noid, false)
59 assert_match /ID is required when updating/, message.message
62 def test_from_xml_no_changeset_id
63 nocs = "<osm><way id='123' version='23' /></osm>"
64 message_create = assert_raise(OSM::APIBadXMLError) do
65 Way.from_xml(nocs, true)
67 assert_match /Changeset id is missing/, message_create.message
68 message_update = assert_raise(OSM::APIBadXMLError) do
69 Way.from_xml(nocs, false)
71 assert_match /Changeset id is missing/, message_update.message
74 def test_from_xml_no_version
75 no_version = "<osm><way id='123' changeset='23' /></osm>"
76 assert_nothing_raised(OSM::APIBadXMLError) do
77 Way.from_xml(no_version, true)
79 message_update = assert_raise(OSM::APIBadXMLError) do
80 Way.from_xml(no_version, false)
82 assert_match /Version is required when updating/, message_update.message
85 def test_from_xml_id_zero
86 id_list = ["", "0", "00", "0.0", "a"]
88 zero_id = "<osm><way id='#{id}' changeset='33' version='23' /></osm>"
89 assert_nothing_raised(OSM::APIBadUserInput) do
90 Way.from_xml(zero_id, true)
92 message_update = assert_raise(OSM::APIBadUserInput) do
93 Way.from_xml(zero_id, false)
95 assert_match /ID of way cannot be zero when updating/, message_update.message
99 def test_from_xml_no_text
101 message_create = assert_raise(OSM::APIBadXMLError) do
102 Way.from_xml(no_text, true)
104 assert_match /Must specify a string with one or more characters/, message_create.message
105 message_update = assert_raise(OSM::APIBadXMLError) do
106 Way.from_xml(no_text, false)
108 assert_match /Must specify a string with one or more characters/, message_update.message
111 def test_from_xml_no_k_v
112 nokv = "<osm><way id='23' changeset='23' version='23'><tag /></way></osm>"
113 message_create = assert_raise(OSM::APIBadXMLError) do
114 Way.from_xml(nokv, true)
116 assert_match /tag is missing key/, message_create.message
117 message_update = assert_raise(OSM::APIBadXMLError) do
118 Way.from_xml(nokv, false)
120 assert_match /tag is missing key/, message_update.message
123 def test_from_xml_no_v
124 no_v = "<osm><way id='23' changeset='23' version='23'><tag k='key' /></way></osm>"
125 message_create = assert_raise(OSM::APIBadXMLError) do
126 Way.from_xml(no_v, true)
128 assert_match /tag is missing value/, message_create.message
129 message_update = assert_raise(OSM::APIBadXMLError) do
130 Way.from_xml(no_v, false)
132 assert_match /tag is missing value/, message_update.message
135 def test_from_xml_duplicate_k
136 dupk = "<osm><way id='23' changeset='23' version='23'><tag k='dup' v='test' /><tag k='dup' v='tester' /></way></osm>"
137 message_create = assert_raise(OSM::APIDuplicateTagsError) do
138 Way.from_xml(dupk, true)
140 assert_equal "Element way/ has duplicate tags with key dup", message_create.message
141 message_update = assert_raise(OSM::APIDuplicateTagsError) do
142 Way.from_xml(dupk, false)
144 assert_equal "Element way/23 has duplicate tags with key dup", message_update.message
149 node1 = create(:way_node, :way => way, :sequence_id => 1).node
150 node2 = create(:way_node, :way => way, :sequence_id => 2).node
151 node3 = create(:way_node, :way => way, :sequence_id => 3).node
153 nodes = Way.find(way.id).way_nodes
154 assert_equal 3, nodes.count
155 assert_equal node1.id, nodes[0].node_id
156 assert_equal node2.id, nodes[1].node_id
157 assert_equal node3.id, nodes[2].node_id
162 node1 = create(:way_node, :way => way, :sequence_id => 1).node
163 node2 = create(:way_node, :way => way, :sequence_id => 2).node
164 node3 = create(:way_node, :way => way, :sequence_id => 3).node
166 nodes = Way.find(way.id).nodes
167 assert_equal 3, nodes.count
168 assert_equal node1.id, nodes[0].id
169 assert_equal node2.id, nodes[1].id
170 assert_equal node3.id, nodes[2].id
175 node1 = create(:way_node, :way => way, :sequence_id => 1).node
176 node2 = create(:way_node, :way => way, :sequence_id => 2).node
177 node3 = create(:way_node, :way => way, :sequence_id => 3).node
179 nodes = Way.find(way.id).nds
180 assert_equal 3, nodes.count
181 assert_equal node1.id, nodes[0]
182 assert_equal node2.id, nodes[1]
183 assert_equal node3.id, nodes[2]
188 taglist = create_list(:way_tag, 2, :way => way)
189 tags = Way.find(way.id).way_tags.order(:k)
190 assert_equal taglist.count, tags.count
191 taglist.sort_by!(&:k).each_index do |i|
192 assert_equal taglist[i].k, tags[i].k
193 assert_equal taglist[i].v, tags[i].v
199 taglist = create_list(:way_tag, 2, :way => way)
200 tags = Way.find(way.id).tags
201 assert_equal taglist.count, tags.count
202 taglist.each do |tag|
203 assert_equal tag.v, tags[tag.k]
207 def test_containing_relation_members
209 relation = create(:relation)
210 create(:relation_member, :relation => relation, :member => way)
212 crm = Way.find(way.id).containing_relation_members.order(:relation_id)
213 # assert_equal 1, crm.size
214 assert_equal relation.id, crm.first.relation_id
215 assert_equal "Way", crm.first.member_type
216 assert_equal way.id, crm.first.member_id
217 assert_equal relation.id, crm.first.relation.id
220 def test_containing_relations
222 relation = create(:relation)
223 create(:relation_member, :relation => relation, :member => way)
225 cr = Way.find(way.id).containing_relations.order(:id)
226 assert_equal 1, cr.size
227 assert_equal relation.id, cr.first.id