Unit test stuff on new contact fields.
[libgcal.git] / utests / utest_query.c
blob3a84c1094196fa9de2264001390e4a77de8e27af
2 #include "utest_query.h"
3 #include "gcal.h"
4 #include "gcontact.h"
5 #include "gcal_status.h"
6 #include "internal_gcal.h"
7 #include <string.h>
8 #include <stdio.h>
9 #include <unistd.h>
10 #include <stdlib.h>
11 #include <time.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)
34 int result, flag = 1;
35 size_t length, i;
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
49 * a set of strings.
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),
63 "-04:00");
64 if (result == -1) {
65 msg = "Cannot create timestamp!";
66 goto cleanup;
68 result = gcal_query_updated(ptr_gcal, current_timestamp, "GData-Version: 2");
70 if (result == -1) {
71 msg = "Failed querying!";
72 goto cleanup;
75 entries = gcal_get_entries(ptr_gcal, &length);
76 if (length != 0) {
77 msg = "Query returned results!";
78 goto cleanup;
81 /* A query with NULL will use current day, starting by 06:00AM plus
82 * the timezone.
84 result = gcal_set_timezone(ptr_gcal, "-04:00");
85 result = gcal_query_updated(ptr_gcal, NULL, "GData-Version: 2");
86 if (result == -1) {
87 msg = "Failed querying!";
88 goto cleanup;
91 entries = gcal_get_entries(ptr_gcal, &length);
92 if((entries == NULL) || (length > 1)) {
93 msg = "Query returned inconsistent results!";
94 goto cleanup;
97 for (i = 0; i < length; ++i)
98 if (!(strcmp(entries[i].common.title, event.common.title))) {
99 flag = 0;
100 goto cleanup;
103 msg = "Cannot find newly added event!";
106 cleanup:
107 result = gcal_delete_event(ptr_gcal, &edit);
108 gcal_destroy_entries(entries, length);
109 gcal_destroy_entry(&edit);
110 fail_if(flag, msg);
113 END_TEST
115 START_TEST (test_query_nulltz)
117 int result, flag = 1;
118 char *msg = NULL;
119 size_t length;
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");
126 if (result == -1) {
127 msg = "Failed querying!";
128 goto cleanup;
131 entries = gcal_get_entries(ptr_gcal, &length);
132 if(entries == NULL) {
133 msg = "Query returned inconsistent results!";
134 goto cleanup;
137 flag = 0;
139 cleanup:
140 gcal_destroy_entries(entries, length);
141 fail_if(flag, msg);
143 END_TEST
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;
153 char *msg = NULL;
154 size_t length;
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");
164 if (result == -1) {
165 msg = "Failed querying!";
166 goto cleanup;
169 entries = gcal_get_entries(ptr_gcal, &length);
170 if((entries == NULL) || (length > 1)) {
171 msg = "Query returned inconsistent results!";
172 goto cleanup;
175 flag = 0;
177 cleanup:
178 gcal_destroy_entries(entries, length);
179 /* Dirt trick to make sure that cleanup code for
180 * timezone+location is executed 8-)
182 if (flag)
183 gcal_destroy(ptr_gcal);
184 fail_if(flag, msg);
187 END_TEST
190 START_TEST (test_query_contact)
192 int result, flag = 0;
193 struct gcal_contact contact, updated;
194 struct gcal_resource *obj_gcal = NULL;
195 size_t count;
196 struct gcal_contact *contacts = NULL;
197 char *msg = 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");
220 if (result == -1) {
221 msg = "Failed querying for updated contacts!";
222 flag = 1;
223 goto cleanup;
226 contacts = gcal_get_all_contacts(obj_gcal, &count);
227 if(contacts == NULL) {
228 msg = "Failed extracting the contacts vector!";
229 flag = 1;
230 goto cleanup;
233 /* Google contacts *dont* display deleted contacts by default */
234 if(count > 1) {
235 msg = "Query returned inconsistent results!";
236 flag = 1;
237 goto cleanup;
240 cleanup:
242 gcal_delete_contact(obj_gcal, &updated);
243 gcal_destroy_contact(&updated);
244 gcal_destroy_contacts(contacts, count);
245 gcal_destroy(obj_gcal);
247 fail_if(flag, msg);
249 END_TEST
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;
258 size_t count = 0;
259 char *msg = 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'
279 * unit test).
281 gcal_deleted(obj_gcal, SHOW);
282 result = gcal_query_updated(obj_gcal, NULL, "GData-Version: 3.0");
283 if (result == -1) {
284 msg = "Failed querying for updated contacts!";
285 flag = 1;
286 goto cleanup;
289 contacts = gcal_get_all_contacts(obj_gcal, &count);
290 if((count < 1) || (contacts == NULL)) {
291 msg = "Query didn't return deleted contacts!";
292 flag = 1;
293 goto cleanup;
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");
300 if (result == -1) {
301 msg = "Failed querying for updated contacts!";
302 flag = 1;
303 goto cleanup;
306 contacts = gcal_get_all_contacts(obj_gcal, &count);
307 if(count > 1) {
308 msg = "Query returned inconsistent results!";
309 flag = 1;
310 goto cleanup;
314 cleanup:
316 gcal_destroy_contacts(contacts, count);
317 gcal_destroy(obj_gcal);
319 fail_if(flag, msg);
321 END_TEST
323 START_TEST (test_query_generic)
326 int result, flag = 0;
327 struct gcal_contact *contacts = NULL;
328 struct gcal_resource *obj_gcal = NULL;
329 size_t count = 0;
330 char *msg = 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!";
343 flag = 1;
344 goto cleanup;
347 contacts = gcal_get_all_contacts(obj_gcal, &count);
348 if((count < 1) || (contacts == NULL)) {
349 msg = "Query returned more contacts!";
350 flag = 1;
351 goto cleanup;
354 cleanup:
356 gcal_destroy_contacts(contacts, count);
357 gcal_destroy(obj_gcal);
359 fail_if(flag, msg);
363 END_TEST
365 TCase *gcal_query_tcase_create(void)
368 TCase *tc = NULL;
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);
380 return tc;