[M85 Migration] Add an evas gl option for rotation
[platform/framework/web/chromium-efl.git] / sql / statement_unittest.cc
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.
4
5 #include <string>
6
7 #include "base/bind.h"
8 #include "base/files/file_util.h"
9 #include "base/files/scoped_temp_dir.h"
10 #include "sql/database.h"
11 #include "sql/statement.h"
12 #include "sql/test/error_callback_support.h"
13 #include "sql/test/scoped_error_expecter.h"
14 #include "sql/test/sql_test_base.h"
15 #include "testing/gtest/include/gtest/gtest.h"
16 #include "third_party/sqlite/sqlite3.h"
17
18 namespace {
19
20 using SQLStatementTest = sql::SQLTestBase;
21
22 }  // namespace
23
24 TEST_F(SQLStatementTest, Assign) {
25   sql::Statement s;
26   EXPECT_FALSE(s.is_valid());
27
28   s.Assign(db().GetUniqueStatement("CREATE TABLE foo (a, b)"));
29   EXPECT_TRUE(s.is_valid());
30 }
31
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)"));
35
36   sql::Statement s(db().GetUniqueStatement("SELECT b FROM foo WHERE a=?"));
37   EXPECT_FALSE(s.Succeeded());
38
39   // Stepping it won't work since we haven't bound the value.
40   EXPECT_FALSE(s.Step());
41
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());
49
50   // Resetting it should put it back to the previous state (not runnable).
51   s.Reset(true);
52   EXPECT_FALSE(s.Succeeded());
53
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());
61 }
62
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)"));
66
67   int error = SQLITE_OK;
68   sql::ScopedErrorCallback sec(
69       &db(), base::BindRepeating(&sql::CaptureErrorCallback, &error));
70
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);
79 }
80
81 // Error expecter works for error running a statement.
82 TEST_F(SQLStatementTest, ScopedIgnoreError) {
83   ASSERT_TRUE(db().Execute("CREATE TABLE foo (a INTEGER PRIMARY KEY, b)"));
84
85   sql::Statement s(db().GetUniqueStatement("INSERT INTO foo (a) VALUES (?)"));
86   EXPECT_TRUE(s.is_valid());
87
88   {
89     sql::test::ScopedErrorExpecter expecter;
90     expecter.ExpectError(SQLITE_MISMATCH);
91     s.BindCString(0, "bad bad");
92     ASSERT_FALSE(s.Run());
93     ASSERT_TRUE(expecter.SawExpectedErrors());
94   }
95 }
96
97 TEST_F(SQLStatementTest, Reset) {
98   ASSERT_TRUE(db().Execute("CREATE TABLE foo (a, b)"));
99   ASSERT_TRUE(db().Execute("INSERT INTO foo (a, b) VALUES (3, 12)"));
100   ASSERT_TRUE(db().Execute("INSERT INTO foo (a, b) VALUES (4, 13)"));
101
102   sql::Statement s(db().GetUniqueStatement(
103       "SELECT b FROM foo WHERE a = ? "));
104   s.BindInt(0, 3);
105   ASSERT_TRUE(s.Step());
106   EXPECT_EQ(12, s.ColumnInt(0));
107   ASSERT_FALSE(s.Step());
108
109   s.Reset(false);
110   // Verify that we can get all rows again.
111   ASSERT_TRUE(s.Step());
112   EXPECT_EQ(12, s.ColumnInt(0));
113   EXPECT_FALSE(s.Step());
114
115   s.Reset(true);
116   ASSERT_FALSE(s.Step());
117 }