1 from org.maemo.hermes.engine.facebook.service import Service
2 from org.maemo.hermes.engine.names import canonical
3 from org.maemo.hermes.engine.friend import Friend
10 def __init__(self, name, addr, id=None):
13 self.id = id or FakeContact.id_counter
14 FakeContact.id_counter = FakeContact.id_counter + 1;
19 def get_identifiers(self):
20 return [canonical(self.name)]
22 class TestFacebookService(unittest.TestCase):
25 self.testee = Service(None)
28 def test_that_process_known_contact_returns_friend_object(self):
29 known_url = 'http://www.facebook.com/profile.php?id=123456'
30 known_contact = FakeContact('Facebook Junkie', [known_url])
31 self._fake_server_response([{'uid':'123456','name':'Facebook Junkie'}])
33 self.testee.process_friends()
34 result = self.testee.process_contact(known_contact)
35 assert isinstance(result, Friend)
38 def test_main_flow_one_match_by_url_one_by_name(self):
40 self.existing_address = 'http://www.facebook.com/profile.php?id=123456'
41 self.existing_contact = FakeContact("Facebook Person", [self.existing_address])
42 existing_fake = {'uid':'123456','name':'Name Doesnt Match but URL Does'}
44 self.other_address = 'http://twitter.com/not/correct/site'
45 self.other_contact = FakeContact("Twitter Person", [self.other_address])
46 other_fake = {'uid':'123','name':self.other_contact.get_name()}
48 self.none_contact = FakeContact("No URLson", [])
50 fake_data = [existing_fake, other_fake]
51 self._fake_server_response(fake_data)
54 self._run_service([self.existing_contact, self.other_contact, self.none_contact])
57 friends = self.testee.get_friends()
58 contacts = self.testee.get_contacts_with_match()
59 assert len(friends) == 2
60 assert len(contacts) == 2
61 assert self.other_contact in contacts
62 assert self.other_contact == contacts[self.other_contact].get_contact()
63 assert self.existing_contact in contacts
64 assert self.existing_contact == contacts[self.existing_contact].get_contact()
65 assert self.none_contact not in contacts
68 def test_name_collision_avoided_by_previous_matching(self):
69 contact_do_match = FakeContact("Same Name", ["http://www.facebook.com/profile.php?id=123"], 1);
70 contact_no_match = FakeContact("Same Name", [None], 2)
72 data = [{'uid':'123','name':'Same Name'}]
73 self._fake_server_response(data)
75 self._run_service([contact_no_match, contact_do_match])
77 assert len(self.testee.get_unmatched_friends()) == 0
78 matchers = self.testee.get_contacts_with_match().keys()
79 assert len(matchers) == 1
80 assert matchers[0].id == 1
83 def test_name_collision_avoided_only_one_person_matched(self):
84 contact_do_match = FakeContact("Same Name", ["http://twitter.com/same_name"]);
85 contact_no_match = FakeContact("Same Name", [None])
87 data = [{'uid':'123','name':'Same Name'}]
88 self._fake_server_response(data)
90 self._run_service([contact_no_match, contact_do_match])
92 matchers = self.testee.get_contacts_with_match().keys()
93 assert len(matchers) == 1
94 assert len(self.testee.get_unmatched_friends()) == 0
97 def _run_service(self, contacts):
98 for contact in contacts:
99 self.testee.pre_process_contact(contact)
100 self.testee.process_friends()
101 for contact in contacts:
102 self.testee.process_contact(contact)
104 def _fake_server_response(self, data):
105 self.testee._get_friends_data = self._get_friends_data
106 self._server_response = data
108 def _get_friends_data(self):
109 return self._server_response
113 if __name__ == '__main__':