~ubuntu-branches/ubuntu/precise/desktopcouch/precise

« back to all changes in this revision

Viewing changes to desktopcouch/recordtypes/contacts/tests/test_view.py

  • Committer: Bazaar Package Importer
  • Author(s): Chad MILLER
  • Date: 2011-01-12 15:08:25 UTC
  • mfrom: (1.5.10 upstream)
  • Revision ID: james.westby@ubuntu.com-20110112150825-bzvn23kzufr0qdyb
Tags: 1.0.5-0ubuntu1
* New upstream release, skipping a few buggy releases.
* Split code into binary packages:
  - desktopcouch, configuration files and dependencies, but no code.
  - python-desktopcouch: transitional package
  - python-desktopcouch-application: local DB startup and discovery
  - python-desktopcouch-records: library for DB access anywhere
  - python-desktopcouch-recordtypes: support specific data structures
  - desktopcouch-ubuntuone, replication and pairing with cloud service
* Drop patch that some maverick apps incorrectly needed.
  patches/0-items-should-expose-private-data-for-now.patch
* Update package compatibility-version, 6 -> 7.
* Use newer debhelper and use python-support instead of python-central.
* Depend on contemporary python-couchdb, instead of ancient version.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright 2010 Canonical Ltd.
 
2
#
 
3
# This file is part of desktopcouch.
 
4
#
 
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.
 
8
#
 
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.
 
13
#
 
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/>.
 
16
#
 
17
# Authors: Chad Miller <chad.miller@canonical.com>
 
18
 
 
19
"""Test view."""
 
20
 
 
21
import random
 
22
import string  # pylint: disable=W0402
 
23
 
 
24
from unittest import TestCase
 
25
 
 
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)
 
30
 
 
31
 
 
32
FRANCES_RECORD_DATA = {
 
33
    "record_type": CONTACT_RECORD_TYPE,
 
34
    "first_name": "Frances",
 
35
    "middle_name": None,
 
36
    "last_name": "Hicks",
 
37
    "title": None,
 
38
    "suffix": None,
 
39
    "birth_date": "1918-08-23",
 
40
    "nick_name": "Tiny",
 
41
    "spouse_name": "Louie",
 
42
    "wedding_date": "1970-02-27",
 
43
    "company": None,
 
44
    "department": None,
 
45
    "job_title": None,
 
46
    "manager_name": None,
 
47
    "assistant_name": None,
 
48
    "office": None,
 
49
    "addresses": (
 
50
       {
 
51
           "city": "Edwardburgh",
 
52
           "description": "summer",
 
53
           "street": "15 Libby Lane",
 
54
           "state": "MA",
 
55
           "country": "USA",
 
56
           "postalcode": "04412"},
 
57
       {
 
58
           "city": "Miami",
 
59
           "description": "winter",
 
60
           "street": "42 Snowbird Crawl",
 
61
           "state": "FL",
 
62
           "country": "USA",
 
63
           "postalcode": "33012"},
 
64
    ),
 
65
    "phone_numbers": (
 
66
       {
 
67
           "description": "home",
 
68
           "number": "313-876-3123"},
 
69
    ),
 
70
    "email_addresses": (
 
71
       {
 
72
           "description": "home",
 
73
           "address": "frances@blah.example.com"},
 
74
       {
 
75
           "description": "home",
 
76
           "address": "berkeley!uucp!frances"},
 
77
    ),
 
78
    "urls": (
 
79
       {
 
80
           "description": "blog",
 
81
           "address": "http://frances.blag.example.com/"},
 
82
    ),
 
83
    "im_addresses": (
 
84
        {
 
85
            "address": "frances0",
 
86
            "description": "Frances",
 
87
            "protocol": "jabber"},)}
 
88
 
 
89
 
 
90
def new_str(size):
 
91
    """Create a string of particular size."""
 
92
    return "".join(random.choice(string.lowercase) for _ in range(size))
 
93
 
 
94
 
 
95
def new_fake_record():
 
96
    """Get a fake record."""
 
97
    return {
 
98
        "record_type": CONTACT_RECORD_TYPE,
 
99
        "first_name": "random" + new_str(10),
 
100
        "middle_name": None,
 
101
        "last_name": new_str(10),
 
102
        "title": None,
 
103
        "suffix": None,
 
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),
 
110
        "job_title": None,
 
111
        "manager_name": None,
 
112
        "assistant_name": None,
 
113
        "office": None,
 
114
        "addresses": (
 
115
            {
 
116
                "city": new_str(10),
 
117
                "description": new_str(10),
 
118
                "street": new_str(10),
 
119
                "state": new_str(2),
 
120
                "country": "USA",
 
121
                "postalcode": "04412"},
 
122
            {
 
123
                "city": new_str(10),
 
124
                "description": new_str(10),
 
125
                "street": new_str(10),
 
126
                "state": "FL",
 
127
                "country": "USA",
 
128
                "postalcode": "33012"},),
 
129
        "phone_numbers": (
 
130
            {
 
131
                "description": new_str(6),
 
132
                "number": "313-876-3123"},),
 
133
        "email_addresses": (
 
134
            {
 
135
                "description": "home",
 
136
                "address": "x" + new_str(5) + "@example.com"},),
 
137
        "urls": (
 
138
            {
 
139
                "description": "blog",
 
140
                "address": "http://other.blag.example.com/"},),
 
