b766d2f704827690cd80f8a475cec68a420e6be2
[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_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'}])
31             self._run_service([])
32             friends = self.testee.get_friends_to_create_contacts_for()
33             assert len(friends) == expected_length
34             
35         # default is to NOT create contacts
36         self.testee = Service("facebook", None)
37         run_test(0)
38         
39         # passing False explicitly
40         self.testee = Service("facebook", None, False)
41         run_test(0)
42         
43         # passing True to constructor
44         self.testee = Service("facebook", None, True)
45         run_test(1)
46         
47     
48     def test_that_gftccf_returns_friends_with_birth_date(self):
49         self.testee = Service("facebook", None, True)
50         bday = '1980-10-15'
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])
54         self._run_service([])
55         
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 
61         
62         
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'}])
67         
68         self.testee.process_friends()
69         result = self.testee.process_contact(known_contact)
70         assert isinstance(result, Friend)
71
72
73     def test_that_process_contact_returns_None_for_unknown_contact(self):
74         known_contact = FakeContact('Facebook Junkie', [])
75         self._fake_server_response([])
76         
77         self.testee.process_friends()
78         result = self.testee.process_contact(known_contact)
79         assert result is None
80
81
82     def test_main_flow_one_match_by_url_one_by_name(self):
83         # arrange
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'}
87         
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()}
91         
92         self.none_contact = FakeContact("No URLson", [])
93         
94         fake_data = [existing_fake, other_fake]
95         self._fake_server_response(fake_data)
96
97         # act        
98         self._run_service([self.existing_contact, self.other_contact, self.none_contact])
99         
100         # assert
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
110         
111         
112     def test_name_collision_avoided_by_previous_matching(self):
113         name = "Same Name"
114         contact_do_match = FakeContact(name, ["http://www.facebook.com/profile.php?id=123"], 1);
115         contact_no_match = FakeContact(name, [None], 2)
116         
117         data = [{'uid':'123','name':name}]
118         self._fake_server_response(data)
119         
120         self._run_service([contact_no_match, contact_do_match])
121         
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
126
127       
128     def test_name_collision_avoided_only_one_person_matched(self):
129         name = "Same Name"
130         contact_do_match = FakeContact(name, ["Contact 1"]);
131         contact_no_match = FakeContact(name, ["Contact 2"])
132         
133         data = [{'uid':'123','name':name}]
134         self._fake_server_response(data)
135         
136         self._run_service([contact_no_match, contact_do_match])
137         
138         matchers = self.testee.get_contacts_with_match().keys()
139         assert len(matchers) == 1
140         assert len(self.testee.get_unmatched_friends()) == 0
141         
142         
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)
149         
150     def _fake_server_response(self, data):
151         self.testee._get_friends_data = self._get_friends_data
152         self._server_response = data
153     
154     def _get_friends_data(self):
155         return self._server_response
156
157
158     
159 if __name__ == '__main__':
160     unittest.main()