Dependency inject service ID, so that it can be stamped on friends and
[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 from org.maemo.hermes.engine.friend import Friend
4 import unittest
5
6
7
8 class FakeContact():
9     id_counter = 0
10     def __init__(self, name, addr, id=None):
11         self.name = name;
12         self.urls = addr
13         self.id = id or FakeContact.id_counter
14         FakeContact.id_counter = FakeContact.id_counter + 1;
15     def get_urls(self):
16         return self.urls
17     def get_name(self):
18         return self.name
19     def get_identifiers(self):
20         return [canonical(self.name)]
21     
22 class TestFacebookService(unittest.TestCase):
23     
24     def setUp(self):
25         self.testee = Service("facebook", None)
26         
27         
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'}])
32         
33         self.testee.process_friends()
34         result = self.testee.process_contact(known_contact)
35         assert isinstance(result, Friend)
36
37
38     def test_main_flow_one_match_by_url_one_by_name(self):
39         # arrange
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'}
43         
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()}
47         
48         self.none_contact = FakeContact("No URLson", [])
49         
50         fake_data = [existing_fake, other_fake]
51         self._fake_server_response(fake_data)
52
53         # act        
54         self._run_service([self.existing_contact, self.other_contact, self.none_contact])
55         
56         # assert
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
66         
67         
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)
71         
72         data = [{'uid':'123','name':'Same Name'}]
73         self._fake_server_response(data)
74         
75         self._run_service([contact_no_match, contact_do_match])
76         
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
81
82       
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])
86         
87         data = [{'uid':'123','name':'Same Name'}]
88         self._fake_server_response(data)
89         
90         self._run_service([contact_no_match, contact_do_match])
91         
92         matchers = self.testee.get_contacts_with_match().keys()
93         assert len(matchers) == 1
94         assert len(self.testee.get_unmatched_friends()) == 0
95         
96         
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)
103         
104     def _fake_server_response(self, data):
105         self.testee._get_friends_data = self._get_friends_data
106         self._server_response = data
107     
108     def _get_friends_data(self):
109         return self._server_response
110
111
112     
113 if __name__ == '__main__':
114     unittest.main()