Roll src/third_party/WebKit f298044:aa8346d (svn 202628:202629)
[chromium-blink-merge.git] / sql / statement_unittest.cc
blob1565b3e48a97bdd936ae3fd52aee324d4a8e906d
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/correct_sql_test_base.h"
12 #include "sql/statement.h"
13 #include "sql/test/error_callback_support.h"
14 #include "sql/test/scoped_error_ignorer.h"
15 #include "testing/gtest/include/gtest/gtest.h"
16 #include "third_party/sqlite/sqlite3.h"
18 namespace {
20 using SQLStatementTest = sql::SQLTestBase;
22 } // namespace
24 TEST_F(SQLStatementTest, Assign) {
25 sql::Statement s;
26 EXPECT_FALSE(s.is_valid());
28 s.Assign(db().GetUniqueStatement("CREATE TABLE foo (a, b)"));
29 EXPECT_TRUE(s.is_valid());
32 TEST_F(SQLStatementTest, Run) {
33 ASSERT_TRUE(db().Execute("CREATE TABLE foo (a, b)"));
34 ASSERT_TRUE(db().Execute("INSERT INTO foo (a, b) VALUES (3, 12)"));
36 sql::Statement s(db().GetUniqueStatement("SELECT b FROM foo WHERE a=?"));
37 EXPECT_FALSE(s.Succeeded());
39 // Stepping it won't work since we haven't bound the value.
40 EXPECT_FALSE(s.Step());
42 // Run should fail since this produces output, and we should use Step(). This
43 // gets a bit wonky since sqlite says this is OK so succeeded is set.
44 s.Reset(true);
45 s.BindInt(0, 3);
46 EXPECT_FALSE(s.Run());
47 EXPECT_EQ(SQLITE_ROW, db().GetErrorCode());
48 EXPECT_TRUE(s.Succeeded());
50 // Resetting it should put it back to the previous state (not runnable).
51 s.Reset(true);
52 EXPECT_FALSE(s.Succeeded());
54 // Binding and stepping should produce one row.
55 s.BindInt(0, 3);
56 EXPECT_TRUE(s.Step());
57 EXPECT_TRUE(s.Succeeded());
58 EXPECT_EQ(12, s.ColumnInt(0));
59 EXPECT_FALSE(s.Step());
60 EXPECT_TRUE(s.Succeeded());
63 // Error callback called for error running a statement.
64 TEST_F(SQLStatementTest, ErrorCallback) {
65 ASSERT_TRUE(db().Execute("CREATE TABLE foo (a INTEGER PRIMARY KEY, b)"));
67 int error = SQLITE_OK;
68 sql::ScopedErrorCallback sec(
69 &db(), base::Bind(&sql::CaptureErrorCallback, &error));
71 // Insert in the foo table the primary key. It is an error to insert
72 // something other than an number. This error causes the error callback
73 // handler to be called with SQLITE_MISMATCH as error code.
74 sql::Statement s(db().GetUniqueStatement("INSERT INTO foo (a) VALUES (?)"));
75 EXPECT_TRUE(s.is_valid());
76 s.BindCString(0, "bad bad");
77 EXPECT_FALSE(s.Run());
78 EXPECT_EQ(SQLITE_MISMATCH, error);
81 // Error ignorer works for error running a statement.
82 TEST_F(SQLStatementTest, ScopedIgnoreError) {
83 ASSERT_TRUE(db().Execute("CREATE TABLE foo (a INTEGER PRIMARY KEY, b)"));
85 sql::Statement s(db().GetUniqueStatement("INSERT INTO foo (a) VALUES (?)"));
86 EXPECT_TRUE(s.is_valid());
88 sql::ScopedErrorIgnorer ignore_errors;
89 ignore_errors.IgnoreError(SQLITE_MISMATCH);
90 s.BindCString(0, "bad bad");
91 ASSERT_FALSE(s.Run());
92 ASSERT_TRUE(ignore_errors.CheckIgnoredErrors());
95 TEST_F(SQLStatementTest, Reset) {
96 ASSERT_TRUE(db().Execute("CREATE TABLE foo (a, b)"));
97 ASSERT_TRUE(db().Execute("INSERT INTO foo (a, b) VALUES (3, 12)"));
98 ASSERT_TRUE(db().Execute("INSERT INTO foo (a, b) VALUES (4, 13)"));
100 sql::Statement s(db().GetUniqueStatement(
101 "SELECT b FROM foo WHERE a = ? "));
102 s.BindInt(0, 3);
103 ASSERT_TRUE(s.Step());
104 EXPECT_EQ(12, s.ColumnInt(0));
105 ASSERT_FALSE(s.Step());
107 s.Reset(false);
108 // Verify that we can get all rows again.
109 ASSERT_TRUE(s.Step());
110 EXPECT_EQ(12, s.ColumnInt(0));
111 EXPECT_FALSE(s.Step());
113 s.Reset(true);
114 ASSERT_FALSE(s.Step());