Expanding tests to the file based addressbook
authorepage <eopage@byu.net>
Fri, 29 May 2009 01:34:24 +0000 (01:34 +0000)
committerepage <eopage@byu.net>
Fri, 29 May 2009 01:34:24 +0000 (01:34 +0000)
git-svn-id: file:///svnroot/gc-dialer/trunk@343 c39d3808-3fe2-4d86-a59f-b7f623ee9f21

src/file_backend.py
tests/basic_data/basic.csv [new file with mode: 0644]
tests/basic_data/empty.csv [new file with mode: 0644]
tests/basic_data/google.csv [new file with mode: 0644]
tests/basic_data/grandcentral.csv [new file with mode: 0644]
tests/test_file_backend.py [new file with mode: 0644]
tests/test_startup.py

index ddd89b3..31fc52a 100644 (file)
@@ -48,7 +48,12 @@ class CsvAddressBook(object):
 
        @classmethod
        def read_csv(cls, csvPath):
-               csvReader = iter(csv.reader(open(csvPath, "rU")))
+               try:
+                       csvReader = iter(csv.reader(open(csvPath, "rU")))
+               except IOError, e:
+                       if e.errno != 2:
+                               raise
+                       return
 
                header = csvReader.next()
                nameColumn, phoneColumns = cls._guess_columns(header)
