Disable size_t check for //components/undo:unit_tests to fix build.
[chromium-blink-merge.git] / sql / statement_unittest.cc
blob38f1778d7361675dde3fcde4e17e6d4e8ffcfdfe
1 // Copyright (c) 2012 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.
5 #include <string>
7 #include "base/bind.h"
8 #include "base/files/file_util.h"
9 #include "base/files/scoped_temp_dir.h"
10 #include "sql/connection.h"
11 #include "sql/statement.h"
12 #include "sql/test/error_callback_support.h"
13 #include "sql/test/scoped_error_ignorer.h"
14 #include "testing/gtest/include/gtest/gtest.h"
15 #include "third_party/sqlite/sqlite3.h"
17 namespace {
19 class SQLStatementTest : public testing::Test {
20 public:
21 void SetUp() override {
22 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
23 ASSERT_TRUE(db_.Open(temp_dir_.path().AppendASCII("SQLStatementTest.db")));
26 void TearDown() override { db_.Close(); }
28 sql::Connection& db() { return db_; }
30 private:
31 base::ScopedTempDir temp_dir_;
32 sql::Connection db_;
35 } // namespace
37 TEST_F(SQLStatementTest, Assign) {
38 sql::Statement s;
39 EXPECT_FALSE(s.is_valid());
41 s.Assign(db().GetUniqueStatement("CREATE TABLE foo (a, b)"));
42 EXPECT_TRUE(s.is_valid());
45 TEST_F(SQLStatementTest, Run) {
46 ASSERT_TRUE(db().Execute("CREATE TABLE foo (a, b)"));
47 ASSERT_TRUE(db().Execute("INSERT INTO foo (a, b) VALUES (3, 12)"));
49 sql::Statement s(db().GetUniqueStatement("SELECT b FROM foo WHERE a=?"));
50 EXPECT_FALSE(s.Succeeded());
52 // Stepping it won't work since we haven't bound the value.
53 EXPECT_FALSE(s.Step());
55 // Run should fail since this produces output, and we should use Step(). This
56 // gets a bit wonky since sqlite says this is OK so succeeded is set.
57 s.Reset(true);
58 s.BindInt(0, 3);
59 EXPECT_FALSE(s.Run());
60 EXPECT_EQ(SQLITE_ROW, db().GetErrorCode());
61 EXPECT_TRUE(s.Succeeded());
63 // Resetting it should put it back to the previous state (not runnable).
64 s.Reset(true);
65 EXPECT_FALSE(s.Succeeded());
67 // Binding and stepping should produce one row.
68 s.BindInt(0, 3);
69 EXPECT_TRUE(s.Step());
70 EXPECT_TRUE(s.Succeeded());
71 EXPECT_EQ(12, s.ColumnInt(0));
72 EXPECT_FALSE(s.Step());
73 EXPECT_TRUE(s.Succeeded());
76 // Error callback called for error running a statement.
77 TEST_F(SQLStatementTest, ErrorCallback) {
78 ASSERT_TRUE(db().Execute("CREATE TABLE foo (a INTEGER PRIMARY KEY, b)"));
80 int error = SQLITE_OK;
81 sql::ScopedErrorCallback sec(
82 &db(), base::Bind(&sql::CaptureErrorCallback, &error));
84 // Insert in the foo table the primary key. It is an error to insert
85 // something other than an number. This error causes the error callback
86 // handler to be called with SQLITE_MISMATCH as error code.
87 sql::Statement s(db().GetUniqueStatement("INSERT INTO foo (a) VALUES (?)"));
88 EXPECT_TRUE(s.is_valid());
89 s.BindCString(0, "bad bad");
90 EXPECT_FALSE(s.Run());
91 EXPECT_EQ(SQLITE_MISMATCH, error);
94 // Error ignorer works for error running a statement.
95 TEST_F(SQLStatementTest, ScopedIgnoreError) {
96 ASSERT_TRUE(db().Execute("CREATE TABLE foo (a INTEGER PRIMARY KEY, b)"));
98 sql::Statement s(db().GetUniqueStatement("INSERT INTO foo (a) VALUES (?)"));
99 EXPECT_TRUE(s.is_valid());
101 sql::ScopedErrorIgnorer ignore_errors;
102 ignore_errors.IgnoreError(SQLITE_MISMATCH);
103 s.BindCString(0, "bad bad");
104 ASSERT_FALSE(s.Run());
105 ASSERT_TRUE(ignore_errors.CheckIgnoredErrors());
108 TEST_F(SQLStatementTest, Reset) {
109 ASSERT_TRUE(db().Execute("CREATE TABLE foo (a, b)"));
110 ASSERT_TRUE(db().Execute("INSERT INTO foo (a, b) VALUES (3, 12)"));
111 ASSERT_TRUE(db().Execute("INSERT INTO foo (a, b) VALUES (4, 13)"));
113 sql::Statement s(db().GetUniqueStatement(
114 "SELECT b FROM foo WHERE a = ? "));
115 s.BindInt(0, 3);
116 ASSERT_TRUE(s.Step());
117 EXPECT_EQ(12, s.ColumnInt(0));
118 ASSERT_FALSE(s.Step());
120 s.Reset(false);
121 // Verify that we can get all rows again.
122 ASSERT_TRUE(s.Step());
123 EXPECT_EQ(12, s.ColumnInt(0));
124 EXPECT_FALSE(s.Step());
126 s.Reset(true);
127 ASSERT_FALSE(s.Step());