summaryrefslogtreecommitdiffstats
path: root/app/sql/statement_unittest.cc
diff options
context:
space:
mode:
authorbrettw@chromium.org <brettw@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-09-11 21:30:56 +0000
committerbrettw@chromium.org <brettw@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-09-11 21:30:56 +0000
commite5ffd0e471417e75ddcd5af20c3254c0ec2f1f5d (patch)
tree60e8d7c1de4ee33cc063cfa98a168e8fe9fcf27b /app/sql/statement_unittest.cc
parent92c3dc6b3fffbaf9fa2fa409120ca051bf317234 (diff)
downloadchromium_src-e5ffd0e471417e75ddcd5af20c3254c0ec2f1f5d.zip
chromium_src-e5ffd0e471417e75ddcd5af20c3254c0ec2f1f5d.tar.gz
chromium_src-e5ffd0e471417e75ddcd5af20c3254c0ec2f1f5d.tar.bz2
Add a new wrapper for sqlite. This is mostly a large cleanup of the existing
one, combined with the statement cache in a nice way. It is designed to entirely wrap sqlite so that we can catch corrupt errors in the future and "do something" when we get them without having to change all the calling code. There is also a new meta_table file which is almost exactly like the old one but which uses the new sql interface. This patch changes Chrome's history TextDatabase to use this new wrapper as a proof of concept, because this usage is relatively well-confined. Review URL: http://codereview.chromium.org/199047 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@26022 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'app/sql/statement_unittest.cc')
-rw-r--r--app/sql/statement_unittest.cc80
1 files changed, 80 insertions, 0 deletions
diff --git a/app/sql/statement_unittest.cc b/app/sql/statement_unittest.cc
new file mode 100644
index 0000000..30a869c
--- /dev/null
+++ b/app/sql/statement_unittest.cc
@@ -0,0 +1,80 @@
+// Copyright (c) 2009 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "app/sql/connection.h"
+#include "app/sql/statement.h"
+#include "base/file_path.h"
+#include "base/file_util.h"
+#include "base/path_service.h"
+#include "testing/gtest/include/gtest/gtest.h"
+#include "third_party/sqlite/preprocessed/sqlite3.h"
+
+class SQLStatementTest : public testing::Test {
+ public:
+ SQLStatementTest() {}
+
+ void SetUp() {
+ ASSERT_TRUE(PathService::Get(base::DIR_TEMP, &path_));
+ path_ = path_.AppendASCII("SQLStatementTest.db");
+ file_util::Delete(path_, false);
+ ASSERT_TRUE(db_.Init(path_));
+ }
+
+ void TearDown() {
+ db_.Close();
+
+ // If this fails something is going on with cleanup and later tests may
+ // fail, so we want to identify problems right away.
+ ASSERT_TRUE(file_util::Delete(path_, false));
+ }
+
+ sql::Connection& db() { return db_; }
+
+ private:
+ FilePath path_;
+ sql::Connection db_;
+};
+
+TEST_F(SQLStatementTest, Assign) {
+ sql::Statement s;
+ EXPECT_FALSE(s); // bool conversion operator.
+ EXPECT_TRUE(!s); // ! operator.
+ EXPECT_FALSE(s.is_valid());
+
+ s.Assign(db().GetUniqueStatement("CREATE TABLE foo (a, b)"));
+ EXPECT_TRUE(s);
+ EXPECT_FALSE(!s);
+ EXPECT_TRUE(s.is_valid());
+}
+
+TEST_F(SQLStatementTest, Run) {
+ ASSERT_TRUE(db().Execute("CREATE TABLE foo (a, b)"));
+ ASSERT_TRUE(db().Execute("INSERT INTO foo (a, b) VALUES (3, 12)"));
+
+ sql::Statement s(db().GetUniqueStatement("SELECT b FROM foo WHERE a=?"));
+ EXPECT_FALSE(s.Succeeded());
+
+ // Stepping it won't work since we haven't bound the value.
+ EXPECT_FALSE(s.Step());
+
+ // Run should fail since this produces output, and we should use Step(). This
+ // gets a bit wonky since sqlite says this is OK so succeeded is set.
+ s.Reset();
+ s.BindInt(0, 3);
+ EXPECT_FALSE(s.Run());
+ EXPECT_EQ(SQLITE_ROW, db().GetErrorCode());
+ EXPECT_TRUE(s.Succeeded());
+
+ // Resetting it should put it back to the previous state (not runnable).
+ s.Reset();
+ EXPECT_FALSE(s.Succeeded());
+
+ // Binding and stepping should produce one row.
+ s.BindInt(0, 3);
+ EXPECT_TRUE(s.Step());
+ EXPECT_TRUE(s.Succeeded());
+ EXPECT_EQ(12, s.ColumnInt(0));
+ EXPECT_FALSE(s.Step());
+ EXPECT_TRUE(s.Succeeded());
+}