~ubuntu-branches/ubuntu/trusty/desktopcouch/trusty

« back to all changes in this revision

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

  • Committer: Bazaar Package Importer
  • Author(s): Ken VanDine
  • Date: 2010-04-19 12:52:22 UTC
  • mfrom: (12.1.9 lucid)
  • Revision ID: james.westby@ubuntu.com-20100419125222-zsh9lrm15h84951j
* debian/patches/lp_522538.patch
  - Handle reconnects if the server isn't running (LP: #522538)

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
 
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
22
 
 
23
 
import testtools
24
 
import random
25
 
import string
26
 
 
27
 
 
28
 
frances_record_data = {
29
 
    "record_type": contact_record.CONTACT_RECORD_TYPE,
30
 
    "first_name": "Frances",
31
 
    "middle_name": None,
32
 
    "last_name": "Hicks",
33
 
    "title": None,
34
 
    "suffix": None,
35
 
    "birth_date": "1918-08-23",
36
 
    "nick_name": "Tiny",
37
 
    "spouse_name": "Louie",
38
 
    "wedding_date": "1970-02-27",
39
 
    "company": None,
40
 
    "department": None,
41
 
    "job_title": None,
42
 
    "manager_name": None,
43
 
    "assistant_name": None,
44
 
    "office": None,
45
 
    "addresses": (
46
 
       {
47
 
           "city": "Edwardburgh",
48
 
           "description": "summer",
49
 
           "street": "15 Libby Lane",
50
 
           "state": "MA",
51
 
           "country": "USA",
52
 
           "postalcode": "04412"
53
 
       },
54
 
       {
55
 
           "city": "Miami",
56
 
           "description": "winter",
57
 
           "street": "42 Snowbird Crawl",
58
 
           "state": "FL",
59
 
           "country": "USA",
60
 
           "postalcode": "33012"
61
 
       },
62
 
    ),
63
 
    "phone_numbers": (
64
 
       {
65
 
           "description": "home",
66
 
           "number": "313-876-3123"
67
 
       },
68
 
    ),
69
 
    "email_addresses": (
70
 
       {
71
 
           "description": "home",
72
 
           "address": "frances@example.com"
73
 
       },
74
 
    ),
75
 
    "urls": (
76
 
       {
77
 
           "description": "blog",
78
 
           "address": "http://frances.blag.example.com/"
79
 
       },
80
 
    ),
81
 
    "im_addresses": (
82
 
        {
83
 
            "address": "frances0",
84
 
            "description": "Frances",
85
 
            "protocol": "jabber"
86
 
        },
87
 
    )
88
 
}
89
 
 
90
 
 
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" },) }
94
 
 
95
 
 
96
 
class TestLocalFiles(testtools.TestCase):
97
 
 
98
 
    def setUp(self):
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)])
104
 
 
105
 
        
106
 
    def tearDown(self):
107
 
        super(TestLocalFiles, self).tearDown()
108
 
        del self.db._server[self.dbname]
109
 
 
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)
115
 
 
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")
119
 
 
120
 
        self.assertEqual([], list(contact_view.find_contacts_starting(self.db, first_name="Chad")))
121
 
 
122
 
        self.assertEqual([], list(contact_view.find_contacts_starting(self.db, first_name="Frank")))
123
 
 
124
 
        contacts = list(contact_view.find_contacts_starting(self.db, first_name="Frances"))
125
 
        self.assertEqual(len(contacts), 1)
126
 
 
127
 
        contacts = list(contact_view.find_contacts_starting(self.db, birth_date="1918"))
128
 
        self.assertEqual(len(contacts), 1)
129
 
 
130
 
        contacts = list(contact_view.find_contacts_starting(self.db, birth_date="1918-08"))
131
 
        self.assertEqual(len(contacts), 1)
132
 
 
133
 
        contacts = list(contact_view.find_contacts_starting(self.db, first_name="random"))
134
 
        self.assertEqual(len(contacts), 100)
135
 
 
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")))
139
 
 
140
 
        contacts = list(contact_view.find_contacts_exact(self.db, first_name="Frances"))
141
 
        self.assertEqual(len(contacts), 1)
142
 
 
143
 
        contacts = list(contact_view.find_contacts_exact(self.db, birth_date="-08-23"))
144
 
        self.assertEqual(len(contacts), 1)