1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
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
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/>.
20 #include <libecal/libecal.h>
22 #include "test-cal-cache-utils.h"
24 static ECalComponentId
*
25 extract_id_from_component (ECalComponent
*component
)
29 g_assert (component
!= NULL
);
31 id
= e_cal_component_get_id (component
);
32 g_assert (id
!= NULL
);
33 g_assert (id
->uid
!= NULL
);
38 static ECalComponentId
*
39 extract_id_from_string (const gchar
*icalstring
)
41 ECalComponent
*component
;
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
);
57 test_get_one (ECalCache
*cal_cache
,
60 gboolean expect_failure
)
62 ECalComponent
*component
= NULL
;
64 gchar
*icalstring
= NULL
;
68 success
= e_cal_cache_get_component (cal_cache
, uid
, rid
, &component
, NULL
, &error
);
70 g_assert_error (error
, E_CACHE_ERROR
, E_CACHE_ERROR_NOT_FOUND
);
72 g_assert (!component
);
74 g_clear_error (&error
);
76 g_assert_no_error (error
);
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
);
91 g_assert_error (error
, E_CACHE_ERROR
, E_CACHE_ERROR_NOT_FOUND
);
93 g_assert (!icalstring
);
95 g_clear_error (&error
);
97 g_assert_no_error (error
);
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
);
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 */
129 test_get_all (ECalCache
*cal_cache
,
134 GSList
*items
, *link
;
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
);
145 tmp
= va_arg (va
, const gchar
*);
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
*);
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
);
164 g_clear_error (&error
);
166 g_assert_no_error (error
);
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
);
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
);
192 g_clear_error (&error
);
194 g_assert_no_error (error
);
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
);
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
);
230 TCUClosure closure_events
= { TCU_LOAD_COMPONENT_SET_EVENTS
};
232 #if !GLIB_CHECK_VERSION (2, 35, 1)
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 ();