--- /dev/null
+require File.dirname(__FILE__) + '/../test_helper'
+require 'node_controller'
+
+# Re-raise errors caught by the controller.
+class NodeController; def rescue_action(e) raise e end; end
+
+class NodeControllerTest < Test::Unit::TestCase
+ fixtures :current_nodes, :nodes, :users
+ set_fixture_class :current_nodes => :Node
+ set_fixture_class :nodes => :OldNode
+
+ def setup
+ @controller = NodeController.new
+ @request = ActionController::TestRequest.new
+ @response = ActionController::TestResponse.new
+ end
+
+ def test_create
+ # cannot read password from fixture as it is stored as MD5 digest
+ basic_authorization("test@openstreetmap.org", "test");
+ # create a node with random lat/lon
+ lat = rand(100)-50 + rand
+ lon = rand(100)-50 + rand
+ content("<osm><node lat='#{lat}' lon='#{lon}' /></osm>")
+ put :create
+ # hope for success
+ assert_response :success, "node upload did not return success status"
+ # read id of created node and search for it
+ nodeid = @response.body
+ checknode = Node.find(nodeid)
+ assert_not_nil checknode, "uploaded node not found in data base after upload"
+ # compare values
+ assert_in_delta lat, checknode.latitude, 1E-8, "saved node does not match requested latitude"
+ assert_in_delta lon, checknode.longitude, 1E-8, "saved node does not match requested longitude"
+ assert_equal users(:normal_user).id, checknode.user_id, "saved node does not belong to user that created it"
+ assert_equal true, checknode.visible, "saved node is not visible"
+ end
+
+ def test_read
+ # check that a visible node is returned properly
+ get :read, :id => current_nodes(:visible_node).id
+ assert_response :success
+
+ # check that an invisible node is not returned
+ get :read, :id => current_nodes(:invisible_node).id
+ assert_response :gone
+
+ # check chat a non-existent node is not returned
+ get :read, :id => 0
+ assert_response :not_found
+ end
+
+ def basic_authorization(user, pass)
+ @request.env["HTTP_AUTHORIZATION"] = "Basic %s" % Base64.encode64("#{user}:#{pass}")
+ end
+
+ def content(c)
+ @request.env["RAW_POST_DATA"] = c
+ end
+end
--- /dev/null
+ENV["RAILS_ENV"] = "test"
+require File.expand_path(File.dirname(__FILE__) + "/../config/environment")
+require 'test_help'
+
+class Test::Unit::TestCase
+ # Transactional fixtures accelerate your tests by wrapping each test method
+ # in a transaction that's rolled back on completion. This ensures that the
+ # test database remains unchanged so your fixtures don't have to be reloaded
+ # between every test method. Fewer database queries means faster tests.
+ #
+ # Read Mike Clark's excellent walkthrough at
+ # http://clarkware.com/cgi/blosxom/2005/10/24#Rails10FastTesting
+ #
+ # Every Active Record database supports transactions except MyISAM tables
+ # in MySQL. Turn off transactional fixtures in this case; however, if you
+ # don't care one way or the other, switching from MyISAM to InnoDB tables
+ # is recommended.
+ self.use_transactional_fixtures = false
+
+ # Instantiated fixtures are slow, but give you @david where otherwise you
+ # would need people(:david). If you don't want to migrate your existing
+ # test cases which use the @david style and don't mind the speed hit (each
+ # instantiated fixtures translates to a database query per test method),
+ # then set this back to true.
+ self.use_instantiated_fixtures = false
+
+ # Add more helper methods to be used by all tests here...
+end
--- /dev/null
+require File.dirname(__FILE__) + '/../test_helper'
+
+class NodeTest < Test::Unit::TestCase
+ fixtures :current_nodes, :nodes, :users
+ set_fixture_class :current_nodes => :Node
+ set_fixture_class :nodes => :OldNode
+
+ def test_create
+ node_template = Node.new(:latitude => 12.3456,
+ :longitude => 65.4321,
+ :user_id => users(:normal_user).id,
+ :visible => 1,
+ :tags => "")
+ assert node_template.save_with_history
+
+ node = Node.find(node_template.id)
+ assert_not_nil node
+ assert_equal node_template.latitude, node.latitude
+ assert_equal node_template.longitude, node.longitude
+ assert_equal node_template.user_id, node.user_id
+ assert_equal node_template.visible, node.visible
+ assert_equal node_template.tags, node.tags
+ assert_equal node_template.timestamp.to_i, node.timestamp.to_i
+
+ assert_equal OldNode.find(:all, :conditions => [ "id = ?", node_template.id ]).length, 1
+ old_node = OldNode.find(:first, :conditions => [ "id = ?", node_template.id ])
+ assert_not_nil old_node
+ assert_equal node_template.latitude, old_node.latitude
+ assert_equal node_template.longitude, old_node.longitude
+ assert_equal node_template.user_id, old_node.user_id
+ assert_equal node_template.visible, old_node.visible
+ assert_equal node_template.tags, old_node.tags
+ assert_equal node_template.timestamp.to_i, old_node.timestamp.to_i
+ end
+
+ def test_update
+ node_template = Node.find(1)
+ assert_not_nil node_template
+
+ assert_equal OldNode.find(:all, :conditions => [ "id = ?", node_template.id ]).length, 1
+ old_node_template = OldNode.find(:first, :conditions => [ "id = ?", node_template.id ])
+ assert_not_nil old_node_template
+
+ node_template.latitude = 12.3456
+ node_template.longitude = 65.4321
+ node_template.tags = "updated=yes"
+ assert node_template.save_with_history
+
+ node = Node.find(node_template.id)
+ assert_not_nil node
+ assert_equal node_template.latitude, node.latitude
+ assert_equal node_template.longitude, node.longitude
+ assert_equal node_template.user_id, node.user_id
+ assert_equal node_template.visible, node.visible
+ assert_equal node_template.tags, node.tags
+ assert_equal node_template.timestamp.to_i, node.timestamp.to_i
+
+ assert_equal OldNode.find(:all, :conditions => [ "id = ?", node_template.id ]).length, 2
+ assert_equal OldNode.find(:all, :conditions => [ "id = ? and timestamp = ?", node_template.id, node_template.timestamp ]).length, 1
+ old_node = OldNode.find(:first, :conditions => [ "id = ? and timestamp = ?", node_template.id, node_template.timestamp ])
+ assert_not_nil old_node
+ assert_equal node_template.latitude, old_node.latitude
+ assert_equal node_template.longitude, old_node.longitude
+ assert_equal node_template.user_id, old_node.user_id
+ assert_equal node_template.visible, old_node.visible
+ assert_equal node_template.tags, old_node.tags
+ assert_equal node_template.timestamp.to_i, old_node.timestamp.to_i
+ end
+
+ def test_delete
+ node_template = Node.find(1)
+ assert_not_nil node_template
+
+ assert_equal OldNode.find(:all, :conditions => [ "id = ?", node_template.id ]).length, 1
+ old_node_template = OldNode.find(:first, :conditions => [ "id = ?", node_template.id ])
+ assert_not_nil old_node_template
+
+ node_template.visible = 0
+ assert node_template.save_with_history
+
+ node = Node.find(node_template.id)
+ assert_not_nil node
+ assert_equal node_template.latitude, node.latitude
+ assert_equal node_template.longitude, node.longitude
+ assert_equal node_template.user_id, node.user_id
+ assert_equal node_template.visible, node.visible
+ assert_equal node_template.tags, node.tags
+ assert_equal node_template.timestamp.to_i, node.timestamp.to_i
+
+ assert_equal OldNode.find(:all, :conditions => [ "id = ?", node_template.id ]).length, 2
+ assert_equal OldNode.find(:all, :conditions => [ "id = ? and timestamp = ?", node_template.id, node_template.timestamp ]).length, 1
+ old_node = OldNode.find(:first, :conditions => [ "id = ? and timestamp = ?", node_template.id, node_template.timestamp ])
+ assert_not_nil old_node
+ assert_equal node_template.latitude, old_node.latitude
+ assert_equal node_template.longitude, old_node.longitude
+ assert_equal node_template.user_id, old_node.user_id
+ assert_equal node_template.visible, old_node.visible
+ assert_equal node_template.tags, old_node.tags
+ assert_equal node_template.timestamp.to_i, old_node.timestamp.to_i
+ end
+end