ephy-sqlite-test.c 7.47 KB
Newer Older
Michael Catanzaro's avatar
Michael Catanzaro committed
1
/* -*- Mode: C; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2
/*
Michael Catanzaro's avatar
Michael Catanzaro committed
3
 *  Copyright © 2011 Igalia S.L.
4
 *
Michael Catanzaro's avatar
Michael Catanzaro committed
5
 *  This file is part of Epiphany.
6
 *
Michael Catanzaro's avatar
Michael Catanzaro committed
7 8 9 10
 *  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 3 of the License, or
 *  (at your option) any later version.
11
 *
Michael Catanzaro's avatar
Michael Catanzaro committed
12 13 14 15
 *  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.
16
 *
Michael Catanzaro's avatar
Michael Catanzaro committed
17 18
 *  You should have received a copy of the GNU General Public License
 *  along with Epiphany.  If not, see <http://www.gnu.org/licenses/>.
19 20 21 22 23 24 25 26 27 28 29
 */

#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 *
Michael Catanzaro's avatar
Michael Catanzaro committed
30
ensure_empty_database (const char *filename)
31
{
32
  EphySQLiteConnection *connection = ephy_sqlite_connection_new (EPHY_SQLITE_CONNECTION_MODE_READWRITE);
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
  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);

Michael Catanzaro's avatar
Michael Catanzaro committed
52
  g_assert (g_file_test (temporary_file, G_FILE_TEST_IS_REGULAR));
53
  g_unlink (temporary_file);
Michael Catanzaro's avatar
Michael Catanzaro committed
54
  g_assert (!g_file_test (temporary_file, G_FILE_TEST_IS_REGULAR));
55 56 57 58 59 60 61 62 63 64 65 66 67 68
  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);
Michael Catanzaro's avatar
Michael Catanzaro committed
69
  EphySQLiteConnection *connection = ensure_empty_database (temporary_file);
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
  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);
Michael Catanzaro's avatar
Michael Catanzaro committed
135
  EphySQLiteConnection *connection = ensure_empty_database (temporary_file);
136 137 138 139 140 141 142 143 144 145 146 147

  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);
Michael Catanzaro's avatar
Michael Catanzaro committed
148
  EphySQLiteConnection *connection = ensure_empty_database (temporary_file);
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
  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);
Michael Catanzaro's avatar
Michael Catanzaro committed
186
  EphySQLiteConnection *connection = ensure_empty_database (temporary_file);
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211

  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 ();
}