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 = current_relations(:relation_with_versions)
108 members = Relation.find(relation.id).relation_members
109 assert_equal 3, members.count
110 assert_equal "some node", members[0].member_role
111 assert_equal "Node", members[0].member_type
112 assert_equal 15, members[0].member_id
113 assert_equal "some way", members[1].member_role
114 assert_equal "Way", members[1].member_type
115 assert_equal 4, members[1].member_id
116 assert_equal "some relation", members[2].member_role
117 assert_equal "Relation", members[2].member_type
118 assert_equal 7, members[2].member_id
122 relation = current_relations(:relation_with_versions)
123 members = Relation.find(relation.id).members
124 assert_equal 3, members.count
125 assert_equal ["Node", 15, "some node"], members[0]
126 assert_equal ["Way", 4, "some way"], members[1]
127 assert_equal ["Relation", 7, "some relation"], members[2]
130 def test_relation_tags
131 relation = current_relations(:relation_with_versions)
132 taglist = create_list(:relation_tag, 2, :relation => relation)
134 tags = Relation.find(relation.id).relation_tags.order(:k)
135 assert_equal 2, tags.count
136 taglist.sort_by!(&:k).each_index do |i|
137 assert_equal taglist[i].k, tags[i].k
138 assert_equal taglist[i].v, tags[i].v
143 relation = current_relations(:relation_with_versions)
144 taglist = create_list(:relation_tag, 2, :relation => relation)
146 tags = Relation.find(relation.id).tags
147 assert_equal 2, tags.size
148 taglist.each do |tag|
149 assert_equal tag.v, tags[tag.k]
153 def test_containing_relation_members
154 relation = current_relations(:used_relation)
155 crm = Relation.find(relation.id).containing_relation_members.order(:relation_id)
156 # assert_equal 1, crm.size
157 assert_equal 1, crm.first.relation_id
158 assert_equal "Relation", crm.first.member_type
159 assert_equal relation.id, crm.first.member_id
160 assert_equal 1, crm.first.relation.id
163 def test_containing_relations
164 relation = current_relations(:used_relation)
165 cr = Relation.find(relation.id).containing_relations.order(:id)
166 assert_equal 1, cr.size
167 assert_equal 1, cr.first.id