Log In
New Account
  
Home My Page Project Cloud Code Snippets Project Openings Hermes
Summary Lists News SCM
authorFredrik Wendt <fredrik@wendt.se>
Sat, 5 Jun 2010 21:34:16 +0000 (23:34 +0200)
committerFredrik Wendt <fredrik@wendt.se>
Sat, 5 Jun 2010 21:34:16 +0000 (23:34 +0200)
Signed-off-by: Fredrik Wendt <fredrik@wendt.se>

package/src/org/maemo/hermes/engine/test_facebook.py [new file with mode: 0644]
package/src/org/maemo/hermes/engine/test_gravatar.py [new file with mode: 0644]
package/src/org/maemo/hermes/engine/test_syncjob.py [new file with mode: 0644]

diff --git a/package/src/org/maemo/hermes/engine/test_facebook.py b/package/src/org/maemo/hermes/engine/test_facebook.py
new file mode 100644 (file)
index 0000000..2194a7a
--- /dev/null
@@ -0,0 +1,98 @@
+from 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()
diff --git a/package/src/org/maemo/hermes/engine/test_gravatar.py b/package/src/org/maemo/hermes/engine/test_gravatar.py
new file mode 100644 (file)
index 0000000..819274b
--- /dev/null
@@ -0,0 +1,80 @@
+from gravatar.service import Service
+from friend import Friend 
+import unittest
+
+class FakeContact():
+    def __init__(self, addr):
+        self.emails = addr
+    def get_emails(self):
+        return self.emails
+    def get_name(self):
+        return self.emails[0]
+    
+class TestGravatarService(unittest.TestCase):
+    
+    def setUp(self):
+        self._setUp('maemohermes@wendt.se', 'b14ec179822b')
+        
+    def test_main_flow(self):
+        self.testee.pre_process_contact(self.existing_contact)
+        self.testee.pre_process_contact(self.missing_contact)
+        self.testee.process_friends()
+        self.testee.process_contact(self.existing_contact)
+        self.testee.process_contact(self.missing_contact)
+        
+        friends = self.testee.get_friends()
+        contacts = self.testee.get_contacts_with_match()
+        assert len(friends) == 1
+        assert len(contacts) == 1
+        assert self.missing_contact not in contacts.keys()
+        assert self.existing_contact in contacts.keys()
+        assert friends[0].get_name() == self.existing_contact.get_name()
+        
+        
+    def test_that_a_person_with_two_addresses_and_one_gravatar_works(self):
+        self._fake_server_response({self.missing_address: None, self.existing_address: "http://url.to.img/"})
+        
+        self.testee.pre_process_contact(self.multiple_contact)
+        self.testee.process_friends()
+        self.testee.process_contact(self.multiple_contact)
+        
+        friends = self.testee.get_friends()
+        contacts = self.testee.get_contacts_with_match()
+        assert len(friends) == 1
+        assert len(contacts) == 1
+        assert self.multiple_contact in contacts
+        assert self.missing_contact not in contacts
+        assert self.existing_contact not in contacts.keys()
+        assert friends[0].get_name() == self.existing_contact.get_name()
+        
+        
+    def _fake_server_response(self, map):
+        self.testee._get_hash_info_from_server = self._get_hash_info_from_server
+        # in real results the addresses hashed, so we'll add that here, and keep originals for easier debugging/inspection
+        for key in map.keys():
+            hash = self.testee._get_hash_for_address(key)
+            map[hash] = map[key]
+        self._server_response = map
+    
+    def _get_hash_info_from_server(self, args, url):
+        self._server_args = args
+        self._server_url = url
+        return self._server_response
+
+    def _setUp(self, email, key):
+        self.testee = Service(email, key)
+        
+        self.existing_address = 'fredrik@wendt.se'
+        self.existing_contact = FakeContact([self.existing_address])
+        self.existing_friend = Friend("Fredrik Wendt")
+        
+        self.missing_address = 'will_not_ever_exist_i_truly_hope_at_least@wendt.se'
+        self.missing_contact = FakeContact([self.missing_address])
+        self.missing_friend = Friend("Unknown Person")
+        
+        self.multiple_contact = FakeContact([self.existing_address, self.missing_address])
+        self.multiple_friend = Friend("Another Person")
+
+    
+if __name__ == '__main__':
+    unittest.main()
diff --git a/package/src/org/maemo/hermes/engine/test_syncjob.py b/package/src/org/maemo/hermes/engine/test_syncjob.py
new file mode 100644 (file)
index 0000000..076e7d0
--- /dev/null
@@ -0,0 +1,42 @@
+import unittest
+from syncjob import Syncjob
+
+class FakeContact():
+    def __init__(self, name, addr):
+        self.name = name
+        self.emails = [addr]
+    def get_emails(self):
+        return self.emails
+    def get_name(self):
+        return self.name
+    
+class FakeService():
+    def __init__(self):
+        self.contacts_preprocessed = []
+        self.contacts_processed = []
+    def get_name(self):
+        return "fake service"
+    def pre_process_contact(self, contact):
+        self.contacts_preprocessed.append(contact)
+    def process_friends(self):
+        pass
+    def process_contact(self, contact):
+        self.contacts_processed.append(contact)
+     
+    
+class TestSyncJob(unittest.TestCase):
+    
+    def setUp(self):
+        self.service = FakeService() 
+        self.services = [self.service]
+        self.contact = FakeContact('Fredrik Wendt', 'fredrik@wendt.se')
+        self.contacts = [self.contact]
+        self.testee = Syncjob(self.services, self.contacts)
+        
+    def test_main_flow(self):
+        self.testee.run()
+        assert self.contact in self.service.contacts_preprocessed
+        assert self.contact in self.service.contacts_processed
+    
+if __name__ == '__main__':
+    unittest.main()

Terms of Use    Privacy Policy    Contribution Guidelines    Feedback

Powered By GForge Collaborative Development Environment