updated changelog
[platform/upstream/evolution-data-server.git] / tests / libedata-book / test-sqlite-cursor-set-target.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2
3 #include <stdlib.h>
4 #include <locale.h>
5 #include <libebook/libebook.h>
6
7 #include "data-test-utils.h"
8
9 /*****************************************************
10  *          Expect the same results twice            *
11  *****************************************************/
12 static void
13 test_cursor_set_target_reset_cursor (EbSqlCursorFixture *fixture,
14                                      gconstpointer user_data)
15 {
16         GSList *results = NULL;
17         GError *error = NULL;
18
19         /* First batch */
20         if (e_book_sqlite_cursor_step (((EbSqlFixture *) fixture)->ebsql,
21                                        fixture->cursor,
22                                        EBSQL_CURSOR_STEP_MOVE | EBSQL_CURSOR_STEP_FETCH,
23                                        EBSQL_CURSOR_ORIGIN_BEGIN,
24                                        5, &results, NULL, &error) < 0)
25                 g_error ("Error fetching cursor results: %s", error->message);
26
27         print_results (results);
28
29         /* Assert the first 5 contacts in en_US order */
30         g_assert_cmpint (g_slist_length (results), ==, 5);
31         assert_contacts_order (
32                 results,
33                 "sorted-11",
34                 "sorted-1",
35                 "sorted-2",
36                 "sorted-5",
37                 "sorted-6",
38                 NULL);
39
40         g_slist_foreach (results, (GFunc) e_book_sqlite_search_data_free, NULL);
41         g_slist_free (results);
42         results = NULL;
43
44         /* Second batch reset (same results) */
45         if (e_book_sqlite_cursor_step (((EbSqlFixture *) fixture)->ebsql,
46                                        fixture->cursor,
47                                        EBSQL_CURSOR_STEP_MOVE | EBSQL_CURSOR_STEP_FETCH,
48                                        EBSQL_CURSOR_ORIGIN_BEGIN,
49                                        5, &results, NULL, &error) < 0)
50                 g_error ("Error fetching cursor results: %s", error->message);
51
52         print_results (results);
53
54         /* Assert the first 5 contacts in en_US order again */
55         g_assert_cmpint (g_slist_length (results), ==, 5);
56         assert_contacts_order (
57                 results,
58                 "sorted-11",
59                 "sorted-1",
60                 "sorted-2",
61                 "sorted-5",
62                 "sorted-6",
63                 NULL);
64
65         g_slist_foreach (results, (GFunc) e_book_sqlite_search_data_free, NULL);
66         g_slist_free (results);
67 }
68
69 /*****************************************************
70  * Expect results with family name starting with 'C' *
71  *****************************************************/
72 static void
73 test_cursor_set_target_c_next_results (EbSqlCursorFixture *fixture,
74                                        gconstpointer user_data)
75 {
76         GSList *results = NULL;
77         GError *error = NULL;
78         ECollator *collator;
79         gint n_labels;
80         const gchar *const *labels;
81
82         /* First verify our test... in en_US locale the label 'C' should exist with the index 3 */
83         collator = e_book_sqlite_ref_collator (((EbSqlFixture *) fixture)->ebsql);
84         labels = e_collator_get_index_labels (collator, &n_labels, NULL, NULL, NULL);
85         g_assert_cmpstr (labels[3], ==, "C");
86         e_collator_unref (collator);
87
88         /* Set the cursor at the start of family names beginning with 'C' */
89         e_book_sqlite_cursor_set_target_alphabetic_index (
90                 ((EbSqlFixture *) fixture)->ebsql,
91                 fixture->cursor, 3);
92
93         if (e_book_sqlite_cursor_step (((EbSqlFixture *) fixture)->ebsql,
94                                        fixture->cursor,
95                                        EBSQL_CURSOR_STEP_MOVE | EBSQL_CURSOR_STEP_FETCH,
96                                        EBSQL_CURSOR_ORIGIN_CURRENT,
97                                        5, &results, NULL, &error) < 0)
98                 g_error ("Error fetching cursor results: %s", error->message);
99
100         print_results (results);
101
102         /* Assert that we got the results starting at C */
103         g_assert_cmpint (g_slist_length (results), ==, 5);
104         assert_contacts_order (
105                 results,
106                 "sorted-10",
107                 "sorted-14",
108                 "sorted-12",
109                 "sorted-13",
110                 "sorted-9",
111                 NULL);
112
113         g_slist_foreach (results, (GFunc) e_book_sqlite_search_data_free, NULL);
114         g_slist_free (results);
115 }
116
117 /*****************************************************
118  *       Expect results before the letter 'C'        *
119  *****************************************************/
120 static void
121 test_cursor_set_target_c_prev_results (EbSqlCursorFixture *fixture,
122                                        gconstpointer user_data)
123 {
124         GSList *results = NULL;
125         GError *error = NULL;
126         ECollator *collator;
127         gint n_labels;
128         const gchar *const *labels;
129
130         /* First verify our test... in en_US locale the label 'C' should exist with the index 3 */
131         collator = e_book_sqlite_ref_collator (((EbSqlFixture *) fixture)->ebsql);
132         labels = e_collator_get_index_labels (collator, &n_labels, NULL, NULL, NULL);
133         g_assert_cmpstr (labels[3], ==, "C");
134         e_collator_unref (collator);
135
136         /* Set the cursor at the start of family names beginning with 'C' */
137         e_book_sqlite_cursor_set_target_alphabetic_index (
138                 ((EbSqlFixture *) fixture)->ebsql,
139                 fixture->cursor, 3);
140
141         if (e_book_sqlite_cursor_step (((EbSqlFixture *) fixture)->ebsql,
142                                        fixture->cursor,
143                                        EBSQL_CURSOR_STEP_MOVE | EBSQL_CURSOR_STEP_FETCH,
144                                        EBSQL_CURSOR_ORIGIN_CURRENT,
145                                        -5, &results, NULL, &error) < 0)
146                 g_error ("Error fetching cursor results: %s", error->message);
147
148         print_results (results);
149
150         /* Assert that we got the results before C */
151         g_assert_cmpint (g_slist_length (results), ==, 5);
152         assert_contacts_order (
153                 results,
154                 "sorted-18",
155                 "sorted-16",
156                 "sorted-17",
157                 "sorted-15",
158                 "sorted-8",
159                 NULL);
160
161         g_slist_foreach (results, (GFunc) e_book_sqlite_search_data_free, NULL);
162         g_slist_free (results);
163 }
164
165 static EbSqlCursorClosure closures[] = {
166         { { FALSE, NULL }, NULL, E_BOOK_CURSOR_SORT_ASCENDING },
167         { { TRUE, NULL }, NULL, E_BOOK_CURSOR_SORT_ASCENDING },
168         { { FALSE, setup_empty_book }, NULL, E_BOOK_CURSOR_SORT_ASCENDING },
169         { { TRUE, setup_empty_book }, NULL, E_BOOK_CURSOR_SORT_ASCENDING }
170 };
171
172 static const gchar *prefixes[] = {
173         "/EBookSqlite/DefaultSummary/StoreVCards",
174         "/EBookSqlite/DefaultSummary/NoVCards",
175         "/EBookSqlite/EmptySummary/StoreVCards",
176         "/EBookSqlite/EmptrySummary/NoVCards"
177 };
178
179 gint
180 main (gint argc,
181       gchar **argv)
182 {
183         gint i;
184
185 #if !GLIB_CHECK_VERSION (2, 35, 1)
186         g_type_init ();
187 #endif
188         g_test_init (&argc, &argv, NULL);
189
190         for (i = 0; i < G_N_ELEMENTS (closures); i++) {
191                 gchar *path;
192
193                 path = g_strconcat (prefixes[i], "/SetTarget/ResetCursor", NULL);
194                 g_test_add (
195                         path, EbSqlCursorFixture, &closures[i],
196                         e_sqlite_cursor_fixture_setup,
197                         test_cursor_set_target_reset_cursor,
198                         e_sqlite_cursor_fixture_teardown);
199                 g_free (path);
200
201                 path = g_strconcat (prefixes[i], "/SetTarget/Alphabetic/C/NextResults", NULL);
202                 g_test_add (
203                         path, EbSqlCursorFixture, &closures[i],
204                         e_sqlite_cursor_fixture_setup,
205                         test_cursor_set_target_c_next_results,
206                         e_sqlite_cursor_fixture_teardown);
207                 g_free (path);
208
209                 path = g_strconcat (prefixes[i], "/SetTarget/Alphabetic/C/PreviousResults", NULL);
210                 g_test_add (
211                         path, EbSqlCursorFixture, &closures[i],
212                         e_sqlite_cursor_fixture_setup,
213                         test_cursor_set_target_c_prev_results,
214                         e_sqlite_cursor_fixture_teardown);
215                 g_free (path);
216         }
217
218         return g_test_run ();
219 }