aboutsummaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorXan Lopez <xlopez@igalia.com>2011-11-24 17:37:23 +0800
committerXan Lopez <xan@igalia.com>2012-03-07 04:49:42 +0800
commit77ee0fdf4c5383f2134608e741b73f51ef30f430 (patch)
treec46e275c07d732d6e65cc45673f56f9e7fc9660a /tests
parenta915b03c590e60f557d9097893601e1b1bf8c08d (diff)
downloadgsoc2013-epiphany-77ee0fdf4c5383f2134608e741b73f51ef30f430.tar
gsoc2013-epiphany-77ee0fdf4c5383f2134608e741b73f51ef30f430.tar.gz
gsoc2013-epiphany-77ee0fdf4c5383f2134608e741b73f51ef30f430.tar.bz2
gsoc2013-epiphany-77ee0fdf4c5383f2134608e741b73f51ef30f430.tar.lz
gsoc2013-epiphany-77ee0fdf4c5383f2134608e741b73f51ef30f430.tar.xz
gsoc2013-epiphany-77ee0fdf4c5383f2134608e741b73f51ef30f430.tar.zst
gsoc2013-epiphany-77ee0fdf4c5383f2134608e741b73f51ef30f430.zip
Add GObject wrapper classes for SQLite
We'll use them to implement the new history/bookmarks storage backend. Code by Martin Robinson (mrobinson@igalia.com) and Claudio Saavedra (csaavedra@igalia.com)
Diffstat (limited to 'tests')
-rw-r--r--tests/Makefile.am4
-rw-r--r--tests/ephy-sqlite.c213
2 files changed, 217 insertions, 0 deletions
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 2571843fb..1a0d2cc26 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -4,6 +4,7 @@ noinst_PROGRAMS = \
test-ephy-embed-utils \
test-ephy-location-entry \
test-ephy-search-entry \
+ test-ephy-sqlite \
$(NULL)
# Mostly copied from Makefile.decl in glib
@@ -119,3 +120,6 @@ test_ephy_location_entry_SOURCES = \
test_ephy_search_entry_SOURCES = \
ephy-search-entry-test.c
+
+test_ephy_sqlite_SOURCES = \
+ ephy-sqlite.c
diff --git a/tests/ephy-sqlite.c b/tests/ephy-sqlite.c
new file mode 100644
index 000000000..309f7cfb5
--- /dev/null
+++ b/tests/ephy-sqlite.c
@@ -0,0 +1,213 @@
+/*
+ * ephy-sqlite-statement.c
+ * This file is part of Epiphany
+ *
+ * Copyright © 2011 Igalia S.L.
+ *
+ * Epiphany is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * Epiphany is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Epiphany; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor,
+ * Boston, MA 02110-1301 USA
+ */
+
+#include "config.h"
+
+#include "ephy-sqlite-connection.h"
+#include "ephy-sqlite-statement.h"
+#include <glib.h>
+#include <glib/gstdio.h>
+#include <gtk/gtk.h>
+
+static EphySQLiteConnection *
+ensure_empty_database (const char* filename)
+{
+ EphySQLiteConnection *connection = ephy_sqlite_connection_new ();
+ GError *error = NULL;
+
+ if (g_file_test (filename, G_FILE_TEST_IS_REGULAR))
+ g_unlink (filename);
+
+ g_assert (ephy_sqlite_connection_open (connection, filename, &error));
+ g_assert (!error);
+ return connection;
+}
+
+static void
+test_create_connection (void)
+{
+ GError *error = NULL;
+ gchar *temporary_file = g_build_filename (g_get_tmp_dir (), "epiphany-sqlite-test.db", NULL);
+
+ EphySQLiteConnection *connection = ensure_empty_database (temporary_file);
+ ephy_sqlite_connection_close (connection);
+
+ g_assert ( g_file_test (temporary_file, G_FILE_TEST_IS_REGULAR));
+ g_unlink (temporary_file);
+ g_assert ( !g_file_test (temporary_file, G_FILE_TEST_IS_REGULAR));
+ g_free (temporary_file);
+
+ temporary_file = g_build_filename (g_get_tmp_dir (), "directory-that-does-not-exist", "epiphany_sqlite_test.db", NULL);
+ g_assert (!ephy_sqlite_connection_open (connection, temporary_file, &error));
+ g_assert (error);
+ g_assert (!g_file_test (temporary_file, G_FILE_TEST_IS_REGULAR));
+ g_object_unref (connection);
+}
+
+
+static void
+test_create_statement (void)
+{
+ gchar *temporary_file = g_build_filename (g_get_tmp_dir (), "epiphany-sqlite-test.db", NULL);
+ EphySQLiteConnection* connection = ensure_empty_database (temporary_file);
+ GError *error = NULL;
+ EphySQLiteStatement *statement = NULL;
+
+ statement = ephy_sqlite_connection_create_statement (connection, "CREATE TABLE TEST (id INTEGER)", &error);
+ g_assert (statement);
+ g_assert (!error);
+ g_object_unref (statement);
+
+ statement = ephy_sqlite_connection_create_statement (connection, "BLAHBLAHBLAHBA", &error);
+ g_assert (!statement);
+ g_assert (error);
+
+ ephy_sqlite_connection_close (connection);
+ g_unlink (temporary_file);
+ g_free (temporary_file);
+
+ g_object_unref (connection);
+}
+
+static void
+create_table_and_insert_row (EphySQLiteConnection *connection)
+{
+ GError *error = NULL;
+ EphySQLiteStatement *statement = ephy_sqlite_connection_create_statement (connection, "CREATE TABLE test (id INTEGER, text LONGVARCHAR)", &error);
+ g_assert (statement);
+ g_assert (!error);
+ ephy_sqlite_statement_step (statement, &error);
+ g_assert (!error);
+ g_object_unref (statement);
+
+ statement = ephy_sqlite_connection_create_statement (connection, "SELECT * FROM test", &error);
+ g_assert (statement);
+ g_assert (!error);
+ g_assert (!ephy_sqlite_statement_step (statement, &error));
+ g_assert (!error);
+ g_object_unref (statement);
+
+ statement = ephy_sqlite_connection_create_statement (connection, "INSERT INTO test (id, text) VALUES (3, \"test\")", &error);
+ g_assert (statement);
+ g_assert (!error);
+ ephy_sqlite_statement_step (statement, &error);
+ g_assert (!error);
+ g_object_unref (statement);
+
+ statement = ephy_sqlite_connection_create_statement (connection, "SELECT * FROM test", &error);
+ g_assert (statement);
+ g_assert (!error);
+
+ g_assert (ephy_sqlite_statement_step (statement, &error));
+ g_assert (!error);
+
+ g_assert_cmpint (ephy_sqlite_connection_get_last_insert_id (connection), ==, 1);
+ g_assert_cmpint (ephy_sqlite_statement_get_column_count (statement), ==, 2);
+ g_assert_cmpint (ephy_sqlite_statement_get_column_type (statement, 0), ==, EPHY_SQLITE_COLUMN_TYPE_INT);
+ g_assert_cmpint (ephy_sqlite_statement_get_column_type (statement, 1), ==, EPHY_SQLITE_COLUMN_TYPE_STRING);
+
+ /* Step will return false here since there is only one row. */
+ g_assert (!ephy_sqlite_statement_step (statement, &error));
+ g_object_unref (statement);
+}
+
+static void
+test_create_table_and_insert_row (void)
+{
+ gchar *temporary_file = g_build_filename (g_get_tmp_dir (), "epiphany-sqlite-test.db", NULL);
+ EphySQLiteConnection* connection = ensure_empty_database (temporary_file);
+
+ create_table_and_insert_row (connection);
+
+ g_object_unref (connection);
+ g_unlink (temporary_file);
+ g_free (temporary_file);
+}
+
+static void
+test_bind_data (void)
+{
+ gchar *temporary_file = g_build_filename (g_get_tmp_dir (), "epiphany-sqlite-test.db", NULL);
+ EphySQLiteConnection* connection = ensure_empty_database (temporary_file);
+ GError *error = NULL;
+ EphySQLiteStatement *statement = NULL;
+
+ ephy_sqlite_connection_execute (connection, "CREATE TABLE test (id INTEGER, text LONGVARCHAR)", &error);
+
+ statement = ephy_sqlite_connection_create_statement (connection, "INSERT INTO test (id, text) VALUES (?, ?)", &error);
+ g_assert (statement);
+ g_assert (!error);
+
+ g_assert (ephy_sqlite_statement_bind_int (statement, 0, 3, &error));
+ g_assert (!error);
+ g_assert (ephy_sqlite_statement_bind_string (statement, 1, "foo", &error));
+ g_assert (!error);
+
+ /* Will return false since there are no resulting rows. */
+ g_assert (!ephy_sqlite_statement_step (statement, &error));
+ g_assert (!error);
+ g_object_unref (statement);
+
+ statement = ephy_sqlite_connection_create_statement (connection, "SELECT * FROM test", &error);
+ g_assert (statement);
+ g_assert (!error);
+ g_assert (ephy_sqlite_statement_step (statement, &error));
+ g_assert (!error);
+ g_assert_cmpint (ephy_sqlite_statement_get_column_count (statement), ==, 2);
+ g_assert_cmpint (ephy_sqlite_statement_get_column_as_int (statement, 0), ==, 3);
+ g_assert_cmpstr (ephy_sqlite_statement_get_column_as_string (statement, 1), ==, "foo");
+
+ g_object_unref (connection);
+ g_unlink (temporary_file);
+ g_free (temporary_file);
+}
+
+static void
+test_table_exists (void)
+{
+ gchar *temporary_file = g_build_filename (g_get_tmp_dir (), "epiphany-sqlite-test.db", NULL);
+ EphySQLiteConnection* connection = ensure_empty_database (temporary_file);
+
+ g_assert (!ephy_sqlite_connection_table_exists (connection, "test"));
+ g_assert (!ephy_sqlite_connection_table_exists (connection, "something_fakey"));
+ create_table_and_insert_row (connection);
+ g_assert (ephy_sqlite_connection_table_exists (connection, "test"));
+ g_assert (!ephy_sqlite_connection_table_exists (connection, "something_fakey"));
+
+ g_object_unref (connection);
+ g_unlink (temporary_file);
+ g_free (temporary_file);
+}
+
+int
+main (int argc, char *argv[])
+{
+ gtk_test_init (&argc, &argv);
+
+ g_test_add_func ("/lib/sqlite/ephy-sqlite/create_connection", test_create_connection);
+ g_test_add_func ("/lib/sqlite/ephy-sqlite/create_statement", test_create_statement);
+ g_test_add_func ("/lib/sqlite/ephy-sqlite/create_table_and_insert_row", test_create_table_and_insert_row);
+ g_test_add_func ("/lib/sqlite/ephy-sqlite/bind_data", test_bind_data);
+ g_test_add_func ("/lib/sqlite/ephy-sqlite/table_exists", test_table_exists);
+
+ return g_test_run ();
+}