9603143a0bb8a93b3f04650e7cecc460b0bb5b0d
[hermes] / package / test / unit / test_facebook.py
1 from org.maemo.hermes.engine.facebook.service import Service
2 from org.maemo.hermes.engine.names import canonical
3 import unittest
4
5
6 class FakeContact():
7     id_counter = 0
8     def __init__(self, name, addr, id=None):
9         self.name = name;
10         self.urls = addr
11         self.id = id or FakeContact.id_counter
12         FakeContact.id_counter = FakeContact.id_counter + 1;
13     def get_urls(self):
14         return self.urls
15     def get_name(self):
16         return self.name
17     def get_identifiers(self):
18         return [canonical(self.name)]
19     
20 class TestFacebookService(unittest.TestCase):
21     
22     def setUp(self):
23         self.testee = Service(None)
24         
25     def test_main_flow_one_match_by_url_one_by_name(self):
26         # arrange
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'}
30         
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()}
34         
35         self.none_contact = FakeContact("No URLson", [])
36         
37         fake_data = [existing_fake, other_fake]
38         self._fake_server_response(fake_data)
39
40         # act        
41         self._run_service([self.existing_contact, self.other_contact, self.none_contact])
42         
43         # assert
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
51         
52         
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)
56         
57         data = [{'uid':'123','name':'Same Name'}]
58         self._fake_server_response(data)
59         
60         self._run_service([contact_no_match, contact_do_match])
61         
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
66         
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])
70         
71         data = [{'uid':'123','name':'Same Name'}]
72         self._fake_server_response(data)
73         
74         self._run_service([contact_no_match, contact_do_match])
75         
76         matchers = self.testee.get_contacts_with_match().keys()
77         assert len(matchers) == 1
78         assert len(self.testee.get_unmatched_friends()) == 0
79         
80         
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)
87         
88     def _fake_server_response(self, data):
89         self.testee._get_friends_data = self._get_friends_data
90         self._server_response = data
91     
92     def _get_friends_data(self):
93         return self._server_response
94
95
96     
97 if __name__ == '__main__':
98     unittest.main()