@@ -129,7 +134,11 @@ class FilesystemAddressBookFactory(object):
                """
                for root, dirs, filenames in os.walk(self.__path):
                        for filename in filenames:
-                               name, ext = filename.rsplit(".", 1)
+                               try:
+                                       name, ext = filename.rsplit(".", 1)
+                               except ValueError:
+                                       continue
+
                                if ext in self.FILETYPE_SUPPORT:
                                        yield self, os.path.join(root, filename), name
 
diff --git a/tests/basic_data/basic.csv b/tests/basic_data/basic.csv
new file mode 100644 (file)
index 0000000..6ffd844
--- /dev/null
@@ -0,0 +1,2 @@
+familyname,phone,addr1,addr2,addr3,addr4,name1,name2,name3,name4\r
+"Last, First","555-123-4567","1234 Foo St","Austin, Texas 78727","","","First <name@domain.com>"\r
diff --git a/tests/basic_data/empty.csv b/tests/basic_data/empty.csv
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/tests/basic_data/google.csv b/tests/basic_data/google.csv
new file mode 100644 (file)
index 0000000..3fa4d3e
--- /dev/null
@@ -0,0 +1,7 @@
+Name,E-mail,Notes,Section 1 - Description,Section 1 - Email,Section 1 - IM,Section 1 - Phone,Section 1 - Mobile,Section 1 - Pager,Section 1 - Fax,Section 1 - Company,Section 1 - Title,Section 1 - Other,Section 1 - Address,Section 2 - Description,Section 2 - Email,Section 2 - IM,Section 2 - Phone,Section 2 - Mobile,Section 2 - Pager,Section 2 - Fax,Section 2 - Company,Section 2 - Title,Section 2 - Other,Section 2 - Address,Section 3 - Description,Section 3 - Email,Section 3 - IM,Section 3 - Phone,Section 3 - Mobile,Section 3 - Pager,Section 3 - Fax,Section 3 - Company,Section 3 - Title,Section 3 - Other,Section 3 - Address\r
+First Last,name@domain.com,"Categories: Others\r
+\r
+\r
+Categories: Others",Other,name2@domain.com,,,,,,,,,,Personal,name3@domain.com; name4@domain.com; name5@domain.com; name6@domain.com,,17471234567,5551234567,,,,,,\r
+First1 Last,,"Categories: Friends\r
+",Personal,,,,5557654321,,,,,,\r
diff --git a/tests/basic_data/grandcentral.csv b/tests/basic_data/grandcentral.csv
new file mode 100644 (file)
index 0000000..4808cf2
--- /dev/null
@@ -0,0 +1,3 @@
+Name,E-mail Address,Categories,Company,Job Title,Home Address,Home Phone,Mobile Phone,Business Phone,GrandCentral,Suffix,Title,Initials,Web Page,Notes\r
+First Last,,Family,,,1234567 Foo St Austn Tx 78727,5559983254,5554023626,5559988899,,,,,,\r
+First1 Last,,Others,,,,5556835460,,,,,,,,\r
diff --git a/tests/test_file_backend.py b/tests/test_file_backend.py
new file mode 100644 (file)
index 0000000..edd6c85
--- /dev/null
@@ -0,0 +1,155 @@
+from __future__ import with_statement
+
+import os
+import warnings
+
+import test_utils
+
+import sys
+sys.path.append("../src")
+
+import file_backend
+
+
+def test_factory():
+       warnings.simplefilter("always")
+       try:
+               csvPath = os.path.join(os.path.dirname(__file__), "basic_data")
+               factory = file_backend.FilesystemAddressBookFactory(csvPath)
+               assert factory.factory_name() == "File"
+               abooks = list(factory.get_addressbooks())
+               abooks.sort()
+               assert len(abooks) == 4
+               abookNames = [abook[2] for abook in abooks]
+               assert abookNames == ["basic", "empty", "google", "grandcentral"], "%s" % abookNames
+
+               for abook_factory, abookId, abookName in abooks:
+                       abook = abook_factory.open_addressbook(abookId)
+                       assert isinstance(abook, file_backend.CsvAddressBook)
+       finally:
+               warnings.resetwarnings()
+
+
+def test_nonexistent_csv():
+       warnings.simplefilter("always")
+       try:
+               csvPath = os.path.join(os.path.dirname(__file__), "basic_data", "nonexistent.csv")
+               abook = file_backend.CsvAddressBook(csvPath)
+
+               assert abook.factory_name() == "csv"
+
+               contacts = list(abook.get_contacts())
+               assert len(contacts) == 0
+       finally:
+               warnings.resetwarnings()
+
+
+def test_empty_csv():
+       warnings.simplefilter("always")
+       try:
+               csvPath = os.path.join(os.path.dirname(__file__), "basic_data", "empty.csv")
+               abook = file_backend.CsvAddressBook(csvPath)
+
+               assert abook.factory_name() == "csv"
+
+               contacts = list(abook.get_contacts())
+               assert len(contacts) == 0
+       finally:
+               warnings.resetwarnings()
+
+
+def test_basic_csv():
+       warnings.simplefilter("always")
+       try:
+               csvPath = os.path.join(os.path.dirname(__file__), "basic_data", "basic.csv")
+               abook = file_backend.CsvAddressBook(csvPath)
+
+               assert abook.factory_name() == "csv"
+
+               contacts = list(abook.get_contacts())
+               contacts.sort()
+               assert len(contacts) == 1
+
+               contactId, contactName = contacts[0]
+               assert contactName == "Last, First"
+               assert abook.contact_source_short_name(contactId) == "csv"
+
+               details = list(abook.get_contact_details(contactId))
+               assert len(details) == 1
+               details.sort()
+               assert details == [("phone", "555-123-4567")], "%s" % details
+       finally:
+               warnings.resetwarnings()
+
+
+def test_google_csv():
+       warnings.simplefilter("always")
+       try:
+               csvPath = os.path.join(os.path.dirname(__file__), "basic_data", "google.csv")
+               abook = file_backend.CsvAddressBook(csvPath)
+
+               assert abook.factory_name() == "csv"
+
+               contacts = list(abook.get_contacts())
+               contacts.sort()
+               assert len(contacts) == 2
+
+               contactId, contactName = contacts[0]
+               assert contactName == "First Last"
+               assert abook.contact_source_short_name(contactId) == "csv"
+
+               details = list(abook.get_contact_details(contactId))
+               assert len(details) == 2
+               details.sort()
+               assert details == [
+                       ("Section 2 - Mobile", "5551234567"),
+                       ("Section 2 - Phone", "17471234567"),
+               ], "%s" % details
+
+               contactId, contactName = contacts[1]
+               assert contactName == "First1 Last"
+               assert abook.contact_source_short_name(contactId) == "csv"
+
+               details = list(abook.get_contact_details(contactId))
+               assert len(details) == 1
+               details.sort()
+               assert details == [("Section 1 - Mobile", "5557654321")], "%s" % details
+       finally:
+               warnings.resetwarnings()
+
+
+def test_grandcentral_csv():
+       warnings.simplefilter("always")
+       try:
+               csvPath = os.path.join(os.path.dirname(__file__), "basic_data", "grandcentral.csv")
+               abook = file_backend.CsvAddressBook(csvPath)
+
+               assert abook.factory_name() == "csv"
+
+               contacts = list(abook.get_contacts())
+               contacts.sort()
+               assert len(contacts) == 2
+
+               contactId, contactName = contacts[0]
+               assert contactName == "First Last"
+               assert abook.contact_source_short_name(contactId) == "csv"
+
+               details = list(abook.get_contact_details(contactId))
+               assert len(details) == 3
+               details.sort()
+               assert details == [
+                       ("Business Phone", "5559988899"),
+                       ("Home Phone", "5559983254"),
+                       ("Mobile Phone", "5554023626"),
+               ], "%s" % details
+
+               contactId, contactName = contacts[1]
+               assert contactName == "First1 Last"
+               assert abook.contact_source_short_name(contactId) == "csv"
+
+               details = list(abook.get_contact_details(contactId))
+               assert len(details) == 1
+               details.sort()
+               assert details == [("Home Phone", "5556835460")], "%s" % details
+       finally:
+               warnings.resetwarnings()
index 99c03f7..5058fc0 100644 (file)
@@ -2,6 +2,7 @@ from __future__ import with_statement
 
 import os
 import time
+import warnings
 
 import test_utils
 
@@ -31,7 +32,8 @@ def startup(factory):
        del handle
 
 
-def test_startup_with_no_data_dir_with_dummy_hildon():
+def atest_startup_with_no_data_dir_with_dummy_hildon():
+       warnings.simplefilter("always")
        hildonPath = os.path.join(os.path.dirname(__file__), "dummy_hildon")
        sys.path.insert(0, hildonPath)
        import hildon
@@ -54,9 +56,11 @@ def test_startup_with_no_data_dir_with_dummy_hildon():
        finally:
                dc_glade.hildon = None
                sys.path.remove(hildonPath)
+               warnings.resetwarnings()
 
 
-def test_startup_with_no_data_dir():
+def atest_startup_with_no_data_dir():
+       warnings.simplefilter("always")
        dc_glade.Dialcentral._data_path = os.path.join(os.path.dirname(__file__), "notexistent_data")
        dc_glade.Dialcentral._user_settings = "%s/settings.ini" % dc_glade.Dialcentral._data_path
 
@@ -71,9 +75,11 @@ def test_startup_with_no_data_dir():
                        os.removedirs(dc_glade.Dialcentral._data_path)
                except:
                        pass
+               warnings.resetwarnings()
 
 
-def test_startup_with_empty_data_dir():
+def atest_startup_with_empty_data_dir():
+       warnings.simplefilter("always")
        dc_glade.Dialcentral._data_path = os.path.join(os.path.dirname(__file__), "empty_data")
        dc_glade.Dialcentral._user_settings = "%s/settings.ini" % dc_glade.Dialcentral._data_path
 
@@ -88,10 +94,15 @@ def test_startup_with_empty_data_dir():
                        os.removedirs(dc_glade.Dialcentral._data_path)
                except:
                        pass
+               warnings.resetwarnings()
 
 
-def test_startup_with_basic_data_dir():
-       dc_glade.Dialcentral._data_path = os.path.join(os.path.dirname(__file__), "basic_data")
-       dc_glade.Dialcentral._user_settings = "%s/settings.ini" % dc_glade.Dialcentral._data_path
+def atest_startup_with_basic_data_dir():
+       warnings.simplefilter("always")
+       try:
+               dc_glade.Dialcentral._data_path = os.path.join(os.path.dirname(__file__), "basic_data")
+               dc_glade.Dialcentral._user_settings = "%s/settings.ini" % dc_glade.Dialcentral._data_path
 
-       startup(dc_glade.Dialcentral)
+               startup(dc_glade.Dialcentral)
+       finally:
+               warnings.resetwarnings()