added more tests

This commit is contained in:
Frederik Ramm 2007-07-26 22:05:51 +00:00
parent 9dbe1a497a
commit efc4c6f85d
14 changed files with 549 additions and 5 deletions

View file

@ -5,12 +5,41 @@ visible_node:
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
latitude: 2
longitude: 2
user_id: 1
visible: 0
tags: test=yes
timestamp: 2007-01-01 00:00:00
used_node_1:
id: 3
latitude: 3
longitude: 3
user_id: 1
visible: 1
tags: test=yes
timestamp: 2007-01-01 00:00:00
used_node_2:
id: 4
latitude: 4
longitude: 4
user_id: 1
visible: 1
tags: test=yes
timestamp: 2007-01-01 00:00:00
node_used_by_relationship:
id: 5
latitude: 5
longitude: 5
user_id: 1
visible: 1
tags: test=yes
timestamp: 2007-01-01 00:00:00

27
test/fixtures/current_segments.yml vendored Normal file
View file

@ -0,0 +1,27 @@
# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html
visible_segment:
id: 1
node_a: 3
node_b: 4
user_id: 1
visible: 1
tags: test=yes
timestamp: 2007-01-01 00:00:00
invisible_segment:
id: 2
node_a: 3
node_b: 4
user_id: 1
visible: 0
tags: test=yes
timestamp: 2007-01-01 00:00:00
used_segment:
id: 3
node_a: 3
node_b: 4
user_id: 1
visible: 1
tags: test=yes
timestamp: 2007-01-01 00:00:00

View file

@ -0,0 +1,9 @@
t1:
id: 1
segment_id: 3
sequence_id: 1
t2:
id: 2
segment_id: 3
sequence_id: 1

9
test/fixtures/current_way_tags.yml vendored Normal file
View file

@ -0,0 +1,9 @@
t1:
id: 1
k: test
v: yes
t1:
id: 2
k: test
v: yes

11
test/fixtures/current_ways.yml vendored Normal file
View file

@ -0,0 +1,11 @@
visible_way:
id: 1
user_id: 1
timestamp: 2007-01-01 00:00:00
visible: 1
invisible_way:
id: 2
user_id: 1
timestamp: 2007-01-01 00:00:00
visible: 0

View file

@ -10,9 +10,37 @@ visible_node:
invisible_node:
id: 2
latitude: 1
longitude: 1
latitude: 2
longitude: 2
user_id: 1
visible: 0
tags: test=yes
timestamp: 2007-01-01 00:00:00
used_node_1:
id: 3
latitude: 3
longitude: 3
user_id: 1
visible: 1
tags: test=yes
timestamp: 2007-01-01 00:00:00
used_node_2:
id: 4
latitude: 4
longitude: 4
user_id: 1
visible: 1
tags: test=yes
timestamp: 2007-01-01 00:00:00
node_used_by_relationship:
id: 5
latitude: 5
longitude: 5
user_id: 1
visible: 1
tags: test=yes
timestamp: 2007-01-01 00:00:00

27
test/fixtures/segments.yml vendored Normal file
View file

@ -0,0 +1,27 @@
# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html
visible_segment:
id: 1
node_a: 3
node_b: 4
user_id: 1
visible: 1
tags: test=yes
timestamp: 2007-01-01 00:00:00
invisible_segment:
id: 2
node_a: 3
node_b: 4
user_id: 1
visible: 0
tags: test=yes
timestamp: 2007-01-01 00:00:00
used_segment:
id: 3
node_a: 3
node_b: 4
user_id: 1
visible: 1
tags: test=yes
timestamp: 2007-01-01 00:00:00

11
test/fixtures/way_segments.yml vendored Normal file
View file

@ -0,0 +1,11 @@
t1:
id: 1
segment_id: 3
sequence_id: 1
version: 1
t2:
id: 2
segment_id: 3
sequence_id: 1
version: 1

11
test/fixtures/way_tags.yml vendored Normal file
View file

@ -0,0 +1,11 @@
t1:
id: 1
k: test
v: yes
version: 1
t2:
id: 2
k: test
v: yes
version: 1

13
test/fixtures/ways.yml vendored Normal file
View file

