c1725b9260dc84710acc05ab025013da32b98f78
[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 class FakeContact():
8     id_counter = 0
9     def __init__(self, name, addr, id=None):
10         self.name = name;
11         self.urls = addr
12         self.id = id or FakeContact.id_counter
13         FakeContact.id_counter = FakeContact.id_counter + 1;
14     def get_urls(self):
15         return self.urls
16     def get_name(self):
17         return self.name
18     def get_identifiers(self):
19         return [canonical(self.name)]
20     
21
22 class TestFacebookService(unittest.TestCase):
23     
24     def setUp(self):
25         self.testee = Service("facebook", None)
26         
27         
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'}])
32         
33         self.testee.process_friends()
34         result = self.testee.process_contact(known_contact)
35         assert isinstance(result, Friend)
36
37
38     def test_that_process_contact_returns_None_for_unknown_contact(self):
39         known_contact = FakeContact('Facebook Junkie', [])
40         self._fake_server_response([])
41         
42         self.testee.process_friends()
43         result = self.testee.process_contact(known_contact)
44         assert result is None
45
46
47     def test_main_flow_one_match_by_url_one_by_name(self):
48         # arrange
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'}
52         
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()}
56         
57         self.none_contact = FakeContact("No URLson", [])
58         
59         fake_data = [existing_fake, other_fake]
60         self._fake_server_response(fake_data)
61
62         # act        
63         self._run_service([self.existing_contact, self.other_contact, self.none_contact])
64         
65         # assert
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
75         
76         
77     def test_name_collision_avoided_by_previous_matching(self):
78         name = "Same Name"
79         contact_do_match = FakeContact(name, ["http://www.facebook.com/profile.php?id=123"], 1);
80         contact_no_match = FakeContact(name, [None], 2)
81         
82         data = [{'uid':'123','name':name}]
83         self._fake_server_response(data)
84         
85         self._run_service([contact_no_match, contact_do_match])
86         
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
91
92       
93     def test_name_collision_avoided_only_one_person_matched(self):
94         name = "Same Name"
95         contact_do_match = FakeContact(name, ["Contact 1"]);
96         contact_no_match = FakeContact(name, ["Contact 2"])
97         
98         data = [{'uid':'123','name':name}]
99         self._fake_server_response(data)
100         
101         self._run_service([contact_no_match, contact_do_match])
102         
103         matchers = self.testee.get_contacts_with_match().keys()
104         assert len(matchers) == 1
105         assert len(self.testee.get_unmatched_friends()) == 0
106         
107         
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)
114         
115     def _fake_server_response(self, data):
116         self.testee._get_friends_data = self._get_friends_data
117         self._server_response = data
118     
119     def _get_friends_data(self):
120         return self._server_response
121
122
123     
124 if __name__ == '__main__':
125     unittest.main()