# HG changeset patch # User Atul Varma # Date 1261637219 28800 # Node ID e61a42133a33653651cc1c5b1cd95b891d7a5040 # Parent f5d7f4b40537fec3f8d417e3bf5f878245743094 added test to ensure observers work diff -r f5d7f4b40537 -r e61a42133a33 bzapi.py --- a/bzapi.py Wed Dec 23 22:18:00 2009 -0800 +++ b/bzapi.py Wed Dec 23 22:46:59 2009 -0800 @@ -49,17 +49,17 @@ sanitize(item) class CachedSearch(object): - def __init__(self, api, collection, observers=None, **kwargs): - if observers is None: - observers = [] - + def __init__(self, api, collection, **kwargs): + self.observers = [] self.options = kwargs - self.observers = observers self.bugs = collection self.api = api self._update_last_update() + def add_observer(self, observer): + self.observers.append(observer) + def _update_last_update(self): bugs = self.bugs.find().sort("retrieved_time", pymongo.ASCENDING).limit(1) diff -r f5d7f4b40537 -r e61a42133a33 test.py --- a/test.py Wed Dec 23 22:18:00 2009 -0800 +++ b/test.py Wed Dec 23 22:46:59 2009 -0800 @@ -79,13 +79,29 @@ class CachedSearchTests(_MongoTestCase): class FakeApi(object): def __init__(self): - self._urls = {} + self._bugs = {} - def set(self, url, params, response): - self._urls[url + repr(params)] = response + def add_fake_bug(self, **info): + for name in ['last_change_time', 'creation_time']: + if name not in info: + info[name] = '2009-06-11T22:31:24Z' + self._bugs[info['id']] = info def get(self, url, **kwargs): - return self._urls[url + repr(kwargs)] + if url != '/bug': + raise ValueError(url) + if 'id' in kwargs and kwargs['id_mode'] == 'include': + ids = kwargs['id'].split(",") + bugs = [self._bugs[bugid] for bugid in ids] + else: + bugs = self._bugs.values() + bugs = deepcopy(bugs) + if kwargs.get('comments') != '1': + for info in bugs: + if 'comments' in info: + del info['comments'] + return {'data': {'bugs': bugs}, + 'date': datetime.utcnow()} _collections = ['bugs'] @@ -95,29 +111,25 @@ self.search = bzapi.CachedSearch(self.api, testdb.bugs) def test_update_with_no_bugs(self): - self.api.set('/bug', {}, {'data': {'bugs': []}}) self.search.update() self.assertEqual(testdb.bugs.find().count(), 0) def test_update_with_bug(self): - bug = {'id': '1034', - 'creation_time': '2009-06-11T22:31:24Z', - 'last_change_time': '2009-06-11T22:31:24Z'} - response = {'data': {'bugs': [bug]}, - 'date': datetime.utcnow()} - - self.api.set('/bug', {}, deepcopy(response)) - - bug['comments'] = 'blah' - - self.api.set('/bug', {'id_mode': 'include', - 'id': '1034', - 'comments': '1', - 'history': '1'}, deepcopy(response)) - + self.api.add_fake_bug(id='1034', comments='blah') self.search.update() self.assertEqual(testdb.bugs.find({'comments': 'blah'}).count(), 1) + def test_observers_are_notified(self): + notifications = [] + class Observer(object): + def notify(self, info): + notifications.append(info) + self.api.add_fake_bug(id='1034') + self.search.add_observer(Observer()) + self.search.update() + self.assertEqual(len(notifications), 1) + self.assertEqual(notifications[0]['bug'], '1034') + class ApiTests(_MongoTestCase): class FakeOpenUrl(object): def __init__(self):