From: Tom Hughes Date: Tue, 24 Jul 2007 22:58:16 +0000 (+0000) Subject: Add some basic tests for nodes. X-Git-Tag: live~8814 X-Git-Url: https://git.openstreetmap.org./rails.git/commitdiff_plain/b13eb8824e29a7a0caddab76703fddb00e17a227 Add some basic tests for nodes. --- diff --git a/test/fixtures/current_nodes.yml b/test/fixtures/current_nodes.yml new file mode 100644 index 000000000..3de38ab15 --- /dev/null +++ b/test/fixtures/current_nodes.yml @@ -0,0 +1,16 @@ +# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html +visible_node: + id: 1 + latitude: 1 + longitude: 1 + user_id: 1 + visible: 1 + timestamp: 2007-01-01 00:00:00 + +invisible_node: + id: 2 + latitude: 1 + longitude: 1 + user_id: 1 + visible: 0 + timestamp: 2007-01-01 00:00:00 diff --git a/test/fixtures/nodes.yml b/test/fixtures/nodes.yml new file mode 100644 index 000000000..5bdd7a52a --- /dev/null +++ b/test/fixtures/nodes.yml @@ -0,0 +1,18 @@ +# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html +visible_node: + id: 1 + latitude: 1 + longitude: 1 + user_id: 1 + visible: 1 + tags: test=yes + timestamp: 2007-01-01 00:00:00 + +invisible_node: + id: 2 + latitude: 1 + longitude: 1 + user_id: 1 + visible: 0 + tags: test=yes + timestamp: 2007-01-01 00:00:00 diff --git a/test/fixtures/users.yml b/test/fixtures/users.yml new file mode 100644 index 000000000..bcce2f7db --- /dev/null +++ b/test/fixtures/users.yml @@ -0,0 +1,13 @@ +# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html +normal_user: + email: test@openstreetmap.org + id: 1 + active: 1 + pass_crypt: <%= Digest::MD5.hexdigest('test') %> + creation_time: "2007-01-01 00:00:00" + display_name: test + data_public: 0 + description: test + home_lat: 1 + home_lon: 1 + home_zoom: 3 diff --git a/test/functional/node_controller_test.rb b/test/functional/node_controller_test.rb new file mode 100644 index 000000000..afe09c91b --- /dev/null +++ b/test/functional/node_controller_test.rb @@ -0,0 +1,60 @@ +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("") + 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 diff --git a/test/test_helper.rb b/test/test_helper.rb new file mode 100644 index 000000000..d7bb9f15e --- /dev/null +++ b/test/test_helper.rb @@ -0,0 +1,28 @@ +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 diff --git a/test/unit/node_test.rb b/test/unit/node_test.rb new file mode 100644 index 000000000..b3e834370 --- /dev/null +++ b/test/unit/node_test.rb @@ -0,0 +1,101 @@ +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 diff --git a/test/unit/user_test.rb b/test/unit/user_test.rb new file mode 100644 index 000000000..5468f7a2d --- /dev/null +++ b/test/unit/user_test.rb @@ -0,0 +1,10 @@ +require File.dirname(__FILE__) + '/../test_helper' + +class UserTest < Test::Unit::TestCase + fixtures :users + + # Replace this with your real tests. + def test_truth + assert true + end +end