#!/usr/bin/python '''Unit tests for cacher_mod''' import unittest import gdbm_ctypes import cacher_mod class test_cacher_mod(unittest.TestCase): # pylint: disable=R0904 # R0904: We're a module inheriting from unittest.TestCase; of course we have lots of public methods '''Unit tests for cacher_mod''' def test_open_and_close(self): '''Test a single file''' dummy = self cacher = cacher_mod.Cacher(max_entries = 2) cacher['a'] = gdbm_ctypes.open('a.gdbm', 'w') cacher['a'].close() def test_open_value_close(self): '''Test a single file with a single value''' dummy = self cacher = cacher_mod.Cacher(max_entries = 2) cacher['a'] = gdbm_ctypes.open('a.gdbm', 'n') cacher['a']['key'] = 'value' cacher['a'].close() def test_2_open_value_close(self): '''Test 2 files with values''' dummy = self cacher = cacher_mod.Cacher(max_entries = 2) cacher['a'] = gdbm_ctypes.open('a.gdbm', 'n') cacher['b'] = gdbm_ctypes.open('b.gdbm', 'n') cacher['a']['key'] = 'value' cacher['b']['key2'] = 'value2' cacher['a'].close() cacher['b'].close() def test_3_open_value_close(self): '''Test 3 files with values in a cacher that'll only hold 2 - close the remaining two, one at a time''' dummy = self cacher = cacher_mod.Cacher(max_entries = 2) assert len(cacher) == 0, len(cacher) cacher['a'] = gdbm_ctypes.open('a.gdbm', 'n') assert len(cacher) == 1, len(cacher) cacher['b'] = gdbm_ctypes.open('b.gdbm', 'n') assert len(cacher) == 2, len(cacher) cacher['c'] = gdbm_ctypes.open('c.gdbm', 'n') assert len(cacher) == 2, len(cacher) try: cacher['a']['key'] = 'value' except KeyError: pass else: raise AssertionError cacher['b']['key2'] = 'value2' cacher['c']['key3'] = 'value3' cacher['b'].close() cacher['c'].close() def test_3_open_value_agg_close(self): '''Test 3 files with values in a cacher that'll only hold 2 - close en masse''' dummy = self cacher = cacher_mod.Cacher(max_entries = 2) assert len(cacher) == 0, len(cacher) cacher['a'] = gdbm_ctypes.open('a.gdbm', 'n') assert len(cacher) == 1, len(cacher) cacher['b'] = gdbm_ctypes.open('b.gdbm', 'n') assert len(cacher) == 2, len(cacher) cacher['c'] = gdbm_ctypes.open('c.gdbm', 'n') assert len(cacher) == 2, len(cacher) try: cacher['a']['key'] = 'value' except KeyError: pass else: raise AssertionError cacher['b']['key2'] = 'value2' cacher['c']['key3'] = 'value3' cacher.close() def test_3_open_part_then_agg_close(self): '''3 files, two values, close en masse''' dummy = self cacher = cacher_mod.Cacher(max_entries = 2) assert len(cacher) == 0, len(cacher) cacher['a'] = gdbm_ctypes.open('a.gdbm', 'n') assert len(cacher) == 1, len(cacher) cacher['b'] = gdbm_ctypes.open('b.gdbm', 'n') assert len(cacher) == 2, len(cacher) cacher['c'] = gdbm_ctypes.open('c.gdbm', 'n') assert len(cacher) == 2, len(cacher) try: cacher['a']['key'] = 'value' except KeyError: pass else: raise AssertionError cacher['b']['key2'] = 'value2' cacher.close() unittest.main()