3 class RelationTest < ActiveSupport::TestCase
4 def test_from_xml_no_id
5 noid = "<osm><relation version='12' changeset='23' /></osm>"
6 assert_nothing_raised do
7 Relation.from_xml(noid, :create => true)
9 message = assert_raise(OSM::APIBadXMLError) do
10 Relation.from_xml(noid, :create => false)
12 assert_match(/ID is required when updating/, message.message)
15 def test_from_xml_no_changeset_id
16 nocs = "<osm><relation id='123' version='12' /></osm>"
17 message_create = assert_raise(OSM::APIBadXMLError) do
18 Relation.from_xml(nocs, :create => true)
20 assert_match(/Changeset id is missing/, message_create.message)
21 message_update = assert_raise(OSM::APIBadXMLError) do
22 Relation.from_xml(nocs, :create => false)
24 assert_match(/Changeset id is missing/, message_update.message)
27 def test_from_xml_no_version
28 no_version = "<osm><relation id='123' changeset='23' /></osm>"
29 assert_nothing_raised do
30 Relation.from_xml(no_version, :create => true)
32 message_update = assert_raise(OSM::APIBadXMLError) do
33 Relation.from_xml(no_version, :create => false)
35 assert_match(/Version is required when updating/, message_update.message)
38 def test_from_xml_id_zero
39 id_list = ["", "0", "00", "0.0", "a"]
41 zero_id = "<osm><relation id='#{id}' changeset='332' version='23' /></osm>"
42 assert_nothing_raised do
43 Relation.from_xml(zero_id, :create => true)
45 message_update = assert_raise(OSM::APIBadUserInput) do
46 Relation.from_xml(zero_id, :create => false)
48 assert_match(/ID of relation cannot be zero when updating/, message_update.message)
52 def test_from_xml_no_text
54 message_create = assert_raise(OSM::APIBadXMLError) do
55 Relation.from_xml(no_text, :create => true)
57 assert_match(/Must specify a string with one or more characters/, message_create.message)
58 message_update = assert_raise(OSM::APIBadXMLError) do
59 Relation.from_xml(no_text, :create => false)
61 assert_match(/Must specify a string with one or more characters/, message_update.message)
64 def test_from_xml_no_k_v
65 nokv = "<osm><relation id='23' changeset='23' version='23'><tag /></relation></osm>"
66 message_create = assert_raise(OSM::APIBadXMLError) do
67 Relation.from_xml(nokv, :create => true)
69 assert_match(/tag is missing key/, message_create.message)
70 message_update = assert_raise(OSM::APIBadXMLError) do
71 Relation.from_xml(nokv, :create => false)
73 assert_match(/tag is missing key/, message_update.message)
76 def test_from_xml_no_v
77 no_v = "<osm><relation id='23' changeset='23' version='23'><tag k='key' /></relation></osm>"
78 message_create = assert_raise(OSM::APIBadXMLError) do
79 Relation.from_xml(no_v, :create => true)
81 assert_match(/tag is missing value/, message_create.message)
82 message_update = assert_raise(OSM::APIBadXMLError) do
83 Relation.from_xml(no_v, :create => false)
85 assert_match(/tag is missing value/, message_update.message)
88 def test_from_xml_duplicate_k
89 dupk = "<osm><relation id='23' changeset='23' version='23'><tag k='dup' v='test'/><tag k='dup' v='tester'/></relation></osm>"
90 message_create = assert_raise(OSM::APIDuplicateTagsError) do
91 Relation.from_xml(dupk, :create => true)
93 assert_equal "Element relation/ has duplicate tags with key dup", message_create.message
94 message_update = assert_raise(OSM::APIDuplicateTagsError) do
95 Relation.from_xml(dupk, :create => false)
97 assert_equal "Element relation/23 has duplicate tags with key dup", message_update.message
100 def test_relation_members
101 relation = create(:relation)
104 other_relation = create(:relation)
105 create(:relation_member, :relation => relation, :member => node, :member_role => "some node")
106 create(:relation_member, :relation => relation, :member => way, :member_role => "some way")
107 create(:relation_member, :relation => relation, :member => other_relation, :member_role => "some relation")
109 members = Relation.find(relation.id).relation_members
110 assert_equal 3, members.count
111 assert_equal "some node", members[0].member_role
112 assert_equal "Node", members[0].member_type
113 assert_equal node.id, members[0].member_id
114 assert_equal "some way", members[1].member_role
115 assert_equal "Way", members[1].member_type
116 assert_equal way.id, members[1].member_id
117 assert_equal "some relation", members[2].member_role
118 assert_equal "Relation", members[2].member_type
119 assert_equal other_relation.id, members[2].member_id
123 relation = create(:relation)
126 other_relation = create(:relation)
127 create(:relation_member, :relation => relation, :member => node, :member_role => "some node")
128 create(:relation_member, :relation => relation, :member => way, :member_role => "some way")
129 create(:relation_member, :relation => relation, :member => other_relation, :member_role => "some relation")
131 members = Relation.find(relation.id).members
132 assert_equal 3, members.count
133 assert_equal ["Node", node.id, "some node"], members[0]
134 assert_equal ["Way", way.id, "some way"], members[1]
135 assert_equal ["Relation", other_relation.id, "some relation"], members[2]
138 def test_relation_tags
139 relation = create(:relation)
140 taglist = create_list(:relation_tag, 2, :relation => relation)
142 tags = Relation.find(relation.id).relation_tags.order(:k)
143 assert_equal taglist.count, tags.count
144 taglist.sort_by!(&:k).each_index do |i|
145 assert_equal taglist[i].k, tags[i].k
146 assert_equal taglist[i].v, tags[i].v
151 relation = create(:relation)
152 taglist = create_list(:relation_tag, 2, :relation => relation)
154 tags = Relation.find(relation.id).tags
155 assert_equal taglist.count, tags.count
156 taglist.each do |tag|
157 assert_equal tag.v, tags[tag.k]
161 def test_containing_relation_members
162 relation = create(:relation)
163 super_relation = create(:relation)
164 create(:relation_member, :relation => super_relation, :member => relation)
166 crm = Relation.find(relation.id).containing_relation_members.order(:relation_id)
167 # assert_equal 1, crm.size
168 assert_equal super_relation.id, crm.first.relation_id
169 assert_equal "Relation", crm.first.member_type
170 assert_equal relation.id, crm.first.member_id
171 assert_equal super_relation.id, crm.first.relation.id
174 def test_containing_relations
175 relation = create(:relation)
176 super_relation = create(:relation)
177 create(:relation_member, :relation => super_relation, :member => relation)
179 cr = Relation.find(relation.id).containing_relations.order(:id)
180 assert_equal 1, cr.size
181 assert_equal super_relation.id, cr.first.id
184 def test_update_changeset_bbox_any_relation
185 relation = create(:relation)
186 super_relation = create(:relation)
187 node = create(:node, :longitude => 116, :latitude => 39)
188 create(:relation_member, :relation => super_relation, :member_type => "Relation", :member_id => relation.id)
189 node_member = create(:relation_member, :relation => super_relation, :member_type => "Node", :member_id => node.id)
191 changeset = create(:changeset, :user => user)
192 assert_nil changeset.min_lon
193 assert_nil changeset.max_lon
194 assert_nil changeset.max_lat
195 assert_nil changeset.min_lat
196 new_relation = build(:relation, :id => super_relation.id,
197 :version => super_relation.version,
198 :changeset => changeset)
199 new_relation.add_member node_member.member_type, node_member.member_id, node_member.member_role
200 # one member(relation type) was removed, so any_relation flag is expected to be true.
201 super_relation.update_from(new_relation, user)
203 # changeset updated by node member, representing any_relation flag true.
204 assert_equal 116, changeset.min_lon
205 assert_equal 116, changeset.max_lon
206 assert_equal 39, changeset.min_lat
207 assert_equal 39, changeset.max_lat
210 def test_changeset_bbox_delete_relation
211 orig_relation = create(:relation)
212 node1 = create(:node, :longitude => 116, :latitude => 39)
213 node2 = create(:node, :longitude => 39, :latitude => 116)
214 create(:relation_member, :relation => orig_relation, :member_type => "Node", :member_id => node1.id)
215 create(:relation_member, :relation => orig_relation, :member_type => "Node", :member_id => node2.id)
217 changeset = create(:changeset, :user => user)
218 assert_nil changeset.min_lon
219 assert_nil changeset.max_lon
220 assert_nil changeset.max_lat
221 assert_nil changeset.min_lat
223 new_relation = build(:relation, :id => orig_relation.id,
224 :version => orig_relation.version,
225 :changeset_id => changeset.id)
226 orig_relation.delete_with_history!(new_relation, user)
228 assert_equal 39, changeset.min_lon
229 assert_equal 116, changeset.max_lon
230 assert_equal 39, changeset.min_lat
231 assert_equal 116, changeset.max_lat
234 # Check that the preconditions fail when you are over the defined limit of
235 # the maximum number of members in a relation.
236 def test_max_members_per_relation_limit
237 # Speed up unit test by using a small relation member limit
238 default_limit = Settings.max_number_of_relation_members
239 Settings.max_number_of_relation_members = 20
242 changeset = create(:changeset, :user => user)
243 relation = create(:relation, :changeset => changeset)
244 node = create(:node, :longitude => 116, :latitude => 39)
245 # Create relation which exceeds the relation member limit by one
246 0.upto(Settings.max_number_of_relation_members) do |i|
247 create(:relation_member, :relation => relation, :member_type => "Node", :member_id => node.id, :sequence_id => i)
250 assert_raise OSM::APITooManyRelationMembersError do
251 relation.create_with_history user
254 Settings.max_number_of_relation_members = default_limit