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) 
Exemple #4
0
    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=?')
Exemple #5
0
    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 
Exemple #6
0
 def test_setitem_typeerror(self):
     foo = database.dbdict('/tmp/test_guachi')
     self.assertRaises(sqlite3.InterfaceError, foo.__setitem__, 'bar', {'a':'b'})
Exemple #7
0
 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'})
Exemple #8
0
 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)
Exemple #9
0
 def test_get_item(self):
     foo = database.dbdict('/tmp/test_guachi')
     foo['bar'] = 'beer'
     self.assertEqual(foo['bar'], u'beer')
Exemple #10
0
 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')
Exemple #11
0
 def test_keys_get_value(self):
     foo = database.dbdict('/tmp/test_guachi')
     foo['bar'] = 'value'
     self.assertEqual(foo.get('bar'), 'value')
Exemple #12
0
 def test_key_empty(self):
     foo = database.dbdict('/tmp/test_guachi')
     self.assertEqual(foo.keys(), [])
Exemple #13
0
 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
Exemple #14
0
 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
Exemple #15
0
 def integrity_check(self):
     """Verify the database is OK"""
     db = dbdict(self.path)
     return db._integrity_check()
Exemple #16
0
 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
Exemple #17
0
 def get_dict_config(self):
     """Returns the full stored configuration values as a dictionary"""
     db = dbdict(self.path)
     return db.get_all()
Exemple #18
0
 def test_delitem_keyerror(self):
     foo = database.dbdict('/tmp/test_guachi')
     self.assertRaises(KeyError, foo.__delitem__, 'meh')
Exemple #19
0
 def test_keys(self):
     foo = database.dbdict('/tmp/test_guachi')
     foo['bar'] = 'beer'
     self.assertEqual(foo.keys(), ['bar'])
Exemple #20
0
 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')
Exemple #21
0
 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 
Exemple #22
0
 def test_keys_get_none(self):
     foo = database.dbdict('/tmp/test_guachi')
     self.assertEqual(foo.get('does-not-exist'), None)
Exemple #23
0
 def get_default_options(self):
     """Returns the default options we hold"""
     db = dbdict(self.path, table='_guachi_defaults') 
     return db
Exemple #24
0
 def test_keys_get_value_w_default(self):
     foo = database.dbdict('/tmp/test_guachi')
     self.assertTrue(foo.get('foobar', True))
Exemple #25
0
 def test_create_database(self):
     foo = database.dbdict('/tmp/test_guachi')
     self.assertTrue(os.path.isfile('/tmp/test_guachi'))
Exemple #26
0
 def test_integrity_check_true(self):
     foo = database.dbdict('/tmp/test_guachi')
     self.assertTrue(foo._integrity_check())
Exemple #27
0
 def __call__(self):
     db = dbdict(self.path)
     return db