@ -0,0 +1,13 @@
visible_way:
id: 1
user_id: 1
timestamp: 2007-01-01 00:00:00
visible: 1
version: 1
invisible_way:
id: 2
user_id: 1
timestamp: 2007-01-01 00:00:00
visible: 0
version: 1

View file

@ -5,9 +5,11 @@ require 'node_controller'
class NodeController; def rescue_action(e) raise e end; end
class NodeControllerTest < Test::Unit::TestCase
fixtures :current_nodes, :nodes, :users
fixtures :current_nodes, :nodes, :users, :current_segments, :segments
set_fixture_class :current_nodes => :Node
set_fixture_class :nodes => :OldNode
set_fixture_class :current_segments => :Segment
set_fixture_class :segments => :OldSegment
def setup
@controller = NodeController.new
@ -50,6 +52,35 @@ class NodeControllerTest < Test::Unit::TestCase
assert_response :not_found
end
# this tests deletion restrictions - basic deletion is tested in the unit
# tests for node!
def test_delete
# first try to delete node without auth
delete :delete, :id => current_nodes(:visible_node).id
assert_response :unauthorized
# now set auth
basic_authorization("test@openstreetmap.org", "test");
# this should work
delete :delete, :id => current_nodes(:visible_node).id
assert_response :success
# this won't work since the node is already deleted
delete :delete, :id => current_nodes(:invisible_node).id
assert_response :gone
# this won't work since the node never existed
delete :delete, :id => 0
assert_response :not_found
# this won't work since the node is in use
delete :delete, :id => current_nodes(:used_node_1).id
assert_response :precondition_failed
end
def basic_authorization(user, pass)
@request.env["HTTP_AUTHORIZATION"] = "Basic %s" % Base64.encode64("#{user}:#{pass}")
end

View file

@ -0,0 +1,102 @@
require File.dirname(__FILE__) + '/../test_helper'
require 'segment_controller'
# Re-raise errors caught by the controller.
class SegmentController; def rescue_action(e) raise e end; end
class SegmentControllerTest < Test::Unit::TestCase
fixtures :current_nodes, :nodes, :users, :current_segments, :segments
set_fixture_class :current_nodes => :Node
set_fixture_class :nodes => :OldNode
set_fixture_class :current_segments => :Segment
set_fixture_class :segments => :OldSegment
def setup
@controller = SegmentController.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");
na = current_nodes(:used_node_1).id
nb = current_nodes(:used_node_2).id
content("<osm><segment from='#{na}' to='#{nb}' /></osm>")
put :create
# hope for success
assert_response :success, "segment upload did not return success status"
# read id of created segment and search for it
segmentid = @response.body
checksegment = Segment.find(segmentid)
assert_not_nil checksegment, "uploaded segment not found in data base after upload"
# compare values
assert_equal na, checksegment.node_a, "saved segment does not match requested from-node"
assert_equal nb, checksegment.node_b, "saved segment does not match requested to-node"
assert_equal users(:normal_user).id, checksegment.user_id, "saved segment does not belong to user that created it"
assert_equal true, checksegment.visible, "saved segment is not visible"
end
def test_create_invalid
basic_authorization("test@openstreetmap.org", "test");
# create a segment with one invalid node
na = current_nodes(:used_node_1).id
nb = 0
content("<osm><segment from='#{na}' to='#{nb}' /></osm>")
put :create
# expect failure
assert_response :precondition_failed, "upload of invalid segment did not return 'precondition failed'"
end
def test_read
# check that a visible segment is returned properly
get :read, :id => current_segments(:visible_segment).id
assert_response :success
# TODO: check for <segment> tag in return data
# check that an invisible segment is not returned
get :read, :id => current_segments(:invisible_segment).id
assert_response :gone
# check chat a non-existent segment is not returned
get :read, :id => 0
assert_response :not_found
end
# this tests deletion restrictions - basic deletion is tested in the unit
# tests for segment!
def test_delete
# first try to delete segment without auth
delete :delete, :id => current_segments(:visible_segment).id
assert_response :unauthorized
# now set auth
basic_authorization("test@openstreetmap.org", "test");
# this should work
delete :delete, :id => current_segments(:visible_segment).id
assert_response :success
# this won't work since the segment is already deleted
delete :delete, :id => current_segments(:invisible_segment).id
assert_response :gone
# this won't work since the segment never existed
delete :delete, :id => 0
assert_response :not_found
# this won't work since the segment is in use
delete :delete, :id => current_segments(:used_segment).id
assert_response :precondition_failed
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

