]> git.openstreetmap.org Git - rails.git/blob - test/models/way_test.rb
Refactor some changesets controller tests to use factories.
[rails.git] / test / models / way_test.rb
1 require "test_helper"
2
3 class WayTest < ActiveSupport::TestCase
4   api_fixtures
5
6   # Check that we have the correct number of currnet ways in the db
7   # This will need to updated whenever the current_ways.yml is updated
8   def test_db_count
9     assert_equal 7, Way.count
10   end
11
12   def test_bbox
13     node = create(:node)
14     visible_way = create(:way)
15     create(:way_node, :way => visible_way, :node => node)
16     invisible_way = create(:way, :deleted)
17     create(:way_node, :way => invisible_way, :node => node)
18     used_way = create(:way)
19     create(:way_node, :way => used_way, :node => node)
20     create(:relation_member, :member => used_way)
21
22     [visible_way, invisible_way, used_way].each do |way|
23       assert_equal node.bbox.min_lon, way.bbox.min_lon, "min_lon"
24       assert_equal node.bbox.min_lat, way.bbox.min_lat, "min_lat"
25       assert_equal node.bbox.max_lon, way.bbox.max_lon, "max_lon"
26       assert_equal node.bbox.max_lat, way.bbox.max_lat, "max_lat"
27     end
28   end
29
30   # Check that the preconditions fail when you are over the defined limit of
31   # the maximum number of nodes in each way.
32   def test_max_nodes_per_way_limit
33     node_a = create(:node)
34     node_b = create(:node)
35     node_c = create(:node)
36     way = create(:way_with_nodes, :nodes_count => 1)
37     # Take one of the current ways and add nodes to it until we are near the limit
38     way = Way.find(current_ways(:visible_way).id)
39     assert way.valid?
40     # it already has 1 node
41     1.upto(MAX_NUMBER_OF_WAY_NODES / 2) do
42       way.add_nd_num(node_a.id)
43       way.add_nd_num(node_b.id)
44     end
45     way.save
46     # print way.nds.size
47     assert way.valid?
48     way.add_nd_num(node_c.id)
49     assert way.valid?
50   end
51
52   def test_from_xml_no_id
53     noid = "<osm><way version='12' changeset='23' /></osm>"
54     assert_nothing_raised(OSM::APIBadXMLError) do
55       Way.from_xml(noid, true)
56     end
57     message = assert_raise(OSM::APIBadXMLError) do
58       Way.from_xml(noid, false)
59     end
60     assert_match /ID is required when updating/, message.message
61   end
62
63   def test_from_xml_no_changeset_id
64     nocs = "<osm><way id='123' version='23' /></osm>"
65     message_create = assert_raise(OSM::APIBadXMLError) do
66       Way.from_xml(nocs, true)
67     end
68     assert_match /Changeset id is missing/, message_create.message
69     message_update = assert_raise(OSM::APIBadXMLError) do
70       Way.from_xml(nocs, false)
71     end
72     assert_match /Changeset id is missing/, message_update.message
73   end
74
75   def test_from_xml_no_version
76     no_version = "<osm><way id='123' changeset='23' /></osm>"
77     assert_nothing_raised(OSM::APIBadXMLError) do
78       Way.from_xml(no_version, true)
79     end
80     message_update = assert_raise(OSM::APIBadXMLError) do
81       Way.from_xml(no_version, false)
82     end
83     assert_match /Version is required when updating/, message_update.message
84   end
85
86   def test_from_xml_id_zero
87     id_list = ["", "0", "00", "0.0", "a"]
88     id_list.each do |id|
89       zero_id = "<osm><way id='#{id}' changeset='33' version='23' /></osm>"
90       assert_nothing_raised(OSM::APIBadUserInput) do
91         Way.from_xml(zero_id, true)
92       end
93       message_update = assert_raise(OSM::APIBadUserInput) do
94         Way.from_xml(zero_id, false)
95       end
96       assert_match /ID of way cannot be zero when updating/, message_update.message
97     end
98   end
99
100   def test_from_xml_no_text
101     no_text = ""
102     message_create = assert_raise(OSM::APIBadXMLError) do
103       Way.from_xml(no_text, true)
104     end
105     assert_match /Must specify a string with one or more characters/, message_create.message
106     message_update = assert_raise(OSM::APIBadXMLError) do
107       Way.from_xml(no_text, false)
108     end
109     assert_match /Must specify a string with one or more characters/, message_update.message
110   end
111
112   def test_from_xml_no_k_v
113     nokv = "<osm><way id='23' changeset='23' version='23'><tag /></way></osm>"
114     message_create = assert_raise(OSM::APIBadXMLError) do
115       Way.from_xml(nokv, true)
116     end
117     assert_match /tag is missing key/, message_create.message
118     message_update = assert_raise(OSM::APIBadXMLError) do
119       Way.from_xml(nokv, false)
120     end
121     assert_match /tag is missing key/, message_update.message
122   end
123
124   def test_from_xml_no_v
125     no_v = "<osm><way id='23' changeset='23' version='23'><tag k='key' /></way></osm>"
126     message_create = assert_raise(OSM::APIBadXMLError) do
127       Way.from_xml(no_v, true)
128     end
129     assert_match /tag is missing value/, message_create.message
130     message_update = assert_raise(OSM::APIBadXMLError) do
131       Way.from_xml(no_v, false)
132     end
133     assert_match /tag is missing value/, message_update.message
134   end
135
136   def test_from_xml_duplicate_k
137     dupk = "<osm><way id='23' changeset='23' version='23'><tag k='dup' v='test' /><tag k='dup' v='tester' /></way></osm>"
138     message_create = assert_raise(OSM::APIDuplicateTagsError) do
139       Way.from_xml(dupk, true)
140     end
141     assert_equal "Element way/ has duplicate tags with key dup", message_create.message
142     message_update = assert_raise(OSM::APIDuplicateTagsError) do
143       Way.from_xml(dupk, false)
144     end
145     assert_equal "Element way/23 has duplicate tags with key dup", message_update.message
146   end
147
148   def test_way_nodes
149     way = current_ways(:way_with_multiple_nodes)
150     nodes = Way.find(way.id).way_nodes
151     assert_equal 3, nodes.count
152     assert_equal 4, nodes[0].node_id
153     assert_equal 15, nodes[1].node_id
154     assert_equal 11, nodes[2].node_id
155   end
156
157   def test_nodes
158     way = current_ways(:way_with_multiple_nodes)
159     nodes = Way.find(way.id).nodes
160     assert_equal 3, nodes.count
161     assert_equal 4, nodes[0].id
162     assert_equal 15, nodes[1].id
163     assert_equal 11, nodes[2].id
164   end
165
166   def test_nds
167     way = current_ways(:way_with_multiple_nodes)
168     nodes = Way.find(way.id).nds
169     assert_equal 3, nodes.count
170     assert_equal 4, nodes[0]
171     assert_equal 15, nodes[1]
172     assert_equal 11, nodes[2]
173   end
174
175   def test_way_tags
176     way = current_ways(:way_with_versions)
177     taglist = create_list(:way_tag, 2, :way => way)
178     tags = Way.find(way.id).way_tags.order(:k)
179     assert_equal taglist.count, tags.count
180     taglist.sort_by!(&:k).each_index do |i|
181       assert_equal taglist[i].k, tags[i].k
182       assert_equal taglist[i].v, tags[i].v
183     end
184   end
185
186   def test_tags
187     way = current_ways(:way_with_versions)
188     taglist = create_list(:way_tag, 2, :way => way)
189     tags = Way.find(way.id).tags
190     assert_equal taglist.count, tags.count
191     taglist.each do |tag|
192       assert_equal tag.v, tags[tag.k]
193     end
194   end
195
196   def test_containing_relation_members
197     way = current_ways(:used_way)
198     crm = Way.find(way.id).containing_relation_members.order(:relation_id)
199     #    assert_equal 1, crm.size
200     assert_equal 1, crm.first.relation_id
201     assert_equal "Way", crm.first.member_type
202     assert_equal way.id, crm.first.member_id
203     assert_equal 1, crm.first.relation.id
204   end
205
206   def test_containing_relations
207     way = current_ways(:used_way)
208     cr = Way.find(way.id).containing_relations.order(:id)
209     assert_equal 1, cr.size
210     assert_equal 1, cr.first.id
211   end
212 end