2 #include "utest_query.h"
5 #include "gcal_status.h"
6 #include "internal_gcal.h"
13 static struct gcal_resource
*ptr_gcal
= NULL
;
15 static void setup(void)
17 /* here goes any common data allocation */
18 ptr_gcal
= gcal_construct(GCALENDAR
);
21 static void teardown(void)
23 /* and here we clean up */
24 gcal_destroy(ptr_gcal
);
27 /* ATTENTION: this test will only succeed if *no* calendar operations
28 * were done in this day.
29 * This simulates the case where no new changes were done in the calendar
30 * and we are asking for then (i.e. think in a sync operation).
32 START_TEST (test_query_updated
)
36 struct gcal_event event
, edit
;
37 struct gcal_event
*entries
= NULL
;
38 char *msg
= NULL
, current_timestamp
[30];
40 gcal_init_event(&event
);
41 gcal_init_event(&edit
);
43 event
.common
.title
= "A test for updated query";
44 event
.content
= "I will insert a new event and query just for it";
45 event
.dt_start
= "2008-06-18T20:00:00-04:00";
46 event
.dt_end
= "2008-06-18T21:00:00-04:00";
47 event
.where
= "Place is -4GMT";
48 /* TODO: think in a better way to describe the status, maybe use
51 event
.status
= "confirmed";
53 result
= gcal_get_authentication(ptr_gcal
, "gcalntester", "77libgcal");
54 fail_if(result
== -1, "Authentication should work.");
55 result
= gcal_create_event(ptr_gcal
, &event
, &edit
);
56 fail_if(result
== -1, "Failed creating a new event!");
58 /* This must return no results, since this user doesn't have
59 * calendar events last updated *right now*.
61 result
= get_mili_timestamp(current_timestamp
,
62 sizeof(current_timestamp
),
65 msg
= "Cannot create timestamp!";
68 result
= gcal_query_updated(ptr_gcal
, current_timestamp
, "GData-Version: 2");
71 msg
= "Failed querying!";
75 entries
= gcal_get_entries(ptr_gcal
, &length
);
77 msg
= "Query returned results!";
81 /* A query with NULL will use current day, starting by 06:00AM plus
84 result
= gcal_set_timezone(ptr_gcal
, "-04:00");
85 result
= gcal_query_updated(ptr_gcal
, NULL
, "GData-Version: 2");
87 msg
= "Failed querying!";
91 entries
= gcal_get_entries(ptr_gcal
, &length
);
92 if((entries
== NULL
) || (length
> 1)) {
93 msg
= "Query returned inconsistent results!";
97 for (i
= 0; i
< length
; ++i
)
98 if (!(strcmp(entries
[i
].common
.title
, event
.common
.title
))) {
103 msg
= "Cannot find newly added event!";
107 result
= gcal_delete_event(ptr_gcal
, &edit
);
108 gcal_destroy_entries(entries
, length
);
109 gcal_destroy_entry(&edit
);
115 START_TEST (test_query_nulltz
)
117 int result
, flag
= 1;
120 struct gcal_event
*entries
= NULL
;
122 result
= gcal_get_authentication(ptr_gcal
, "gcalntester", "77libgcal");
123 fail_if(result
== -1, "Authentication should work.");
125 result
= gcal_query_updated(ptr_gcal
, NULL
, "GData-Version: 2");
127 msg
= "Failed querying!";
131 entries
= gcal_get_entries(ptr_gcal
, &length
);
132 if(entries
== NULL
) {
133 msg
= "Query returned inconsistent results!";
140 gcal_destroy_entries(entries
, length
);
145 /* ATTENTION: this test will only succeed if *no* calendar operations
146 * were done in this day.
147 * This simulates the case where no new changes were done in the calendar
148 * and we are asking for then (i.e. think in a sync operation).
150 START_TEST (test_query_locationtz
)
152 int result
, flag
= 1;
155 struct gcal_event
*entries
= NULL
;
157 result
= gcal_get_authentication(ptr_gcal
, "gcalntester", "77libgcal");
158 fail_if(result
== -1, "Authentication should work.");
160 result
= gcal_set_timezone(ptr_gcal
, "-04:00");
161 result
= gcal_set_location(ptr_gcal
, "America/Manaus");
163 result
= gcal_query_updated(ptr_gcal
, NULL
, "GData-Version: 2");
165 msg
= "Failed querying!";
169 entries
= gcal_get_entries(ptr_gcal
, &length
);
170 if((entries
== NULL
) || (length
> 1)) {
171 msg
= "Query returned inconsistent results!";
178 gcal_destroy_entries(entries
, length
);
179 /* Dirt trick to make sure that cleanup code for
180 * timezone+location is executed 8-)
183 gcal_destroy(ptr_gcal
);
190 START_TEST (test_query_contact
)
192 int result
, flag
= 0;
193 struct gcal_contact contact
, updated
;
194 struct gcal_resource
*obj_gcal
= NULL
;
196 struct gcal_contact
*contacts
= NULL
;
199 gcal_init_contact(&contact
);
200 gcal_init_contact(&updated
);
202 contact
.common
.title
= "John Doe Query";
203 contact
.emails_field
= malloc(sizeof(char*));
204 contact
.emails_field
[0] = "john.doe.query@foo.bar.com";
205 contact
.emails_nr
= 1;
206 contact
.pref_email
= 0;
207 contact
.emails_type
= malloc(sizeof(char*));
208 contact
.emails_type
[0] = "home";
210 obj_gcal
= gcal_construct(GCONTACT
);
211 fail_if(obj_gcal
== NULL
, "Failed to create gcal resource!");
213 result
= gcal_get_authentication(obj_gcal
, "gcalntester", "77libgcal");
214 fail_if(result
== -1, "Authentication should work.");
216 result
= gcal_create_contact(obj_gcal
, &contact
, &updated
);
217 fail_if(result
== -1, "Failed creating a new contact!");
219 result
= gcal_query_updated(obj_gcal
, NULL
, "GData-Version: 3.0");
221 msg
= "Failed querying for updated contacts!";
226 contacts
= gcal_get_all_contacts(obj_gcal
, &count
);
227 if(contacts
== NULL
) {
228 msg
= "Failed extracting the contacts vector!";
233 /* Google contacts *dont* display deleted contacts by default */
235 msg
= "Query returned inconsistent results!";
242 gcal_delete_contact(obj_gcal
, &updated
);
243 gcal_destroy_contact(&updated
);
244 gcal_destroy_contacts(contacts
, count
);
245 gcal_destroy(obj_gcal
);
252 START_TEST (test_query_delcontact
)
254 int result
, flag
= 0;
255 struct gcal_contact contact
;
256 struct gcal_contact
*contacts
= NULL
;
257 struct gcal_resource
*obj_gcal
= NULL
;
261 gcal_init_contact(&contact
);
263 contact
.common
.title
= "John Doe Query";
264 contact
.emails_field
= malloc(sizeof(char*));
265 contact
.emails_field
[0] = "john.doe.query@foo.bar.com";
266 contact
.emails_nr
= 1;
267 contact
.pref_email
= 0;
268 contact
.emails_type
= malloc(sizeof(char*));
269 contact
.emails_type
[0] = "home";
271 obj_gcal
= gcal_construct(GCONTACT
);
272 fail_if(obj_gcal
== NULL
, "Failed to create gcal resource!");
274 result
= gcal_get_authentication(obj_gcal
, "gcalntester", "77libgcal");
275 fail_if(result
== -1, "Authentication should work.");
277 /* Setting for deleted contacts should display at least
278 * one contact (the one added by 'test_query_contact'
281 gcal_deleted(obj_gcal
, SHOW
);
282 result
= gcal_query_updated(obj_gcal
, NULL
, "GData-Version: 3.0");
284 msg
= "Failed querying for updated contacts!";
289 contacts
= gcal_get_all_contacts(obj_gcal
, &count
);
290 if((count
< 1) || (contacts
== NULL
)) {
291 msg
= "Query didn't return deleted contacts!";
296 /* Default will not show deleted contacts */
297 gcal_destroy_contacts(contacts
, count
);
298 gcal_deleted(obj_gcal
, HIDE
);
299 result
= gcal_query_updated(obj_gcal
, NULL
, "GData-Version: 3.0");
301 msg
= "Failed querying for updated contacts!";
306 contacts
= gcal_get_all_contacts(obj_gcal
, &count
);
308 msg
= "Query returned inconsistent results!";
316 gcal_destroy_contacts(contacts
, count
);
317 gcal_destroy(obj_gcal
);
323 START_TEST (test_query_generic
)
326 int result
, flag
= 0;
327 struct gcal_contact
*contacts
= NULL
;
328 struct gcal_resource
*obj_gcal
= NULL
;
331 char *query
="updated-min=2008-06-20T06:00:00Z&"
332 "alt=atom&max-results=1200&showdeleted=true";
334 obj_gcal
= gcal_construct(GCONTACT
);
335 fail_if(obj_gcal
== NULL
, "Failed to create gcal resource!");
337 result
= gcal_get_authentication(obj_gcal
, "gcalntester", "77libgcal");
338 fail_if(result
== -1, "Authentication should work.");
340 result
= gcal_query(obj_gcal
, query
, "GData-Version: 2");
341 if ((result
== -1) || (gcal_status_httpcode(obj_gcal
) != 200)) {
342 msg
= "Failed using generic query!";
347 contacts
= gcal_get_all_contacts(obj_gcal
, &count
);
348 if((count
< 1) || (contacts
== NULL
)) {
349 msg
= "Query returned more contacts!";
356 gcal_destroy_contacts(contacts
, count
);
357 gcal_destroy(obj_gcal
);
365 TCase
*gcal_query_tcase_create(void)
369 int timeout_seconds
= 100;
370 tc
= tcase_create("gqueries");
372 tcase_add_checked_fixture(tc
, setup
, teardown
);
373 tcase_set_timeout (tc
, timeout_seconds
);
374 tcase_add_test(tc
, test_query_updated
);
375 tcase_add_test(tc
, test_query_nulltz
);
376 tcase_add_test(tc
, test_query_locationtz
);
377 tcase_add_test(tc
, test_query_contact
);
378 tcase_add_test(tc
, test_query_delcontact
);
379 tcase_add_test(tc
, test_query_generic
);