3 class WayTest < ActiveSupport::TestCase
6 visible_way = create(:way)
7 create(:way_node, :way => visible_way, :node => node)
8 invisible_way = create(:way, :deleted)
9 create(:way_node, :way => invisible_way, :node => node)
10 used_way = create(:way)
11 create(:way_node, :way => used_way, :node => node)
12 create(:relation_member, :member => used_way)
14 [visible_way, invisible_way, used_way].each do |way|
15 assert_equal node.bbox.min_lon, way.bbox.min_lon, "min_lon"
16 assert_equal node.bbox.min_lat, way.bbox.min_lat, "min_lat"
17 assert_equal node.bbox.max_lon, way.bbox.max_lon, "max_lon"
18 assert_equal node.bbox.max_lat, way.bbox.max_lat, "max_lat"
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 node_a = create(:node)
26 node_b = create(:node)
27 node_c = create(:node)
28 way = create(:way_with_nodes, :nodes_count => 1)
29 # Take one of the current ways and add nodes to it until we are near the limit
30 assert_predicate way, :valid?
31 # it already has 1 node
32 1.upto(Settings.max_number_of_way_nodes / 2) do
33 way.add_nd_num(node_a.id)
34 way.add_nd_num(node_b.id)
37 assert_predicate way, :valid?
38 way.add_nd_num(node_c.id)
39 assert_predicate way, :valid?
42 def test_from_xml_no_id
43 noid = "<osm><way version='12' changeset='23' /></osm>"
44 assert_nothing_raised do
45 Way.from_xml(noid, :create => true)
47 message = assert_raise(OSM::APIBadXMLError) do
48 Way.from_xml(noid, :create => false)
50 assert_match(/ID is required when updating/, message.message)
53 def test_from_xml_no_changeset_id
54 nocs = "<osm><way id='123' version='23' /></osm>"
55 message_create = assert_raise(OSM::APIBadXMLError) do
56 Way.from_xml(nocs, :create => true)
58 assert_match(/Changeset id is missing/, message_create.message)
59 message_update = assert_raise(OSM::APIBadXMLError) do
60 Way.from_xml(nocs, :create => false)
62 assert_match(/Changeset id is missing/, message_update.message)
65 def test_from_xml_no_version
66 no_version = "<osm><way id='123' changeset='23' /></osm>"
67 assert_nothing_raised do
68 Way.from_xml(no_version, :create => true)
70 message_update = assert_raise(OSM::APIBadXMLError) do
71 Way.from_xml(no_version, :create => false)
73 assert_match(/Version is required when updating/, message_update.message)
76 def test_from_xml_id_zero
77 id_list = ["", "0", "00", "0.0", "a"]
79 zero_id = "<osm><way id='#{id}' changeset='33' version='23' /></osm>"
80 assert_nothing_raised do
81 Way.from_xml(zero_id, :create => true)
83 message_update = assert_raise(OSM::APIBadUserInput) do
84 Way.from_xml(zero_id, :create => false)
86 assert_match(/ID of way cannot be zero when updating/, message_update.message)
90 def test_from_xml_no_text
92 message_create = assert_raise(OSM::APIBadXMLError) do
93 Way.from_xml(no_text, :create => true)
95 assert_match(/Must specify a string with one or more characters/, message_create.message)
96 message_update = assert_raise(OSM::APIBadXMLError) do
97 Way.from_xml(no_text, :create => false)
99 assert_match(/Must specify a string with one or more characters/, message_update.message)
102 def test_from_xml_no_k_v
103 nokv = "<osm><way id='23' changeset='23' version='23'><tag /></way></osm>"
104 message_create = assert_raise(OSM::APIBadXMLError) do
105 Way.from_xml(nokv, :create => true)
107 assert_match(/tag is missing key/, message_create.message)
108 message_update = assert_raise(OSM::APIBadXMLError) do
109 Way.from_xml(nokv, :create => false)
111 assert_match(/tag is missing key/, message_update.message)
114 def test_from_xml_no_v
115 no_v = "<osm><way id='23' changeset='23' version='23'><tag k='key' /></way></osm>"
116 message_create = assert_raise(OSM::APIBadXMLError) do
117 Way.from_xml(no_v, :create => true)
119 assert_match(/tag is missing value/, message_create.message)
120 message_update = assert_raise(OSM::APIBadXMLError) do
121 Way.from_xml(no_v, :create => false)
123 assert_match(/tag is missing value/, message_update.message)
126 def test_from_xml_duplicate_k
127 dupk = "<osm><way id='23' changeset='23' version='23'><tag k='dup' v='test' /><tag k='dup' v='tester' /></way></osm>"
128 message_create = assert_raise(OSM::APIDuplicateTagsError) do
129 Way.from_xml(dupk, :create => true)
131 assert_equal "Element way/ has duplicate tags with key dup", message_create.message
132 message_update = assert_raise(OSM::APIDuplicateTagsError) do
133 Way.from_xml(dupk, :create => false)
135 assert_equal "Element way/23 has duplicate tags with key dup", message_update.message
140 node1 = create(:way_node, :way => way, :sequence_id => 1).node
141 node2 = create(:way_node, :way => way, :sequence_id => 2).node
142 node3 = create(:way_node, :way => way, :sequence_id => 3).node
144 nodes = Way.find(way.id).way_nodes
145 assert_equal 3, nodes.count
146 assert_equal node1.id, nodes[0].node_id
147 assert_equal node2.id, nodes[1].node_id
148 assert_equal node3.id, nodes[2].node_id
153 node1 = create(:way_node, :way => way, :sequence_id => 1).node
154 node2 = create(:way_node, :way => way, :sequence_id => 2).node
155 node3 = create(:way_node, :way => way, :sequence_id => 3).node
157 nodes = Way.find(way.id).nodes
158 assert_equal 3, nodes.count
159 assert_equal node1.id, nodes[0].id
160 assert_equal node2.id, nodes[1].id
161 assert_equal node3.id, nodes[2].id
166 node1 = create(:way_node, :way => way, :sequence_id => 1).node
167 node2 = create(:way_node, :way => way, :sequence_id => 2).node
168 node3 = create(:way_node, :way => way, :sequence_id => 3).node
170 nodes = Way.find(way.id).nds
171 assert_equal 3, nodes.count
172 assert_equal node1.id, nodes[0]
173 assert_equal node2.id, nodes[1]
174 assert_equal node3.id, nodes[2]
179 taglist = create_list(:way_tag, 2, :way => way)
180 tags = Way.find(way.id).way_tags.order(:k)
181 assert_equal taglist.count, tags.count
182 taglist.sort_by!(&:k).each_index do |i|
183 assert_equal taglist[i].k, tags[i].k
184 assert_equal taglist[i].v, tags[i].v
190 taglist = create_list(:way_tag, 2, :way => way)
191 tags = Way.find(way.id).tags
192 assert_equal taglist.count, tags.count
193 taglist.each do |tag|
194 assert_equal tag.v, tags[tag.k]
198 def test_containing_relation_members
200 relation = create(:relation)
201 create(:relation_member, :relation => relation, :member => way)
203 crm = Way.find(way.id).containing_relation_members.order(:relation_id)
204 # assert_equal 1, crm.size
205 assert_equal relation.id, crm.first.relation_id
206 assert_equal "Way", crm.first.member_type
207 assert_equal way.id, crm.first.member_id
208 assert_equal relation.id, crm.first.relation.id
211 def test_containing_relations
213 relation = create(:relation)
214 create(:relation_member, :relation => relation, :member => way)
216 cr = Way.find(way.id).containing_relations.order(:id)
217 assert_equal 1, cr.size
218 assert_equal relation.id, cr.first.id