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
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 = {}
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]
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):
34 def _will_return_friend_for_contact(self, friend, contact):
35 self._response_for_contact[contact] = friend
37 class FakeAddressBook():
40 self.contacts_committed = []
41 def get_all_contacts(self):
43 def add_contact(self, contact):
44 self.contacts.append(contact)
45 def commit_contact(self, contact):
46 self.contacts_committed.append(contact)
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):
58 def add_mapping(self, id):
59 self._add('mapping', id)
60 def get_mappings(self):
61 return self._safe_get('mapping')
68 def __init__(self, name):
72 def get_property(self, key):
75 class TestHermes(unittest.TestCase):
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
86 def test_empty_run(self):
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
98 def test_empty_abook_and_service_returns_one_friend_to_create(self):
99 self.service.friends_to_create = [Friend("Fredrik")]
103 assert len(self.abook.contacts) == 1
104 assert len(self.service.contacts_processed) == 0
105 assert len(self.service.contacts_pre_processed) == 0
108 def test_one_contact_and_no_match(self):
109 self.abook.contacts.append(FakeEContact("Fredrik"))
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
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)
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
137 # faked to avoid evolution/ebook dependency
138 def _get_address_book(self):
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)
145 if __name__ == '__main__':