blob: 30a869c50da0ae85df399a9a37bfe6920c7cea3f [file] [log] [blame]
[email protected]e5ffd0e42009-09-11 21:30:561// Copyright (c) 2009 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 "app/sql/connection.h"
6#include "app/sql/statement.h"
7#include "base/file_path.h"
8#include "base/file_util.h"
9#include "base/path_service.h"
10#include "testing/gtest/include/gtest/gtest.h"
11#include "third_party/sqlite/preprocessed/sqlite3.h"
12
13class SQLStatementTest : public testing::Test {
14 public:
15 SQLStatementTest() {}
16
17 void SetUp() {
18 ASSERT_TRUE(PathService::Get(base::DIR_TEMP, &path_));
19 path_ = path_.AppendASCII("SQLStatementTest.db");
20 file_util::Delete(path_, false);
21 ASSERT_TRUE(db_.Init(path_));
22 }
23
24 void TearDown() {
25 db_.Close();
26
27 // If this fails something is going on with cleanup and later tests may
28 // fail, so we want to identify problems right away.
29 ASSERT_TRUE(file_util::Delete(path_, false));
30 }
31
32 sql::Connection& db() { return db_; }
33
34 private:
35 FilePath path_;
36 sql::Connection db_;
37};
38
39TEST_F(SQLStatementTest, Assign) {
40 sql::Statement s;
41 EXPECT_FALSE(s); // bool conversion operator.
42 EXPECT_TRUE(!s); // ! operator.
43 EXPECT_FALSE(s.is_valid());
44
45 s.Assign(db().GetUniqueStatement("CREATE TABLE foo (a, b)"));
46 EXPECT_TRUE(s);
47 EXPECT_FALSE(!s);
48 EXPECT_TRUE(s.is_valid());
49}
50
51TEST_F(SQLStatementTest, Run) {
52 ASSERT_TRUE(db().Execute("CREATE TABLE foo (a, b)"));
53 ASSERT_TRUE(db().Execute("INSERT INTO foo (a, b) VALUES (3, 12)"));
54
55 sql::Statement s(db().GetUniqueStatement("SELECT b FROM foo WHERE a=?"));
56 EXPECT_FALSE(s.Succeeded());
57
58 // Stepping it won't work since we haven't bound the value.
59 EXPECT_FALSE(s.Step());
60
61 // Run should fail since this produces output, and we should use Step(). This
62 // gets a bit wonky since sqlite says this is OK so succeeded is set.
63 s.Reset();
64 s.BindInt(0, 3);
65 EXPECT_FALSE(s.Run());
66 EXPECT_EQ(SQLITE_ROW, db().GetErrorCode());
67 EXPECT_TRUE(s.Succeeded());
68
69 // Resetting it should put it back to the previous state (not runnable).
70 s.Reset();
71 EXPECT_FALSE(s.Succeeded());
72
73 // Binding and stepping should produce one row.
74 s.BindInt(0, 3);
75 EXPECT_TRUE(s.Step());
76 EXPECT_TRUE(s.Succeeded());
77 EXPECT_EQ(12, s.ColumnInt(0));
78 EXPECT_FALSE(s.Step());
79 EXPECT_TRUE(s.Succeeded());
80}