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_get_friends_to_create_contacts_for_works(self):
29 def run_test(expected_length):
30 self._fake_server_response([{'uid':'123456','name':'Facebook Junkie', 'birthday_date':'now'}])
32 friends = self.testee.get_friends_to_create_contacts_for()
33 assert len(friends) == expected_length
35 # default is to NOT create contacts
36 self.testee = Service("facebook", None)
39 # passing False explicitly
40 self.testee = Service("facebook", None, False)
43 # passing True to constructor
44 self.testee = Service("facebook", None, True)
48 def test_that_gftccf_returns_friends_with_birth_date(self):
49 self.testee = Service("facebook", None, True)
51 props_with_bday = {'uid':'123456','name':'Facebook Junkie', 'birthday_date':bday}
52 props_without = {'uid':'123457','name':'Johnny Secret'}
53 self._fake_server_response([props_with_bday, props_without])
56 assert len(self.testee.get_friends()) == 2
57 friends = self.testee.get_friends_to_create_contacts_for()
58 assert len(friends) == 1
59 assert friends[0].has_birthday_date()
60 assert friends[0].get_birthday_date() == bday
63 def test_that_process_contact_returns_friend_object_for_known_contact(self):
64 known_url = 'http://www.facebook.com/profile.php?id=123456'
65 known_contact = FakeContact('Facebook Junkie', [known_url])
66 self._fake_server_response([{'uid':'123456','name':'Facebook Junkie'}])
68 self.testee.process_friends()
69 result = self.testee.process_contact(known_contact)
70 assert isinstance(result, Friend)
73 def test_that_process_contact_returns_None_for_unknown_contact(self):
74 known_contact = FakeContact('Facebook Junkie', [])
75 self._fake_server_response([])
77 self.testee.process_friends()
78 result = self.testee.process_contact(known_contact)
82 def test_main_flow_one_match_by_url_one_by_name(self):
84 self.existing_address = 'http://www.facebook.com/profile.php?id=123456'
85 self.existing_contact = FakeContact("Facebook Person", [self.existing_address])
86 existing_fake = {'uid':'123456','name':'Name Doesnt Match but URL Does'}
88 self.other_address = 'http://twitter.com/not/correct/site'
89 self.other_contact = FakeContact("Twitter Person", [self.other_address])
90 other_fake = {'uid':'123','name':self.other_contact.get_name()}
92 self.none_contact = FakeContact("No URLson", [])
94 fake_data = [existing_fake, other_fake]
95 self._fake_server_response(fake_data)
98 self._run_service([self.existing_contact, self.other_contact, self.none_contact])
101 friends = self.testee.get_friends()
102 contacts = self.testee.get_contacts_with_match()
103 assert len(friends) == 2
104 assert len(contacts) == 2
105 assert self.other_contact in contacts
106 assert self.other_contact == contacts[self.other_contact].get_contact()
107 assert self.existing_contact in contacts
108 assert self.existing_contact == contacts[self.existing_contact].get_contact()
109 assert self.none_contact not in contacts
112 def test_name_collision_avoided_by_previous_matching(self):
114 contact_do_match = FakeContact(name, ["http://www.facebook.com/profile.php?id=123"], 1);
115 contact_no_match = FakeContact(name, [None], 2)
117 data = [{'uid':'123','name':name}]
118 self._fake_server_response(data)
120 self._run_service([contact_no_match, contact_do_match])
122 assert len(self.testee.get_unmatched_friends()) == 0
123 matchers = self.testee.get_contacts_with_match().keys()
124 assert len(matchers) == 1
125 assert matchers[0].id == 1
128 def test_name_collision_avoided_only_one_person_matched(self):
130 contact_do_match = FakeContact(name, ["Contact 1"]);
131 contact_no_match = FakeContact(name, ["Contact 2"])
133 data = [{'uid':'123','name':name}]
134 self._fake_server_response(data)
136 self._run_service([contact_no_match, contact_do_match])
138 matchers = self.testee.get_contacts_with_match().keys()
139 assert len(matchers) == 1
140 assert len(self.testee.get_unmatched_friends()) == 0
143 def _run_service(self, contacts):
144 for contact in contacts:
145 self.testee.pre_process_contact(contact)
146 self.testee.process_friends()
147 for contact in contacts:
148 self.testee.process_contact(contact)
150 def _fake_server_response(self, data):
151 self.testee._get_friends_data = self._get_friends_data
152 self._server_response = data
154 def _get_friends_data(self):
155 return self._server_response
159 if __name__ == '__main__':