1
# Copyright 2010 Canonical Ltd.
3
# This file is part of desktopcouch.
5
# desktopcouch is free software: you can redistribute it and/or modify
6
# it under the terms of the GNU Lesser General Public License version 3
7
# as published by the Free Software Foundation.
9
# desktopcouch is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
# GNU Lesser General Public License for more details.
14
# You should have received a copy of the GNU Lesser General Public License
15
# along with desktopcouch. If not, see <http://www.gnu.org/licenses/>.
17
# Authors: Chad Miller <chad.miller@canonical.com>
22
import string # pylint: disable=W0402
24
from unittest import TestCase
26
import desktopcouch.application.tests as test_environment
27
from desktopcouch.application import server
28
from desktopcouch.recordtypes.contacts import (
29
CONTACT_RECORD_TYPE, view, Contact)
32
FRANCES_RECORD_DATA = {
33
"record_type": CONTACT_RECORD_TYPE,
34
"first_name": "Frances",
39
"birth_date": "1918-08-23",
41
"spouse_name": "Louie",
42
"wedding_date": "1970-02-27",
47
"assistant_name": None,
51
"city": "Edwardburgh",
52
"description": "summer",
53
"street": "15 Libby Lane",
56
"postalcode": "04412"},
59
"description": "winter",
60
"street": "42 Snowbird Crawl",
63
"postalcode": "33012"},
67
"description": "home",
68
"number": "313-876-3123"},
72
"description": "home",
73
"address": "frances@blah.example.com"},
75
"description": "home",
76
"address": "berkeley!uucp!frances"},
80
"description": "blog",
81
"address": "http://frances.blag.example.com/"},
85
"address": "frances0",
86
"description": "Frances",
87
"protocol": "jabber"},)}
91
"""Create a string of particular size."""
92
return "".join(random.choice(string.lowercase) for _ in range(size))
95
def new_fake_record():
96
"""Get a fake record."""
98
"record_type": CONTACT_RECORD_TYPE,
99
"first_name": "random" + new_str(10),
101
"last_name": new_str(10),
104
"birth_date": "1970-02-13",
105
"nick_name": new_str(10),
106
"spouse_name": new_str(10),
107
"wedding_date": "1968-01-07",
108
"company": new_str(10),
109
"department": new_str(10),
111
"manager_name": None,
112
"assistant_name": None,
117
"description": new_str(10),
118
"street": new_str(10),
121
"postalcode": "04412"},
124
"description": new_str(10),
125
"street": new_str(10),
128
"postalcode": "33012"},),
131
"description": new_str(6),
132
"number": "313-876-3123"},),
135
"description": "home",
136
"address": "x" + new_str(5) + "@example.com"},),
139
"description": "blog",
140
"address": "http://other.blag.example.com/"},),
143
"address": new_str(10),
144
"description": new_str(10),
145
"protocol": "jabber"},)}
148
class TestLocalFiles(TestCase):
149
"""Test local files."""
152
super(TestLocalFiles, self).setUp()
153
self.dbname = "contacts"
154
self.db = server.DesktopDatabase(
155
self.dbname, create=True, ctx=test_environment.test_context)
156
self.db.put_record(Contact(data=FRANCES_RECORD_DATA))
157
self.db.put_records_batch(
158
[Contact(data=new_fake_record()) for _ in range(100)])
159
self.db.put_record(Contact(data={
160
"record_type": CONTACT_RECORD_TYPE,
161
"first_name": "y" + new_str(10),
163
"last_name": new_str(10),
166
"birth_date": "1970-02-13",
167
"nick_name": new_str(10),
168
"spouse_name": new_str(10),
169
"wedding_date": "1944-10-20",
170
"company": new_str(10),
171
"department": new_str(10),
173
"manager_name": None,
174
"assistant_name": None,
179
"description": new_str(10),
180
"street": new_str(10),
183
"postalcode": "04412"},
186
"description": new_str(10),
187
"street": new_str(10),
190
"postalcode": "33012"},),
193
"description": new_str(6),
194
"number": "313-876-3123"},),
197
"description": "home",
198
"address": "@example.com"},),
201
"description": "blog",
202
"address": "http://other.blag.example.com/"},),
205
"address": new_str(10),
206
"description": new_str(10),
207
"protocol": "jabber"},)}))
208
self.db.put_record(Contact(data={
209
"record_type": CONTACT_RECORD_TYPE,
210
"first_name": "y" + new_str(10),
212
"last_name": new_str(10),
215
"birth_date": "1944-06-15",
216
"nick_name": new_str(10),
217
"spouse_name": new_str(10),
218
"wedding_date": "1951-01-03",
219
"company": new_str(10),
220
"department": new_str(10),
222
"manager_name": None,
223
"assistant_name": None,
228
"description": new_str(10),
229
"street": new_str(10),
232
"postalcode": "04412"},
235
"description": new_str(10),
236
"street": new_str(10),
239
"postalcode": "33012"},),
242
"description": new_str(6),
243
"number": "313-876-3123"},),
246
"description": "home",
247
"address": "example.com!"},),
250
"description": "blog",
251
"address": "http://other.blag.example.com/"},),
254
"address": new_str(10),
255
"description": new_str(10),
256
"protocol": "jabber"},)}))
259
super(TestLocalFiles, self).tearDown()
260
del self.db._server[self.dbname] # pylint: disable=W0212
262
def test_find_contact_starting(self):
263
"""Test finding a contact starting with a prefix."""
264
contacts = list(view.find_contacts_starting(self.db, first_name="F"))
265
self.assertEqual(len(contacts), 1)
266
self.assertEqual(contacts[0].key, "first_name:Frances")
267
self.assertEqual(contacts[0].doc, None)
270
view.find_contacts_starting(
271
self.db, include_docs=True, first_name="F"))
272
self.assertNotEqual(contacts[0].doc, None)
273
self.assertEqual(contacts[0].doc["nick_name"], "Tiny")
275
self.assertEqual([], list(
276
view.find_contacts_starting(self.db, first_name="Chad")))
278
self.assertEqual([], list(
279
view.find_contacts_starting(self.db, first_name="Frank")))
282
view.find_contacts_starting(self.db, first_name="Frances"))
283
self.assertEqual(len(contacts), 1)
286
view.find_contacts_starting(self.db, birth_date="1918"))
287
self.assertEqual(len(contacts), 1)
290
view.find_contacts_starting(self.db, birth_date="1918-08"))
291
self.assertEqual(len(contacts), 1)
294
view.find_contacts_starting(self.db, wedding_date="1970"))
295
self.assertEqual(len(contacts), 1)
298
view.find_contacts_starting(
299
self.db, email_addressesaddress="blah.example.com"))
300
self.assertEqual(len(contacts), 1)
303
view.find_contacts_starting(
304
self.db, email_addressesaddress="berkeley"))
305
self.assertEqual(len(contacts), 1)
308
view.find_contacts_starting(self.db, first_name="random"))
309
self.assertEqual(len(contacts), 100)
311
def test_find_contacts_exact(self):
312
"""Test finding exact matches for contacts."""
313
self.assertEqual([], list(
314
view.find_contacts_exact(self.db, first_name="Frank")))
315
self.assertEqual([], list(
316
view.find_contacts_exact(self.db, first_name="Fran")))
319
view.find_contacts_exact(self.db, first_name="Frances"))
320
self.assertEqual(len(contacts), 1)
322
contacts = list(view.find_contacts_exact(self.db, birth_date="-08-23"))
323
self.assertEqual(len(contacts), 1)