def test_picklecache_discrete_load(self): """Tests that picklecache can load data on discrete calls""" if os.path.exists('datastore.pkl'): os.unlink('datastore.pkl') pcache = PickleCache() self.assertEqual(len(pcache), 0) fhandler = open('datastore.pkl', 'w') pickle.dump({'apples': 'oranges'}, fhandler) fhandler.close() pcache.load() self.assertEqual(pcache['apples'], 'oranges')
def test_picklecache_data(self): """Tests the default value of __data""" if os.path.exists('datastore.pkl'): os.unlink('datastore.pkl') cacher = PickleCache() self.assertEqual(cacher._PickleCache__data, {})
def test_picklecache_flush_data(self): """Tests that flush saves data to the file object.""" if os.path.exists('datastore.pkl'): os.unlink('datastore.pkl') data = ['Winkin', 'Blinkin', 'Nod'] fpath = 'datastore.pkl' pcache = PickleCache(fpath) pcache['data'] = data pcache.flush() fhandler = open(fpath, 'r') retrieved = pickle.load(fhandler) fhandler.close() self.assertEqual(retrieved, {'data': data})
def test_picklecache_delete(self): """Tests the delete functionality of PickleCache.""" pcache = PickleCache() pcache['foo'] = 'bar' self.assertEqual(pcache['foo'], 'bar') del pcache['foo'] with self.assertRaises(KeyError): pcache['foo']
def test_picklecache_autosync_del(self): """Tests that autosync triggers when deleting an item.""" if os.path.exists('datastore.pkl'): os.unlink('datastore.pkl') data = ['Winkin', 'Blinkin', 'Nod'] fpath = 'datastore.pkl' pcache = PickleCache(fpath) pcache['data'] = data pcache.flush() pcache.autosync = True del pcache['data'] fhandler = open(fpath, 'r') retrieved = pickle.load(fhandler) fhandler.close() self.assertEqual(retrieved, {})
def test_picklecache_load_constructor(self): """Tests that PickleCache load method loads the pickled data.""" if os.path.exists('datastore.pkl'): os.unlink('datastore.pkl') fhandler = open('datastore.pkl', 'w') pickle.dump({'toads': ['Rash', 'Zitz', 'Pimple']}, fhandler) fhandler.close() pcache = PickleCache() self.assertEqual(pcache['toads'], ['Rash', 'Zitz', 'Pimple'])
def test_picklecache_get(self): """Gets a value from picklecache.""" legos = ['brick', 'axel', 'wheel', 'wing', 'figure'] pcache = PickleCache() pcache['apple'] = 'jack' pcache['legos'] = legos self.assertEqual(pcache['apple'], 'jack') self.assertIs(pcache['legos'], legos)
def test_picklecache_autosync_set(self): """Tests that autosync triggers when adding/changing an item.""" if os.path.exists('datastore.pkl'): os.unlink('datastore.pkl') data = ['Winkin', 'Blinkin', 'Nod'] fpath = 'datastore.pkl' pcache = PickleCache(fpath, autosync=True) pcache['data'] = data fhandler = open(fpath, 'r') retrieved = pickle.load(fhandler) fhandler.close() self.assertEqual(retrieved, {'data': data})
def test_picklecache_file_path_default(self): """Tests the default value of the file_path parameter""" cacher = PickleCache() self.assertEqual(cacher._PickleCache__file_path, 'datastore.pkl')
def test_picklecache_autosync(self): """Tests the value of the autosync attribute""" cacher = PickleCache() self.assertFalse(cacher.autosync) cacher = PickleCache(autosync=True) self.assertTrue(cacher.autosync)
def test_picklecache_file_path(self): """Tests the value of the file_path autosync""" cacher = PickleCache('banana.pkl') self.assertEqual(cacher._PickleCache__file_path, 'banana.pkl')
def test_picklecache_len(self): cacher = PickleCache() cacher['apple'] = 'jack' cacher['legos'] = ['brick', 'axel', 'wheel', 'wing', 'figure'] self.assertEqual(len(cacher), 2)
def test_picklecache_set(self): cacher = PickleCache() cacher['apple'] = 'jack' self.assertEqual(cacher._PickleCache__data['apple'], 'jack')
def test_picklecache_get_missing(self): """Confirm that it throws a KeyError when a key is missing.""" pcache = PickleCache() with self.assertRaises(KeyError): pcache['apple']