def traverseChilds(self, parentid, indent=0): cursor = nafdb.connection.cursor() cursor.execute("select * from ordereditems where id==?", (parentid, )) row = cursor.fetchone() if row is not None: (itemid, pid, typeid, title) = row table = nafdb.getTableForTypeId(typeid) self.renderItem(table, itemid, table.name[:-1], self.getRelatedItems(itemid)) self.renderTocItem(table, itemid, table.name[:-1], indent) else: pass cursor.execute("select * from ordereditems where parentid==?", (parentid, )) while True: row = cursor.fetchone() if row is None: break (itemid, pid, typeid, title) = row if typeid == nafdb.TYPE_FOLDER: self.traverseChilds(itemid, indent + 1) else: table = nafdb.getTableForTypeId(typeid) self.renderItem(table, itemid, table.name[:-1], self.getRelatedItems(itemid)) self.renderTocItem(table, itemid, table.name[:-1], indent + 1) return
def differentTypes(self, id, typeid1, typeid2): table1 = nafdb.getTableForTypeId(typeid1) table2 = nafdb.getTableForTypeId(typeid2) self.outputFile.write( """ <tr><td>%s</td><td>N/A</td> <td>%s</td><td>%s</td></tr>""" % (id, table1.displayname, table2.displayname) )
def createTestRunTables(dbObj): dbObj.exec_(_getCreateTableStatement(TESTRUN_TABLE)) # testsuiteTable = nafdb.getTableForTypeId(nafdb.TYPE_TESTSUITE) dbObj.exec_(_getCreateTableStatement(testsuiteTable)) # dbObj.exec_(_getCreateTableStatement(TESTRUNINFO_TABLE)) # imagesTable = nafdb.getTableForTypeId(nafdb.TYPE_IMAGE) dbObj.exec_(_getCreateTableStatement(imagesTable))
def missingItem(self, id, typeid1, typeid2): if typeid1 == None: s1 = 'missing item' else: s1 = nafdb.getTableForTypeId(typeid1).displayname if typeid2 == None: s2 = 'missing item' else: s2 = nafdb.getTableForTypeId(typeid2).displayname self.outputFile.write(""" <tr><td>%s</td><td>N/A</td> <td>%s</td><td>%s</td></tr>""" % (id, s1, s2))
def traverseChilds(self, parentid, parentnode, parenttocnode, indent=0): cursor = nafdb.connection.cursor() cursor.execute("select * from ordereditems where id==?", (parentid, )) row = cursor.fetchone() if row is not None: (itemid, pid, typeid, title) = row table = nafdb.getTableForTypeId(typeid) node = self.renderItem(table, itemid, table.name[:-1]) tocnode = self.renderTocItem(table, itemid, 'toc' + table.name[:-1], indent) else: node = parentnode tocnode = parenttocnode cursor.execute("select * from ordereditems where parentid==?", (parentid, )) while True: row = cursor.fetchone() if row is None: break (itemid, pid, typeid, title) = row if typeid == nafdb.TYPE_FOLDER: subnode, subtocnode = self.traverseChilds( itemid, node, tocnode, indent + 1) else: table = nafdb.getTableForTypeId(typeid) subnode = self.renderItem(table, itemid, table.name[:-1]) subtocnode = self.renderTocItem(table, itemid, 'toc' + table.name[:-1], indent) relatedNode = self._createElement("relations") relatedCnt = 0 for relatedId in nafdb.connection.execute( "select relatedid from relations where id==?", (itemid, )): (relatedTypeId, relatedTitle) = nafdb.connection.execute( "select typeid, title from ordereditems where id==?", relatedId).fetchone() relatedTableName = nafdb.getTableForTypeId( relatedTypeId).name tmpNode = self._createElement(relatedTableName[:-1]) tmpNode.appendChild( self._createTextElement('id', str(relatedId[0]))) tmpNode.appendChild( self._createTextElement('title', relatedTitle)) relatedNode.appendChild(tmpNode) relatedCnt += 1 relatedNode.setAttribute('cnt', str(relatedCnt)) subnode.appendChild(relatedNode) node.appendChild(subnode) tocnode.appendChild(subtocnode) return node, tocnode
def missingItem(self, id, typeid1, typeid2): if typeid1 == None: s1 = "missing item" else: s1 = nafdb.getTableForTypeId(typeid1).displayname if typeid2 == None: s2 = "missing item" else: s2 = nafdb.getTableForTypeId(typeid2).displayname self.outputFile.write( """ <tr><td>%s</td><td>N/A</td> <td>%s</td><td>%s</td></tr>""" % (id, s1, s2) )
def getRelatedItems(self, itemid): relatedItems = [] for relatedId in nafdb.connection.execute("select relatedid from relations where id==?", (itemid, )): (relatedTypeId, relatedTitle) = nafdb.connection.execute("select typeid, title from ordereditems where id==?", relatedId).fetchone() relatedTableName = nafdb.getTableForTypeId(relatedTypeId) relatedItems.append({'table': relatedTableName, 'id': relatedId[0], 'title':relatedTitle}) return relatedItems
def createTestRunDatabase(srcDatabaseName, destDatabaseName, srcTestsuiteId, infoDict): """ Create a database file for the test run. srcDatabaseName is the filename of the database with the testsuite and testcases destDatabaseName is the filename of the database to be created srcTestsuiteId is the id of the testsuite in srcDatabaseName to be run """ # get the columns of testcases columnNames = nafdb.getColumnNames("testcases") columnNamesList = ",".join(columnNames) # createTestRunTables(destDatabaseName) conn = sqlite3.connect(destDatabaseName) cursor = conn.cursor() cursorProxy = CursorProxy(cursor) createTestRunTables(cursorProxy) cursor.execute("ATTACH DATABASE ? AS srcdb", (srcDatabaseName,)) # --- create lookup tables for enumeration types for name, values in LOOKUP_TABLES.iteritems(): cursor.execute("CREATE TABLE %s (key INTEGER, value TEXT);" % name) for value in values: cursor.execute("INSERT INTO %s VALUES (?, ?);" % name, (values.index(value), value)) # --- populate artifact tables command = ( "INSERT INTO testruns (%s) select * from srcdb.testcases where id in (select relatedid from relations where id=?)" % columnNamesList ) cursor.execute(command, (srcTestsuiteId,)) # copy images referenced by any testcases from src into dest # --- first, identifiy table columns which may reference an image testcaseTable = nafdb.getTableForTypeId(nafdb.TYPE_TESTCASE) columns = [c.name for c in testcaseTable.columns if c.view == nafdb.VIEW_MULTI_LINE] # --- next, read all fields containing an img tag imageIds = set() pattern = re.compile(r'<img src="#(\d+)"') command = """select %s from testcases where %s like '%%<img src="%%'""" for c in columns: # iterate all fields which references an image and extract the image id's cursor.execute(command % (c, c)) row = cursor.fetchone() if row is None: continue result = pattern.findall(row[0]) map(imageIds.add, [int(r) for r in result]) cursor.execute("INSERT INTO images SELECT * FROM srcdb.images WHERE id IN %s" % str(tuple(imageIds))) # copy testsuite table from src to dest cursor.execute("INSERT INTO testsuites SELECT * FROM srcdb.testsuites WHERE id=?", (srcTestsuiteId,)) # populate info table columnNames = getColumnNames(TESTRUNINFO_TABLE) columnNamesList = ",".join(columnNames) command = "insert into testruninfo (%s) values (%s)" % (columnNamesList, ",".join(["?"] * len(columnNames))) values = [infoDict[k] for k in columnNames] cursor.execute(command, values) # done everything, commit and close conn.commit() conn.close()
def traverseChilds(self, parentid, parentnode, parenttocnode, indent=0): cursor = nafdb.connection.cursor() cursor.execute("select * from ordereditems where id==?", (parentid,)) row = cursor.fetchone() if row is not None: (itemid, pid, typeid, title) = row table = nafdb.getTableForTypeId(typeid) node = self.renderItem(table, itemid, table.name[:-1]) tocnode = self.renderTocItem(table, itemid, 'toc'+table.name[:-1], indent) else: node = parentnode tocnode = parenttocnode cursor.execute("select * from ordereditems where parentid==?", (parentid,)) while True: row = cursor.fetchone() if row is None: break (itemid, pid, typeid, title) = row if typeid == nafdb.TYPE_FOLDER: subnode, subtocnode = self.traverseChilds(itemid, node, tocnode, indent+1) else: table = nafdb.getTableForTypeId(typeid) subnode = self.renderItem(table, itemid, table.name[:-1]) subtocnode = self.renderTocItem(table, itemid, 'toc'+table.name[:-1], indent) relatedNode = self._createElement("relations") relatedCnt = 0 for relatedId in nafdb.connection.execute("select relatedid from relations where id==?", (itemid, )): (relatedTypeId, relatedTitle) = nafdb.connection.execute("select typeid, title from ordereditems where id==?", relatedId).fetchone() relatedTableName = nafdb.getTableForTypeId(relatedTypeId).name tmpNode = self._createElement(relatedTableName[:-1]) tmpNode.appendChild(self._createTextElement('id', str(relatedId[0]))) tmpNode.appendChild(self._createTextElement('title', relatedTitle)) relatedNode.appendChild(tmpNode) relatedCnt += 1 relatedNode.setAttribute('cnt', str(relatedCnt)) subnode.appendChild(relatedNode) node.appendChild(subnode) tocnode.appendChild(subtocnode) return node, tocnode
def traverseChilds(self, parentid, indent=0): cursor = nafdb.connection.cursor() cursor.execute("select * from ordereditems where id==?", (parentid,)) row = cursor.fetchone() if row is not None: (itemid, pid, typeid, title) = row table = nafdb.getTableForTypeId(typeid) self.renderItem(table, itemid, table.name[:-1], self.getRelatedItems(itemid)) self.renderTocItem(table, itemid, table.name[:-1], indent) else: pass cursor.execute("select * from ordereditems where parentid==?", (parentid,)) while True: row = cursor.fetchone() if row is None: break (itemid, pid, typeid, title) = row if typeid == nafdb.TYPE_FOLDER: self.traverseChilds(itemid, indent+1) else: table = nafdb.getTableForTypeId(typeid) self.renderItem(table, itemid, table.name[:-1], self.getRelatedItems(itemid)) self.renderTocItem(table, itemid, table.name[:-1], indent+1) return
def getRelatedItems(self, itemid): relatedItems = [] for relatedId in nafdb.connection.execute( "select relatedid from relations where id==?", (itemid, )): (relatedTypeId, relatedTitle) = nafdb.connection.execute( "select typeid, title from ordereditems where id==?", relatedId).fetchone() relatedTableName = nafdb.getTableForTypeId(relatedTypeId) relatedItems.append({ 'table': relatedTableName, 'id': relatedId[0], 'title': relatedTitle }) return relatedItems
import re from PyQt4 import QtCore import _naf_database as nafdb LOOKUP_TABLES = { "statusLUT": ["pending", "failed", "passed", "skipped"], "priorityLUT": nafdb.lookupTables["priorityLUT"], } STATUS_PENDING = 0 STATUS_FAILED = 1 STATUS_PASSED = 2 STATUS_SKIPPED = 3 # create testruns table class columns = copy.deepcopy(nafdb.getTableForTypeId(nafdb.TYPE_TESTCASE).columns) columns.insert(1, nafdb.cColumn(name="status", _type="integer", displayname="Status", default=STATUS_PENDING)) columns.append(nafdb.cColumn(name="user", _type="text", displayname="Tester")) columns.append(nafdb.cColumn(name="date", _type="text", displayname="Date")) columns.append(nafdb.cColumn(name="action", _type="text", displayname="Action")) columns.append(nafdb.cColumn(name="remark", _type="text", displayname="Remark")) TESTRUN_TABLE = nafdb.cTable( name="testruns", isFilterable=True, displayname="Test Runs", typeid=nafdb.TYPE_TESTRUN, columns=columns ) TESTRUNINFO_TABLE = nafdb.cTable( name="testruninfo", displayname="Test Run Information", isFilterable=True, typeid=-1, columns=(
def createTestRunDatabase(srcDatabaseName, destDatabaseName, srcTestsuiteId, infoDict): """ Create a database file for the test run. srcDatabaseName is the filename of the database with the testsuite and testcases destDatabaseName is the filename of the database to be created srcTestsuiteId is the id of the testsuite in srcDatabaseName to be run """ # get the columns of testcases columnNames = nafdb.getColumnNames('testcases') columnNamesList = ','.join(columnNames) #createTestRunTables(destDatabaseName) conn = sqlite3.connect(destDatabaseName) cursor = conn.cursor() cursorProxy = CursorProxy(cursor) createTestRunTables(cursorProxy) cursor.execute("ATTACH DATABASE ? AS srcdb", (srcDatabaseName, )) #--- create lookup tables for enumeration types for name, values in LOOKUP_TABLES.iteritems(): cursor.execute("CREATE TABLE %s (key INTEGER, value TEXT);" % name) for value in values: cursor.execute("INSERT INTO %s VALUES (?, ?);" % name, (values.index(value), value)) #--- populate artifact tables command = 'INSERT INTO testruns (%s) select * from srcdb.testcases where id in (select relatedid from relations where id=?)' % columnNamesList cursor.execute(command, (srcTestsuiteId, )) # copy images referenced by any testcases from src into dest # --- first, identifiy table columns which may reference an image testcaseTable = nafdb.getTableForTypeId(nafdb.TYPE_TESTCASE) columns = [ c.name for c in testcaseTable.columns if c.view == nafdb.VIEW_MULTI_LINE ] # --- next, read all fields containing an img tag imageIds = set() pattern = re.compile(r'<img src="#(\d+)"') command = """select %s from testcases where %s like '%%<img src="%%'""" for c in columns: # iterate all fields which references an image and extract the image id's cursor.execute(command % (c, c)) row = cursor.fetchone() if row is None: continue result = pattern.findall(row[0]) map(imageIds.add, [int(r) for r in result]) cursor.execute( "INSERT INTO images SELECT * FROM srcdb.images WHERE id IN %s" % str(tuple(imageIds))) # copy testsuite table from src to dest cursor.execute( 'INSERT INTO testsuites SELECT * FROM srcdb.testsuites WHERE id=?', (srcTestsuiteId, )) # populate info table columnNames = getColumnNames(TESTRUNINFO_TABLE) columnNamesList = ','.join(columnNames) command = 'insert into testruninfo (%s) values (%s)' % ( columnNamesList, ','.join(['?'] * len(columnNames))) values = [infoDict[k] for k in columnNames] cursor.execute(command, values) # done everything, commit and close conn.commit() conn.close()
import re from PyQt4 import QtCore import _naf_database as nafdb LOOKUP_TABLES = { 'statusLUT': ["pending", "failed", "passed", "skipped"], 'priorityLUT': nafdb.lookupTables['priorityLUT'] } STATUS_PENDING = 0 STATUS_FAILED = 1 STATUS_PASSED = 2 STATUS_SKIPPED = 3 # create testruns table class columns = copy.deepcopy(nafdb.getTableForTypeId(nafdb.TYPE_TESTCASE).columns) columns.insert( 1, nafdb.cColumn(name='status', _type='integer', displayname='Status', default=STATUS_PENDING)) columns.append(nafdb.cColumn(name='user', _type='text', displayname='Tester')) columns.append(nafdb.cColumn(name='date', _type='text', displayname='Date')) columns.append(nafdb.cColumn(name='action', _type='text', displayname='Action')) columns.append(nafdb.cColumn(name='remark', _type='text', displayname='Remark')) TESTRUN_TABLE = nafdb.cTable(name='testruns', isFilterable=True, displayname="Test Runs",
def run(args): report = cReport(args) report.start(args.databaseName1, args.databaseName2) connection1 = sqlite3.connect(args.databaseName1) connection2 = sqlite3.connect(args.databaseName2) cursor1 = connection1.cursor() cursor2 = connection2.cursor() version1 = cursor1.execute("select version from __info__;").fetchone()[0] version2 = cursor2.execute("select version from __info__;").fetchone()[0] if version1 != version2: report.nonMatchingVersions(version1, version2) sys.exit() command = [] for tableName in nafdb.getTableNames(): command.append('select id, typeid from %s' % (tableName, )) command = ' union '.join(command) command = 'create temporary view allids as ' + command + ';' cursor1.execute(command) cursor2.execute(command) # TODO: check if parents of two items have changed # TODO: check if viewpos of two items has changed # TODO: check if relations of two items has changed # look at all id present in first file cursor1.execute("select * from allids;") for (id1, typeid1) in cursor1: try: (id2, typeid2) = cursor2.execute("select * from allids where id=?", (id1, )).fetchone() if typeid1 == nafdb.TYPE_ROOT and typeid2 == nafdb.TYPE_ROOT: pass elif typeid1 == typeid2: # same types, so compare all columns logging.debug("same types (%d, %d), (%d, %d)" % (id1, typeid1, id2, typeid2)) table = nafdb.getTableForTypeId(typeid1) columns = table.columns printId = True for column in columns: item1 = getItemForId(connection1, table.name, id1, column.name) item2 = getItemForId(connection2, table.name, id2, column.name) if column._type == 'text':# and (item1.lstrip().startswith("<!DOCTYPE") or item2.lstrip().startswith("<!DOCTYPE")): doc = QtGui.QTextDocument() doc.setHtml(item1) item1 = '<br/>'.join(unicode(doc.toPlainText()).splitlines(1)) doc.setHtml(item2) item2 = '<br/>'.join(unicode(doc.toPlainText()).splitlines(1)) if item1 != item2: report.differentItem(id1, column.displayname, item1, item2, printId) printId = False else: # different types, nothing to compare, report only logging.debug("different types (%d, %d), (%d, %d)" % (id1, typeid1, id2, typeid2)) report.differentTypes(id1, typeid1, typeid2) except TypeError: # id present in first file but not in second file logging.debug("item (%d, %d) not found in second file" % (id1, typeid1)) report.missingItem(id1, typeid1, None) pass # finally we look for all id present in second file but not in first file cursor2.execute("select * from allids;") for (id2, typeid2) in cursor2: try: (id2, typeid2) = cursor1.execute("select * from allids where id=?", (id2, )).fetchone() except TypeError: # id present in second file but not in first file logging.debug("item (%d, %d) not found in first file" % (id2, typeid2)) report.missingItem(id2, None, typeid2) pass connection1.close() connection2.close()
def differentTypes(self, id, typeid1, typeid2): table1 = nafdb.getTableForTypeId(typeid1) table2 = nafdb.getTableForTypeId(typeid2) self.outputFile.write(""" <tr><td>%s</td><td>N/A</td> <td>%s</td><td>%s</td></tr>""" % (id, table1.displayname, table2.displayname))
def run(args): report = cReport(args) report.start(args.databaseName1, args.databaseName2) connection1 = sqlite3.connect(args.databaseName1) connection2 = sqlite3.connect(args.databaseName2) cursor1 = connection1.cursor() cursor2 = connection2.cursor() version1 = cursor1.execute("select version from __info__;").fetchone()[0] version2 = cursor2.execute("select version from __info__;").fetchone()[0] if version1 != version2: report.nonMatchingVersions(version1, version2) sys.exit() command = [] for tableName in nafdb.getTableNames(): command.append("select id, typeid from %s" % (tableName,)) command = " union ".join(command) command = "create temporary view allids as " + command + ";" cursor1.execute(command) cursor2.execute(command) # TODO: check if parents of two items have changed # TODO: check if viewpos of two items has changed # TODO: check if relations of two items has changed # look at all id present in first file cursor1.execute("select * from allids;") for (id1, typeid1) in cursor1: try: (id2, typeid2) = cursor2.execute("select * from allids where id=?", (id1,)).fetchone() if typeid1 == nafdb.TYPE_ROOT and typeid2 == nafdb.TYPE_ROOT: pass elif typeid1 == typeid2: # same types, so compare all columns logging.debug("same types (%d, %d), (%d, %d)" % (id1, typeid1, id2, typeid2)) table = nafdb.getTableForTypeId(typeid1) columns = table.columns printId = True for column in columns: item1 = getItemForId(connection1, table.name, id1, column.name) item2 = getItemForId(connection2, table.name, id2, column.name) if ( column._type == "text" ): # and (item1.lstrip().startswith("<!DOCTYPE") or item2.lstrip().startswith("<!DOCTYPE")): doc = QtGui.QTextDocument() doc.setHtml(item1) item1 = "<br/>".join(unicode(doc.toPlainText()).splitlines(1)) doc.setHtml(item2) item2 = "<br/>".join(unicode(doc.toPlainText()).splitlines(1)) if item1 != item2: report.differentItem(id1, column.displayname, item1, item2, printId) printId = False else: # different types, nothing to compare, report only logging.debug("different types (%d, %d), (%d, %d)" % (id1, typeid1, id2, typeid2)) report.differentTypes(id1, typeid1, typeid2) except TypeError: # id present in first file but not in second file logging.debug("item (%d, %d) not found in second file" % (id1, typeid1)) report.missingItem(id1, typeid1, None) pass # finally we look for all id present in second file but not in first file cursor2.execute("select * from allids;") for (id2, typeid2) in cursor2: try: (id2, typeid2) = cursor1.execute("select * from allids where id=?", (id2,)).fetchone() except TypeError: # id present in second file but not in first file logging.debug("item (%d, %d) not found in first file" % (id2, typeid2)) report.missingItem(id2, None, typeid2) pass connection1.close() connection2.close()