1 from org.maemo.hermes.engine.facebook.service import Service
2 from org.maemo.hermes.engine.names import canonical
8 def __init__(self, name, addr, id=None):
11 self.id = id or FakeContact.id_counter
12 FakeContact.id_counter = FakeContact.id_counter + 1;
17 def get_identifiers(self):
18 return [canonical(self.name)]
20 class TestFacebookService(unittest.TestCase):
23 self.testee = Service(None)
25 def test_main_flow_one_match_by_url_one_by_name(self):
27 self.existing_address = 'http://www.facebook.com/profile.php?id=123456'
28 self.existing_contact = FakeContact("Facebook Person", [self.existing_address])
29 existing_fake = {'uid':'123456','name':'Name Doesnt Match but URL Does'}
31 self.other_address = 'http://twitter.com/not/correct/site'
32 self.other_contact = FakeContact("Twitter Person", [self.other_address])
33 other_fake = {'uid':'123','name':self.other_contact.get_name()}
35 self.none_contact = FakeContact("No URLson", [])
37 fake_data = [existing_fake, other_fake]
38 self._fake_server_response(fake_data)
41 self._run_service([self.existing_contact, self.other_contact, self.none_contact])
44 friends = self.testee.get_friends()
45 contacts = self.testee.get_contacts_with_match()
46 assert len(friends) == 2
47 assert len(contacts) == 2
48 assert self.other_contact in contacts
49 assert self.existing_contact in contacts
50 assert self.none_contact not in contacts
53 def test_name_collision_avoided_by_previous_matching(self):
54 contact_do_match = FakeContact("Same Name", ["http://www.facebook.com/profile.php?id=123"], 1);
55 contact_no_match = FakeContact("Same Name", [None], 2)
57 data = [{'uid':'123','name':'Same Name'}]
58 self._fake_server_response(data)
60 self._run_service([contact_no_match, contact_do_match])
62 assert len(self.testee.get_unmatched_friends()) == 0
63 matchers = self.testee.get_contacts_with_match().keys()
64 assert len(matchers) == 1
65 assert matchers[0].id == 1
67 def test_name_collision_avoided_only_one_person_matched(self):
68 contact_do_match = FakeContact("Same Name", ["http://twitter.com/same_name"]);
69 contact_no_match = FakeContact("Same Name", [None])
71 data = [{'uid':'123','name':'Same Name'}]
72 self._fake_server_response(data)
74 self._run_service([contact_no_match, contact_do_match])
76 matchers = self.testee.get_contacts_with_match().keys()
77 assert len(matchers) == 1
78 assert len(self.testee.get_unmatched_friends()) == 0
81 def _run_service(self, contacts):
82 for contact in contacts:
83 self.testee.pre_process_contact(contact)
84 self.testee.process_friends()
85 for contact in contacts:
86 self.testee.process_contact(contact)
88 def _fake_server_response(self, data):
89 self.testee._get_friends_data = self._get_friends_data
90 self._server_response = data
92 def _get_friends_data(self):
93 return self._server_response
97 if __name__ == '__main__':