def addMockLabIdQuery(self, id): hostName = socket.gethostname() cursor = MockCursor("GetLabId") self.conn.addCursor(cursor) qry = 'select lab_id from Laboratory where name="' + hostName + '"' cursor.addExpectedExecute(qry, (id, ))
def testGetDOMIdNone(self): dptDB = DOMProdTestDB(self.conn) domTag = 'TEDY1234' expId = None cursor = MockCursor("NoResult") self.conn.addCursor(cursor) qry = 'select prod_id from Product where tag_serial="' + domTag + '"' cursor.addExpectedExecute(qry, None) id = dptDB.getDOMId(domTag) self.assertEqual(id, expId, "Expected empty query to return None")
def testInsertNoData(self): dptDB = DOMProdTestDB(self.conn) temp = 12.34 binSize = 17 mon = Monitor(temp) mon.setBinSize(binSize) monId = 123 fatId = 456 cursor = MockCursor("GetMonId") self.conn.addCursor(cursor) qry = 'select max(fat_mon_id) from FATMonitor' cursor.addExpectedExecute(qry, (monId - 1, )) cursor = MockCursor("InsMon") self.conn.addCursor(cursor) qry = 'insert into FATMonitor(fat_mon_id,fat_id,temp,binsize)' + \ 'values(' + str(monId) + ',' + str(fatId) + ',"' + \ str(temp) + '",' + str(binSize) + ')' cursor.addExpectedExecute(qry, None) mon.insert(dptDB, fatId)
def testSave(self): dptDB = DOMProdTestDB(self.conn) expLabId = 12345 self.addMockLabIdQuery(expLabId) runName = 'A FAT RUN' startDate = '2005-08-04' expId = 1234 cursor = MockCursor("GetNextId") self.conn.addCursor(cursor) qry = 'select max(fat_id) from FAT' cursor.addExpectedExecute(qry, (expId - 1, )) cursor = MockCursor("Save") self.conn.addCursor(cursor) ins = 'insert into FAT(fat_id,lab_id,name,start_date,end_date' + \ ',comment)values(' + str(expId) + ',' + str(expLabId) + ',"' + \ runName + '","' + startDate + '",null,null)' cursor.addExpectedExecute(ins) data = FATData(runName, startDate, None, None) data.save(dptDB)
def testGetLabIdNone(self): dptDB = DOMProdTestDB(self.conn) hostName = 'foo.bar.com' expId = None cursor = MockCursor("SubNameNone") self.conn.addCursor(cursor) qry = 'select lab_id from Laboratory where name="' + hostName + '"' cursor.addExpectedExecute(qry, None) cursor = MockCursor("SubMachNone") self.conn.addCursor(cursor) tmpName = hostName while True: qry = 'select lab_id from LabMachine where machine="' + tmpName + \ '"' cursor.addExpectedExecute(qry, None) dot = tmpName.find('.') if dot < 0: break tmpName = tmpName[dot + 1:] try: id = dptDB.getLabId(hostName) self.fail("Expected getLabId(" + hostName + ") to fail") except IOError as msg: pass # expect this to fail
def testGetMainBoardIdNone(self): dptDB = DOMProdTestDB(self.conn) mbSerial = 'fedcba543210' expId = None cursor = MockCursor("NoResult") self.conn.addCursor(cursor) qry = 'select prod_id from Product where hardware_serial="' + \ mbSerial + '"' cursor.addExpectedExecute(qry, None) id = dptDB.getMainBoardId(mbSerial) self.assertEqual(id, expId, "Expected empty query to return None")
def testGetDOMIdDirect(self): dptDB = DOMProdTestDB(self.conn) domTag = 'TEDY1234' expId = 12345 cursor = MockCursor("Direct") self.conn.addCursor(cursor) qry = 'select prod_id from Product where tag_serial="' + domTag + '"' cursor.addExpectedExecute(qry, (expId, )) id = dptDB.getDOMId(domTag) self.assertEqual(id, expId, "Expected ID#" + str(expId) + ", got ID#" + str(id))
def testGetLabIdRtnName(self): dptDB = DOMProdTestDB(self.conn) hostName = 'foo.bar.com' expId = 12345 cursor = MockCursor("SubName") self.conn.addCursor(cursor) qry = 'select lab_id from Laboratory where name="' + hostName + '"' cursor.addExpectedExecute(qry, (expId, )) id = dptDB.getLabId(hostName) self.assertEqual( id, expId, "Expected getLabId to return " + str(expId) + ", not " + str(id))
def testDeleteOldRows(self): conn = MockConnection() dptDB = DOMProdTestDB(conn) fatId = 1234 cursor = MockCursor('delete') conn.addCursor(cursor) qry = 'delete from FATLCChain where fat_id=' + str(fatId) cursor.addExpectedExecute(qry) LCChainFile.deleteOldRows(dptDB, fatId) conn.verify()
def testGetProductMB(self): dptDB = DOMProdTestDB(self.conn) mbSerial = 'fedcba543210' expId = 12345 cursor = MockCursor("Direct") self.conn.addCursor(cursor) qry = 'select prod_id from Product' + \ ' where hardware_serial="' + mbSerial + '"' cursor.addExpectedExecute(qry, (expId, )) id = dptDB.getProductId(mbSerial) self.assertEqual(id, expId, "Expected ID#" + str(expId) + ", got ID#" + str(id))
def testNoRuns(self): dptDB = DOMProdTestDB(self.conn) runName = 'A FAT RUN' cursor = MockCursor("Empty") self.conn.addCursor(cursor) qry = 'select fat_id,name,start_date,end_date,comment' + \ ' from FAT order by fat_id' cursor.addExpectedExecute(qry, None) run = FATRun(dptDB) self.failIf(run.isName(runName), 'Run ' + runName + ' should not exist') self.assertEqual(None, run.getId(runName), 'Should not have ID for run ' + runName)
def testExists(self): dptDB = DOMProdTestDB(self.conn) expLabId = 12345 self.addMockLabIdQuery(expLabId) runName = 'A FAT RUN' expId = 12345 cursor = MockCursor("NoResult") self.conn.addCursor(cursor) qry = 'select fat_id from FAT where name="' + runName + \ '" and lab_id=' + str(expLabId) cursor.addExpectedExecute(qry, (expId, )) self.failUnless(FATData.exists(dptDB, runName), 'Run ' + runName + ' should exist')
def testGetMainBoardIdIndirect(self): dptDB = DOMProdTestDB(self.conn) domTag = 'TEDY1234' expId = 12345 cursor = MockCursor("Indirect") self.conn.addCursor(cursor) qry = 'select mb.prod_id' + \ ' from Product mb,AssemblyProduct ap,Assembly a,Product d' + \ ' where d.tag_serial="' + domTag + '" and d.prod_id=a.prod_id' + \ ' and a.assem_id=ap.assem_id and ap.prod_id=mb.prod_id' cursor.addExpectedExecute(qry, (expId, )) id = dptDB.getMainBoardId(domTag) self.assertEqual(id, expId, "Expected ID#" + str(expId) + ", got ID#" + str(id))
def testGetDOMIdIndirect(self): dptDB = DOMProdTestDB(self.conn) mbSerial = 'fedcba543210' expId = 12345 cursor = MockCursor("Indirect") self.conn.addCursor(cursor) qry = 'select d.prod_id' + \ ' from Product mb,AssemblyProduct ap,Assembly a,Product d' + \ ' where mb.hardware_serial="' + mbSerial + '"' + \ ' and mb.prod_id=ap.prod_id and ap.assem_id=a.assem_id' + \ ' and a.prod_id=d.prod_id' cursor.addExpectedExecute(qry, (expId, )) id = dptDB.getDOMId(mbSerial) self.assertEqual(id, expId, "Expected ID#" + str(expId) + ", got ID#" + str(id))
def testGetNextIdFirst(self): conn = MockConnection() dptDB = DOMProdTestDB(conn) tblName = "Table" colName = "Column" expId = 1 cursor = MockCursor("NoNextId") conn.addCursor(cursor) qry = 'select max(' + colName + ') from ' + tblName cursor.addExpectedExecute(qry, None) id = getNextId(dptDB, tblName, colName) self.assertEqual(id, expId, "Expected empty query to return 1") conn.verify()
def testGetNextId(self): conn = MockConnection() dptDB = DOMProdTestDB(conn) tblName = "Table" colName = "Column" expId = 123 cursor = MockCursor("NoNextId") conn.addCursor(cursor) qry = 'select max(' + colName + ') from ' + tblName cursor.addExpectedExecute(qry, (expId - 1, )) id = getNextId(dptDB, tblName, colName) self.assertEqual(id, expId, "Unexpected next ID") conn.verify()
def testOneRun(self): dptDB = DOMProdTestDB(self.conn) runName = 'A FAT RUN' expId = 123 startDate = '2005-08-04' cursor = MockCursor("Empty") self.conn.addCursor(cursor) qry = 'select fat_id,name,start_date,end_date,comment' + \ ' from FAT order by fat_id' cursor.addExpectedExecute(qry, (expId, runName, startDate, None, None)) run = FATRun(dptDB) self.failUnless(run.isName(runName), 'Run ' + runName + ' should exist') self.assertEqual(expId, run.getId(runName), 'Unexpected ID for run ' + runName)
def testGetLabIdRtnMach(self): dptDB = DOMProdTestDB(self.conn) pieces = ['foo', 'bar', 'com'] hostName = '.'.join(pieces) expId = 12345 for i in range(len(pieces)): cursor = MockCursor("SubNameNone#2x" + str(i)) self.conn.addCursor(cursor) qry = 'select lab_id from Laboratory where name="' + hostName + '"' cursor.addExpectedExecute(qry, None) cursor = MockCursor("SubMachRtn#" + str(i)) self.conn.addCursor(cursor) n = 0 tmpName = hostName while True: qry = 'select lab_id from LabMachine where machine="' + \ tmpName + '"' if n < i: cursor.addExpectedExecute(qry, None) else: cursor.addExpectedExecute(qry, (expId, )) break n = n + 1 dot = tmpName.find('.') if dot < 0: break tmpName = tmpName[dot + 1:] id = dptDB.getLabId(hostName) self.assertEqual( id, expId, "Expected getLabId to return " + str(expId) + ", not " + str(id))
def testGetLabIdByMachNone(self): dptDB = DOMProdTestDB(self.conn) hostName = 'foo.bar.com' expId = None cursor = MockCursor("LabMachNone") tmpName = hostName while True: qry = 'select lab_id from LabMachine where machine="' + tmpName + \ '"' cursor.addExpectedExecute(qry, None) dot = tmpName.find('.') if dot < 0: break tmpName = tmpName[dot + 1:] id = dptDB.getLabIdByMachine(cursor, hostName) self.assertEqual( id, expId, "Expected getLabIdByName to return " + str(expId) + ", not " + str(id)) cursor.verify()
def testTwoRuns(self): dptDB = DOMProdTestDB(self.conn) nameList = ['A FAT RUN', 'ANOTHER'] idList = [123, 345] startDate = '2005-08-04' cursor = MockCursor("Empty") self.conn.addCursor(cursor) qry = 'select fat_id,name,start_date,end_date,comment' + \ ' from FAT order by fat_id' cursor.addExpectedExecute( qry, (idList[0], nameList[0], startDate, None, None), (idList[1], nameList[1], startDate, '2005-08-05', 'A comment')) run = FATRun(dptDB) for i in range(len(nameList)): self.failUnless(run.isName(nameList[i]), 'Run ' + nameList[i] + ' should exist') self.assertEqual(idList[i], run.getId(nameList[i]), 'Unexpected ID for run ' + nameList[i])
def testGetLabIdByNameNone(self): dptDB = DOMProdTestDB(self.conn) hostName = 'foo.bar.com' expId = None cursor = MockCursor("LabNameNone") qry = 'select lab_id from Laboratory where name="' + hostName + '"' cursor.addExpectedExecute(qry, None) id = dptDB.getLabIdByName(cursor, hostName) self.assertEqual( id, expId, "Expected getLabIdByName to return " + str(expId) + ", not " + str(id)) cursor.verify()
def testInit(self): dptDB = DOMProdTestDB(self.conn) mbSerial = '123fed456cba' maxTemp = 12.34 minTemp = 1.234 avgTemp = 6.78 maxHV = 234 minHV = 123 avgHV = 178 maxPT = 34.56 minPT = 3.456 avgPT = 18.76 maxRate = 456 minRate = 321 avgRate = 388.888 width = 56.7 const = 678.9 numSpikes = 7 r2 = 8.90 histo = [0, 0, 1, 5, 10, 7, 4, 1, 1, 1, 0, 0] mon = MonData(mbSerial, maxTemp, minTemp, avgTemp, maxHV, minHV, avgHV, maxPT, minPT, avgPT, maxRate, minRate, avgRate, width, const, numSpikes, r2, histo) prodId = 777 monId = 666 dataId = 600 cursor = MockCursor("GetProdId") self.conn.addCursor(cursor) qry = 'select d.prod_id from Product mb,AssemblyProduct ap' + \ ',Assembly a,Product d' + \ ' where mb.hardware_serial="' + mbSerial + \ '" and mb.prod_id=ap.prod_id' + \ ' and ap.assem_id=a.assem_id and a.prod_id=d.prod_id' cursor.addExpectedExecute(qry, (prodId, )) cursor = MockCursor("InsMonData") self.conn.addCursor(cursor) qry = ('insert into FATMonData(fat_mondata_id,fat_mon_id' + ',prod_id,temp_max,temp_min,temp_avg' + ',hv_max,hv_min,hv_avg,pt_max,pt_min,pt_avg' + ',rate_max,rate_min,rate_avg' + ',width,constant,num_spikes,r2)' + 'values(%d,%d' + ',%d,%f,%f,%f' + ',%d,%d,%d,%f,%f,%f' + ',%d,%d,%f' + ',%f,%f,%d,%f)') % \ (dataId, monId, prodId, maxTemp, minTemp, avgTemp, maxHV, minHV, avgHV, maxPT, minPT, avgPT, maxRate, minRate, avgRate, width, const, numSpikes, r2) cursor.addExpectedExecute(qry) for bin in range(len(histo)): qry = 'insert into FATMonHisto(fat_mondata_id,bin,value)' + \ 'values(%d,%d,%d)' % (dataId, bin, histo[bin]) cursor.addExpectedExecute(qry) mon.insert(dptDB, monId, dataId)
def testDeleteOldRows(self): conn = MockConnection() dptDB = DOMProdTestDB(conn) fatId = 123 monId = 456 monDataId = 789 cursor = MockCursor('delete') conn.addCursor(cursor) qry = 'select fat_mon_id from FATMonitor where fat_id=' + str(fatId) cursor.addExpectedExecute(qry, monId) qry = 'select fat_mondata_id from FATMonData where fat_mon_id=' + \ str(monId) cursor.addExpectedExecute(qry, monDataId) qry = 'delete from FATMonHisto where fat_mondata_id=' + str(monDataId) cursor.addExpectedExecute(qry) qry = 'delete from FATMonData where fat_mon_id=' + str(monId) cursor.addExpectedExecute(qry) qry = 'delete from FATMonitor where fat_id=' + str(fatId) cursor.addExpectedExecute(qry) MonitorFile.deleteOldRows(dptDB, fatId) conn.verify()
def testInsert(self): dptDB = DOMProdTestDB(self.conn) fatId = 123 loDOM = 'UX3P0123' loId = 543 hiDOM = 'AE3H0001' hiId = 678 result = TestResult(loDOM, hiDOM) for dn in range(1): for dp in range(1): for un in range(1): for up in range(1): result.setDownNeg(dn == 1) result.setDownPos(dp == 1) result.setUpNeg(un == 1) result.setUpPos(up == 1) self.failUnless(result.isFilled(), 'Result[' + str(loDOM) + '/' + str(hiDOM) + '] should be filled') if (dn + dp + un + up) == 4: self.failUnless(result.isSuccess(), 'Result[' + str(loDOM) + '/' + str(hiDOM) + '] should be a success') else: self.failIf(result.isSuccess(), 'Result[' + str(loDOM) + '/' + str(hiDOM) + '] should not be a success') name = self.notChar(dn, 'D-') + \ self.notChar(dp, 'D+') + \ self.notChar(un, 'U-') + \ self.notChar(up, 'U+') cursor = MockCursor(name + " LoDOM") self.conn.addCursor(cursor) qry = 'select prod_id from Product' + \ ' where tag_serial="' + loDOM + '"' cursor.addExpectedExecute(qry, (loId, )) cursor = MockCursor(name + " LoDOM") self.conn.addCursor(cursor) qry = 'select prod_id from Product' + \ ' where tag_serial="' + hiDOM + '"' cursor.addExpectedExecute(qry, (hiId, )) cursor = MockCursor(name + " Insert") self.conn.addCursor(cursor) qry = 'insert into FATLCChain(fat_id,hi_prod_id' + \ ',lo_prod_id,down_neg,down_pos,up_neg,up_pos)' + \ 'values(' + str(fatId) + ',' + str(loId) + ',' + \ str(hiId) + ',' + str(dn) + ',' + \ str(dp) + ',' + str(un) + ',' + str(up) + ')' cursor.addExpectedExecute(qry) result.insert(dptDB, fatId)
def testInsert(self): dptDB = DOMProdTestDB(self.conn) temp = 12.34 binSize = 17 mon = Monitor(temp) mon.setBinSize(binSize) data = [ self.fakeData(111111), self.fakeData(24680), self.fakeData(3691224) ] for d in data: mon.append(d) monId = 123 fatId = 456 nextDataId = 789 cursor = MockCursor("GetMonId") self.conn.addCursor(cursor) qry = 'select max(fat_mon_id) from FATMonitor' cursor.addExpectedExecute(qry, (monId - 1, )) cursor = MockCursor("InsMon") self.conn.addCursor(cursor) qry = 'insert into FATMonitor(fat_mon_id,fat_id,temp,binsize)' + \ 'values(' + str(monId) + ',' + str(fatId) + ',"' + \ str(temp) + '",' + str(binSize) + ')' cursor.addExpectedExecute(qry, None) cursor = MockCursor("GetDataId") self.conn.addCursor(cursor) qry = 'select max(fat_mondata_id) from FATMonData' cursor.addExpectedExecute(qry, (nextDataId - 1, )) nextProdId = 333 for i in range(len(data)): d = data[i] cursor = MockCursor("GetProdId#" + str(i)) self.conn.addCursor(cursor) qry = 'select d.prod_id from Product mb,AssemblyProduct ap' + \ ',Assembly a,Product d' + \ ' where mb.hardware_serial="' + d.mbId + \ '" and mb.prod_id=ap.prod_id' + \ ' and ap.assem_id=a.assem_id and a.prod_id=d.prod_id' cursor.addExpectedExecute(qry, (nextProdId, )) cursor = MockCursor("InsMonData#" + str(i)) self.conn.addCursor(cursor) qry = ('insert into FATMonData(fat_mondata_id,fat_mon_id' + ',prod_id,temp_max,temp_min,temp_avg' + ',hv_max,hv_min,hv_avg,pt_max,pt_min,pt_avg' + ',rate_max,rate_min,rate_avg' + ',width,constant,num_spikes,r2)' + 'values(%d,%d' + ',%d,%f,%f,%f' + ',%d,%d,%d,%f,%f,%f' + ',%d,%d,%f' + ',%f,%f,%d,%f)') % \ (nextDataId, monId, nextProdId, d.maxTemp, d.minTemp, d.avgTemp, d.maxHV, d.minHV, d.avgHV, d.maxPT, d.minPT, d.avgPT, d.maxRate, d.minRate, d.avgRate, d.width, d.const, d.numSpikes, d.r2) cursor.addExpectedExecute(qry) for bin in range(len(d.histo)): qry = 'insert into FATMonHisto(fat_mondata_id,bin,value)' + \ 'values(%d,%d,%d)' % (nextDataId, bin, d.histo[bin]) cursor.addExpectedExecute(qry) nextDataId = nextDataId + 1 nextProdId = nextProdId + 1 mon.insert(dptDB, fatId)