1 /* -*- Mode: C; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /*
3  *  Copyright © 2011 Igalia S.L.
4  *
5  *  This file is part of Epiphany.
6  *
7  *  Epiphany is free software: you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License as published by
9  *  the Free Software Foundation, either version 3 of the License, or
10  *  (at your option) any later version.
11  *
12  *  Epiphany is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *  GNU General Public License for more details.
16  *
17  *  You should have received a copy of the GNU General Public License
18  *  along with Epiphany.  If not, see <http://www.gnu.org/licenses/>.
19  */
20 
21 #include "config.h"
22 
23 #include "ephy-sqlite-connection.h"
24 #include "ephy-sqlite-statement.h"
25 #include <glib.h>
26 #include <gtk/gtk.h>
27 
28 static void
test_create_connection(void)29 test_create_connection (void)
30 {
31   gchar *temporary_file;
32   EphySQLiteConnection *connection;
33   GError *error = NULL;
34 
35   temporary_file = g_build_filename (g_get_tmp_dir (), "epiphany-sqlite-test.db", NULL);
36   connection = ephy_sqlite_connection_new (EPHY_SQLITE_CONNECTION_MODE_READWRITE, temporary_file);
37   g_assert_true (ephy_sqlite_connection_open (connection, &error));
38   g_assert_no_error (error);
39   g_assert_true (g_file_test (temporary_file, G_FILE_TEST_IS_REGULAR));
40 
41   ephy_sqlite_connection_close (connection);
42   ephy_sqlite_connection_delete_database (connection);
43   g_assert_false (g_file_test (temporary_file, G_FILE_TEST_IS_REGULAR));
44 
45   g_free (temporary_file);
46   g_object_unref (connection);
47 
48   temporary_file = g_build_filename (g_get_tmp_dir (), "directory-that-does-not-exist", "epiphany_sqlite_test.db", NULL);
49   connection = ephy_sqlite_connection_new (EPHY_SQLITE_CONNECTION_MODE_READWRITE, temporary_file);
50   g_assert_false (ephy_sqlite_connection_open (connection, &error));
51   g_assert_nonnull (error);
52   g_assert_false (g_file_test (temporary_file, G_FILE_TEST_IS_REGULAR));
53 
54   g_free (temporary_file);
55   g_object_unref (connection);
56 }
57 
58 static void
test_create_statement(void)59 test_create_statement (void)
60 {
61   gchar *temporary_file;
62   EphySQLiteConnection *connection;
63   GError *error = NULL;
64   EphySQLiteStatement *statement = NULL;
65 
66   temporary_file = g_build_filename (g_get_tmp_dir (), "epiphany-sqlite-test.db", NULL);
67   connection = ephy_sqlite_connection_new (EPHY_SQLITE_CONNECTION_MODE_READWRITE, temporary_file);
68   g_assert_true (ephy_sqlite_connection_open (connection, &error));
69   g_assert_no_error (error);
70 
71   statement = ephy_sqlite_connection_create_statement (connection, "CREATE TABLE TEST (id INTEGER)", &error);
72   g_assert_nonnull (statement);
73   g_assert_no_error (error);
74   g_object_unref (statement);
75 
76   statement = ephy_sqlite_connection_create_statement (connection, "BLAHBLAHBLAHBA", &error);
77   g_assert_null (statement);
78   g_assert_nonnull (error);
79 
80   ephy_sqlite_connection_close (connection);
81   ephy_sqlite_connection_delete_database (connection);
82 
83   g_free (temporary_file);
84   g_object_unref (connection);
85 }
86 
87 static void
create_table_and_insert_row(EphySQLiteConnection * connection)88 create_table_and_insert_row (EphySQLiteConnection *connection)
89 {
90   GError *error = NULL;
91   EphySQLiteStatement *statement = ephy_sqlite_connection_create_statement (connection, "CREATE TABLE test (id INTEGER, text LONGVARCHAR)", &error);
92   g_assert_nonnull (statement);
93   g_assert_no_error (error);
94   ephy_sqlite_statement_step (statement, &error);
95   g_assert_no_error (error);
96   g_object_unref (statement);
97 
98   statement = ephy_sqlite_connection_create_statement (connection, "SELECT * FROM test", &error);
99   g_assert_nonnull (statement);
100   g_assert_no_error (error);
101   g_assert_false (ephy_sqlite_statement_step (statement, &error));
102   g_assert_no_error (error);
103   g_object_unref (statement);
104 
105   statement = ephy_sqlite_connection_create_statement (connection, "INSERT INTO test (id, text) VALUES (3, \"test\")", &error);
106   g_assert_nonnull (statement);
107   g_assert_no_error (error);
108   ephy_sqlite_statement_step (statement, &error);
109   g_assert_no_error (error);
110   g_object_unref (statement);
111 
112   statement = ephy_sqlite_connection_create_statement (connection, "SELECT * FROM test", &error);
113   g_assert_nonnull (statement);
114   g_assert_no_error (error);
115 
116   g_assert_true (ephy_sqlite_statement_step (statement, &error));
117   g_assert_no_error (error);
118 
119   g_assert_cmpint (ephy_sqlite_connection_get_last_insert_id (connection), ==, 1);
120   g_assert_cmpint (ephy_sqlite_statement_get_column_count (statement), ==, 2);
121   g_assert_cmpint (ephy_sqlite_statement_get_column_type (statement, 0), ==, EPHY_SQLITE_COLUMN_TYPE_INT);
122   g_assert_cmpint (ephy_sqlite_statement_get_column_type (statement, 1), ==, EPHY_SQLITE_COLUMN_TYPE_STRING);
123 
124   /* Step will return false here since there is only one row. */
125   g_assert_false (ephy_sqlite_statement_step (statement, &error));
126   g_object_unref (statement);
127 }
128 
129 static void
test_create_table_and_insert_row(void)130 test_create_table_and_insert_row (void)
131 {
132   gchar *temporary_file;
133   EphySQLiteConnection *connection;
134   GError *error = NULL;
135 
136   temporary_file = g_build_filename (g_get_tmp_dir (), "epiphany-sqlite-test.db", NULL);
137   connection = ephy_sqlite_connection_new (EPHY_SQLITE_CONNECTION_MODE_READWRITE, temporary_file);
138   g_assert_true (ephy_sqlite_connection_open (connection, &error));
139   g_assert_no_error (error);
140 
141   create_table_and_insert_row (connection);
142 
143   ephy_sqlite_connection_close (connection);
144   ephy_sqlite_connection_delete_database (connection);
145 
146   g_free (temporary_file);
147   g_object_unref (connection);
148 }
149 
150 static void
test_bind_data(void)151 test_bind_data (void)
152 {
153   gchar *temporary_file;
154   EphySQLiteConnection *connection;
155   GError *error = NULL;
156   EphySQLiteStatement *statement = NULL;
157 
158   temporary_file = g_build_filename (g_get_tmp_dir (), "epiphany-sqlite-test.db", NULL);
159   connection = ephy_sqlite_connection_new (EPHY_SQLITE_CONNECTION_MODE_READWRITE, temporary_file);
160   g_assert_true (ephy_sqlite_connection_open (connection, &error));
161   g_assert_no_error (error);
162 
163   ephy_sqlite_connection_execute (connection, "CREATE TABLE test (id INTEGER, text LONGVARCHAR)", &error);
164 
165   statement = ephy_sqlite_connection_create_statement (connection, "INSERT INTO test (id, text) VALUES (?, ?)", &error);
166   g_assert_nonnull (statement);
167   g_assert_no_error (error);
168 
169   g_assert_true (ephy_sqlite_statement_bind_int (statement, 0, 3, &error));
170   g_assert_no_error (error);
171   g_assert_true (ephy_sqlite_statement_bind_string (statement, 1, "foo", &error));
172   g_assert_no_error (error);
173 
174   /* Will return false since there are no resulting rows. */
175   g_assert_false (ephy_sqlite_statement_step (statement, &error));
176   g_assert_no_error (error);
177   g_object_unref (statement);
178 
179   statement = ephy_sqlite_connection_create_statement (connection, "SELECT * FROM test", &error);
180   g_assert_nonnull (statement);
181   g_assert_no_error (error);
182   g_assert_true (ephy_sqlite_statement_step (statement, &error));
183   g_assert_no_error (error);
184   g_assert_cmpint (ephy_sqlite_statement_get_column_count (statement), ==, 2);
185   g_assert_cmpint (ephy_sqlite_statement_get_column_as_int (statement, 0), ==, 3);
186   g_assert_cmpstr (ephy_sqlite_statement_get_column_as_string (statement, 1), ==, "foo");
187 
188   ephy_sqlite_connection_close (connection);
189   ephy_sqlite_connection_delete_database (connection);
190 
191   g_object_unref (connection);
192   g_free (temporary_file);
193 }
194 
195 static void
test_table_exists(void)196 test_table_exists (void)
197 {
198   gchar *temporary_file;
199   EphySQLiteConnection *connection;
200   GError *error = NULL;
201 
202   temporary_file = g_build_filename (g_get_tmp_dir (), "epiphany-sqlite-test.db", NULL);
203   connection = ephy_sqlite_connection_new (EPHY_SQLITE_CONNECTION_MODE_READWRITE, temporary_file);
204   g_assert_true (ephy_sqlite_connection_open (connection, &error));
205   g_assert_no_error (error);
206 
207   g_assert_false (ephy_sqlite_connection_table_exists (connection, "test"));
208   g_assert_false (ephy_sqlite_connection_table_exists (connection, "something_fakey"));
209   create_table_and_insert_row (connection);
210   g_assert_true (ephy_sqlite_connection_table_exists (connection, "test"));
211   g_assert_false (ephy_sqlite_connection_table_exists (connection, "something_fakey"));
212 
213   ephy_sqlite_connection_close (connection);
214   ephy_sqlite_connection_delete_database (connection);
215 
216   g_object_unref (connection);
217   g_free (temporary_file);
218 }
219 
220 int
main(int argc,char * argv[])221 main (int   argc,
222       char *argv[])
223 {
224   gtk_test_init (&argc, &argv);
225 
226   g_test_add_func ("/lib/sqlite/ephy-sqlite/create_connection", test_create_connection);
227   g_test_add_func ("/lib/sqlite/ephy-sqlite/create_statement", test_create_statement);
228   g_test_add_func ("/lib/sqlite/ephy-sqlite/create_table_and_insert_row", test_create_table_and_insert_row);
229   g_test_add_func ("/lib/sqlite/ephy-sqlite/bind_data", test_bind_data);
230   g_test_add_func ("/lib/sqlite/ephy-sqlite/table_exists", test_table_exists);
231 
232   return g_test_run ();
233 }
234