| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2011 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_util.h" | |
| 8 #include "base/scoped_temp_dir.h" | |
| 9 #include "testing/gtest/include/gtest/gtest.h" | |
| 10 #include "third_party/sqlite/sqlite3.h" | |
| 11 | |
| 12 class SQLConnectionTest : public testing::Test { | |
| 13 public: | |
| 14 SQLConnectionTest() {} | |
| 15 | |
| 16 void SetUp() { | |
| 17 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | |
| 18 ASSERT_TRUE(db_.Open(temp_dir_.path().AppendASCII("SQLConnectionTest.db"))); | |
| 19 } | |
| 20 | |
| 21 void TearDown() { | |
| 22 db_.Close(); | |
| 23 } | |
| 24 | |
| 25 sql::Connection& db() { return db_; } | |
| 26 | |
| 27 private: | |
| 28 ScopedTempDir temp_dir_; | |
| 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. | |
| 38 ASSERT_FALSE(db().Execute("CREATE TAB foo (a, b")); | |
| 39 EXPECT_EQ(SQLITE_ERROR, db().GetErrorCode()); | |
| 40 } | |
| 41 | |
| 42 TEST_F(SQLConnectionTest, CachedStatement) { | |
| 43 sql::StatementID id1("foo", 12); | |
| 44 | |
| 45 ASSERT_TRUE(db().Execute("CREATE TABLE foo (a, b)")); | |
| 46 ASSERT_TRUE(db().Execute("INSERT INTO foo(a, b) VALUES (12, 13)")); | |
| 47 | |
| 48 // Create a new cached statement. | |
| 49 { | |
| 50 sql::Statement s(db().GetCachedStatement(id1, "SELECT a FROM foo")); | |
| 51 ASSERT_FALSE(!s); // Test ! operator for validity. | |
| 52 | |
| 53 ASSERT_TRUE(s.Step()); | |
| 54 EXPECT_EQ(12, s.ColumnInt(0)); | |
| 55 } | |
| 56 | |
| 57 // The statement should be cached still. | |
| 58 EXPECT_TRUE(db().HasCachedStatement(id1)); | |
| 59 | |
| 60 { | |
| 61 // Get the same statement using different SQL. This should ignore our | |
| 62 // SQL and use the cached one (so it will be valid). | |
| 63 sql::Statement s(db().GetCachedStatement(id1, "something invalid(")); | |
| 64 ASSERT_FALSE(!s); // Test ! operator for validity. | |
| 65 | |
| 66 ASSERT_TRUE(s.Step()); | |
| 67 EXPECT_EQ(12, s.ColumnInt(0)); | |
| 68 } | |
| 69 | |
| 70 // Make sure other statements aren't marked as cached. | |
| 71 EXPECT_FALSE(db().HasCachedStatement(SQL_FROM_HERE)); | |
| 72 } | |
| 73 | |
| 74 TEST_F(SQLConnectionTest, DoesStuffExist) { | |
| 75 // Test DoesTableExist. | |
| 76 EXPECT_FALSE(db().DoesTableExist("foo")); | |
| 77 ASSERT_TRUE(db().Execute("CREATE TABLE foo (a, b)")); | |
| 78 EXPECT_TRUE(db().DoesTableExist("foo")); | |
| 79 | |
| 80 // Should be case sensitive. | |
| 81 EXPECT_FALSE(db().DoesTableExist("FOO")); | |
| 82 | |
| 83 // Test DoesColumnExist. | |
| 84 EXPECT_FALSE(db().DoesColumnExist("foo", "bar")); | |
| 85 EXPECT_TRUE(db().DoesColumnExist("foo", "a")); | |
| 86 | |
| 87 // Testing for a column on a nonexistent table. | |
| 88 EXPECT_FALSE(db().DoesColumnExist("bar", "b")); | |
| 89 } | |
| 90 | |
| 91 TEST_F(SQLConnectionTest, GetLastInsertRowId) { | |
| 92 ASSERT_TRUE(db().Execute("CREATE TABLE foo (id INTEGER PRIMARY KEY, value)")); | |
| 93 | |
| 94 ASSERT_TRUE(db().Execute("INSERT INTO foo (value) VALUES (12)")); | |
| 95 | |
| 96 // Last insert row ID should be valid. | |
| 97 int64 row = db().GetLastInsertRowId(); | |
| 98 EXPECT_LT(0, row); | |
| 99 | |
| 100 // It should be the primary key of the row we just inserted. | |
| 101 sql::Statement s(db().GetUniqueStatement("SELECT value FROM foo WHERE id=?")); | |
| 102 s.BindInt64(0, row); | |
| 103 ASSERT_TRUE(s.Step()); | |
| 104 EXPECT_EQ(12, s.ColumnInt(0)); | |
| 105 } | |
| 106 | |
| OLD | NEW |