[email protected] | 3a305db | 2011-04-12 13:40:53 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 5 | #include "base/file_util.h" |
[email protected] | e078590 | 2011-05-19 23:34:17 | [diff] [blame] | 6 | #include "base/scoped_temp_dir.h" |
[email protected] | f0a54b2 | 2011-07-19 18:40:21 | [diff] [blame] | 7 | #include "sql/connection.h" |
| 8 | #include "sql/statement.h" |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 9 | #include "testing/gtest/include/gtest/gtest.h" |
[email protected] | e33cba4 | 2010-08-18 23:37:03 | [diff] [blame] | 10 | #include "third_party/sqlite/sqlite3.h" |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 11 | |
| 12 | class SQLConnectionTest : public testing::Test { |
| 13 | public: |
| 14 | SQLConnectionTest() {} |
| 15 | |
| 16 | void SetUp() { |
[email protected] | 3a305db | 2011-04-12 13:40:53 | [diff] [blame] | 17 | ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 18 | ASSERT_TRUE(db_.Open(temp_dir_.path().AppendASCII("SQLConnectionTest.db"))); |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 19 | } |
| 20 | |
| 21 | void TearDown() { |
| 22 | db_.Close(); |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 23 | } |
| 24 | |
| 25 | sql::Connection& db() { return db_; } |
| 26 | |
| 27 | private: |
[email protected] | 3a305db | 2011-04-12 13:40:53 | [diff] [blame] | 28 | ScopedTempDir temp_dir_; |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 29 | sql::Connection db_; |
| 30 | }; |
| 31 | |
| 32 | TEST_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] | eff1fa52 | 2011-12-12 23:50:59 | [diff] [blame] | 38 | ASSERT_EQ(SQLITE_ERROR, |
| 39 | db().ExecuteAndReturnErrorCode("CREATE TAB foo (a, b")); |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 40 | EXPECT_EQ(SQLITE_ERROR, db().GetErrorCode()); |
| 41 | } |
| 42 | |
[email protected] | eff1fa52 | 2011-12-12 23:50:59 | [diff] [blame] | 43 | TEST_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] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 53 | TEST_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] | eff1fa52 | 2011-12-12 23:50:59 | [diff] [blame] | 62 | ASSERT_TRUE(s.is_valid()); |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 63 | |
| 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] | eff1fa52 | 2011-12-12 23:50:59 | [diff] [blame] | 75 | ASSERT_TRUE(s.is_valid()); |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 76 | |
| 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] | eff1fa52 | 2011-12-12 23:50:59 | [diff] [blame] | 85 | TEST_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] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 91 | TEST_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] | e7afe245 | 2010-08-22 16:19:13 | [diff] [blame] | 104 | // Testing for a column on a nonexistent table. |
[email protected] | e5ffd0e4 | 2009-09-11 21:30:56 | [diff] [blame] | 105 | EXPECT_FALSE(db().DoesColumnExist("bar", "b")); |
| 106 | } |
| 107 | |
| 108 | TEST_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 | } |