View file

@ -0,0 +1,132 @@
require File.dirname(__FILE__) + '/../test_helper'
require 'way_controller'
# Re-raise errors caught by the controller.
class WayController; def rescue_action(e) raise e end; end
class WayControllerTest < Test::Unit::TestCase
fixtures :current_nodes, :nodes, :users, :current_segments, :segments, :ways, :current_ways, :way_tags, :current_way_tags, :way_segments, :current_way_segments
set_fixture_class :current_ways => :Way
set_fixture_class :ways => :OldWay
set_fixture_class :current_segments => :Segment
set_fixture_class :segments => :OldSegment
def setup
@controller = WayController.new
@request = ActionController::TestRequest.new
@response = ActionController::TestResponse.new
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
# -------------------------------------
# Test reading ways.
# -------------------------------------
def test_read
# check that a visible way is returned properly
get :read, :id => current_ways(:visible_way).id
assert_response :success
# check that an invisible way is not returned
get :read, :id => current_ways(:invisible_way).id
assert_response :gone
# check chat a non-existent way is not returned
get :read, :id => 0
assert_response :not_found
end
# -------------------------------------
# Test simple way creation.
# -------------------------------------
def test_create
sid = current_segments(:used_segment).id
basic_authorization "test@openstreetmap.org", "test"
# create a way with pre-existing segment
content "<osm><way><seg id='#{sid}'/><tag k='test' v='yes' /></way></osm>"
put :create
# hope for success
assert_response :success,
"way upload did not return success status"
# read id of created way and search for it
wayid = @response.body
checkway = Way.find(wayid)
assert_not_nil checkway,
"uploaded way not found in data base after upload"
# compare values
assert_equal checkway.segs.length, 1,
"saved way does not contain exactly one segment"
assert_equal checkway.segs[0], sid,
"saved way does not contain the right segment"
assert_equal users(:normal_user).id, checkway.user_id,
"saved way does not belong to user that created it"
assert_equal true, checkway.visible,
"saved way is not visible"
end
# -------------------------------------
# Test creating some invalid ways.
# -------------------------------------
def test_create_invalid
basic_authorization "test@openstreetmap.org", "test"
# create a way with non-existing segment
content "<osm><way><seg id='0'/><tag k='test' v='yes' /></way></osm>"
put :create
# expect failure
assert_response :precondition_failed,
"way upload with invalid segment did not return 'precondition failed'"
# create a way with no segments
content "<osm><way><tag k='test' v='yes' /></way></osm>"
put :create
# expect failure
assert_response :precondition_failed,
"way upload with no segments did not return 'precondition failed'"
# create a way that has the same segment, twice
# (commented out - this is currently allowed!)
#sid = current_segments(:used_segment).id
#content "<osm><way><seg id='#{sid}'/><seg id='#{sid}'/><tag k='test' v='yes' /></way></osm>"
#put :create
#assert_response :internal_server_error,
# "way upload with double segment did not return 'internal server error'"
end
# -------------------------------------
# Test deleting ways.
# -------------------------------------
def test_delete
# first try to delete way without auth
delete :delete, :id => current_ways(:visible_way).id
assert_response :unauthorized
# now set auth
basic_authorization("test@openstreetmap.org", "test");
# this should work
delete :delete, :id => current_ways(:visible_way).id
assert_response :success
# this won't work since the way is already deleted
delete :delete, :id => current_ways(:invisible_way).id
assert_response :gone
# this won't work since the way never existed
delete :delete, :id => 0
assert_response :not_found
end
end

104
test/unit/segment_test.rb Normal file
View file

