def test_set_config_dict(self): foo = ConfigMapper('/tmp') foo.set_config(DEFAULT_CONFIG) db = dbdict('/tmp/guachi.db') actual = db.get_all() expected = DEFAULT_CONFIG self.assertEqual(actual, expected)
def test_set_config_dict_empty(self): """Pass an empty dict and get defaults back""" foo = ConfigMapper('/tmp') foo.set_config({}) db = dbdict('/tmp/guachi.db') actual = db.get_all() expected ={} self.assertEqual(actual, expected)
def test_set_default_options(self): foo = ConfigMapper('/tmp') my_config = {'db_port':1234} foo.set_default_options(my_config) db = dbdict(path='/tmp/guachi.db', table='_guachi_defaults') expected = my_config actual = db.get_all() self.assertEqual(actual, expected)
def test_init(self): foo = database.dbdict('/tmp/test_guachi') self.assertEqual(foo.db_filename, '/tmp/test_guachi') self.assertEqual(foo.table, '_guachi_data') self.assertEqual(foo.select_value, 'SELECT value FROM _guachi_data WHERE key=?') self.assertEqual(foo.select_key, 'SELECT key FROM _guachi_data WHERE key=?') self.assertEqual(foo.update_value, 'UPDATE _guachi_data SET value=? WHERE key=?') self.assertEqual(foo.insert_key_value, 'INSERT INTO _guachi_data (key,value) VALUES (?,?)') self.assertEqual(foo.delete_key, 'DELETE FROM _guachi_data WHERE key=?')
def set_config(self, configuration=None): """Accepts a dictionary or a file to set persistent configurations""" mapped_ini = self.get_ini_options() mapped_defaults = self.get_default_options() # First make sure that whatever we get, gets translated # into a dictionary dict_match = DictMatch(configuration, mapped_ini, mapped_defaults) dict_config = dict_match.options() if len(dict_config.items()) > 0: db = dbdict(self.path) for key, value in dict_config.items(): db[key] = value
def test_setitem_typeerror(self): foo = database.dbdict('/tmp/test_guachi') self.assertRaises(sqlite3.InterfaceError, foo.__setitem__, 'bar', {'a':'b'})
def test_close_db(self): foo = database.dbdict('/tmp/test_guachi') foo['bar'] = 'beer' foo._close() self.assertRaises(sqlite3.ProgrammingError, foo.__setitem__, 'bar', {'a':'b'})
def test_setitem_update(self): """If it already exists, you need to do an update""" foo = database.dbdict('/tmp/test_guachi') foo['a'] = 1 foo['a'] = 2 self.assertEqual(foo['a'], 2)
def test_get_item(self): foo = database.dbdict('/tmp/test_guachi') foo['bar'] = 'beer' self.assertEqual(foo['bar'], u'beer')
def test_init_guachi_table(self): """Make sure we can check other tables""" foo = database.dbdict('/tmp/test_guachi', table='_guachi_options') self.assertEqual(foo.table, '_guachi_options')
def test_keys_get_value(self): foo = database.dbdict('/tmp/test_guachi') foo['bar'] = 'value' self.assertEqual(foo.get('bar'), 'value')
def test_key_empty(self): foo = database.dbdict('/tmp/test_guachi') self.assertEqual(foo.keys(), [])
def get_ini_options(self): """Returns the dictionary that maps INI style options to dictionary options""" db = dbdict(self.path, table='_guachi_options') return db
def set_default_options(self, dictionary): """Maps the default values that we can fill in if keys are empty""" db = dbdict(self.path, table='_guachi_defaults') for key, value in dictionary.items(): db[key] = value
def integrity_check(self): """Verify the database is OK""" db = dbdict(self.path) return db._integrity_check()
def stored_config(self): """Access the DB directly for pay-as-you-go configuration needs You get an empty dict but with access to any keys when requested """ db = dbdict(self.path) return db
def get_dict_config(self): """Returns the full stored configuration values as a dictionary""" db = dbdict(self.path) return db.get_all()
def test_delitem_keyerror(self): foo = database.dbdict('/tmp/test_guachi') self.assertRaises(KeyError, foo.__delitem__, 'meh')
def test_keys(self): foo = database.dbdict('/tmp/test_guachi') foo['bar'] = 'beer' self.assertEqual(foo.keys(), ['bar'])
def test_delitem(self): foo = database.dbdict('/tmp/test_guachi') foo['bar'] = 'beer' self.assertEqual(foo['bar'], 'beer') del foo['bar'] self.assertRaises(KeyError, foo.__delitem__, 'bar')
def set_ini_options(self, dictionary): """Maps your INI configuration keys to dictionary keys""" db = dbdict(self.path, table='_guachi_options') for key, value in dictionary.items(): db[key] = value
def test_keys_get_none(self): foo = database.dbdict('/tmp/test_guachi') self.assertEqual(foo.get('does-not-exist'), None)
def get_default_options(self): """Returns the default options we hold""" db = dbdict(self.path, table='_guachi_defaults') return db
def test_keys_get_value_w_default(self): foo = database.dbdict('/tmp/test_guachi') self.assertTrue(foo.get('foobar', True))
def test_create_database(self): foo = database.dbdict('/tmp/test_guachi') self.assertTrue(os.path.isfile('/tmp/test_guachi'))
def test_integrity_check_true(self): foo = database.dbdict('/tmp/test_guachi') self.assertTrue(foo._integrity_check())
def __call__(self): db = dbdict(self.path) return db