OLD | NEW |
1 // Copyright (c) 2010 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 "app/sql/connection.h" | 5 #include "app/sql/connection.h" |
6 #include "app/sql/statement.h" | 6 #include "app/sql/statement.h" |
7 #include "base/file_path.h" | |
8 #include "base/file_util.h" | 7 #include "base/file_util.h" |
9 #include "base/path_service.h" | 8 #include "base/memory/scoped_temp_dir.h" |
10 #include "testing/gtest/include/gtest/gtest.h" | 9 #include "testing/gtest/include/gtest/gtest.h" |
11 #include "third_party/sqlite/sqlite3.h" | 10 #include "third_party/sqlite/sqlite3.h" |
12 | 11 |
13 class SQLConnectionTest : public testing::Test { | 12 class SQLConnectionTest : public testing::Test { |
14 public: | 13 public: |
15 SQLConnectionTest() {} | 14 SQLConnectionTest() {} |
16 | 15 |
17 void SetUp() { | 16 void SetUp() { |
18 ASSERT_TRUE(PathService::Get(base::DIR_TEMP, &path_)); | 17 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
19 path_ = path_.AppendASCII("SQLConnectionTest.db"); | 18 ASSERT_TRUE(db_.Open(temp_dir_.path().AppendASCII("SQLConnectionTest.db"))); |
20 file_util::Delete(path_, false); | |
21 ASSERT_TRUE(db_.Open(path_)); | |
22 } | 19 } |
23 | 20 |
24 void TearDown() { | 21 void TearDown() { |
25 db_.Close(); | 22 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 } | 23 } |
31 | 24 |
32 sql::Connection& db() { return db_; } | 25 sql::Connection& db() { return db_; } |
33 | 26 |
34 private: | 27 private: |
35 FilePath path_; | 28 ScopedTempDir temp_dir_; |
36 sql::Connection db_; | 29 sql::Connection db_; |
37 }; | 30 }; |
38 | 31 |
39 TEST_F(SQLConnectionTest, Execute) { | 32 TEST_F(SQLConnectionTest, Execute) { |
40 // Valid statement should return true. | 33 // Valid statement should return true. |
41 ASSERT_TRUE(db().Execute("CREATE TABLE foo (a, b)")); | 34 ASSERT_TRUE(db().Execute("CREATE TABLE foo (a, b)")); |
42 EXPECT_EQ(SQLITE_OK, db().GetErrorCode()); | 35 EXPECT_EQ(SQLITE_OK, db().GetErrorCode()); |
43 | 36 |
44 // Invalid statement should fail. | 37 // Invalid statement should fail. |
45 ASSERT_FALSE(db().Execute("CREATE TAB foo (a, b")); | 38 ASSERT_FALSE(db().Execute("CREATE TAB foo (a, b")); |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
104 int64 row = db().GetLastInsertRowId(); | 97 int64 row = db().GetLastInsertRowId(); |
105 EXPECT_LT(0, row); | 98 EXPECT_LT(0, row); |
106 | 99 |
107 // It should be the primary key of the row we just inserted. | 100 // It should be the primary key of the row we just inserted. |
108 sql::Statement s(db().GetUniqueStatement("SELECT value FROM foo WHERE id=?")); | 101 sql::Statement s(db().GetUniqueStatement("SELECT value FROM foo WHERE id=?")); |
109 s.BindInt64(0, row); | 102 s.BindInt64(0, row); |
110 ASSERT_TRUE(s.Step()); | 103 ASSERT_TRUE(s.Step()); |
111 EXPECT_EQ(12, s.ColumnInt(0)); | 104 EXPECT_EQ(12, s.ColumnInt(0)); |
112 } | 105 } |
113 | 106 |
OLD | NEW |