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>
19
import desktopcouch.tests as test_environment
20
from desktopcouch.records import server, record
21
from desktopcouch.contacts import record as contact_record, view as contact_view
28
frances_record_data = {
29
"record_type": contact_record.CONTACT_RECORD_TYPE,
30
"first_name": "Frances",
35
"birth_date": "1918-08-23",
37
"spouse_name": "Louie",
38
"wedding_date": "1970-02-27",
43
"assistant_name": None,
47
"city": "Edwardburgh",
48
"description": "summer",
49
"street": "15 Libby Lane",
56
"description": "winter",
57
"street": "42 Snowbird Crawl",
65
"description": "home",
66
"number": "313-876-3123"
71
"description": "home",
72
"address": "frances@example.com"
77
"description": "blog",
78
"address": "http://frances.blag.example.com/"
83
"address": "frances0",
84
"description": "Frances",
91
def new_fake_record():
92
new_str = lambda size: "".join(random.choice(string.lowercase) for n in range(size))
93
return { "record_type": contact_record.CONTACT_RECORD_TYPE, "first_name": "random" + new_str(10), "middle_name": None, "last_name": new_str(10), "title": None, "suffix": None, "birth_date": "1970-02-13", "nick_name": new_str(10), "spouse_name": new_str(10), "wedding_date": "1970-02-27", "company": new_str(10), "department": new_str(10), "job_title": None, "manager_name": None, "assistant_name": None, "office": None, "addresses": ( { "city": new_str(10), "description": new_str(10), "street": new_str(10), "state": new_str(2), "country": "USA", "postalcode": "04412" }, { "city": new_str(10), "description": new_str(10), "street": new_str(10), "state": "FL", "country": "USA", "postalcode": "33012" },), "phone_numbers": ( { "description": new_str(6), "number": "313-876-3123" },), "email_addresses": ( { "description": "home", "address": new_str(5) + "@example.com" },), "urls": ( { "description": "blog", "address": "http://other.blag.example.com/" },), "im_addresses": ( { "address": new_str(10), "description": new_str(10), "protocol": "jabber" },) }
96
class TestLocalFiles(testtools.TestCase):
99
super(TestLocalFiles, self).setUp()
100
self.dbname = "contacts"
101
self.db = server.CouchDatabase(self.dbname, create=True, ctx=test_environment.test_context)
102
self.db.put_record(contact_record.Contact(data=frances_record_data))
103
self.db.put_records_batch([contact_record.Contact(data=new_fake_record()) for m in range(100)])
107
super(TestLocalFiles, self).tearDown()
108
del self.db._server[self.dbname]
110
def test_find_contact_starting(self):
111
contacts = list(contact_view.find_contacts_starting(self.db, first_name="F"))
112
self.assertEqual(len(contacts), 1)
113
self.assertEqual(contacts[0].key, "first_name:Frances")
114
self.assertEqual(contacts[0].doc, None)
116
contacts = list(contact_view.find_contacts_starting(self.db, include_docs=True, first_name="F"))
117
self.assertNotEqual(contacts[0].doc, None)
118
self.assertEqual(contacts[0].doc["nick_name"], "Tiny")
120
self.assertEqual([], list(contact_view.find_contacts_starting(self.db, first_name="Chad")))
122
self.assertEqual([], list(contact_view.find_contacts_starting(self.db, first_name="Frank")))
124
contacts = list(contact_view.find_contacts_starting(self.db, first_name="Frances"))
125
self.assertEqual(len(contacts), 1)
127
contacts = list(contact_view.find_contacts_starting(self.db, birth_date="1918"))
128
self.assertEqual(len(contacts), 1)
130
contacts = list(contact_view.find_contacts_starting(self.db, birth_date="1918-08"))
131
self.assertEqual(len(contacts), 1)
133
contacts = list(contact_view.find_contacts_starting(self.db, first_name="random"))
134
self.assertEqual(len(contacts), 100)
136
def test_find_contacts_exact(self):
137
self.assertEqual([], list(contact_view.find_contacts_exact(self.db, first_name="Frank")))
138
self.assertEqual([], list(contact_view.find_contacts_exact(self.db, first_name="Fran")))
140
contacts = list(contact_view.find_contacts_exact(self.db, first_name="Frances"))
141
self.assertEqual(len(contacts), 1)
143
contacts = list(contact_view.find_contacts_exact(self.db, birth_date="-08-23"))
144
self.assertEqual(len(contacts), 1)