added tests for Hermes.py
[hermes] / package / test / unit / test_hermes.py
1 from org.maemo.hermes.engine.hermes import Hermes
2 from org.maemo.hermes.engine.friend import Friend
3 from org.maemo.hermes.engine.names import canonical
4
5 import unittest
6
7 class FakeService():
8     def __init__(self):
9         self.friends_to_create = []
10         self.unmatched_friends = []
11         self.contacts_pre_processed = []
12         self.contacts_processed = []
13         self.process_friends_called = False
14         self._response_for_contact = {}
15         
16     def get_id(self): 
17         return "fakebook"
18     def pre_process_contact(self, contact): 
19         self.contacts_pre_processed.append(contact)
20     def process_friends(self): 
21         self.process_friends_called = True
22     def process_contact(self, contact): 
23         self.contacts_processed.append(contact)
24         econtact = contact.get_econtact()
25         if self._response_for_contact.has_key(econtact):
26             return self._response_for_contact[econtact]
27         return None
28     def get_friends_to_create_contacts_for(self): 
29         return self.friends_to_create
30     def get_unmatched_friends(self): 
31         return self.unmatched_friends
32     def finalise(self, updated, overwrite=False): 
33         pass
34     def _will_return_friend_for_contact(self, friend, contact):
35         self._response_for_contact[contact] = friend
36     
37 class FakeAddressBook():
38     def __init__(self):
39         self.contacts = []
40         self.contacts_committed = []
41     def get_all_contacts(self): 
42         return self.contacts
43     def add_contact(self, contact):
44         self.contacts.append(contact)
45     def commit_contact(self, contact): 
46         self.contacts_committed.append(contact)
47         
48 class FakeContact(Friend):
49     def __init__(self, book=None, econtact=None, props=None):
50         Friend.__init__(self, props)
51         self.econtact = econtact
52     def get_property(self, key):
53         return self._safe_get(key)
54     def get_identifiers(self):
55         return [self.get_name(), self.get_nickname()]
56     def get_econtact(self):
57         return self.econtact
58     def add_mapping(self, id):
59         self._add('mapping', id)
60     def get_mappings(self):
61         return self._safe_get('mapping')
62     def get_photo(self):
63         return None
64     def get_emails(self):
65         return []
66
67 class FakeEContact():
68     def __init__(self, name):
69         self.name = name
70     def get_name(self):
71         return self.name
72     def get_property(self, key):
73         return None
74
75 class TestHermes(unittest.TestCase):
76
77     def setUp(self):
78         self.service = FakeService()
79         self.abook = FakeAddressBook()
80         self.hermes = Hermes([self.service], None)
81         self.hermes._get_address_book = self._get_address_book
82         self.hermes._create_empty_contact = self._create_empty_contact
83         self.hermes._create_contact_wrapper = self._create_contact_wrapper
84     
85     
86     def test_empty_run(self):
87         self.hermes.run()
88
89         assert self.service.process_friends_called
90         assert len(self.abook.contacts) == 0
91         assert len(self.service.contacts_processed) == 0
92         assert len(self.service.contacts_pre_processed) == 0
93         assert len(self.hermes.unmatched) == 0
94         assert len(self.hermes.matched) == 0
95         assert len(self.hermes.updated) == 0
96
97         
98     def test_empty_abook_and_service_returns_one_friend_to_create(self):
99         self.service.friends_to_create = [Friend("Fredrik")]
100         
101         self.hermes.run()
102         
103         assert len(self.abook.contacts) == 1
104         assert len(self.service.contacts_processed) == 0
105         assert len(self.service.contacts_pre_processed) == 0
106         
107     
108     def test_one_contact_and_no_match(self):
109         self.abook.contacts.append(FakeEContact("Fredrik"))
110         
111         self.hermes.run()
112         
113         assert len(self.abook.contacts) == 1
114         assert len(self.service.contacts_processed) == 1
115         assert len(self.service.contacts_pre_processed) == 1
116         assert len(self.hermes.unmatched) == 1
117         assert len(self.hermes.matched) == 0
118         assert len(self.hermes.updated) == 0
119         
120         
121     def test_one_contact_and_one_match(self):
122         contact = FakeEContact("Fredrik")
123         friend = Friend("Fredrik", props={'birthday_date':'1980-10-15'})
124         self.abook.contacts.append(contact)
125         self.service._will_return_friend_for_contact(friend, contact)
126         
127         self.hermes.run()
128         
129         assert len(self.abook.contacts) == 1
130         assert len(self.service.contacts_processed) == 1
131         assert len(self.service.contacts_pre_processed) == 1
132         assert len(self.hermes.unmatched) == 0
133         assert len(self.hermes.matched) == 1
134         assert len(self.hermes.updated) == 1
135         
136         
137     # faked to avoid evolution/ebook dependency
138     def _get_address_book(self):
139         return self.abook
140     def _create_empty_contact(self, friend):
141         return FakeEContact(friend.get_name())
142     def _create_contact_wrapper(self, econtact):
143         return FakeContact(econtact=econtact)
144
145 if __name__ == '__main__':
146     unittest.main()