Merge "SVACE issue fix" into tizen
[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                 contacts_filter_destroy(filter);
218                 contacts_query_destroy(query);
219                 return;
220         }
221
222         contacts_query_set_filter(query, filter);
223         contacts_filter_destroy(filter);
224
225         if (out_query)
226                 *out_query = query;
227 }
228
229 static int _check_list(const char *uri, contacts_list_h in_list, bool is_snippet)
230 {
231         if (NULL == in_list) {
232                 ERR("NO list");
233                 return 0;
234         }
235
236         int uri_type = -1;
237         if (0 == strcmp(uri, _contacts_person_contact._uri)) {
238                 uri_type = TEST_URI_PERSON_CONTACT;
239         } else if (0 == strcmp(uri, _contacts_person_grouprel._uri)) {
240                 uri_type = TEST_URI_PERSON_GROUPREL;
241         } else if (0 == strcmp(uri, _contacts_person_group_assigned._uri)) {
242                 uri_type = TEST_URI_PERSON_GROUP_ASSIGNED;
243         } else if (0 == strcmp(uri, _contacts_person_group_not_assigned._uri)) {
244                 uri_type = TEST_URI_PERSON_GROUP_NOT_ASSIGNED;
245         } else if (0 == strcmp(uri, _contacts_person_number._uri)) {
246                 uri_type = TEST_URI_PERSON_NUMBER;
247         } else if (0 == strcmp(uri, _contacts_person_email._uri)) {
248                 uri_type = TEST_URI_PERSON_EMAIL;
249         } else if (0 == strcmp(uri, _contacts_person._uri)) {
250                 uri_type = TEST_URI_PERSON;
251         } else {
252                 ERR("Invalid uri[%s]", uri);
253                 return 0;
254         }
255
256         int count = 0;
257         contacts_list_get_count(in_list, &count);
258         DBG("count(%d)", count);
259
260         contacts_list_first(in_list);
261         contacts_record_h record = NULL;
262         while (CONTACTS_ERROR_NONE == contacts_list_get_current_record_p(in_list, &record)) {
263                 int contact_id = 0;
264                 char *display_name = NULL;
265                 char *number = NULL;
266                 int snippet_type = 0;
267                 char *snippet_string = NULL;
268
269                 switch (uri_type) {
270                 case TEST_URI_PERSON_CONTACT:
271                         contacts_record_get_int(record, _contacts_person_contact.contact_id, &contact_id);
272                         contacts_record_get_str_p(record, _contacts_person_contact.display_name, &display_name);
273                         if (false == is_snippet)
274                                 break;
275
276                         contacts_record_get_int(record, _contacts_person_contact.snippet_type, &snippet_type);
277                         contacts_record_get_str_p(record, _contacts_person_contact.snippet_string, &snippet_string);
278                         break;
279                 case TEST_URI_PERSON_GROUPREL:
280                         contacts_record_get_int(record, _contacts_person_grouprel.contact_id, &contact_id);
281                         contacts_record_get_str_p(record, _contacts_person_grouprel.display_name, &display_name);
282                         if (false == is_snippet)
283                                 break;
284
285                         contacts_record_get_int(record, _contacts_person_grouprel.snippet_type, &snippet_type);
286                         contacts_record_get_str_p(record, _contacts_person_grouprel.snippet_string, &snippet_string);
287                         break;
288                 case TEST_URI_PERSON_GROUP_ASSIGNED:
289                         contacts_record_get_int(record, _contacts_person_group_assigned.contact_id, &contact_id);
290                         contacts_record_get_str_p(record, _contacts_person_group_assigned.display_name, &display_name);
291                         if (false == is_snippet)
292                                 break;
293
294                         contacts_record_get_int(record, _contacts_person_group_assigned.snippet_type, &snippet_type);
295                         contacts_record_get_str_p(record, _contacts_person_group_assigned.snippet_string, &snippet_string);
296                         break;
297                 case TEST_URI_PERSON_GROUP_NOT_ASSIGNED:
298                         contacts_record_get_int(record, _contacts_person_group_not_assigned.contact_id, &contact_id);
299                         contacts_record_get_str_p(record, _contacts_person_group_not_assigned.display_name, &display_name);
300                         if (false == is_snippet)
301                                 break;
302
303                         contacts_record_get_int(record, _contacts_person_group_not_assigned.snippet_type, &snippet_type);
304                         contacts_record_get_str_p(record, _contacts_person_group_not_assigned.snippet_string, &snippet_string);
305                         break;
306                 case TEST_URI_PERSON_NUMBER:
307                         contacts_record_get_int(record, _contacts_person_number.display_contact_id, &contact_id);
308                         contacts_record_get_str_p(record, _contacts_person_number.display_name, &display_name);
309                         contacts_record_get_str_p(record, _contacts_person_number.number, &number);
310                         if (false == is_snippet)
311                                 break;
312
313                         contacts_record_get_int(record, _contacts_person_number.snippet_type, &snippet_type);
314                         contacts_record_get_str_p(record, _contacts_person_number.snippet_string, &snippet_string);
315                         break;
316                 case TEST_URI_PERSON_EMAIL:
317                         contacts_record_get_int(record, _contacts_person_email.display_contact_id, &contact_id);
318                         contacts_record_get_str_p(record, _contacts_person_email.display_name, &display_name);
319                         if (false == is_snippet)
320                                 break;
321
322                         contacts_record_get_int(record, _contacts_person_email.snippet_type, &snippet_type);
323                         contacts_record_get_str_p(record, _contacts_person_email.snippet_string, &snippet_string);
324                         break;
325                 case TEST_URI_PERSON:
326                         contacts_record_get_int(record, _contacts_person.display_contact_id, &contact_id);
327                         contacts_record_get_str_p(record, _contacts_person.display_name, &display_name);
328                         if (false == is_snippet)
329                                 break;
330
331                         contacts_record_get_int(record, _contacts_person.snippet_type, &snippet_type);
332                         contacts_record_get_str_p(record, _contacts_person.snippet_string, &snippet_string);
333                         break;
334                 }
335                 DBG("id(%d) name[%s] |%d|%s|", contact_id, display_name, snippet_type, snippet_string);
336
337                 contacts_list_next(in_list);
338         }
339
340         return 0;
341 }
342
343 static int test_get_records(int argc, char **argv)
344 {
345         DBG("[%s]", __func__);
346
347         int i;
348         int i_count = sizeof(uris)/sizeof(uris[0]);
349
350         const char *uri = NULL;
351         int offset = 0;
352         int limit = 0;
353
354         if (3 < argc)
355                 i_count = 1;
356
357         contacts_connect();
358
359         int ids[TEST_CONTACT_COUNT] = {0};
360         _insert_contact_01(&ids[0]);
361         _insert_contact_02(&ids[1]);
362
363         for (i = 0; i < i_count; i++) {
364                 uri = 3 < argc ? argv[3] : uris[i];
365                 DBG("uri[%s]", uri);
366
367                 contacts_list_h get_list = NULL;
368
369                 DBG("search_records");
370                 contacts_db_get_all_records(uri, offset, limit, &get_list);
371                 _check_list(uri, get_list, false);
372                 contacts_list_destroy(get_list, true);
373                 get_list = NULL;
374
375                 DBG("search_records_with_query");
376                 contacts_query_h query = NULL;
377                 _get_query_on_uri(uri, limit, offset, &query);
378                 contacts_db_get_records_with_query(query, offset, limit, &get_list);
379                 _check_list(uri, get_list, false);
380                 contacts_list_destroy(get_list, true);
381                 get_list = NULL;
382
383                 contacts_query_destroy(query);
384         }
385
386         for (i = 0; i < TEST_CONTACT_COUNT; i++)
387                 _delete_contact(ids[i]);
388
389         contacts_disconnect();
390         return 0;
391 }
392
393 static int test_search_records(int argc, char **argv)
394 {
395         DBG("[%s]", __func__);
396
397         int i, j;
398         int i_count = sizeof(uris)/sizeof(uris[0]);
399         int j_count = sizeof(keywords)/sizeof(keywords[0]);
400
401         const char *uri = NULL;
402         const char *keyword = NULL;
403         int offset = 0;
404         int limit = 0;
405
406         if (3 < argc) {
407                 i_count = 1;
408                 j_count = 1;
409         }
410
411         contacts_connect();
412
413         int ids[TEST_CONTACT_COUNT] = {0};
414         _insert_contact_01(&ids[0]);
415         _insert_contact_02(&ids[1]);
416
417         for (i = 0; i < i_count; i++) {
418                 uri = 3 < argc ? argv[3] : uris[i];
419                 DBG("uri[%s]", uri);
420
421                 for (j = 0; j < j_count; j++) {
422                         keyword = 4 < argc ? argv[4] : keywords[j];
423                         DBG("keyword[%s]", keyword);
424
425                         contacts_list_h get_list = NULL;
426
427                         DBG("search_records");
428                         contacts_db_search_records(uri, keyword, offset, limit, &get_list);
429                         _check_list(uri, get_list, false);
430                         contacts_list_destroy(get_list, true);
431                         get_list = NULL;
432
433                         DBG("search_records_with_range:CONTACTS_SEARCH_RANGE_NAME");
434                         contacts_db_search_records_with_range(uri, keyword, offset, limit,
435                                         CONTACTS_SEARCH_RANGE_NAME, &get_list);
436                         _check_list(uri, get_list, false);
437                         contacts_list_destroy(get_list, true);
438                         get_list = NULL;
439
440                         DBG("search_records_with_range:CONTACTS_SEARCH_RANGE_NUMBER");
441                         contacts_db_search_records_with_range(uri, keyword, offset, limit,
442                                         CONTACTS_SEARCH_RANGE_NUMBER, &get_list);
443                         _check_list(uri, get_list, false);
444                         contacts_list_destroy(get_list, true);
445                         get_list = NULL;
446
447                         DBG("search_records_with_range:CONTACTS_SEARCH_RANGE_DATA");
448                         contacts_db_search_records_with_range(uri, keyword, offset, limit,
449                                         CONTACTS_SEARCH_RANGE_DATA, &get_list);
450                         _check_list(uri, get_list, false);
451                         contacts_list_destroy(get_list, true);
452                         get_list = NULL;
453
454                         DBG("search_records_with_query");
455                         contacts_query_h query = NULL;
456                         _get_query_on_uri(uri, limit, offset, &query);
457                         contacts_db_search_records_with_query(query, keyword, offset, limit,
458                                         &get_list);
459                         _check_list(uri, get_list, false);
460                         contacts_list_destroy(get_list, true);
461                         get_list = NULL;
462
463                         contacts_query_destroy(query);
464                 }
465         }
466
467         for (i = 0; i < TEST_CONTACT_COUNT; i++)
468                 _delete_contact(ids[i]);
469
470         contacts_disconnect();
471         return 0;
472 }
473
474 /*
475  * /usr/bin//contacts-service-test 1 2 {uri} {keyword}
476  */
477 static int test_search_records_for_snippet(int argc, char **argv)
478 {
479         DBG("[%s]", __func__);
480
481         int i, j;
482         int i_count = sizeof(uris)/sizeof(uris[0]);
483         int j_count = sizeof(keywords)/sizeof(keywords[0]);
484
485         const char *uri = NULL;
486         const char *keyword = NULL;
487         char *start_match = "[";
488         char *end_match = "]";
489         int offset = 0;
490         int limit = 0;
491
492         if (3 < argc) {
493                 i_count = 1;
494                 j_count = 1;
495         }
496
497         if (contacts_connect() != CONTACTS_ERROR_NONE)
498                 DBG("contacts_connect failed \n");
499
500         int ids[TEST_CONTACT_COUNT] = {0};
501         _insert_contact_01(&ids[0]);
502         _insert_contact_02(&ids[1]);
503
504         for (i = 0; i < i_count; i++) {
505                 uri = 3 < argc ? argv[3] : uris[i];
506                 DBG(">>>>>   uri[%s]", uri);
507
508                 for (j = 0; j < j_count; j++) {
509                         keyword = 4 < argc ? argv[4] : keywords[j];
510                         DBG("keyword [%s]", keyword);
511
512                         contacts_list_h get_list = NULL;
513
514                         DBG("search_records_for_snippet");
515                         contacts_db_search_records_for_snippet(uri, keyword, offset, limit,
516                                         start_match, end_match, 0, &get_list);
517                         _check_list(uri, get_list, true);
518                         contacts_list_destroy(get_list, true);
519                         get_list = NULL;
520
521                         DBG("search_records_with_range_for_snippet:CONTACTS_SEARCH_RANGE_NAME");
522                         contacts_db_search_records_with_range_for_snippet(uri, keyword, offset,
523                                         limit, CONTACTS_SEARCH_RANGE_NAME, start_match, end_match, 0, &get_list);
524                         _check_list(uri, get_list, true);
525                         contacts_list_destroy(get_list, true);
526                         get_list = NULL;
527
528                         DBG("search_records_with_range_for_snippet:CONTACTS_SEARCH_RANGE_NUMBER");
529                         contacts_db_search_records_with_range_for_snippet(uri, keyword, offset,
530                                         limit, CONTACTS_SEARCH_RANGE_NUMBER, start_match, end_match, 0, &get_list);
531                         _check_list(uri, get_list, true);
532                         contacts_list_destroy(get_list, true);
533                         get_list = NULL;
534
535                         DBG("search_records_with_range_for_snippet:CONTACTS_SEARCH_RANGE_DATA");
536                         contacts_db_search_records_with_range_for_snippet(uri, keyword, offset,
537                                         limit, CONTACTS_SEARCH_RANGE_DATA, start_match, end_match, 0, &get_list);
538                         _check_list(uri, get_list, true);
539                         contacts_list_destroy(get_list, true);
540                         get_list = NULL;
541
542                         DBG("search_records_with_query_for_snippet");
543                         contacts_query_h query = NULL;
544                         _get_query_on_uri(uri, limit, offset, &query);
545                         contacts_db_search_records_with_query_for_snippet(query, keyword, offset,
546                                         limit, start_match, end_match, 0, &get_list);
547                         _check_list(uri, get_list, true);
548                         contacts_list_destroy(get_list, true);
549                         get_list = NULL;
550                         contacts_query_destroy(query);
551                 }
552         }
553
554         for (i = 0; i < TEST_CONTACT_COUNT; i++)
555                 _delete_contact(ids[i]);
556
557         contacts_disconnect();
558         return 0;
559 }
560
561 static const func _func[] = {
562         test_get_records,
563         test_search_records,
564         test_search_records_for_snippet,
565 };
566
567 int test_query(int argc, char **argv)
568 {
569         ENTER();
570
571         int count = sizeof(_func) / sizeof(func);
572         if (true == test_main_is_selected(argc, argv, 2, _func, count))
573                 return 0;
574
575         int i = 0;
576         for (i = 0; i < count; i++) {
577                 test_utils_cleanup();
578                 if (_func[i](argc, argv) < 0)
579                         break;
580         }
581         return 0;
582 }
583