summaryrefslogtreecommitdiffstats
path: root/sql/test/scoped_error_ignorer.h
blob: 6d1c5df4ec142f2f56b70755db3b069c35634b6b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef SQL_TEST_SCOPED_ERROR_IGNORER_H_
#define SQL_TEST_SCOPED_ERROR_IGNORER_H_

#include <set>

#include "base/macros.h"
#include "sql/connection.h"

// This is not strictly necessary for the operation of ScopedErrorIgnorer, but
// the class is not useful without the SQLite error codes.
#include "third_party/sqlite/sqlite3.h"

// TODO(shess): sql::test:: seems like it could be in order for this.
namespace sql {

// sql::Connection and sql::Statement treat most SQLite errors as
// fatal in debug mode.  The intention is to catch inappropriate uses
// of SQL before the code is shipped to production.  This makes it
// challenging to write tests for things like recovery from
// corruption.  This scoper can be used to ignore selected errors
// during a test.  Errors are ignored globally (on all connections).
//
// Since errors can be very context-dependent, the class is pedantic -
// specific errors must be ignored, and every error ignored must be
// seen.
//
// NOTE(shess): There are still fatal error cases this does not
// address.  If your test is handling database errors and you're
// hitting a case not handled, contact me.
class ScopedErrorIgnorer {
 public:
  ScopedErrorIgnorer();
  ~ScopedErrorIgnorer();

  // Add an error to ignore.  Extended error codes can be ignored
  // specifically, or the base code can ignore an entire group
  // (SQLITE_IOERR_* versus SQLITE_IOERR).
  void IgnoreError(int err);

  // Allow containing test to check if the errors were encountered.
  // Failure to call results in ADD_FAILURE() in destructor.
  bool CheckIgnoredErrors();

  // Record an error and check if it should be ignored.
  bool ShouldIgnore(int err);

  // Expose sqlite3_libversion_number() so that clients don't have to add a
  // dependency on third_party/sqlite.
  static int SQLiteLibVersionNumber();

 private:
  // Storage for callback passed to Connection::SetErrorIgnorer().
  Connection::ErrorIgnorerCallback callback_;

  // Record whether CheckIgnoredErrors() has been called.
  bool checked_;

  // Errors to ignore.
  std::set<int> ignore_errors_;

  // Errors which have been ignored.
  std::set<int> errors_ignored_;

  DISALLOW_COPY_AND_ASSIGN(ScopedErrorIgnorer);
};

}  // namespace sql

#endif  // SQL_TEST_SCOPED_ERROR_IGNORER_H_