3cd73e53da81e7bc214fec4638e5a34633b44623
[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     def set_photo(self, value):
67         self.set_photo_url(value)
68     def get_birthday(self):
69         return self.get_birthday_date()
70
71 class FakeEContact():
72     def __init__(self, name):
73         self.name = name
74     def get_name(self):
75         return self.name
76     def get_property(self, key):
77         return None
78
79 class TestHermes(unittest.TestCase):
80
81     def setUp(self):
82         self.service = FakeService()
83         self.abook = FakeAddressBook()
84         self.hermes = Hermes([self.service], None)
85         self.hermes._get_address_book = self._get_address_book
86         self.hermes._create_empty_contact = self._create_empty_contact
87         self.hermes._create_contact_wrapper = self._create_contact_wrapper
88     
89     
90     def test_empty_run(self):
91         self.hermes.run()
92
93         assert self.service.process_friends_called
94         assert len(self.abook.contacts) == 0
95         assert len(self.service.contacts_processed) == 0
96         assert len(self.service.contacts_pre_processed) == 0
97         assert len(self.hermes.unmatched) == 0
98         assert len(self.hermes.matched) == 0
99         assert len(self.hermes.updated) == 0
100
101         
102     def test_empty_abook_and_service_returns_one_friend_to_create(self):
103         self.service.friends_to_create = [Friend("Fredrik")]
104         
105         self.hermes.run()
106         
107         assert len(self.abook.contacts) == 1
108         assert len(self.service.contacts_processed) == 0
109         assert len(self.service.contacts_pre_processed) == 0
110         
111     
112     def test_one_contact_and_no_match(self):
113         self.abook.contacts.append(FakeEContact("Fredrik"))
114         
115         self.hermes.run()
116         
117         assert len(self.abook.contacts) == 1
118         assert len(self.service.contacts_processed) == 1
119         assert len(self.service.contacts_pre_processed) == 1
120         assert len(self.hermes.unmatched) == 1
121         assert len(self.hermes.matched) == 0
122         assert len(self.hermes.updated) == 0
123         
124         
125     def test_one_contact_and_one_match_and_no_field_updated(self):
126         contact = FakeEContact("Fredrik")
127         friend = Friend("Fredrik", props={'birthday_date':'1980-10-15'})
128         self.abook.contacts.append(contact)
129         self.service._will_return_friend_for_contact(friend, contact)
130         
131         self.hermes.run()
132         
133         assert len(self.abook.contacts) == 1
134         assert len(self.service.contacts_processed) == 1
135         assert len(self.service.contacts_pre_processed) == 1
136         assert len(self.hermes.unmatched) == 0
137         assert len(self.hermes.matched) == 1
138         assert len(self.hermes.updated) == 0
139         
140         
141     # faked to avoid evolution/ebook dependency
142     def _get_address_book(self):
143         return self.abook
144     def _create_empty_contact(self, friend):
145         return FakeEContact(friend.get_name())
146     def _create_contact_wrapper(self, econtact):
147         return FakeContact(econtact=econtact)
148
149 if __name__ == '__main__':
150     unittest.main()