@ -0,0 +1,104 @@
require File.dirname(__FILE__) + '/../test_helper'
class SegmentTest < Test::Unit::TestCase
fixtures :current_nodes, :nodes, :current_segments, :segments, :users
set_fixture_class :current_segments => :Segment
set_fixture_class :segments => :OldSegment
set_fixture_class :current_nodes => :Node
set_fixture_class :nodes => :OldNode
def test_create
segment_template = Segment.new(:node_a => nodes(:used_node_1).id,
:node_b => nodes(:used_node_2).id,
:user_id => users(:normal_user).id,
:visible => 1,
:tags => "")
assert segment_template.save_with_history
segment = Segment.find(segment_template.id)
assert_not_nil segment
assert_equal segment_template.node_a, segment.node_a
assert_equal segment_template.node_b, segment.node_b
assert_equal segment_template.user_id, segment.user_id
assert_equal segment_template.visible, segment.visible
assert_equal segment_template.tags, segment.tags
assert_equal segment_template.timestamp.to_i, segment.timestamp.to_i
assert_equal OldSegment.find(:all, :conditions => [ "id = ?", segment_template.id ]).length, 1
old_segment = OldSegment.find(:first, :conditions => [ "id = ?", segment_template.id ])
assert_not_nil old_segment
assert_equal segment_template.node_a, old_segment.node_a
assert_equal segment_template.node_b, old_segment.node_b
assert_equal segment_template.user_id, old_segment.user_id
assert_equal segment_template.visible, old_segment.visible
assert_equal segment_template.tags, old_segment.tags
assert_equal segment_template.timestamp.to_i, old_segment.timestamp.to_i
end
def test_update
segment_template = Segment.find(1)
assert_not_nil segment_template
assert_equal OldSegment.find(:all, :conditions => [ "id = ?", segment_template.id ]).length, 1
old_segment_template = OldSegment.find(:first, :conditions => [ "id = ?", segment_template.id ])
assert_not_nil old_segment_template
segment_template.node_a = nodes(:used_node_2).id
segment_template.node_b = nodes(:used_node_1).id
segment_template.tags = "updated=yes"
assert segment_template.save_with_history
segment = Segment.find(segment_template.id)
assert_not_nil segment
assert_equal segment_template.node_a, segment.node_a
assert_equal segment_template.node_b, segment.node_b
assert_equal segment_template.user_id, segment.user_id
assert_equal segment_template.visible, segment.visible
assert_equal segment_template.tags, segment.tags
assert_equal segment_template.timestamp.to_i, segment.timestamp.to_i
assert_equal OldSegment.find(:all, :conditions => [ "id = ?", segment_template.id ]).length, 2
assert_equal OldSegment.find(:all, :conditions => [ "id = ? and timestamp = ?", segment_template.id, segment_template.timestamp ]).length, 1
old_segment = OldSegment.find(:first, :conditions => [ "id = ? and timestamp = ?", segment_template.id, segment_template.timestamp ])
assert_not_nil old_segment
assert_equal segment_template.node_a, old_segment.node_a
assert_equal segment_template.node_b, old_segment.node_b
assert_equal segment_template.user_id, old_segment.user_id
assert_equal segment_template.visible, old_segment.visible
assert_equal segment_template.tags, old_segment.tags
assert_equal segment_template.timestamp.to_i, old_segment.timestamp.to_i
end
def test_delete
segment_template = Segment.find(1)
assert_not_nil segment_template
assert_equal OldSegment.find(:all, :conditions => [ "id = ?", segment_template.id ]).length, 1
old_segment_template = OldSegment.find(:first, :conditions => [ "id = ?", segment_template.id ])
assert_not_nil old_segment_template
segment_template.visible = 0
assert segment_template.save_with_history
segment = Segment.find(segment_template.id)
assert_not_nil segment
assert_equal segment_template.node_a, segment.node_a
assert_equal segment_template.node_b, segment.node_b
assert_equal segment_template.user_id, segment.user_id
assert_equal segment_template.visible, segment.visible
assert_equal segment_template.tags, segment.tags
assert_equal segment_template.timestamp.to_i, segment.timestamp.to_i
assert_equal OldSegment.find(:all, :conditions => [ "id = ?", segment_template.id ]).length, 2
assert_equal OldSegment.find(:all, :conditions => [ "id = ? and timestamp = ?", segment_template.id, segment_template.timestamp ]).length, 1
old_segment = OldSegment.find(:first, :conditions => [ "id = ? and timestamp = ?", segment_template.id, segment_template.timestamp ])
assert_not_nil old_segment
assert_equal segment_template.node_a, old_segment.node_a
assert_equal segment_template.node_b, old_segment.node_b
assert_equal segment_template.user_id, old_segment.user_id
assert_equal segment_template.visible, old_segment.visible
assert_equal segment_template.tags, old_segment.tags
assert_equal segment_template.timestamp.to_i, old_segment.timestamp.to_i
end
end