class TestDBquery(unittest.TestCase): def setUp(self): configdb = join('..','resources', 'autoconfig_test.db') self.dbi = DBI(configdb) self.dbi.getconn() def tearDown(self): self.dbi.conn.close() def test_getConfig(self): configid = 'general' data = self.dbi.getConfig(configid) expected = 0 self.assertGreater(len(data),expected) def test_getConfigByName(self): group = 'general' test = 'BINWIDTH' expected = 10 data = self.dbi.getConfigByName(group,test) self.assertEqual(int(data),expected) def test_getConfigByName_None(self): group = 'general' test = 'BINW' expected = None data = self.dbi.getConfigByName(group,test) self.assertEqual(data,expected) def test_getConfigIds(self): data = self.dbi.getConfigIds() print('IDS:', data) self.assertGreater(len(data),0) def test_updateConfig(self): configid='general' configlist = [('BINWIDTH',10,'general'),('COLUMN','TestData','general'),('MINRANGE',0,'general'),('MAXRANGE',100,'general')] cnt = self.dbi.addConfig(configid,configlist) expected = len(configlist) self.assertEqual(expected,cnt) def test_updateConfig_Secondset(self): configid = 'test' configlist = [('BINWIDTH', 10, configid), ('COLUMN', 'TestData', configid), ('MINRANGE', 0, configid),('MAXRANGE', 100, configid)] cnt = self.dbi.addConfig(configid, configlist) expected = len(configlist) self.assertEqual(expected, cnt) def test_updateConfig_duplicate(self): configid='general' configlist = [('BINWIDTH',20,'general'),('COLUMN','TestData2','general'),('MINRANGE',0,'general'),('MAXRANGE',100,'general')] cnt = self.dbi.addConfig(configid,configlist) expected = len(configlist) self.assertEqual(expected,cnt)
class Config(ConfigPanel): def __init__(self, parent): super(Config, self).__init__(parent) self.parent = parent self.configdb = join(self.parent.resourcesdir, 'autoconfig.db') self.dbi = DBI(self.configdb) self.loadController() def loadController(self): self.controller = self.parent.controller self.currentconfig = self.parent.controller.currentconfig self.OnLoadData() def OnLoadData(self): self.dbi.getconn() #load config values cids = self.dbi.getConfigIds() if len(cids)>0: self.cboConfigid.Set(cids) #configid = self.cboConfigid.GetStringSelection() rownum =0 if self.currentconfig in cids: selection = self.cboConfigid.FindString(self.currentconfig) self.cboConfigid.SetSelection(selection) conf = self.dbi.getConfig(self.currentconfig) else: selection = self.cboConfigid.FindString(cids[0]) self.cboConfigid.SetSelection(selection) conf = self.dbi.getConfig(cids[0]) if conf is not None: for k in conf.keys(): self.m_grid1.SetCellValue(rownum, 0, k) self.m_grid1.SetCellValue(rownum, 1, conf[k]) rownum += 1 self.m_grid1.AutoSizeColumns() self.m_grid1.AutoSize() self.dbi.closeconn() def OnLoadConfig(self,event): self.dbi.getconn() #load config values configid = self.cboConfigid.GetValue() try: rownum =0 conf = self.dbi.getConfig(configid) if conf is not None: self.m_grid1.ClearGrid() for k in conf.keys(): self.m_grid1.SetCellValue(rownum, 0, k) self.m_grid1.SetCellValue(rownum, 1, conf[k]) rownum += 1 self.m_grid1.AutoSizeColumns() self.m_grid1.AutoSize() #Notify other controllers self.parent.controller.currentconfig = configid # reload other panels for fp in self.parent.Children: if isinstance(fp, wx.Panel) and self.__class__ != fp.__class__: fp.loadController() # notification msg = "Config loaded: %s" % configid self.Parent.Warn(msg) except Exception as e: self.Parent.Warn(e.args[0]) finally: self.dbi.closeconn() def OnSaveConfig(self, event): self.dbi.getconn() configid = self.cboConfigid.GetValue() configlist=[] data = self.m_grid1.GetTable() for rownum in range(0, data.GetRowsCount()): if not data.IsEmptyCell(rownum, 0): configlist.append((self.m_grid1.GetCellValue(rownum, 0),self.m_grid1.GetCellValue(rownum, 1),configid)) #print('Saved config:', configlist) # Save to DB cnt = self.dbi.addConfig(configid,configlist) # Load as options cids = self.dbi.getConfigIds() if len(cids) > 0: self.cboConfigid.Set(cids) selection = self.cboConfigid.FindString(configid) self.cboConfigid.SetSelection(selection) # Load controller self.parent.controller.currentconfig = configid #reload other panels for fp in self.parent.Children: if isinstance(fp, wx.Panel) and self.__class__ != fp.__class__: fp.loadController() #notification msg = "Config saved: %s" % configid self.Parent.Warn(msg) self.dbi.closeconn() def OnAddRow(self, event): self.m_grid1.AppendRows(1, True)
class TestDBquery(unittest.TestCase): def setUp(self): configdb = join('..', 'resources', 'autoconfig_test.db') self.dbi = DBI(configdb) self.dbi.getconn() def tearDown(self): self.dbi.conn.close() def test_getConfig(self): configid = 'test' data = self.dbi.getConfig(configid) expected = 0 self.assertGreater(len(data), expected) def test_getConfigALL(self): configid = 'test' data = self.dbi.getConfigALL(configid) expected = 0 self.assertGreater(len(data), expected) def test_getConfigByName(self): group = 'test' test = 'BINWIDTH' expected = 20 data = self.dbi.getConfigByName(group, test) self.assertEqual(int(data), expected) def test_getConfigByName_None(self): group = 'test' test = 'BINW' expected = None data = self.dbi.getConfigByName(group, test) self.assertEqual(data, expected) def test_getConfigIds(self): data = self.dbi.getConfigIds() print('IDS:', data) self.assertGreater(len(data), 0) def test_updateConfig(self): configid = 'test' configlist = [ ('BINWIDTH', 10, 'test', "Bin width for histograms"), ('COLUMN', 'TestData', 'test', "Column name for histograms"), ('MINRANGE', 0, 'test', "Minimum range of histograms bins"), ('MAXRANGE', 100, 'test', "Maximum range of histograms bins") ] cnt = self.dbi.addConfig(configid, configlist) expected = len(configlist) self.assertEqual(expected, cnt) def test_updateConfig_Secondset(self): configid = 'test' configlist = [ ('BINWIDTH', 10, configid, "Bin width for histograms"), ('COLUMN', 'TestData', configid, "Column name for histograms"), ('MINRANGE', 0, configid, "Minimum range of histograms bins"), ('MAXRANGE', 100, configid, "Maximum range of histograms bins") ] cnt = self.dbi.addConfig(configid, configlist) expected = len(configlist) self.assertEqual(expected, cnt) def test_updateConfig_duplicate(self): configid = 'test' configlist = [ ('BINWIDTH', 20, 'test', "Bin width for histograms"), ('COLUMN', 'TestData2', 'test', "Column name for histograms"), ('MINRANGE', 5, 'test', "Minimum range of histograms bins"), ('MAXRANGE', 50, 'test', "Maximum range of histograms bins") ] cnt = self.dbi.addConfig(configid, configlist) expected = len(configlist) self.assertEqual(expected, cnt)