previous_error and reset_error_history. snuck in a quick fix to OrderedHash equality

This commit is contained in:
Mike Dirolf 2009-05-26 15:26:20 -04:00
parent dc44751d2f
commit cabca61e0d
3 changed files with 53 additions and 10 deletions

View File

@ -264,6 +264,27 @@ module XGen
error != nil
end
# Get the most recent error to have occured on this database
#
# Only returns errors that have occured since the last call to
# DB#reset_error_history - returns +nil+ if there is no such error.
def previous_error
error = db_command(:getpreverror => 1)
if error["err"]
error
else
nil
end
end
# Reset the error history of this database
#
# Calls to DB#previous_error will only return errors that have occurred
# since the most recent call to this method.
def reset_error_history
db_command(:reseterror => 1)
end
# Returns true if this database is a master (or is not paired with any
# other database), false if it is a slave.
def master?

View File

@ -21,9 +21,13 @@
class OrderedHash < Hash
def ==(other)
!other.nil? &&
keys == other.keys &&
values == other.values
begin
!other.nil? &&
keys == other.keys &&
values == other.values
rescue
false
end
end
# We only need the body of this class if the RUBY_VERSION is before 1.9

View File

@ -122,15 +122,33 @@ class DBTest < Test::Unit::TestCase
end
def test_error
doc = @@db.send(:db_command, :forceerror => 1)
assert @@db.error?
err = @@db.error
assert_match /forced error/, err
@@db.reset_error_history
assert_nil @@db.error
assert !@@db.error?
assert_nil @@db.previous_error
# ask again
@@db.send(:db_command, :forceerror => 1)
assert @@db.error?
err2 = @@db.error
assert_equal err, err2
assert_not_nil @@db.error
assert_not_nil @@db.previous_error
@@db.send(:db_command, :forceerror => 1)
assert @@db.error?
assert @@db.error
prev_error = @@db.previous_error
assert_equal 1, prev_error['nPrev']
assert_equal prev_error["err"], @@db.error
@@db.collection('test').find_first
assert_nil @@db.error
assert !@@db.error?
assert @@db.previous_error
assert_equal 2, @@db.previous_error['nPrev']
@@db.reset_error_history
assert_nil @@db.error
assert !@@db.error?
assert_nil @@db.previous_error
end
def test_text_port_number