]> git.openstreetmap.org Git - rails.git/commitdiff
Added tests for ways_for_node. Altered behaviour to not return deleted ways. Fixed...
authorMatt Amos <zerebubuth@gmail.com>
Tue, 21 Oct 2008 10:52:52 +0000 (10:52 +0000)
committerMatt Amos <zerebubuth@gmail.com>
Tue, 21 Oct 2008 10:52:52 +0000 (10:52 +0000)
app/controllers/way_controller.rb
app/models/way.rb
test/fixtures/ways.yml
test/functional/old_node_controller_test.rb
test/functional/old_way_controller_test.rb
test/functional/way_controller_test.rb
test/test_helper.rb

index b00658cf08e7c4deab178b03e8fccf06f54eb05d..25d535d79e1e396bca2d4e98049efaf7da5bc07b 100644 (file)
@@ -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"
index 591dee9a22c07a000cc4a413b709a2c4f13ad60a..1304d8a184da14254064c732ae252b4db6978756 100644 (file)
@@ -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|
index c1ce7222bdb1a8d43a76f00fe03d2f79fe0fe4e7..4f0af985482695991dd8d36fb6aaafad41d02691 100644 (file)
@@ -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:
index ca9a114b154c11f969e96ae6d7ab0ec28c684e47..cdacca52b23a742bc2210df03287fb35e1d31c1b 100644 (file)
@@ -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.
index b357b0987acb583d484bc328ca157828a26cb48c..c47b45dfcac4af56db4d1cbbf31e748e43ca7c4c 100644 (file)
@@ -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
index 0eb77c2e44e4f383a06f18a912d28184f2b683ee..895e7532fcfb5ebb1e1e11f6ce7df9977e9137f2 100644 (file)
@@ -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)
index 63567cc9b3ef44cab9133b26aa9d9b43f936c307..713bbf6484af8220e3b55f12d2fa19855b34cb64 100644 (file)
@@ -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