edd6c8532abb4d93cfbb6f5257f35d911e410a35
[gc-dialer] / tests / test_file_backend.py
1 from __future__ import with_statement
2
3 import os
4 import warnings
5
6 import test_utils
7
8 import sys
9 sys.path.append("../src")
10
11 import file_backend
12
13
14 def test_factory():
15         warnings.simplefilter("always")
16         try:
17                 csvPath = os.path.join(os.path.dirname(__file__), "basic_data")
18                 factory = file_backend.FilesystemAddressBookFactory(csvPath)
19                 assert factory.factory_name() == "File"
20                 abooks = list(factory.get_addressbooks())
21                 abooks.sort()
22                 assert len(abooks) == 4
23                 abookNames = [abook[2] for abook in abooks]
24                 assert abookNames == ["basic", "empty", "google", "grandcentral"], "%s" % abookNames
25
26                 for abook_factory, abookId, abookName in abooks:
27                         abook = abook_factory.open_addressbook(abookId)
28                         assert isinstance(abook, file_backend.CsvAddressBook)
29         finally:
30                 warnings.resetwarnings()
31
32
33 def test_nonexistent_csv():
34         warnings.simplefilter("always")
35         try:
36                 csvPath = os.path.join(os.path.dirname(__file__), "basic_data", "nonexistent.csv")
37                 abook = file_backend.CsvAddressBook(csvPath)
38
39                 assert abook.factory_name() == "csv"
40
41                 contacts = list(abook.get_contacts())
42                 assert len(contacts) == 0
43         finally:
44                 warnings.resetwarnings()
45
46
47 def test_empty_csv():
48         warnings.simplefilter("always")
49         try:
50                 csvPath = os.path.join(os.path.dirname(__file__), "basic_data", "empty.csv")
51                 abook = file_backend.CsvAddressBook(csvPath)
52
53                 assert abook.factory_name() == "csv"
54
55                 contacts = list(abook.get_contacts())
56                 assert len(contacts) == 0
57         finally:
58                 warnings.resetwarnings()
59
60
61 def test_basic_csv():
62         warnings.simplefilter("always")
63         try:
64                 csvPath = os.path.join(os.path.dirname(__file__), "basic_data", "basic.csv")
65                 abook = file_backend.CsvAddressBook(csvPath)
66
67                 assert abook.factory_name() == "csv"
68
69                 contacts = list(abook.get_contacts())
70                 contacts.sort()
71                 assert len(contacts) == 1
72
73                 contactId, contactName = contacts[0]
74                 assert contactName == "Last, First"
75                 assert abook.contact_source_short_name(contactId) == "csv"
76
77                 details = list(abook.get_contact_details(contactId))
78                 assert len(details) == 1
79                 details.sort()
80                 assert details == [("phone", "555-123-4567")], "%s" % details
81         finally:
82                 warnings.resetwarnings()
83
84
85 def test_google_csv():
86         warnings.simplefilter("always")
87         try:
88                 csvPath = os.path.join(os.path.dirname(__file__), "basic_data", "google.csv")
89                 abook = file_backend.CsvAddressBook(csvPath)
90
91                 assert abook.factory_name() == "csv"
92
93                 contacts = list(abook.get_contacts())
94                 contacts.sort()
95                 assert len(contacts) == 2
96
97                 contactId, contactName = contacts[0]
98                 assert contactName == "First Last"
99                 assert abook.contact_source_short_name(contactId) == "csv"
100
101                 details = list(abook.get_contact_details(contactId))
102                 assert len(details) == 2
103                 details.sort()
104                 assert details == [
105                         ("Section 2 - Mobile", "5551234567"),
106                         ("Section 2 - Phone", "17471234567"),
107                 ], "%s" % details
108
109                 contactId, contactName = contacts[1]
110                 assert contactName == "First1 Last"
111                 assert abook.contact_source_short_name(contactId) == "csv"
112
113                 details = list(abook.get_contact_details(contactId))
114                 assert len(details) == 1
115                 details.sort()
116                 assert details == [("Section 1 - Mobile", "5557654321")], "%s" % details
117         finally:
118                 warnings.resetwarnings()
119
120
121 def test_grandcentral_csv():
122         warnings.simplefilter("always")
123         try:
124                 csvPath = os.path.join(os.path.dirname(__file__), "basic_data", "grandcentral.csv")
125                 abook = file_backend.CsvAddressBook(csvPath)
126
127                 assert abook.factory_name() == "csv"
128
129                 contacts = list(abook.get_contacts())
130                 contacts.sort()
131                 assert len(contacts) == 2
132
133                 contactId, contactName = contacts[0]
134                 assert contactName == "First Last"
135                 assert abook.contact_source_short_name(contactId) == "csv"
136
137                 details = list(abook.get_contact_details(contactId))
138                 assert len(details) == 3
139                 details.sort()
140                 assert details == [
141                         ("Business Phone", "5559988899"),
142                         ("Home Phone", "5559983254"),
143                         ("Mobile Phone", "5554023626"),
144                 ], "%s" % details
145
146                 contactId, contactName = contacts[1]
147                 assert contactName == "First1 Last"
148                 assert abook.contact_source_short_name(contactId) == "csv"
149
150                 details = list(abook.get_contact_details(contactId))
151                 assert len(details) == 1
152                 details.sort()
153                 assert details == [("Home Phone", "5556835460")], "%s" % details
154         finally:
155                 warnings.resetwarnings()