OLD | NEW |
(Empty) | |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef SQL_TEST_SCOPED_ERROR_EXPECTER_H_ |
| 6 #define SQL_TEST_SCOPED_ERROR_EXPECTER_H_ |
| 7 |
| 8 #include <set> |
| 9 |
| 10 #include "base/macros.h" |
| 11 #include "sql/connection.h" |
| 12 |
| 13 // This is not strictly necessary for the operation of ScopedErrorExpecter, but |
| 14 // the class is not useful without the SQLite error codes. |
| 15 #include "third_party/sqlite/sqlite3.h" |
| 16 |
| 17 namespace sql { |
| 18 namespace test { |
| 19 |
| 20 // sql::Connection and sql::Statement treat most SQLite errors as fatal in debug |
| 21 // mode. The goal is to catch SQL errors before code is shipped to production. |
| 22 // That fatal check makes it hard to write tests for error-handling code. This |
| 23 // scoper lists errors to expect and treat as non-fatal. Errors are expected |
| 24 // globally (on all connections). |
| 25 // |
| 26 // Since errors can be very context-dependent, the class is pedantic - specific |
| 27 // errors must be expected, and every expected error must be seen. |
| 28 // |
| 29 // NOTE(shess): There are still fatal error cases this does not address. If |
| 30 // your test is handling database errors and you're hitting a case not handled, |
| 31 // contact me. |
| 32 class ScopedErrorExpecter { |
| 33 public: |
| 34 ScopedErrorExpecter(); |
| 35 ~ScopedErrorExpecter(); |
| 36 |
| 37 // Add an error to expect. Extended error codes can be specified |
| 38 // individually, or the base code can be specified to expect errors for the |
| 39 // entire group (SQLITE_IOERR_* versus SQLITE_IOERR). |
| 40 void ExpectError(int err); |
| 41 |
| 42 // Return |true| if the all of the expected errors were encountered. Failure |
| 43 // to call this results in an EXPECT failure when the instance is destructed. |
| 44 bool SawExpectedErrors() WARN_UNUSED_RESULT; |
| 45 |
| 46 // Expose sqlite3_libversion_number() so that clients don't have to add a |
| 47 // dependency on third_party/sqlite. |
| 48 static int SQLiteLibVersionNumber() WARN_UNUSED_RESULT; |
| 49 |
| 50 private: |
| 51 // The target of the callback passed to Connection::SetErrorExpecter(). If |
| 52 // |err| matches an error passed to ExpectError(), records |err| and returns |
| 53 // |true|; this indicates that the enclosing test expected this error and the |
| 54 // caller should continue as it would in production. Otherwise returns |
| 55 // |false| and adds a failure to the current test. |
| 56 bool ErrorSeen(int err); |
| 57 |
| 58 // Callback passed to Connection::SetErrorExpecter(). |
| 59 Connection::ErrorExpecterCallback callback_; |
| 60 |
| 61 // Record whether SawExpectedErrors() has been called. |
| 62 bool checked_; |
| 63 |
| 64 // Errors to expect. |
| 65 std::set<int> errors_expected_; |
| 66 |
| 67 // Expected errors which have been encountered. |
| 68 std::set<int> errors_seen_; |
| 69 |
| 70 DISALLOW_COPY_AND_ASSIGN(ScopedErrorExpecter); |
| 71 }; |
| 72 |
| 73 } // namespace test |
| 74 } // namespace sql |
| 75 |
| 76 #endif // SQL_TEST_SCOPED_ERROR_EXPECTER_H_ |
OLD | NEW |