3 class RelationTest < ActiveSupport::TestCase
6 def test_relation_count
7 assert_equal 8, Relation.count
10 def test_from_xml_no_id
11 noid = "<osm><relation version='12' changeset='23' /></osm>"
12 assert_nothing_raised(OSM::APIBadXMLError) do
13 Relation.from_xml(noid, true)
15 message = assert_raise(OSM::APIBadXMLError) do
16 Relation.from_xml(noid, false)
18 assert_match /ID is required when updating/, message.message
21 def test_from_xml_no_changeset_id
22 nocs = "<osm><relation id='123' version='12' /></osm>"
23 message_create = assert_raise(OSM::APIBadXMLError) do
24 Relation.from_xml(nocs, true)
26 assert_match /Changeset id is missing/, message_create.message
27 message_update = assert_raise(OSM::APIBadXMLError) do
28 Relation.from_xml(nocs, false)
30 assert_match /Changeset id is missing/, message_update.message
33 def test_from_xml_no_version
34 no_version = "<osm><relation id='123' changeset='23' /></osm>"
35 assert_nothing_raised(OSM::APIBadXMLError) do
36 Relation.from_xml(no_version, true)
38 message_update = assert_raise(OSM::APIBadXMLError) do
39 Relation.from_xml(no_version, false)
41 assert_match /Version is required when updating/, message_update.message
44 def test_from_xml_id_zero
45 id_list = ["", "0", "00", "0.0", "a"]
47 zero_id = "<osm><relation id='#{id}' changeset='332' version='23' /></osm>"
48 assert_nothing_raised(OSM::APIBadUserInput) do
49 Relation.from_xml(zero_id, true)
51 message_update = assert_raise(OSM::APIBadUserInput) do
52 Relation.from_xml(zero_id, false)
54 assert_match /ID of relation cannot be zero when updating/, message_update.message
58 def test_from_xml_no_text
60 message_create = assert_raise(OSM::APIBadXMLError) do
61 Relation.from_xml(no_text, true)
63 assert_match /Must specify a string with one or more characters/, message_create.message
64 message_update = assert_raise(OSM::APIBadXMLError) do
65 Relation.from_xml(no_text, false)
67 assert_match /Must specify a string with one or more characters/, message_update.message
70 def test_from_xml_no_k_v
71 nokv = "<osm><relation id='23' changeset='23' version='23'><tag /></relation></osm>"
72 message_create = assert_raise(OSM::APIBadXMLError) do
73 Relation.from_xml(nokv, true)
75 assert_match /tag is missing key/, message_create.message
76 message_update = assert_raise(OSM::APIBadXMLError) do
77 Relation.from_xml(nokv, false)
79 assert_match /tag is missing key/, message_update.message
82 def test_from_xml_no_v
83 no_v = "<osm><relation id='23' changeset='23' version='23'><tag k='key' /></relation></osm>"
84 message_create = assert_raise(OSM::APIBadXMLError) do
85 Relation.from_xml(no_v, true)
87 assert_match /tag is missing value/, message_create.message
88 message_update = assert_raise(OSM::APIBadXMLError) do
89 Relation.from_xml(no_v, false)
91 assert_match /tag is missing value/, message_update.message
94 def test_from_xml_duplicate_k
95 dupk = "<osm><relation id='23' changeset='23' version='23'><tag k='dup' v='test'/><tag k='dup' v='tester'/></relation></osm>"
96 message_create = assert_raise(OSM::APIDuplicateTagsError) do
97 Relation.from_xml(dupk, true)
99 assert_equal "Element relation/ has duplicate tags with key dup", message_create.message
100 message_update = assert_raise(OSM::APIDuplicateTagsError) do
101 Relation.from_xml(dupk, false)
103 assert_equal "Element relation/23 has duplicate tags with key dup", message_update.message
106 def test_relation_members
107 relation = create(:relation)
110 other_relation = create(:relation)
111 create(:relation_member, :relation => relation, :member => node, :member_role => "some node")
112 create(:relation_member, :relation => relation, :member => way, :member_role => "some way")
113 create(:relation_member, :relation => relation, :member => other_relation, :member_role => "some relation")
115 members = Relation.find(relation.id).relation_members
116 assert_equal 3, members.count
117 assert_equal "some node", members[0].member_role
118 assert_equal "Node", members[0].member_type
119 assert_equal node.id, members[0].member_id
120 assert_equal "some way", members[1].member_role
121 assert_equal "Way", members[1].member_type
122 assert_equal way.id, members[1].member_id
123 assert_equal "some relation", members[2].member_role
124 assert_equal "Relation", members[2].member_type
125 assert_equal other_relation.id, members[2].member_id
129 relation = create(:relation)
132 other_relation = create(:relation)
133 create(:relation_member, :relation => relation, :member => node, :member_role => "some node")
134 create(:relation_member, :relation => relation, :member => way, :member_role => "some way")
135 create(:relation_member, :relation => relation, :member => other_relation, :member_role => "some relation")
137 members = Relation.find(relation.id).members
138 assert_equal 3, members.count
139 assert_equal ["Node", node.id, "some node"], members[0]
140 assert_equal ["Way", way.id, "some way"], members[1]
141 assert_equal ["Relation", other_relation.id, "some relation"], members[2]
144 def test_relation_tags
145 relation = create(:relation)
146 taglist = create_list(:relation_tag, 2, :relation => relation)
148 tags = Relation.find(relation.id).relation_tags.order(:k)
149 assert_equal taglist.count, tags.count
150 taglist.sort_by!(&:k).each_index do |i|
151 assert_equal taglist[i].k, tags[i].k
152 assert_equal taglist[i].v, tags[i].v
157 relation = create(:relation)
158 taglist = create_list(:relation_tag, 2, :relation => relation)
160 tags = Relation.find(relation.id).tags
161 assert_equal taglist.count, tags.count
162 taglist.each do |tag|
163 assert_equal tag.v, tags[tag.k]
167 def test_containing_relation_members
168 relation = create(:relation)
169 super_relation = create(:relation)
170 create(:relation_member, :relation => super_relation, :member => relation)
172 crm = Relation.find(relation.id).containing_relation_members.order(:relation_id)
173 # assert_equal 1, crm.size
174 assert_equal super_relation.id, crm.first.relation_id
175 assert_equal "Relation", crm.first.member_type
176 assert_equal relation.id, crm.first.member_id
177 assert_equal super_relation.id, crm.first.relation.id
180 def test_containing_relations
181 relation = create(:relation)
182 super_relation = create(:relation)
183 create(:relation_member, :relation => super_relation, :member => relation)
185 cr = Relation.find(relation.id).containing_relations.order(:id)
186 assert_equal 1, cr.size
187 assert_equal super_relation.id, cr.first.id