Log In
New Account
  
Home My Page Project Cloud Code Snippets Project Openings Hermes
Summary Lists News SCM
1 from org.maemo.hermes.engine.twitter.service import Service
2 from org.maemo.hermes.engine.names import canonical
3 import unittest
5 class FakeContact():
6     def __init__(self, name, addr):
7         self.name = name
8         self.urls = addr
9     def get_urls(self):
10         return self.urls
11     def get_name(self):
12         return self.name
13     def get_identifiers(self):
14         return [canonical(self.name)]
16 class FakeTweeter():
17     def __init__(self, name, screen_name, props=None):
18         self._props = {}
19         self["name"] = name
20         self["screen_name"] = screen_name
21         if props:
22             for key in props:
23                 self[key] = props[key]
24     def __setitem__(self, key, value):
25         self._props[key] = value
26     def __getattr__(self, key):
27         try:
28             return self._props[key]
29         except:
30             return ""
31         
32     
33 class TestTwitterService(unittest.TestCase):
34     
35     def setUp(self):
36         self.testee = Service(None)
37     
38     
39     def test_main_flow(self):
40         twitterName = "Twitter Name"
41         self._fake_server([FakeTweeter(twitterName, "wendtse", {"url":"http://wendt.se"})])
42         contact = FakeContact("Fredrik Wendt", ["http://twitter.com/wendtse"])
43         
44         self._exercise_service([contact])
45         
46         assert len(self.testee.get_unmatched_friends()) == 0
47         matchers = self.testee.get_contacts_with_match()
48         assert contact in matchers
49         assert twitterName == matchers[contact].get_name()
50     
51     
52     def test_name_collision_avoided_by_previous_matching(self):
53         twitterName = "Same Name"
54         known_tweeter = FakeTweeter(twitterName, "samename")
55         other_tweeter = FakeTweeter(twitterName, "otherscreenname")
56         self._fake_server([other_tweeter, known_tweeter])
57         contact = FakeContact(twitterName, [])
58         
59         self._exercise_service([contact])
60         
61         assert len(self.testee.get_unmatched_friends()) == 1
62         matchers = self.testee.get_contacts_with_match()
63         assert contact in matchers
66     def test_name_collision_avoided_only_one_person_matched(self):
67         twitter_name = "Same Name"
68         screen_name = "samename"
69         known_tweeter = FakeTweeter(twitter_name, screen_name)
70         other_tweeter = FakeTweeter(twitter_name, "otherscreenname")
71         self._fake_server([other_tweeter, known_tweeter])
72         contact = FakeContact(twitter_name, ["http://twitter.com/" + screen_name])
73         
74         self._exercise_service([contact])
75         
76         assert len(self.testee.get_unmatched_friends()) == 1
77         matchers = self.testee.get_contacts_with_match()
78         assert contact in matchers
79         assert screen_name == matchers[contact].get_nickname()
80         
82     def _exercise_service(self, contacts):
83         for contact in contacts:
84             self.testee.pre_process_contact(contact)
85         self.testee.process_friends()
86         for contact in contacts:
87             self.testee.process_contact(contact)
89     def _fake_server(self, data):
90         self._fake_data = data
91         self.testee._get_tweeters = self._get_tweeters
92         
93     def _get_tweeters(self):
94         return self._fake_data
95     
96 if __name__ == '__main__':
97     unittest.main()

Terms of Use    Privacy Policy    Contribution Guidelines    Feedback

Powered By GForge Collaborative Development Environment