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
9 def __init__(self, name, addr, id=None):
12 self.id = id or FakeContact.id_counter
13 FakeContact.id_counter = FakeContact.id_counter + 1;
18 def get_identifiers(self):
19 return [canonical(self.name)]
22 class TestFacebookService(unittest.TestCase):
25 self.testee = Service("facebook", None)
28 def test_that_process_contact_returns_friend_object_for_known_contact(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_that_process_contact_returns_None_for_unknown_contact(self):
39 known_contact = FakeContact('Facebook Junkie', [])
40 self._fake_server_response([])
42 self.testee.process_friends()
43 result = self.testee.process_contact(known_contact)
47 def test_main_flow_one_match_by_url_one_by_name(self):
49 self.existing_address = 'http://www.facebook.com/profile.php?id=123456'
50 self.existing_contact = FakeContact("Facebook Person", [self.existing_address])
51 existing_fake = {'uid':'123456','name':'Name Doesnt Match but URL Does'}
53 self.other_address = 'http://twitter.com/not/correct/site'
54 self.other_contact = FakeContact("Twitter Person", [self.other_address])
55 other_fake = {'uid':'123','name':self.other_contact.get_name()}
57 self.none_contact = FakeContact("No URLson", [])
59 fake_data = [existing_fake, other_fake]
60 self._fake_server_response(fake_data)
63 self._run_service([self.existing_contact, self.other_contact, self.none_contact])
66 friends = self.testee.get_friends()
67 contacts = self.testee.get_contacts_with_match()
68 assert len(friends) == 2
69 assert len(contacts) == 2
70 assert self.other_contact in contacts
71 assert self.other_contact == contacts[self.other_contact].get_contact()
72 assert self.existing_contact in contacts
73 assert self.existing_contact == contacts[self.existing_contact].get_contact()
74 assert self.none_contact not in contacts
77 def test_name_collision_avoided_by_previous_matching(self):
79 contact_do_match = FakeContact(name, ["http://www.facebook.com/profile.php?id=123"], 1);
80 contact_no_match = FakeContact(name, [None], 2)
82 data = [{'uid':'123','name':name}]
83 self._fake_server_response(data)
85 self._run_service([contact_no_match, contact_do_match])
87 assert len(self.testee.get_unmatched_friends()) == 0
88 matchers = self.testee.get_contacts_with_match().keys()
89 assert len(matchers) == 1
90 assert matchers[0].id == 1
93 def test_name_collision_avoided_only_one_person_matched(self):
95 contact_do_match = FakeContact(name, ["Contact 1"]);
96 contact_no_match = FakeContact(name, ["Contact 2"])
98 data = [{'uid':'123','name':name}]
99 self._fake_server_response(data)
101 self._run_service([contact_no_match, contact_do_match])
103 matchers = self.testee.get_contacts_with_match().keys()
104 assert len(matchers) == 1
105 assert len(self.testee.get_unmatched_friends()) == 0
108 def _run_service(self, contacts):
109 for contact in contacts:
110 self.testee.pre_process_contact(contact)
111 self.testee.process_friends()
112 for contact in contacts:
113 self.testee.process_contact(contact)
115 def _fake_server_response(self, data):
116 self.testee._get_friends_data = self._get_friends_data
117 self._server_response = data
119 def _get_friends_data(self):
120 return self._server_response
124 if __name__ == '__main__':