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, true)
9 message = assert_raise(OSM::APIBadXMLError) do
10 Relation.from_xml(noid, 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, true)
20 assert_match(/Changeset id is missing/, message_create.message)
21 message_update = assert_raise(OSM::APIBadXMLError) do
22 Relation.from_xml(nocs, 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, true)
32 message_update = assert_raise(OSM::APIBadXMLError) do
33 Relation.from_xml(no_version, 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, true)
45 message_update = assert_raise(OSM::APIBadUserInput) do
46 Relation.from_xml(zero_id, 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, 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, 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, true)
69 assert_match(/tag is missing key/, message_create.message)
70 message_update = assert_raise(OSM::APIBadXMLError) do
71 Relation.from_xml(nokv, 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, 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, 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, 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, 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 = Relation.new
197 new_relation.id = super_relation.id
198 new_relation.version = super_relation.version
199 new_relation.changeset = changeset
200 new_relation.add_member node_member.member_type, node_member.member_id, node_member.member_role
201 # one member(relation type) was removed, so any_relation flag is expected to be true.
202 super_relation.update_from(new_relation, user)
204 # changeset updated by node member, representing any_relation flag true.
205 assert_equal 116, changeset.min_lon
206 assert_equal 116, changeset.max_lon
207 assert_equal 39, changeset.min_lat
208 assert_equal 39, changeset.max_lat
211 def test_changeset_bbox_delete_relation
212 orig_relation = create(:relation)
213 node1 = create(:node, :longitude => 116, :latitude => 39)
214 node2 = create(:node, :longitude => 39, :latitude => 116)
215 create(:relation_member, :relation => orig_relation, :member_type => "Node", :member_id => node1.id)
216 create(:relation_member, :relation => orig_relation, :member_type => "Node", :member_id => node2.id)
218 changeset = create(:changeset, :user => user)
219 assert_nil changeset.min_lon
220 assert_nil changeset.max_lon
221 assert_nil changeset.max_lat
222 assert_nil changeset.min_lat
224 new_relation = Relation.new
225 new_relation.id = orig_relation.id
226 new_relation.version = orig_relation.version
227 new_relation.changeset_id = changeset.id
228 orig_relation.delete_with_history!(new_relation, user)
230 assert_equal 39, changeset.min_lon
231 assert_equal 116, changeset.max_lon
232 assert_equal 39, changeset.min_lat
233 assert_equal 116, changeset.max_lat