1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3  * This program is free software: you can redistribute it and/or modify it
4  * under the terms of the GNU Lesser General Public License as published by
5  * the Free Software Foundation.
6  *
7  * This program is distributed in the hope that it will be useful, but
8  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
9  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
10  * for more details.
11  *
12  * You should have received a copy of the GNU Lesser General Public License
13  * along with this program. If not, see <http://www.gnu.org/licenses/>.
14  *
15  */
16 
17 #include <stdlib.h>
18 #include <locale.h>
19 #include <libebook/libebook.h>
20 
21 #include "e-test-server-utils.h"
22 #include "data-test-utils.h"
23 
24 static EbSqlClosure closure = { FALSE, NULL };
25 
26 static void
test_create_cursor_empty_query(EbSqlFixture * fixture,gconstpointer user_data)27 test_create_cursor_empty_query (EbSqlFixture *fixture,
28                                 gconstpointer user_data)
29 {
30 	EbSqlCursor  *cursor;
31 	EContactField sort_fields[] = { E_CONTACT_FAMILY_NAME, E_CONTACT_GIVEN_NAME };
32 	EBookCursorSortType sort_types[] = { E_BOOK_CURSOR_SORT_ASCENDING, E_BOOK_CURSOR_SORT_ASCENDING };
33 	GError       *error = NULL;
34 
35 	cursor = e_book_sqlite_cursor_new (
36 		fixture->ebsql, NULL,
37 		sort_fields, sort_types, 2, &error);
38 
39 	g_assert (cursor != NULL);
40 	e_book_sqlite_cursor_free (fixture->ebsql, cursor);
41 }
42 
43 static void
test_create_cursor_valid_query(EbSqlFixture * fixture,gconstpointer user_data)44 test_create_cursor_valid_query (EbSqlFixture *fixture,
45                                 gconstpointer user_data)
46 {
47 	EbSqlCursor  *cursor;
48 	EContactField sort_fields[] = { E_CONTACT_FAMILY_NAME, E_CONTACT_GIVEN_NAME };
49 	EBookCursorSortType sort_types[] = { E_BOOK_CURSOR_SORT_ASCENDING, E_BOOK_CURSOR_SORT_ASCENDING };
50 	EBookQuery   *query;
51 	gchar        *sexp;
52 	GError       *error = NULL;
53 
54 	query = e_book_query_field_test (E_CONTACT_FULL_NAME, E_BOOK_QUERY_IS, "James Brown");
55 	sexp = e_book_query_to_string (query);
56 
57 	cursor = e_book_sqlite_cursor_new (
58 		fixture->ebsql, sexp,
59 		sort_fields, sort_types, 2, &error);
60 
61 	g_assert (cursor != NULL);
62 	e_book_sqlite_cursor_free (fixture->ebsql, cursor);
63 	g_free (sexp);
64 	e_book_query_unref (query);
65 }
66 
67 static void
test_create_cursor_invalid_sort(EbSqlFixture * fixture,gconstpointer user_data)68 test_create_cursor_invalid_sort (EbSqlFixture *fixture,
69                                  gconstpointer user_data)
70 {
71 	EbSqlCursor  *cursor;
72 	EContactField sort_fields[] = { E_CONTACT_TEL };
73 	EBookCursorSortType sort_types[] = { E_BOOK_CURSOR_SORT_ASCENDING };
74 	GError       *error = NULL;
75 
76 	cursor = e_book_sqlite_cursor_new (
77 		fixture->ebsql, NULL,
78 		sort_fields, sort_types, 1, &error);
79 
80 	g_assert (cursor == NULL);
81 	g_assert_error (error, E_BOOK_SQLITE_ERROR, E_BOOK_SQLITE_ERROR_INVALID_QUERY);
82 	g_clear_error (&error);
83 }
84 
85 static void
test_create_cursor_missing_sort(EbSqlFixture * fixture,gconstpointer user_data)86 test_create_cursor_missing_sort (EbSqlFixture *fixture,
87                                  gconstpointer user_data)
88 {
89 	EbSqlCursor  *cursor;
90 	GError       *error = NULL;
91 
92 	cursor = e_book_sqlite_cursor_new (fixture->ebsql, NULL, NULL, NULL, 0, &error);
93 
94 	g_assert (cursor == NULL);
95 	g_assert_error (error, E_BOOK_SQLITE_ERROR, E_BOOK_SQLITE_ERROR_INVALID_QUERY);
96 	g_clear_error (&error);
97 }
98 
99 gint
main(gint argc,gchar ** argv)100 main (gint argc,
101       gchar **argv)
102 {
103 #if !GLIB_CHECK_VERSION (2, 35, 1)
104 	g_type_init ();
105 #endif
106 	g_test_init (&argc, &argv, NULL);
107 	g_test_bug_base ("https://gitlab.gnome.org/GNOME/evolution-data-server/");
108 
109 	data_test_utils_read_args (argc, argv);
110 
111 	/* Ensure that the client and server get the same locale */
112 	g_assert (g_setenv ("LC_ALL", "en_US.UTF-8", TRUE));
113 	setlocale (LC_ALL, "");
114 
115 	g_test_add (
116 		"/EbSqlCursor/Create/EmptyQuery", EbSqlFixture, &closure,
117 		e_sqlite_fixture_setup, test_create_cursor_empty_query, e_sqlite_fixture_teardown);
118 	g_test_add (
119 		"/EbSqlCursor/Create/ValidQuery", EbSqlFixture, &closure,
120 		e_sqlite_fixture_setup, test_create_cursor_valid_query, e_sqlite_fixture_teardown);
121 	g_test_add (
122 		"/EbSqlCursor/Create/InvalidSort", EbSqlFixture, &closure,
123 		e_sqlite_fixture_setup, test_create_cursor_invalid_sort, e_sqlite_fixture_teardown);
124 	g_test_add (
125 		"/EbSqlCursor/Create/MissingSort", EbSqlFixture, &closure,
126 		e_sqlite_fixture_setup, test_create_cursor_missing_sort, e_sqlite_fixture_teardown);
127 
128 	return e_test_server_utils_run_full (argc, argv, 0);
129 }
130