class WayControllerTest < ActionController::TestCase
api_fixtures
+ ##
+ # test all routes which lead to this controller
+ def test_routes
+ assert_routing(
+ { :path => "/api/0.6/way/create", :method => :put },
+ { :controller => "way", :action => "create" }
+ )
+ assert_routing(
+ { :path => "/api/0.6/way/1/full", :method => :get },
+ { :controller => "way", :action => "full", :id => "1" }
+ )
+ assert_routing(
+ { :path => "/api/0.6/way/1", :method => :get },
+ { :controller => "way", :action => "read", :id => "1" }
+ )
+ assert_routing(
+ { :path => "/api/0.6/way/1", :method => :put },
+ { :controller => "way", :action => "update", :id => "1" }
+ )
+ assert_routing(
+ { :path => "/api/0.6/way/1", :method => :delete },
+ { :controller => "way", :action => "delete", :id => "1" }
+ )
+ assert_routing(
+ { :path => "/api/0.6/ways", :method => :get },
+ { :controller => "way", :action => "ways" }
+ )
+ end
+
# -------------------------------------
# Test reading ways.
# -------------------------------------
##
# check the "full" mode
def test_full
- Way.find(:all).each do |way|
+ Way.all.each do |way|
get :full, :id => way.id
# full call should say "gone" for non-visible ways...
assert_select "osm way[id=#{way.id}][version=#{way.version}][visible=#{way.visible}]", 1
# check that each node in the way appears once in the output as a
- # reference and as the node element. note the slightly dodgy assumption
- # that nodes appear only once. this is currently the case with the
- # fixtures, but it doesn't have to be.
+ # reference and as the node element.
way.nodes.each do |n|
- assert_select "osm way nd[ref=#{n.id}]", 1
+ count = (way.nodes - (way.nodes - [n])).length
+ assert_select "osm way nd[ref=#{n.id}]", count
assert_select "osm node[id=#{n.id}][version=#{n.version}][lat=#{n.lat}][lon=#{n.lon}]", 1
end
end
end
+ ##
+ # test fetching multiple ways
+ def test_ways
+ # check error when no parameter provided
+ get :ways
+ assert_response :bad_request
+
+ # check error when no parameter value provided
+ get :ways, :ways => ""
+ assert_response :bad_request
+
+ # test a working call
+ get :ways, :ways => "1,2,4,6"
+ assert_response :success
+ assert_select "osm" do
+ assert_select "way", :count => 4
+ assert_select "way[id=1][visible=true]", :count => 1
+ assert_select "way[id=2][visible=false]", :count => 1
+ assert_select "way[id=4][visible=true]", :count => 1
+ assert_select "way[id=6][visible=true]", :count => 1
+ end
+
+ # check error when a non-existent way is included
+ get :ways, :ways => "1,2,4,6,400"
+ assert_response :not_found
+ end
+
# -------------------------------------
# Test simple way creation.
# -------------------------------------
# expect failure
assert_response :precondition_failed,
"way upload with invalid node did not return 'precondition failed'"
+ assert_equal "Precondition failed: Way requires the nodes with id in (0), which either do not exist, or are not visible.", @response.body
# create a way with no nodes
content "<osm><way changeset='#{open_changeset_id}'>" +
# expect failure
assert_response :precondition_failed,
"way upload with no node did not return 'precondition failed'"
+ assert_equal "Precondition failed: Cannot create way: data is invalid.", @response.body
# create a way inside a closed changeset
content "<osm><way changeset='#{closed_changeset_id}'>" +
# expect failure
assert_response :conflict,
"way upload to closed changeset did not return 'conflict'"
+
+ # create a way with a tag which is too long
+ content "<osm><way changeset='#{open_changeset_id}'>" +
+ "<nd ref='#{nid1}'/>" +
+ "<tag k='foo' v='#{'x'*256}'/>" +
+ "</way></osm>"
+ put :create
+ # expect failure
+ assert_response :bad_request,
+ "way upload to with too long tag did not return 'bad_request'"
end
# -------------------------------------
assert_response :forbidden
# Now try without having a changeset
- content "<osm><way id='#{current_ways(:visible_way).id}'></osm>"
+ content "<osm><way id='#{current_ways(:visible_way).id}'/></osm>"
delete :delete, :id => current_ways(:visible_way).id
assert_response :forbidden
assert_response :bad_request
# Now try without having a changeset
- content "<osm><way id='#{current_ways(:visible_way).id}'></osm>"
+ content "<osm><way id='#{current_ways(:visible_way).id}'/></osm>"
delete :delete, :id => current_ways(:visible_way).id
assert_response :bad_request
delete :delete, :id => current_ways(:used_way).id
assert_response :precondition_failed,
"shouldn't be able to delete a way used in a relation (#{@response.body})"
+ assert_equal "Precondition failed: Way 3 is still used by relations 1.", @response.body
# this won't work since the way never existed
delete :delete, :id => 0
put :update, :id => current_ways(:visible_way).id
assert_response :bad_request,
"adding a duplicate tag to a way should fail with 'bad request'"
- assert_equal "Element way/#{current_ways(:visible_way).id} has duplicate tags with key #{current_way_tags(:t1).k}.", @response.body
+ assert_equal "Element way/#{current_ways(:visible_way).id} has duplicate tags with key #{current_way_tags(:t1).k}", @response.body
end
##
put :update, :id => current_ways(:visible_way).id
assert_response :bad_request,
"adding new duplicate tags to a way should fail with 'bad request'"
- assert_equal "Element way/#{current_ways(:visible_way).id} has duplicate tags with key i_am_a_duplicate.", @response.body
+ assert_equal "Element way/#{current_ways(:visible_way).id} has duplicate tags with key i_am_a_duplicate", @response.body
end
put :create
assert_response :bad_request,
"adding new duplicate tags to a way should fail with 'bad request'"
- assert_equal "Element way/ has duplicate tags with key addr:housenumber.", @response.body
+ assert_equal "Element way/ has duplicate tags with key addr:housenumber", @response.body
end
##
current_ways(:used_way).id
]
found_way_ids = ways_xml.find("//osm/way").collect { |w| w["id"].to_i }
- assert_equal expected_way_ids, found_way_ids,
+ assert_equal expected_way_ids.sort, found_way_ids.sort,
"expected ways for node #{current_nodes(:used_node_1).id} did not match found"
# check the full ways to ensure we're not missing anything