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