Mercurial > daily-edition
changeset 7:98f1da3b585c
added WhoisiServer class and tests
author | Atul Varma <varmaa@toolness.com> |
---|---|
date | Thu, 31 Dec 2009 13:33:28 -0800 |
parents | 5a9e578c80fa |
children | 7b6bec689805 |
files | test.py whoisi_cache.py |
diffstat | 2 files changed, 70 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- a/test.py Thu Dec 31 13:08:59 2009 -0800 +++ b/test.py Thu Dec 31 13:33:28 2009 -0800 @@ -3,6 +3,8 @@ import whoisi_cache +json = whoisi_cache.json + class FakeWhoisiServer(object): def __init__(self, people): self._people = people @@ -26,6 +28,38 @@ self._validate_index(person) return {'person': deepcopy(self._people[person-1])} +class WhoisiServerTests(unittest.TestCase): + def setUp(self): + unittest.TestCase.setUp(self) + self.urls = {} + self.server = whoisi_cache.WhoisiServer(url="http://foo/", + urlopen=self.fake_open) + + def fake_open(self, url): + return json.dumps(self.urls[url]) + + def testGetMaxPersonIDWorks(self): + self.urls = { + 'http://foo/api/getMaxPersonID?app=f': ['zap'] + } + self.assertEqual(self.server.get_max_person_id(app="f"), + ['zap']) + + def testGetPeopleWorks(self): + self.urls = { + 'http://foo/api/getPeople?app=f&first=1&last=2': ['bop'] + } + self.assertEqual(self.server.get_people(app="f", first=1, + last=2), + ['bop']) + + def testGetPersonWorks(self): + self.urls = { + 'http://foo/api/getPerson?app=f&person=1': ['blap'] + } + self.assertEqual(self.server.get_person(app="f", person=1), + ['blap']) + class WhoisiCacheTests(unittest.TestCase): def testRefreshPeopleWorks(self): people = [{'name': "bobbo"}, {'name': 'gobbo'}]
--- a/whoisi_cache.py Thu Dec 31 13:08:59 2009 -0800 +++ b/whoisi_cache.py Thu Dec 31 13:33:28 2009 -0800 @@ -1,4 +1,14 @@ +import logging +import urllib +import urllib2 + +try: + import json +except ImportError: + import simplejson as json + MAX_PEOPLE_REQ_SIZE = 100 +DEFAULT_URL = "http://whoisi.com/" def split_seq(seq, size): """ @@ -9,6 +19,32 @@ return [seq[i:i+size] for i in range(0, len(seq), size)] +class WhoisiServer(object): + def __init__(self, url=DEFAULT_URL, urlopen=urllib2.urlopen): + self._urlopen = urlopen + self.url = url + + def _call_api_method(self, name, query_args): + full_url = "%sapi/%s?%s" % (self.url, name, + urllib.urlencode(query_args)) + logging.debug('retrieving %s' % full_url) + return json.loads(self._urlopen(full_url)) + + def get_person(self, app, person): + return self._call_api_method('getPerson', + [('app', app), + ('person', str(person))]) + + def get_people(self, app, first, last): + return self._call_api_method('getPeople', + [('app', app), + ('first', str(first)), + ('last', str(last))]) + + def get_max_person_id(self, app): + return self._call_api_method('getMaxPersonID', + [('app', app)]) + class WhoisiCache(object): APP_NAME = "whoisi-cache"