--- /dev/null
+from org.maemo.hermes.engine.facebook.service import Service
+from org.maemo.hermes.engine.names import canonical
+import unittest
+
+
+class FakeContact():
+ id_counter = 0
+ def __init__(self, name, addr, id=None):
+ self.name = name;
+ self.urls = addr
+ self.id = id or FakeContact.id_counter
+ FakeContact.id_counter = FakeContact.id_counter + 1;
+ def get_urls(self):
+ return self.urls
+ def get_name(self):
+ return self.name
+ def get_identifiers(self):
+ return [canonical(self.name)]
+
+class TestFacebookService(unittest.TestCase):
+
+ def setUp(self):
+ self.testee = Service(None)
+
+ def test_main_flow_one_match_by_url_one_by_name(self):
+ # arrange
+ self.existing_address = 'http://www.facebook.com/profile.php?id=123456'
+ self.existing_contact = FakeContact("Facebook Person", [self.existing_address])
+ existing_fake = {'uid':'123456','name':'Name Doesnt Match but URL Does'}
+
+ self.other_address = 'http://twitter.com/not/correct/site'
+ self.other_contact = FakeContact("Twitter Person", [self.other_address])
+ other_fake = {'uid':'123','name':self.other_contact.get_name()}
+
+ self.none_contact = FakeContact("No URLson", [])
+
+ fake_data = [existing_fake, other_fake]
+ self._fake_server_response(fake_data)
+
+ # act
+ self._run_service([self.existing_contact, self.other_contact, self.none_contact])
+
+ # assert
+ friends = self.testee.get_friends()
+ contacts = self.testee.get_contacts_with_match()
+ assert len(friends) == 2
+ assert len(contacts) == 2
+ assert self.other_contact in contacts
+ assert self.existing_contact in contacts
+ assert self.none_contact not in contacts
+
+
+ def test_name_collision_avoided_by_previous_matching(self):
+ contact_do_match = FakeContact("Same Name", ["http://www.facebook.com/profile.php?id=123"], 1);
+ contact_no_match = FakeContact("Same Name", [None], 2)
+
+ data = [{'uid':'123','name':'Same Name'}]
+ self._fake_server_response(data)
+
+ self._run_service([contact_no_match, contact_do_match])
+
+ assert len(self.testee.get_unmatched_friends()) == 0
+ matchers = self.testee.get_contacts_with_match().keys()
+ assert len(matchers) == 1
+ assert matchers[0].id == 1
+
+ def test_name_collision_avoided_only_one_person_matched(self):
+ contact_do_match = FakeContact("Same Name", ["http://twitter.com/same_name"]);
+ contact_no_match = FakeContact("Same Name", [None])
+
+ data = [{'uid':'123','name':'Same Name'}]
+ self._fake_server_response(data)
+
+ self._run_service([contact_no_match, contact_do_match])
+
+ matchers = self.testee.get_contacts_with_match().keys()
+ assert len(matchers) == 1
+ assert len(self.testee.get_unmatched_friends()) == 0
+
+
+ def _run_service(self, contacts):
+ for contact in contacts:
+ self.testee.pre_process_contact(contact)
+ self.testee.process_friends()
+ for contact in contacts:
+ self.testee.process_contact(contact)
+
+ def _fake_server_response(self, data):
+ self.testee._get_friends_data = self._get_friends_data
+ self._server_response = data
+
+ def _get_friends_data(self):
+ return self._server_response
+
+
+
+if __name__ == '__main__':
+ unittest.main()