blob: 9718ce0042ff16c658ace47c2fd11832287ff36f [file] [log] [blame]
[email protected]3a305db2011-04-12 13:40:531// Copyright (c) 2011 The Chromium Authors. All rights reserved.
[email protected]e5ffd0e42009-09-11 21:30:562// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]e5ffd0e42009-09-11 21:30:565#include "base/file_util.h"
[email protected]e0785902011-05-19 23:34:176#include "base/scoped_temp_dir.h"
[email protected]f0a54b22011-07-19 18:40:217#include "sql/connection.h"
8#include "sql/statement.h"
[email protected]e5ffd0e42009-09-11 21:30:569#include "testing/gtest/include/gtest/gtest.h"
[email protected]e33cba42010-08-18 23:37:0310#include "third_party/sqlite/sqlite3.h"
[email protected]e5ffd0e42009-09-11 21:30:5611
12class SQLConnectionTest : public testing::Test {
13 public:
14 SQLConnectionTest() {}
15
16 void SetUp() {
[email protected]3a305db2011-04-12 13:40:5317 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
18 ASSERT_TRUE(db_.Open(temp_dir_.path().AppendASCII("SQLConnectionTest.db")));
[email protected]e5ffd0e42009-09-11 21:30:5619 }
20
21 void TearDown() {
22 db_.Close();
[email protected]e5ffd0e42009-09-11 21:30:5623 }
24
25 sql::Connection& db() { return db_; }
26
27 private:
[email protected]3a305db2011-04-12 13:40:5328 ScopedTempDir temp_dir_;
[email protected]e5ffd0e42009-09-11 21:30:5629 sql::Connection db_;
30};
31
32TEST_F(SQLConnectionTest, Execute) {
33 // Valid statement should return true.
34 ASSERT_TRUE(db().Execute("CREATE TABLE foo (a, b)"));
35 EXPECT_EQ(SQLITE_OK, db().GetErrorCode());
36
37 // Invalid statement should fail.
[email protected]eff1fa522011-12-12 23:50:5938 ASSERT_EQ(SQLITE_ERROR,
39 db().ExecuteAndReturnErrorCode("CREATE TAB foo (a, b"));
[email protected]e5ffd0e42009-09-11 21:30:5640 EXPECT_EQ(SQLITE_ERROR, db().GetErrorCode());
41}
42
[email protected]eff1fa522011-12-12 23:50:5943TEST_F(SQLConnectionTest, ExecuteWithErrorCode) {
44 ASSERT_EQ(SQLITE_OK,
45 db().ExecuteAndReturnErrorCode("CREATE TABLE foo (a, b)"));
46 ASSERT_EQ(SQLITE_ERROR,
47 db().ExecuteAndReturnErrorCode("CREATE TABLE TABLE"));
48 ASSERT_EQ(SQLITE_ERROR,
49 db().ExecuteAndReturnErrorCode(
50 "INSERT INTO foo(a, b) VALUES (1, 2, 3, 4)"));
51}
52
[email protected]e5ffd0e42009-09-11 21:30:5653TEST_F(SQLConnectionTest, CachedStatement) {
54 sql::StatementID id1("foo", 12);
55
56 ASSERT_TRUE(db().Execute("CREATE TABLE foo (a, b)"));
57 ASSERT_TRUE(db().Execute("INSERT INTO foo(a, b) VALUES (12, 13)"));
58
59 // Create a new cached statement.
60 {
61 sql::Statement s(db().GetCachedStatement(id1, "SELECT a FROM foo"));
[email protected]eff1fa522011-12-12 23:50:5962 ASSERT_TRUE(s.is_valid());
[email protected]e5ffd0e42009-09-11 21:30:5663
64 ASSERT_TRUE(s.Step());
65 EXPECT_EQ(12, s.ColumnInt(0));
66 }
67
68 // The statement should be cached still.
69 EXPECT_TRUE(db().HasCachedStatement(id1));
70
71 {
72 // Get the same statement using different SQL. This should ignore our
73 // SQL and use the cached one (so it will be valid).
74 sql::Statement s(db().GetCachedStatement(id1, "something invalid("));
[email protected]eff1fa522011-12-12 23:50:5975 ASSERT_TRUE(s.is_valid());
[email protected]e5ffd0e42009-09-11 21:30:5676
77 ASSERT_TRUE(s.Step());
78 EXPECT_EQ(12, s.ColumnInt(0));
79 }
80
81 // Make sure other statements aren't marked as cached.
82 EXPECT_FALSE(db().HasCachedStatement(SQL_FROM_HERE));
83}
84
[email protected]eff1fa522011-12-12 23:50:5985TEST_F(SQLConnectionTest, IsSQLValidTest) {
86 ASSERT_TRUE(db().Execute("CREATE TABLE foo (a, b)"));
87 ASSERT_TRUE(db().IsSQLValid("SELECT a FROM foo"));
88 ASSERT_FALSE(db().IsSQLValid("SELECT no_exist FROM foo"));
89}
90
[email protected]e5ffd0e42009-09-11 21:30:5691TEST_F(SQLConnectionTest, DoesStuffExist) {
92 // Test DoesTableExist.
93 EXPECT_FALSE(db().DoesTableExist("foo"));
94 ASSERT_TRUE(db().Execute("CREATE TABLE foo (a, b)"));
95 EXPECT_TRUE(db().DoesTableExist("foo"));
96
97 // Should be case sensitive.
98 EXPECT_FALSE(db().DoesTableExist("FOO"));
99
100 // Test DoesColumnExist.
101 EXPECT_FALSE(db().DoesColumnExist("foo", "bar"));
102 EXPECT_TRUE(db().DoesColumnExist("foo", "a"));
103
[email protected]e7afe2452010-08-22 16:19:13104 // Testing for a column on a nonexistent table.
[email protected]e5ffd0e42009-09-11 21:30:56105 EXPECT_FALSE(db().DoesColumnExist("bar", "b"));
106}
107
108TEST_F(SQLConnectionTest, GetLastInsertRowId) {
109 ASSERT_TRUE(db().Execute("CREATE TABLE foo (id INTEGER PRIMARY KEY, value)"));
110
111 ASSERT_TRUE(db().Execute("INSERT INTO foo (value) VALUES (12)"));
112
113 // Last insert row ID should be valid.
114 int64 row = db().GetLastInsertRowId();
115 EXPECT_LT(0, row);
116
117 // It should be the primary key of the row we just inserted.
118 sql::Statement s(db().GetUniqueStatement("SELECT value FROM foo WHERE id=?"));
119 s.BindInt64(0, row);
120 ASSERT_TRUE(s.Step());
121 EXPECT_EQ(12, s.ColumnInt(0));
122}