From: Matt Amos Date: Tue, 21 Oct 2008 10:52:52 +0000 (+0000) Subject: Added tests for ways_for_node. Altered behaviour to not return deleted ways. Fixed... X-Git-Tag: live~7601^2~251 X-Git-Url: https://git.openstreetmap.org/rails.git/commitdiff_plain/6339473cb77e4e0998b959a0835a8f42767f608d?ds=sidebyside Added tests for ways_for_node. Altered behaviour to not return deleted ways. Fixed ways fixture where most recent history != current. --- diff --git a/app/controllers/way_controller.rb b/app/controllers/way_controller.rb index b00658cf0..25d535d79 100644 --- a/app/controllers/way_controller.rb +++ b/app/controllers/way_controller.rb @@ -125,13 +125,19 @@ class WayController < ApplicationController end end + ## + # returns all the ways which are currently using the node given in the + # :id parameter. note that this used to return deleted ways as well, but + # this seemed not to be the expected behaviour, so it was removed. def ways_for_node - wayids = WayNode.find(:all, :conditions => ['node_id = ?', params[:id]]).collect { |ws| ws.id[0] }.uniq + wayids = WayNode.find(:all, + :conditions => ['node_id = ?', params[:id]] + ).collect { |ws| ws.id[0] }.uniq doc = OSM::API.new.get_xml_doc Way.find(wayids).each do |way| - doc.root << way.to_xml_node + doc.root << way.to_xml_node if way.visible end render :text => doc.to_s, :content_type => "text/xml" diff --git a/app/models/way.rb b/app/models/way.rb index 591dee9a2..1304d8a18 100644 --- a/app/models/way.rb +++ b/app/models/way.rb @@ -51,6 +51,8 @@ class Way < ActiveRecord::Base if pt['timestamp'] way.timestamp = Time.parse(pt['timestamp']) end + # if visible isn't present then it defaults to true + way.visible = (pt['visible'] or true) end pt.find('tag').each do |tag| diff --git a/test/fixtures/ways.yml b/test/fixtures/ways.yml index c1ce7222b..4f0af9854 100644 --- a/test/fixtures/ways.yml +++ b/test/fixtures/ways.yml @@ -16,7 +16,7 @@ used_way: id: 3 changeset_id: 1 timestamp: 2007-01-01 00:00:00 - visible: 0 + visible: 1 version: 1 way_with_versions_v1: diff --git a/test/functional/old_node_controller_test.rb b/test/functional/old_node_controller_test.rb index ca9a114b1..cdacca52b 100644 --- a/test/functional/old_node_controller_test.rb +++ b/test/functional/old_node_controller_test.rb @@ -112,20 +112,6 @@ class OldNodeControllerTest < Test::Unit::TestCase assert_nodes_are_equal current_node, old_node end - ## - # for some reason a==b is false, but there doesn't seem to be any - # difference between the nodes, so i'm checking all the attributes - # manually and blaming it on ActiveRecord - def assert_nodes_are_equal(a, b) - assert_equal a.id, b.id, "node IDs" - assert_equal a.latitude, b.latitude, "latitude on node #{a.id}" - assert_equal a.longitude, b.longitude, "longitude on node #{a.id}" - assert_equal a.changeset_id, b.changeset_id, "changeset ID on node #{a.id}" - assert_equal a.visible, b.visible, "visible on node #{a.id}" - assert_equal a.version, b.version, "version on node #{a.id}" - assert_equal a.tags, b.tags, "tags on node #{a.id}" - end - ## # returns a 16 character long string with some nasty characters in it. # this ought to stress-test the tag handling as well as the versioning. diff --git a/test/functional/old_way_controller_test.rb b/test/functional/old_way_controller_test.rb index b357b0987..c47b45dfc 100644 --- a/test/functional/old_way_controller_test.rb +++ b/test/functional/old_way_controller_test.rb @@ -95,16 +95,4 @@ class OldWayControllerTest < Test::Unit::TestCase end end - ## - # for some reason assert_equal a, b fails when the ways are actually - # equal, so this method manually checks the fields... - def assert_ways_are_equal(a, b) - assert_equal a.id, b.id, "way IDs" - assert_equal a.changeset_id, b.changeset_id, "changeset ID on way #{a.id}" - assert_equal a.visible, b.visible, "visible on way #{a.id}" - assert_equal a.version, b.version, "version on way #{a.id}" - assert_equal a.tags, b.tags, "tags on way #{a.id}" - assert_equal a.nds, b.nds, "node references on way #{a.id}" - end - end diff --git a/test/functional/way_controller_test.rb b/test/functional/way_controller_test.rb index 0eb77c2e4..895e7532f 100644 --- a/test/functional/way_controller_test.rb +++ b/test/functional/way_controller_test.rb @@ -37,21 +37,11 @@ class WayControllerTest < Test::Unit::TestCase # check chat a non-existent way is not returned get :read, :id => 0 assert_response :not_found + end - # check the "ways for node" mode - get :ways_for_node, :id => current_nodes(:used_node_1).id - assert_response :success - # FIXME check whether this contains the stuff we want! - #print @response.body - # Needs to be updated when changing fixtures - # The generator should probably be defined in the environment.rb file - # in the same place as the api version - assert_select "osm[version=#{API_VERSION}][generator=\"OpenStreetMap server\"]", 1 - assert_select "osm way", 3 - assert_select "osm way nd", 3 - assert_select "osm way tag", 3 - - # check the "full" mode + ## + # check the "full" mode + def test_full get :full, :id => current_ways(:visible_way).id assert_response :success # FIXME check whether this contains the stuff we want! @@ -290,6 +280,33 @@ class WayControllerTest < Test::Unit::TestCase end end + ## + # test that a call to ways_for_node returns all ways that contain the node + # and none that don't. + def test_ways_for_node + # in current fixtures ways 1 and 3 all use node 3. ways 2 and 4 + # *used* to use it but doesn't. + get :ways_for_node, :id => current_nodes(:used_node_1).id + assert_response :success + ways_xml = XML::Parser.string(@response.body).parse + assert_not_nil ways_xml, "failed to parse ways_for_node response" + + # check that the set of IDs match expectations + expected_way_ids = [ current_ways(:visible_way).id, + 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, + "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 + expected_way_ids.each do |id| + way_xml = ways_xml.find("//osm/way[@id=#{id}]").first + assert_ways_are_equal(Way.find(id), + Way.from_xml_node(way_xml)) + end + end + ## # update the changeset_id of a node element def update_changeset(xml, changeset_id) diff --git a/test/test_helper.rb b/test/test_helper.rb index 63567cc9b..713bbf648 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -72,5 +72,33 @@ class Test::Unit::TestCase end end + ## + # for some reason assert_equal a, b fails when the ways are actually + # equal, so this method manually checks the fields... + def assert_ways_are_equal(a, b) + assert_not_nil a, "first way is not allowed to be nil" + assert_not_nil b, "second way #{a.id} is not allowed to be nil" + assert_equal a.id, b.id, "way IDs" + assert_equal a.changeset_id, b.changeset_id, "changeset ID on way #{a.id}" + assert_equal a.visible, b.visible, "visible on way #{a.id}, #{a.visible.inspect} != #{b.visible.inspect}" + assert_equal a.version, b.version, "version on way #{a.id}" + assert_equal a.tags, b.tags, "tags on way #{a.id}" + assert_equal a.nds, b.nds, "node references on way #{a.id}" + end + + ## + # for some reason a==b is false, but there doesn't seem to be any + # difference between the nodes, so i'm checking all the attributes + # manually and blaming it on ActiveRecord + def assert_nodes_are_equal(a, b) + assert_equal a.id, b.id, "node IDs" + assert_equal a.latitude, b.latitude, "latitude on node #{a.id}" + assert_equal a.longitude, b.longitude, "longitude on node #{a.id}" + assert_equal a.changeset_id, b.changeset_id, "changeset ID on node #{a.id}" + assert_equal a.visible, b.visible, "visible on node #{a.id}" + assert_equal a.version, b.version, "version on node #{a.id}" + assert_equal a.tags, b.tags, "tags on node #{a.id}" + end + # Add more helper methods to be used by all tests here... end