141
        "im_addresses": (
 
142
            {
 
143
                "address": new_str(10),
 
144
                "description": new_str(10),
 
145
                "protocol": "jabber"},)}
 
146
 
 
147
 
 
148
class TestLocalFiles(TestCase):
 
149
    """Test local files."""
 
150
 
 
151
    def setUp(self):
 
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),
 
162
            "middle_name": None,
 
163
            "last_name": new_str(10),
 
164
            "title": None,
 
165
            "suffix": None,
 
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),
 
172
            "job_title": None,
 
173
            "manager_name": None,
 
174
            "assistant_name": None,
 
175
            "office": None,
 
176
            "addresses": (
 
177
                {
 
178
                    "city": new_str(10),
 
179
                    "description": new_str(10),
 
180
                    "street": new_str(10),
 
181
                    "state": new_str(2),
 
182
                    "country": "USA",
 
183
                    "postalcode": "04412"},
 
184
                {
 
185
                    "city": new_str(10),
 
186
                    "description": new_str(10),
 
187
                    "street": new_str(10),
 
188
                    "state": "FL",
 
189
                    "country": "USA",
 
190
                    "postalcode": "33012"},),
 
191
            "phone_numbers": (
 
192
                {
 
193
                    "description": new_str(6),
 
194
                    "number": "313-876-3123"},),
 
195
            "email_addresses": (
 
196
                {
 
197
                    "description": "home",
 
198
                    "address": "@example.com"},),
 
199
            "urls": (
 
200
                {
 
201
                    "description": "blog",
 
202
                    "address": "http://other.blag.example.com/"},),
 
203
            "im_addresses": (
 
204
                {
 
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),
 
211
            "middle_name": None,
 
212
            "last_name": new_str(10),
 
213
            "title": None,
 
214
            "suffix": None,
 
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),
 
221
            "job_title": None,
 
222
            "manager_name": None,
 
223
            "assistant_name": None,
 
224
            "office": None,
 
225
            "addresses": (
 
226
                {
 
227
                    "city": new_str(10),
 
228
                    "description": new_str(10),
 
229
                    "street": new_str(10),
 
230
                    "state": new_str(2),
 
231
                    "country": "USA",
 
232
                    "postalcode": "04412"},
 
233
                {
 
234
                    "city": new_str(10),
 
235
                    "description": new_str(10),
 
236
                    "street": new_str(10),
 
237
                    "state": "FL",
 
238
                    "country": "USA",
 
239
                    "postalcode": "33012"},),
 
240
            "phone_numbers": (
 
241
                {
 
242
                    "description": new_str(6),
 
243
                    "number": "313-876-3123"},),
 
244
            "email_addresses": (
 
245
                {
 
246
                    "description": "home",
 
247
                    "address": "example.com!"},),
 
248
            "urls": (
 
249
                {
 
250
                    "description": "blog",
 
251
                    "address": "http://other.blag.example.com/"},),
 
252
            "im_addresses": (
 
253
                {
 
254
                    "address": new_str(10),
 
255
                    "description": new_str(10),
 
256
                    "protocol": "jabber"},)}))
 
257
 
 
258
    def tearDown(self):
 
259
        super(TestLocalFiles, self).tearDown()
 
260
        del self.db._server[self.dbname]  # pylint: disable=W0212
 
261
 
 
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)
 
268
 
 
269
        contacts = list(
 
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")
 
274
 
 
275
        self.assertEqual([], list(
 
276
            view.find_contacts_starting(self.db, first_name="Chad")))
 
277
 
 
278
        self.assertEqual([], list(
 
279
            view.find_contacts_starting(self.db, first_name="Frank")))
 
280
 
 
281
        contacts = list(
 
282
            view.find_contacts_starting(self.db, first_name="Frances"))
 
283
        self.assertEqual(len(contacts), 1)
 
284
 
 
285
        contacts = list(
 
286
            view.find_contacts_starting(self.db, birth_date="1918"))
 
287
        self.assertEqual(len(contacts), 1)
 
288
 
 
289
        contacts = list(
 
290
            view.find_contacts_starting(self.db, birth_date="1918-08"))
 
291
        self.assertEqual(len(contacts), 1)
 
292
 
 
293
        contacts = list(
 
294
            view.find_contacts_starting(self.db, wedding_date="1970"))
 
295
        self.assertEqual(len(contacts), 1)
 
296
 
 
297
        contacts = list(
 
298
            view.find_contacts_starting(
 
299
                self.db, email_addressesaddress="blah.example.com"))
 
300
        self.assertEqual(len(contacts), 1)
 
301
 
 
302
        contacts = list(
 
303
            view.find_contacts_starting(
 
304
                self.db, email_addressesaddress="berkeley"))
 
305
        self.assertEqual(len(contacts), 1)
 
306
 
 
307
        contacts = list(
 
308
            view.find_contacts_starting(self.db, first_name="random"))
 
309
        self.assertEqual(len(contacts), 100)
 
310
 
 
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")))
 
317
 
 
318
        contacts = list(
 
319
            view.find_contacts_exact(self.db, first_name="Frances"))
 
320
        self.assertEqual(len(contacts), 1)
 
321
 
 
322
        contacts = list(view.find_contacts_exact(self.db, birth_date="-08-23"))
 
323
        self.assertEqual(len(contacts), 1)