Mercurial > daily-edition
changeset 5:47990d4e6df7
added cache.refresh_people()
author | Atul Varma <varmaa@toolness.com> |
---|---|
date | Thu, 31 Dec 2009 12:53:43 -0800 |
parents | 78a2da337f9f |
children | 5a9e578c80fa |
files | test.py whoisi_cache.py |
diffstat | 2 files changed, 36 insertions(+), 5 deletions(-) [+] |
line wrap: on
line diff
--- a/test.py Thu Dec 31 12:38:16 2009 -0800 +++ b/test.py Thu Dec 31 12:53:43 2009 -0800 @@ -1,4 +1,5 @@ import unittest +from copy import deepcopy import whoisi_cache @@ -9,14 +10,39 @@ def get_max_person_id(self, app): return len(self._people) + def _validate_index(self, index): + if index <= 0 or index > self.get_max_person_id(None): + raise ValueError('index %d out of range' % indexe) + def get_people(self, app, first, last): - if first == 0: - raise ValueError('do not ask for person id 0') - if last > self.get_max_person_id(app): - raise ValueError('bad last id') - return tuple(self._people[first-1:last]) + self._validate_index(first) + self._validate_index(last) + return deepcopy(tuple(self._people[first-1:last])) + + def get_person(self, app, person): + self._validate_index(person) + return deepcopy(self._people[person-1]) class WhoisiCacheTests(unittest.TestCase): + def testRefreshPeopleWorks(self): + people = [{'name': "bobbo"}, {'name': 'gobbo'}] + server = FakeWhoisiServer(people) + cache_storage = [] + cache = whoisi_cache.WhoisiCache(server=server, + storage=cache_storage) + cache.update() + self.assertEqual(people, cache_storage) + + people[0]['name'] = "bleh" + people[1]['name'] = "hmm" + + self.assertNotEqual(people[0], cache_storage[0]) + self.assertNotEqual(people[1], cache_storage[1]) + + cache.refresh_people([0]) + self.assertEqual(people[0], cache_storage[0]) + self.assertNotEqual(people[1], cache_storage[1]) + def testUpdateWorks(self): people = [] server = FakeWhoisiServer(people)
--- a/whoisi_cache.py Thu Dec 31 12:38:16 2009 -0800 +++ b/whoisi_cache.py Thu Dec 31 12:53:43 2009 -0800 @@ -17,6 +17,11 @@ self.batch_size = batch_size self.people = storage + def refresh_people(self, people_ids): + for person_id in people_ids: + person = self.server.get_person(self.APP_NAME, person_id + 1) + self.people[person_id] = person + def update(self): pid = self.server.get_max_person_id(app=self.APP_NAME) interval = range(len(self.people) + 1, pid + 1)