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 | 23 |
27 // If this fails something is going on with cleanup and later tests may | 24 // If this fails something is going on with cleanup and later tests may |
Paweł Hajdan Jr.
2011/04/08 15:58:17
This can also be removed now.
Mike West
2011/04/11 14:47:04
Done.
| |
28 // fail, so we want to identify problems right away. | 25 // fail, so we want to identify problems right away. |
29 ASSERT_TRUE(file_util::Delete(path_, false)); | 26 ASSERT_TRUE(file_util::Delete( |
27 temp_dir_.path().AppendASCII("SQLConnectionTest.db"), false)); | |
30 } | 28 } |
31 | 29 |
32 sql::Connection& db() { return db_; } | 30 sql::Connection& db() { return db_; } |
33 | 31 |
34 private: | 32 private: |
35 FilePath path_; | 33 ScopedTempDir temp_dir_; |
36 sql::Connection db_; | 34 sql::Connection db_; |
37 }; | 35 }; |
38 | 36 |
39 TEST_F(SQLConnectionTest, Execute) { | 37 TEST_F(SQLConnectionTest, Execute) { |
40 // Valid statement should return true. | 38 // Valid statement should return true. |
41 ASSERT_TRUE(db().Execute("CREATE TABLE foo (a, b)")); | 39 ASSERT_TRUE(db().Execute("CREATE TABLE foo (a, b)")); |
42 EXPECT_EQ(SQLITE_OK, db().GetErrorCode()); | 40 EXPECT_EQ(SQLITE_OK, db().GetErrorCode()); |
43 | 41 |
44 // Invalid statement should fail. | 42 // Invalid statement should fail. |
45 ASSERT_FALSE(db().Execute("CREATE TAB foo (a, b")); | 43 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(); | 102 int64 row = db().GetLastInsertRowId(); |
105 EXPECT_LT(0, row); | 103 EXPECT_LT(0, row); |
106 | 104 |
107 // It should be the primary key of the row we just inserted. | 105 // It should be the primary key of the row we just inserted. |
108 sql::Statement s(db().GetUniqueStatement("SELECT value FROM foo WHERE id=?")); | 106 sql::Statement s(db().GetUniqueStatement("SELECT value FROM foo WHERE id=?")); |
109 s.BindInt64(0, row); | 107 s.BindInt64(0, row); |
110 ASSERT_TRUE(s.Step()); | 108 ASSERT_TRUE(s.Step()); |
111 EXPECT_EQ(12, s.ColumnInt(0)); | 109 EXPECT_EQ(12, s.ColumnInt(0)); |
112 } | 110 } |
113 | 111 |
OLD | NEW |