fix coding rule violations
[platform/core/pim/contacts-service.git] / test / test_query.c
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the License);
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <string.h>
20 #include "contacts.h"
21 #include "test_debug.h"
22 #include "test_main.h"
23 #include "test_utils.h"
24
25 #define TEST_CONTACT_COUNT 2
26
27 enum {
28         TEST_URI_PERSON_CONTACT,
29         TEST_URI_PERSON_GROUPREL,
30         TEST_URI_PERSON_GROUP_ASSIGNED,
31         TEST_URI_PERSON_GROUP_NOT_ASSIGNED,
32         TEST_URI_PERSON_NUMBER,
33         TEST_URI_PERSON_EMAIL,
34         TEST_URI_PERSON,
35 };
36
37 static const char *uris[] = {
38         "tizen.contacts_view.person/simple_contact",
39         "tizen.contacts_view.person/simple_contact/number",
40         "tizen.contacts_view.person/simple_contact/email",
41         "tizen.contacts_view.person/simple_contact/group",
42         "tizen.contacts_view.person/simple_contact/group_assigned",
43         "tizen.contacts_view.person/simple_contact/group_not_assigned",
44         "tizen.contacts_view.person",
45 };
46
47 static const char *keywords[] = {
48         "gildo",
49         "123", "ㅎㄱ", "ㄱㄷ", "music", "Seoul",
50         "SEOUL", "run", "tiz", "plat", "hong",
51         "+", " ",
52 };
53
54
55
56 static contacts_record_h _get_name(const char *first, const char *last)
57 {
58         contacts_record_h name = NULL;
59         contacts_record_create(_contacts_name._uri, &name);
60         contacts_record_set_str(name, _contacts_name.first, first);
61         contacts_record_set_str(name, _contacts_name.last, last);
62         return name;
63 }
64
65 static contacts_record_h _get_number(const char *phone_number)
66 {
67         contacts_record_h number = NULL;
68         contacts_record_create(_contacts_number._uri, &number);
69         contacts_record_set_str(number, _contacts_number.number, phone_number);
70         return number;
71 }
72
73 static contacts_record_h _get_email(int type, const char *email_address)
74 {
75         contacts_record_h email = NULL;
76         contacts_record_create(_contacts_email._uri, &email);
77         contacts_record_set_int(email, _contacts_email.type, type);
78         contacts_record_set_str(email, _contacts_email.email, email_address);
79         return email;
80 }
81
82 static contacts_record_h _get_nickname(const char *name)
83 {
84         contacts_record_h nickname = NULL;
85         contacts_record_create(_contacts_nickname._uri, &nickname);
86         contacts_record_set_str(nickname, _contacts_nickname.name, name);
87         return nickname;
88 }
89
90 static contacts_record_h _get_address(const char *street, const char *locality,
91                 const char *region, const char *country)
92 {
93         contacts_record_h address = NULL;
94         contacts_record_create(_contacts_address._uri, &address);
95         contacts_record_set_str(address, _contacts_address.street, street);
96         contacts_record_set_str(address, _contacts_address.locality, locality);
97         contacts_record_set_str(address, _contacts_address.region, region);
98         contacts_record_set_str(address, _contacts_address.country, country);
99         return address;
100 }
101
102 static contacts_record_h _get_note(const char *contents)
103 {
104         contacts_record_h note = NULL;
105         contacts_record_create(_contacts_note._uri, &note);
106         contacts_record_set_str(note, _contacts_note.note, contents);
107         return note;
108 }
109
110 static contacts_record_h _get_company(const char *department, const char *name,
111                 const char *role)
112 {
113         contacts_record_h company = NULL;
114         contacts_record_create(_contacts_company._uri, &company);
115         contacts_record_set_str(company, _contacts_company.department, department);
116         contacts_record_set_str(company, _contacts_company.name, name);
117         contacts_record_set_str(company, _contacts_company.role, role);
118         return company;
119 }
120
121 int _insert_contact_01(int *id)
122 {
123         int contact_id = 0;
124         contacts_record_h contact = NULL;
125         contacts_record_create(_contacts_contact._uri, &contact);
126         contacts_record_add_child_record(contact, _contacts_contact.name,
127                         _get_name("홍길동", "korea123"));
128         contacts_record_add_child_record(contact, _contacts_contact.number,
129                         _get_number("010-1234-1234"));
130         contacts_record_add_child_record(contact, _contacts_contact.email,
131                         _get_email(CONTACTS_EMAIL_TYPE_HOME, "gildong.hong@music.com"));
132         contacts_record_add_child_record(contact, _contacts_contact.nickname,
133                         _get_nickname("super"));
134         contacts_record_add_child_record(contact, _contacts_contact.address,
135                         _get_address("hongic Univ", "mapo", "Seoul", "Korea"));
136         contacts_record_add_child_record(contact, _contacts_contact.note,
137                         _get_note("running"));
138         contacts_record_add_child_record(contact, _contacts_contact.company,
139                         _get_company("Platform Lab", "tizen", "PIMS"));
140         contacts_db_insert_record(contact, &contact_id);
141         contacts_record_destroy(contact, true);
142         DBG("contact_id(%d)", contact_id);
143
144         if (id)
145                 *id = contact_id;
146
147         return 0;
148 }
149
150 int _insert_contact_02(int *id)
151 {
152         int contact_id = 0;
153         contacts_record_h contact = NULL;
154         contacts_record_create(_contacts_contact._uri, &contact);
155         contacts_record_add_child_record(contact, _contacts_contact.name,
156                         _get_name("AlpaGo", "pro9"));
157         contacts_record_add_child_record(contact, _contacts_contact.number,
158                         _get_number("+851034123412"));
159         contacts_record_add_child_record(contact, _contacts_contact.email,
160                         _get_email(CONTACTS_EMAIL_TYPE_HOME, "alpago@baduk.com"));
161         contacts_record_add_child_record(contact, _contacts_contact.nickname,
162                         _get_nickname("prime"));
163         contacts_record_add_child_record(contact, _contacts_contact.address,
164                         _get_address("broad", "young123", "Gyoung", "Korea"));
165         contacts_record_add_child_record(contact, _contacts_contact.note,
166                         _get_note("day123"));
167         contacts_record_add_child_record(contact, _contacts_contact.company,
168                         _get_company("customer", "new123", "role"));
169         contacts_db_insert_record(contact, &contact_id);
170         contacts_record_destroy(contact, true);
171         DBG("contact_id(%d)", contact_id);
172
173         if (id)
174                 *id = contact_id;
175
176         return 0;
177 }
178
179
180 static void _delete_contact(int contact_id)
181 {
182         contacts_db_delete_record(_contacts_contact._uri, contact_id);
183 }
184
185 static void _get_query_on_uri(const char *uri, int limit, int offset, contacts_query_h *out_query)
186 {
187         contacts_query_h query = NULL;
188         contacts_query_create(uri, &query);
189
190         contacts_filter_h filter = NULL;
191         contacts_filter_create(uri, &filter);
192
193
194         if (0 == strcmp(uri, _contacts_person_contact._uri)) {
195                 contacts_filter_add_int(filter, _contacts_person_contact.person_id,
196                                 CONTACTS_MATCH_GREATER_THAN, 0);
197         } else if (0 == strcmp(uri, _contacts_person_grouprel._uri)) {
198                 contacts_filter_add_int(filter, _contacts_person_grouprel.link_count,
199                                 CONTACTS_MATCH_GREATER_THAN, 0);
200         } else if (0 == strcmp(uri, _contacts_person_group_assigned._uri)) {
201                 contacts_filter_add_int(filter, _contacts_person_group_assigned.person_id,
202                                 CONTACTS_MATCH_GREATER_THAN, 0);
203         } else if (0 == strcmp(uri, _contacts_person_group_not_assigned._uri)) {
204                 contacts_filter_add_int(filter, _contacts_person_group_not_assigned.link_count,
205                                 CONTACTS_MATCH_GREATER_THAN, 0);
206         } else if (0 == strcmp(uri, _contacts_person_number._uri)) {
207                 contacts_filter_add_int(filter, _contacts_person_number.person_id,
208                                 CONTACTS_MATCH_GREATER_THAN, 0);
209         } else if (0 == strcmp(uri, _contacts_person_email._uri)) {
210                 contacts_filter_add_int(filter, _contacts_person_email.person_id,
211                                 CONTACTS_MATCH_GREATER_THAN, 0);
212         } else if (0 == strcmp(uri, _contacts_person._uri)) {
213                 contacts_filter_add_int(filter, _contacts_person.link_count,
214                                 CONTACTS_MATCH_GREATER_THAN, 0);
215         } else {
216                 ERR("Invalid uri[%s]", uri);
217                 return;
218         }
219
220         contacts_query_set_filter(query, filter);
221         contacts_filter_destroy(filter);
222
223         if (out_query)
224                 *out_query = query;
225 }
226
227 static int _check_list(const char *uri, contacts_list_h in_list, bool is_snippet)
228 {
229         if (NULL == in_list) {
230                 ERR("NO list");
231                 return 0;
232         }
233
234         int uri_type = -1;
235         if (0 == strcmp(uri, _contacts_person_contact._uri)) {
236                 uri_type = TEST_URI_PERSON_CONTACT;
237         } else if (0 == strcmp(uri, _contacts_person_grouprel._uri)) {
238                 uri_type = TEST_URI_PERSON_GROUPREL;
239         } else if (0 == strcmp(uri, _contacts_person_group_assigned._uri)) {
240                 uri_type = TEST_URI_PERSON_GROUP_ASSIGNED;
241         } else if (0 == strcmp(uri, _contacts_person_group_not_assigned._uri)) {
242                 uri_type = TEST_URI_PERSON_GROUP_NOT_ASSIGNED;
243         } else if (0 == strcmp(uri, _contacts_person_number._uri)) {
244                 uri_type = TEST_URI_PERSON_NUMBER;
245         } else if (0 == strcmp(uri, _contacts_person_email._uri)) {
246                 uri_type = TEST_URI_PERSON_EMAIL;
247         } else if (0 == strcmp(uri, _contacts_person._uri)) {
248                 uri_type = TEST_URI_PERSON;
249         } else {
250                 ERR("Invalid uri[%s]", uri);
251                 return 0;
252         }
253
254         int count = 0;
255         contacts_list_get_count(in_list, &count);
256         DBG("count(%d)", count);
257
258         contacts_list_first(in_list);
259         contacts_record_h record = NULL;
260         while (CONTACTS_ERROR_NONE == contacts_list_get_current_record_p(in_list, &record)) {
261                 int contact_id = 0;
262                 char *display_name = NULL;
263                 char *number = NULL;
264                 int snippet_type = NULL;
265                 char *snippet_string = NULL;
266
267                 switch (uri_type) {
268                 case TEST_URI_PERSON_CONTACT:
269                         contacts_record_get_int(record, _contacts_person_contact.contact_id, &contact_id);
270                         contacts_record_get_str_p(record, _contacts_person_contact.display_name, &display_name);
271                         if (false == is_snippet)
272                                 break;
273
274                         contacts_record_get_int(record, _contacts_person_contact.snippet_type, &snippet_type);
275                         contacts_record_get_str_p(record, _contacts_person_contact.snippet_string, &snippet_string);
276                         break;
277                 case TEST_URI_PERSON_GROUPREL:
278                         contacts_record_get_int(record, _contacts_person_grouprel.contact_id, &contact_id);
279                         contacts_record_get_str_p(record, _contacts_person_grouprel.display_name, &display_name);
280                         if (false == is_snippet)
281                                 break;
282
283                         contacts_record_get_int(record, _contacts_person_grouprel.snippet_type, &snippet_type);
284                         contacts_record_get_str_p(record, _contacts_person_grouprel.snippet_string, &snippet_string);
285                         break;
286                 case TEST_URI_PERSON_GROUP_ASSIGNED:
287                         contacts_record_get_int(record, _contacts_person_group_assigned.contact_id, &contact_id);
288                         contacts_record_get_str_p(record, _contacts_person_group_assigned.display_name, &display_name);
289                         if (false == is_snippet)
290                                 break;
291
292                         contacts_record_get_int(record, _contacts_person_group_assigned.snippet_type, &snippet_type);
293                         contacts_record_get_str_p(record, _contacts_person_group_assigned.snippet_string, &snippet_string);
294                         break;
295                 case TEST_URI_PERSON_GROUP_NOT_ASSIGNED:
296                         contacts_record_get_int(record, _contacts_person_group_not_assigned.contact_id, &contact_id);
297                         contacts_record_get_str_p(record, _contacts_person_group_not_assigned.display_name, &display_name);
298                         if (false == is_snippet)
299                                 break;
300
301                         contacts_record_get_int(record, _contacts_person_group_not_assigned.snippet_type, &snippet_type);
302                         contacts_record_get_str_p(record, _contacts_person_group_not_assigned.snippet_string, &snippet_string);
303                         break;
304                 case TEST_URI_PERSON_NUMBER:
305                         contacts_record_get_int(record, _contacts_person_number.display_contact_id, &contact_id);
306                         contacts_record_get_str_p(record, _contacts_person_number.display_name, &display_name);
307                         contacts_record_get_str_p(record, _contacts_person_number.number, &number);
308                         if (false == is_snippet)
309                                 break;
310
311                         contacts_record_get_int(record, _contacts_person_number.snippet_type, &snippet_type);
312                         contacts_record_get_str_p(record, _contacts_person_number.snippet_string, &snippet_string);
313                         break;
314                 case TEST_URI_PERSON_EMAIL:
315                         contacts_record_get_int(record, _contacts_person_email.display_contact_id, &contact_id);
316                         contacts_record_get_str_p(record, _contacts_person_email.display_name, &display_name);
317                         if (false == is_snippet)
318                                 break;
319
320                         contacts_record_get_int(record, _contacts_person_email.snippet_type, &snippet_type);
321                         contacts_record_get_str_p(record, _contacts_person_email.snippet_string, &snippet_string);
322                         break;
323                 case TEST_URI_PERSON:
324                         contacts_record_get_int(record, _contacts_person.display_contact_id, &contact_id);
325                         contacts_record_get_str_p(record, _contacts_person.display_name, &display_name);
326                         if (false == is_snippet)
327                                 break;
328
329                         contacts_record_get_int(record, _contacts_person.snippet_type, &snippet_type);
330                         contacts_record_get_str_p(record, _contacts_person.snippet_string, &snippet_string);
331                         break;
332                 }
333                 DBG("id(%d) name[%s] |%d|%s|", contact_id, display_name, snippet_type, snippet_string);
334
335                 contacts_list_next(in_list);
336         }
337
338         return 0;
339 }
340
341 static int test_get_records(int argc, char **argv)
342 {
343         DBG("[%s]", __func__);
344
345         int i;
346         int i_count = sizeof(uris)/sizeof(uris[0]);
347
348         const char *uri = NULL;
349         int offset = 0;
350         int limit = 0;
351
352         if (3 < argc)
353                 i_count = 1;
354
355         contacts_connect();
356
357         int ids[TEST_CONTACT_COUNT] = {0};
358         _insert_contact_01(&ids[0]);
359         _insert_contact_02(&ids[1]);
360
361         for (i = 0; i < i_count; i++) {
362                 uri = 3 < argc ? argv[3] : uris[i];
363                 DBG("uri[%s]", uri);
364
365                 contacts_list_h get_list = NULL;
366
367                 DBG("search_records");
368                 contacts_db_get_all_records(uri, offset, limit, &get_list);
369                 _check_list(uri, get_list, false);
370                 contacts_list_destroy(get_list, true);
371                 get_list = NULL;
372
373                 DBG("search_records_with_query");
374                 contacts_query_h query = NULL;
375                 _get_query_on_uri(uri, limit, offset, &query);
376                 contacts_db_get_records_with_query(query, offset, limit, &get_list);
377                 _check_list(uri, get_list, false);
378                 contacts_list_destroy(get_list, true);
379                 get_list = NULL;
380
381                 contacts_query_destroy(query);
382         }
383
384         for (i = 0; i < TEST_CONTACT_COUNT; i++)
385                 _delete_contact(ids[i]);
386
387         contacts_disconnect();
388         return 0;
389 }
390
391 static int test_search_records(int argc, char **argv)
392 {
393         DBG("[%s]", __func__);
394
395         int i, j;
396         int i_count = sizeof(uris)/sizeof(uris[0]);
397         int j_count = sizeof(keywords)/sizeof(keywords[0]);
398
399         const char *uri = NULL;
400         const char *keyword = NULL;
401         int offset = 0;
402         int limit = 0;
403
404         if (3 < argc) {
405                 i_count = 1;
406                 j_count = 1;
407         }
408
409         contacts_connect();
410
411         int ids[TEST_CONTACT_COUNT] = {0};
412         _insert_contact_01(&ids[0]);
413         _insert_contact_02(&ids[1]);
414
415         for (i = 0; i < i_count; i++) {
416                 uri = 3 < argc ? argv[3] : uris[i];
417                 DBG("uri[%s]", uri);
418
419                 for (j = 0; j < j_count; j++) {
420                         keyword = 4 < argc ? argv[4] : keywords[j];
421                         DBG("keyword[%s]", keyword);
422
423                         contacts_list_h get_list = NULL;
424
425                         DBG("search_records");
426                         contacts_db_search_records(uri, keyword, offset, limit, &get_list);
427                         _check_list(uri, get_list, false);
428                         contacts_list_destroy(get_list, true);
429                         get_list = NULL;
430
431                         DBG("search_records_with_range:CONTACTS_SEARCH_RANGE_NAME");
432                         contacts_db_search_records_with_range(uri, keyword, offset, limit,
433                                         CONTACTS_SEARCH_RANGE_NAME, &get_list);
434                         _check_list(uri, get_list, false);
435                         contacts_list_destroy(get_list, true);
436                         get_list = NULL;
437
438                         DBG("search_records_with_range:CONTACTS_SEARCH_RANGE_NUMBER");
439                         contacts_db_search_records_with_range(uri, keyword, offset, limit,
440                                         CONTACTS_SEARCH_RANGE_NUMBER, &get_list);
441                         _check_list(uri, get_list, false);
442                         contacts_list_destroy(get_list, true);
443                         get_list = NULL;
444
445                         DBG("search_records_with_range:CONTACTS_SEARCH_RANGE_DATA");
446                         contacts_db_search_records_with_range(uri, keyword, offset, limit,
447                                         CONTACTS_SEARCH_RANGE_DATA, &get_list);
448                         _check_list(uri, get_list, false);
449                         contacts_list_destroy(get_list, true);
450                         get_list = NULL;
451
452                         DBG("search_records_with_query");
453                         contacts_query_h query = NULL;
454                         _get_query_on_uri(uri, limit, offset, &query);
455                         contacts_db_search_records_with_query(query, keyword, offset, limit,
456                                         &get_list);
457                         _check_list(uri, get_list, false);
458                         contacts_list_destroy(get_list, true);
459                         get_list = NULL;
460
461                         contacts_query_destroy(query);
462                 }
463         }
464
465         for (i = 0; i < TEST_CONTACT_COUNT; i++)
466                 _delete_contact(ids[i]);
467
468         contacts_disconnect();
469         return 0;
470 }
471
472 /*
473  * /usr/bin//contacts-service-test 1 2 {uri} {keyword}
474  */
475 static int test_search_records_for_snippet(int argc, char **argv)
476 {
477         DBG("[%s]", __func__);
478
479         int i, j;
480         int i_count = sizeof(uris)/sizeof(uris[0]);
481         int j_count = sizeof(keywords)/sizeof(keywords[0]);
482
483         const char *uri = NULL;
484         const char *keyword = NULL;
485         char *start_match = "[";
486         char *end_match = "]";
487         int offset = 0;
488         int limit = 0;
489
490         if (3 < argc) {
491                 i_count = 1;
492                 j_count = 1;
493         }
494
495         contacts_connect();
496
497         int ids[TEST_CONTACT_COUNT] = {0};
498         _insert_contact_01(&ids[0]);
499         _insert_contact_02(&ids[1]);
500
501         for (i = 0; i < i_count; i++) {
502                 uri = 3 < argc ? argv[3] : uris[i];
503                 DBG(">>>>>   uri[%s]", uri);
504
505                 for (j = 0; j < j_count; j++) {
506                         keyword = 4 < argc ? argv[4] : keywords[j];
507                         DBG("keyword [%s]", keyword);
508
509                         contacts_list_h get_list = NULL;
510
511                         DBG("search_records_for_snippet");
512                         contacts_db_search_records_for_snippet(uri, keyword, offset, limit,
513                                         start_match, end_match, 0, &get_list);
514                         _check_list(uri, get_list, true);
515                         contacts_list_destroy(get_list, true);
516                         get_list = NULL;
517
518                         DBG("search_records_with_range_for_snippet:CONTACTS_SEARCH_RANGE_NAME");
519                         contacts_db_search_records_with_range_for_snippet(uri, keyword, offset,
520                                         limit, CONTACTS_SEARCH_RANGE_NAME, start_match, end_match, 0, &get_list);
521                         _check_list(uri, get_list, true);
522                         contacts_list_destroy(get_list, true);
523                         get_list = NULL;
524
525                         DBG("search_records_with_range_for_snippet:CONTACTS_SEARCH_RANGE_NUMBER");
526                         contacts_db_search_records_with_range_for_snippet(uri, keyword, offset,
527                                         limit, CONTACTS_SEARCH_RANGE_NUMBER, start_match, end_match, 0, &get_list);
528                         _check_list(uri, get_list, true);
529                         contacts_list_destroy(get_list, true);
530                         get_list = NULL;
531
532                         DBG("search_records_with_range_for_snippet:CONTACTS_SEARCH_RANGE_DATA");
533                         contacts_db_search_records_with_range_for_snippet(uri, keyword, offset,
534                                         limit, CONTACTS_SEARCH_RANGE_DATA, start_match, end_match, 0, &get_list);
535                         _check_list(uri, get_list, true);
536                         contacts_list_destroy(get_list, true);
537                         get_list = NULL;
538
539                         DBG("search_records_with_query_for_snippet");
540                         contacts_query_h query = NULL;
541                         _get_query_on_uri(uri, limit, offset, &query);
542                         contacts_db_search_records_with_query_for_snippet(query, keyword, offset,
543                                         limit, start_match, end_match, 0, &get_list);
544                         _check_list(uri, get_list, true);
545                         contacts_list_destroy(get_list, true);
546                         get_list = NULL;
547                         contacts_query_destroy(query);
548                 }
549         }
550
551         for (i = 0; i < TEST_CONTACT_COUNT; i++)
552                 _delete_contact(ids[i]);
553
554         contacts_disconnect();
555         return 0;
556 }
557
558 static const func _func[] = {
559         test_get_records,
560         test_search_records,
561         test_search_records_for_snippet,
562 };
563
564 int test_query(int argc, char **argv)
565 {
566         ENTER();
567
568         int count = sizeof(_func) / sizeof(func);
569         if (true == test_main_is_selected(argc, argv, 2, _func, count))
570                 return 0;
571
572         int i = 0;
573         for (i = 0; i < count; i++) {
574                 test_utils_cleanup();
575                 if (_func[i](argc, argv) < 0)
576                         break;
577         }
578         return 0;
579 }
580