| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/file_util.h" | 5 #include "base/file_util.h" |
| 6 #include "base/scoped_temp_dir.h" | 6 #include "base/scoped_temp_dir.h" |
| 7 #include "sql/connection.h" | 7 #include "sql/connection.h" |
| 8 #include "sql/statement.h" | 8 #include "sql/statement.h" |
| 9 #include "testing/gtest/include/gtest/gtest.h" | 9 #include "testing/gtest/include/gtest/gtest.h" |
| 10 #include "third_party/sqlite/sqlite3.h" | 10 #include "third_party/sqlite/sqlite3.h" |
| (...skipping 17 matching lines...) Expand all Loading... |
| 28 ScopedTempDir temp_dir_; | 28 ScopedTempDir temp_dir_; |
| 29 sql::Connection db_; | 29 sql::Connection db_; |
| 30 }; | 30 }; |
| 31 | 31 |
| 32 TEST_F(SQLConnectionTest, Execute) { | 32 TEST_F(SQLConnectionTest, Execute) { |
| 33 // Valid statement should return true. | 33 // Valid statement should return true. |
| 34 ASSERT_TRUE(db().Execute("CREATE TABLE foo (a, b)")); | 34 ASSERT_TRUE(db().Execute("CREATE TABLE foo (a, b)")); |
| 35 EXPECT_EQ(SQLITE_OK, db().GetErrorCode()); | 35 EXPECT_EQ(SQLITE_OK, db().GetErrorCode()); |
| 36 | 36 |
| 37 // Invalid statement should fail. | 37 // Invalid statement should fail. |
| 38 ASSERT_FALSE(db().Execute("CREATE TAB foo (a, b")); | 38 ASSERT_EQ(SQLITE_ERROR, |
| 39 db().ExecuteAndReturnErrorCode("CREATE TAB foo (a, b")); |
| 39 EXPECT_EQ(SQLITE_ERROR, db().GetErrorCode()); | 40 EXPECT_EQ(SQLITE_ERROR, db().GetErrorCode()); |
| 40 } | 41 } |
| 41 | 42 |
| 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 |
| 42 TEST_F(SQLConnectionTest, CachedStatement) { | 53 TEST_F(SQLConnectionTest, CachedStatement) { |
| 43 sql::StatementID id1("foo", 12); | 54 sql::StatementID id1("foo", 12); |
| 44 | 55 |
| 45 ASSERT_TRUE(db().Execute("CREATE TABLE foo (a, b)")); | 56 ASSERT_TRUE(db().Execute("CREATE TABLE foo (a, b)")); |
| 46 ASSERT_TRUE(db().Execute("INSERT INTO foo(a, b) VALUES (12, 13)")); | 57 ASSERT_TRUE(db().Execute("INSERT INTO foo(a, b) VALUES (12, 13)")); |
| 47 | 58 |
| 48 // Create a new cached statement. | 59 // Create a new cached statement. |
| 49 { | 60 { |
| 50 sql::Statement s(db().GetCachedStatement(id1, "SELECT a FROM foo")); | 61 sql::Statement s(db().GetCachedStatement(id1, "SELECT a FROM foo")); |
| 51 ASSERT_FALSE(!s); // Test ! operator for validity. | 62 ASSERT_TRUE(s.is_valid()); |
| 52 | 63 |
| 53 ASSERT_TRUE(s.Step()); | 64 ASSERT_TRUE(s.Step()); |
| 54 EXPECT_EQ(12, s.ColumnInt(0)); | 65 EXPECT_EQ(12, s.ColumnInt(0)); |
| 55 } | 66 } |
| 56 | 67 |
| 57 // The statement should be cached still. | 68 // The statement should be cached still. |
| 58 EXPECT_TRUE(db().HasCachedStatement(id1)); | 69 EXPECT_TRUE(db().HasCachedStatement(id1)); |
| 59 | 70 |
| 60 { | 71 { |
| 61 // Get the same statement using different SQL. This should ignore our | 72 // Get the same statement using different SQL. This should ignore our |
| 62 // SQL and use the cached one (so it will be valid). | 73 // SQL and use the cached one (so it will be valid). |
| 63 sql::Statement s(db().GetCachedStatement(id1, "something invalid(")); | 74 sql::Statement s(db().GetCachedStatement(id1, "something invalid(")); |
| 64 ASSERT_FALSE(!s); // Test ! operator for validity. | 75 ASSERT_TRUE(s.is_valid()); |
| 65 | 76 |
| 66 ASSERT_TRUE(s.Step()); | 77 ASSERT_TRUE(s.Step()); |
| 67 EXPECT_EQ(12, s.ColumnInt(0)); | 78 EXPECT_EQ(12, s.ColumnInt(0)); |
| 68 } | 79 } |
| 69 | 80 |
| 70 // Make sure other statements aren't marked as cached. | 81 // Make sure other statements aren't marked as cached. |
| 71 EXPECT_FALSE(db().HasCachedStatement(SQL_FROM_HERE)); | 82 EXPECT_FALSE(db().HasCachedStatement(SQL_FROM_HERE)); |
| 72 } | 83 } |
| 73 | 84 |
| 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 |
| 74 TEST_F(SQLConnectionTest, DoesStuffExist) { | 91 TEST_F(SQLConnectionTest, DoesStuffExist) { |
| 75 // Test DoesTableExist. | 92 // Test DoesTableExist. |
| 76 EXPECT_FALSE(db().DoesTableExist("foo")); | 93 EXPECT_FALSE(db().DoesTableExist("foo")); |
| 77 ASSERT_TRUE(db().Execute("CREATE TABLE foo (a, b)")); | 94 ASSERT_TRUE(db().Execute("CREATE TABLE foo (a, b)")); |
| 78 EXPECT_TRUE(db().DoesTableExist("foo")); | 95 EXPECT_TRUE(db().DoesTableExist("foo")); |
| 79 | 96 |
| 80 // Should be case sensitive. | 97 // Should be case sensitive. |
| 81 EXPECT_FALSE(db().DoesTableExist("FOO")); | 98 EXPECT_FALSE(db().DoesTableExist("FOO")); |
| 82 | 99 |
| 83 // Test DoesColumnExist. | 100 // Test DoesColumnExist. |
| (...skipping 12 matching lines...) Expand all Loading... |
| 96 // Last insert row ID should be valid. | 113 // Last insert row ID should be valid. |
| 97 int64 row = db().GetLastInsertRowId(); | 114 int64 row = db().GetLastInsertRowId(); |
| 98 EXPECT_LT(0, row); | 115 EXPECT_LT(0, row); |
| 99 | 116 |
| 100 // It should be the primary key of the row we just inserted. | 117 // It should be the primary key of the row we just inserted. |
| 101 sql::Statement s(db().GetUniqueStatement("SELECT value FROM foo WHERE id=?")); | 118 sql::Statement s(db().GetUniqueStatement("SELECT value FROM foo WHERE id=?")); |
| 102 s.BindInt64(0, row); | 119 s.BindInt64(0, row); |
| 103 ASSERT_TRUE(s.Step()); | 120 ASSERT_TRUE(s.Step()); |
| 104 EXPECT_EQ(12, s.ColumnInt(0)); | 121 EXPECT_EQ(12, s.ColumnInt(0)); |
| 105 } | 122 } |
| 106 | |
| OLD | NEW |