summaryrefslogtreecommitdiff
path: root/src/addressbook/libedata-book/e-book-backend-sqlitedb-test.c
blob: 2a957e9fe101c2eb203e1a8f7f9acec83d7d8a6a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/*-*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/* e-book-backend-sqlitedb.c
 *
 * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
 *
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
 * for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 *
 * Authors: Chenthill Palanisamy <pchenthill@novell.com>
 */

#include "evolution-data-server-config.h"

#include <libebook-contacts/libebook-contacts.h>
#include "e-book-backend-sqlitedb.h"

static GMainLoop *main_loop;
static gchar *cache_path;
const gchar *op;
GError *error;

const gchar *email="test@localhost";
const gchar *folderid = "test_folder_id";
const gchar *folder_name = "test_folder";
const gchar *uid ="pas-id-4DCB9FF200000000";

const gchar *vcard_str =
"BEGIN:VCARD\n"
"VERSION:3.0\n"
"URL:test.com\n"
"TITLE:\n"
"ROLE:\n"
"X-EVOLUTION-MANAGER:\n"
"X-EVOLUTION-ASSISTANT:\n"
"NICKNAME:\n"
"X-EVOLUTION-SPOUSE:\n"
"NOTE:\n"
"FN:test\n"
"N:;test;;;\n"
"X-EVOLUTION-BLOG-URL:test.wordpress.com\n"
"CALURI:\n"
"FBURL:\n"
"X-EVOLUTION-VIDEO-URL:\n"
"X-MOZILLA-HTML:FALSE\n"
"X-EVOLUTION-FILE-AS:test\n"
"EMAIL;X-EVOLUTION-UI-SLOT=1;TYPE=WORK:test@localhost.com\n"
"EMAIL;X-EVOLUTION-UI-SLOT=2;TYPE=HOME:test@localhome.com\n"
"UID:pas-id-4DCB9FF200000000\n"
"REV:2011-05-12T08:53:06Z\n"
"END:VCARD";

static void
quit_tests (void)
{

	if (error != NULL) {
		g_print ("Tests failed: %s - %s \n", op, error->message);
		g_clear_error (&error);
	}

	g_main_loop_quit (main_loop);
}

static void
add_contacts (EBookBackendSqliteDB *ebsdb)
{
	GSList *contacts = NULL;
	EContact *con;

	g_print ("Adding contact \n");
	op = "add contact";

	con = e_contact_new_from_vcard (vcard_str);
	contacts = g_slist_append (contacts, con);
	e_book_backend_sqlitedb_add_contacts (ebsdb, folderid, contacts, FALSE, &error);

	g_object_unref (con);
}

static void
search_db (EBookBackendSqliteDB *ebsdb,
           const gchar *type,
           const gchar *sexp)
{
	GSList *vcards;
	EbSdbSearchData *s_data;

	g_print ("%s - query: %s \n", type, sexp);
	op = type;
	vcards = e_book_backend_sqlitedb_search (ebsdb, folderid, sexp, NULL, NULL, NULL, &error);
	if (error || !vcards)
		return;

	s_data = vcards->data;
	g_print ("Result: %s \n", s_data->vcard);
	e_book_backend_sqlitedb_search_data_free (s_data);
}

static gboolean
start_tests (gpointer data)
{
	EBookBackendSqliteDB *ebsdb;
	gboolean populated = FALSE;
	gchar *vcard_str = NULL, *sexp;
	EBookQuery *q;
	GSList *uids = NULL;

	g_print ("Creating the sqlitedb \n");
	op = "create sqlitedb";
	ebsdb = e_book_backend_sqlitedb_new
					(cache_path, email, folderid, folder_name,
					 FALSE, &error);
	if (error)
		goto exit;

	add_contacts (ebsdb);
	if (error)
		goto exit;

	g_print ("Getting is_populated \n");
	op = "set is_populated";
	e_book_backend_sqlitedb_set_is_populated (ebsdb, folderid, TRUE, &error);
	if (error)
		goto exit;

	g_print ("Setting is_populated \n");
	op = "set is_populated";
	populated = e_book_backend_sqlitedb_get_is_populated (ebsdb, folderid, &error);
	if (error)
		goto exit;
	g_print ("Populated: %d \n", populated);

	g_print ("Setting key value \n");
	op = "set key/value";
	e_book_backend_sqlitedb_set_key_value (ebsdb, folderid, "customkey", "stored", &error);
	if (error)
		goto exit;

	g_print ("Get Vcard string \n");
	op = "get vcard string";
	vcard_str = e_book_backend_sqlitedb_get_vcard_string (ebsdb, folderid, uid, NULL, NULL, &error);
	if (error)
		goto exit;
	g_print ("VCard: %s \n", vcard_str);
	g_free (vcard_str);

	q = e_book_query_field_test (E_CONTACT_FULL_NAME, E_BOOK_QUERY_CONTAINS, "test");
	sexp = e_book_query_to_string (q);
	search_db (ebsdb, "summary query", sexp);
	e_book_query_unref (q);
	g_free (sexp);
	if (error)
		goto exit;

	/* if (store_vcard) {
		q = e_book_query_any_field_contains ("word");
		sexp = e_book_query_to_string (q);
		search_db (ebsdb, "full_search query", sexp);
		e_book_query_unref (q);
		g_free (sexp);
		if (error)
			goto exit;
	} */

	g_print ("Delete contact \n");
	op = "delete contact";
	uids = g_slist_append (uids, (gchar *) uid);
	e_book_backend_sqlitedb_remove_contacts (ebsdb, folderid, uids, &error);
	g_slist_free (uids);
	if (error)
		goto exit;

	g_print ("Delete addressbook \n");
	op = "delete addressbook";
	e_book_backend_sqlitedb_delete_addressbook (ebsdb, folderid, &error);

exit:
	g_object_unref (ebsdb);
	quit_tests ();
	return FALSE;
}

gint
main (gint argc,
      gchar *argv[])
{
	if (argc != 2) {
		g_print ("Please enter a path to store the cache \n");
		return -1;
	}

	cache_path = argv[1];

	main_loop = g_main_loop_new (NULL, TRUE);
	g_idle_add ((GSourceFunc) start_tests, NULL);
	g_main_loop_run (main_loop);

	/* terminate */
	g_main_loop_unref (main_loop);

	return 0;
}