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')
66 def set_photo(self, value):
67 self.set_photo_url(value)
68 def get_birthday(self):
69 return self.get_birthday_date()
72 def __init__(self, name):
76 def get_property(self, key):
79 class TestHermes(unittest.TestCase):
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
90 def test_empty_run(self):
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
102 def test_empty_abook_and_service_returns_one_friend_to_create(self):
103 self.service.friends_to_create = [Friend("Fredrik")]
107 assert len(self.abook.contacts) == 1
108 assert len(self.service.contacts_processed) == 0
109 assert len(self.service.contacts_pre_processed) == 0
112 def test_one_contact_and_no_match(self):
113 self.abook.contacts.append(FakeEContact("Fredrik"))
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
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)
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
141 # faked to avoid evolution/ebook dependency
142 def _get_address_book(self):
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)
149 if __name__ == '__main__':