From cabca61e0db44c4856859ba6cd9631d020bce4b0 Mon Sep 17 00:00:00 2001 From: Mike Dirolf Date: Tue, 26 May 2009 15:26:20 -0400 Subject: [PATCH] previous_error and reset_error_history. snuck in a quick fix to OrderedHash equality --- lib/mongo/db.rb | 21 +++++++++++++++++++++ lib/mongo/util/ordered_hash.rb | 10 +++++++--- tests/test_db.rb | 32 +++++++++++++++++++++++++------- 3 files changed, 53 insertions(+), 10 deletions(-) diff --git a/lib/mongo/db.rb b/lib/mongo/db.rb index 51f064a..8550dcf 100644 --- a/lib/mongo/db.rb +++ b/lib/mongo/db.rb @@ -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? diff --git a/lib/mongo/util/ordered_hash.rb b/lib/mongo/util/ordered_hash.rb index d514035..8cdbdde 100644 --- a/lib/mongo/util/ordered_hash.rb +++ b/lib/mongo/util/ordered_hash.rb @@ -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 diff --git a/tests/test_db.rb b/tests/test_db.rb index e403887..09d327a 100644 --- a/tests/test_db.rb +++ b/tests/test_db.rb @@ -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