I#27 - [IMAPx] Ignore DavMail's CR/LF in BODYSTRUCTURE response
[evolution-data-server.git] / tests / libedata-cal / test-cal-cache-getters.c
blob4c054133ae23d5dfd9aeb6050b262bddda2bf817
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3 * Copyright (C) 2017 Red Hat, Inc. (www.redhat.com)
5 * This library is free software: you can redistribute it and/or modify it
6 * under the terms of the GNU Lesser General Public License as published by
7 * the Free Software Foundation.
9 * This library is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
12 * for more details.
14 * You should have received a copy of the GNU Lesser General Public License
15 * along with this library. If not, see <http://www.gnu.org/licenses/>.
18 #include <stdlib.h>
19 #include <locale.h>
20 #include <libecal/libecal.h>
22 #include "test-cal-cache-utils.h"
24 static ECalComponentId *
25 extract_id_from_component (ECalComponent *component)
27 ECalComponentId *id;
29 g_assert (component != NULL);
31 id = e_cal_component_get_id (component);
32 g_assert (id != NULL);
33 g_assert (id->uid != NULL);
35 return id;
38 static ECalComponentId *
39 extract_id_from_string (const gchar *icalstring)
41 ECalComponent *component;
42 ECalComponentId *id;
44 g_assert (icalstring != NULL);
46 component = e_cal_component_new_from_string (icalstring);
47 g_assert (component != NULL);
49 id = extract_id_from_component (component);
51 g_object_unref (component);
53 return id;
56 static void
57 test_get_one (ECalCache *cal_cache,
58 const gchar *uid,
59 const gchar *rid,
60 gboolean expect_failure)
62 ECalComponent *component = NULL;
63 ECalComponentId *id;
64 gchar *icalstring = NULL;
65 gboolean success;
66 GError *error = NULL;
68 success = e_cal_cache_get_component (cal_cache, uid, rid, &component, NULL, &error);
69 if (expect_failure) {
70 g_assert_error (error, E_CACHE_ERROR, E_CACHE_ERROR_NOT_FOUND);
71 g_assert (!success);
72 g_assert (!component);
74 g_clear_error (&error);
75 } else {
76 g_assert_no_error (error);
77 g_assert (success);
78 g_assert_nonnull (component);
80 id = extract_id_from_component (component);
82 g_assert_cmpstr (id->uid, ==, uid);
83 g_assert_cmpstr (id->rid, ==, rid && *rid ? rid : NULL);
85 e_cal_component_free_id (id);
86 g_object_unref (component);
89 success = e_cal_cache_get_component_as_string (cal_cache, uid, rid, &icalstring, NULL, &error);
90 if (expect_failure) {
91 g_assert_error (error, E_CACHE_ERROR, E_CACHE_ERROR_NOT_FOUND);
92 g_assert (!success);
93 g_assert (!icalstring);
95 g_clear_error (&error);
96 } else {
97 g_assert_no_error (error);
98 g_assert (success);
99 g_assert_nonnull (icalstring);
101 id = extract_id_from_string (icalstring);
103 g_assert_cmpstr (id->uid, ==, uid);
104 g_assert_cmpstr (id->rid, ==, rid && *rid ? rid : NULL);
106 e_cal_component_free_id (id);
107 g_free (icalstring);
111 static void
112 test_getters_one (TCUFixture *fixture,
113 gconstpointer user_data)
115 test_get_one (fixture->cal_cache, "unexistent-event", NULL, TRUE);
116 test_get_one (fixture->cal_cache, "unexistent-event", "", TRUE);
117 test_get_one (fixture->cal_cache, "event-2", NULL, FALSE);
118 test_get_one (fixture->cal_cache, "event-2", "", FALSE);
119 test_get_one (fixture->cal_cache, "event-5", NULL, FALSE);
120 test_get_one (fixture->cal_cache, "event-5", "", FALSE);
121 test_get_one (fixture->cal_cache, "event-5", "20131231T000000Z", TRUE);
122 test_get_one (fixture->cal_cache, "event-6", NULL, FALSE);
123 test_get_one (fixture->cal_cache, "event-6", "", FALSE);
124 test_get_one (fixture->cal_cache, "event-6", "20170225T134900", FALSE);
127 /* NULL-terminated list of pairs <uid, rid>, what to expect */
128 static void
129 test_get_all (ECalCache *cal_cache,
130 const gchar *uid,
131 ...)
133 ECalComponentId *id;
134 GSList *items, *link;
135 va_list va;
136 const gchar *tmp;
137 GHashTable *expects;
138 gboolean success;
139 GError *error = NULL;
141 expects = g_hash_table_new_full ((GHashFunc) e_cal_component_id_hash, (GEqualFunc) e_cal_component_id_equal,
142 (GDestroyNotify) e_cal_component_free_id, NULL);
144 va_start (va, uid);
145 tmp = va_arg (va, const gchar *);
146 while (tmp) {
147 const gchar *rid = va_arg (va, const gchar *);
148 id = e_cal_component_id_new (tmp, rid);
150 g_hash_table_insert (expects, id, NULL);
152 tmp = va_arg (va, const gchar *);
154 va_end (va);
156 items = NULL;
158 success = e_cal_cache_get_components_by_uid (cal_cache, uid, &items, NULL, &error);
159 if (!g_hash_table_size (expects)) {
160 g_assert_error (error, E_CACHE_ERROR, E_CACHE_ERROR_NOT_FOUND);
161 g_assert (!success);
162 g_assert (!items);
164 g_clear_error (&error);
165 } else {
166 g_assert_no_error (error);
167 g_assert (success);
168 g_assert_nonnull (items);
170 g_assert_cmpint (g_hash_table_size (expects), ==, g_slist_length (items));
172 for (link = items; link; link = g_slist_next (link)) {
173 id = extract_id_from_component (link->data);
175 g_assert_cmpstr (id->uid, ==, uid);
176 g_assert (g_hash_table_contains (expects, id));
178 e_cal_component_free_id (id);
181 g_slist_free_full (items, g_object_unref);
184 items = NULL;
186 success = e_cal_cache_get_components_by_uid_as_string (cal_cache, uid, &items, NULL, &error);
187 if (!g_hash_table_size (expects)) {
188 g_assert_error (error, E_CACHE_ERROR, E_CACHE_ERROR_NOT_FOUND);
189 g_assert (!success);
190 g_assert (!items);
192 g_clear_error (&error);
193 } else {
194 g_assert_no_error (error);
195 g_assert (success);
196 g_assert_nonnull (items);
198 g_assert_cmpint (g_hash_table_size (expects), ==, g_slist_length (items));
200 for (link = items; link; link = g_slist_next (link)) {
201 id = extract_id_from_string (link->data);
203 g_assert_cmpstr (id->uid, ==, uid);
204 g_assert (g_hash_table_contains (expects, id));
206 e_cal_component_free_id (id);
209 g_slist_free_full (items, g_free);
212 g_hash_table_destroy (expects);
215 static void
216 test_getters_all (TCUFixture *fixture,
217 gconstpointer user_data)
219 test_get_all (fixture->cal_cache, "unexistent-event", NULL);
220 test_get_all (fixture->cal_cache, "unexistent-event", NULL);
221 test_get_all (fixture->cal_cache, "event-2", "event-2", NULL, NULL);
222 test_get_all (fixture->cal_cache, "event-5", "event-5", NULL, NULL);
223 test_get_all (fixture->cal_cache, "event-6", "event-6", NULL, "event-6", "20170225T134900", NULL);
226 gint
227 main (gint argc,
228 gchar **argv)
230 TCUClosure closure_events = { TCU_LOAD_COMPONENT_SET_EVENTS };
232 #if !GLIB_CHECK_VERSION (2, 35, 1)
233 g_type_init ();
234 #endif
235 g_test_init (&argc, &argv, NULL);
237 /* Ensure that the client and server get the same locale */
238 g_assert (g_setenv ("LC_ALL", "en_US.UTF-8", TRUE));
239 setlocale (LC_ALL, "");
241 g_test_add ("/ECalCache/Getters/One", TCUFixture, &closure_events,
242 tcu_fixture_setup, test_getters_one, tcu_fixture_teardown);
243 g_test_add ("/ECalCache/Getters/All", TCUFixture, &closure_events,
244 tcu_fixture_setup, test_getters_all, tcu_fixture_teardown);
246 return g_test_run ();