eric7/Unittest/UTTestResultsTree.py

Thu, 12 May 2022 08:59:13 +0200

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Thu, 12 May 2022 08:59:13 +0200
branch
unittest
changeset 9059
e7fd342f8bfc
child 9062
7f27bf3b50c3
permissions
-rw-r--r--

Implemented the basic functionality of the new unit test framework.

# -*- coding: utf-8 -*-

# Copyright (c) 2022 Detlev Offenbach <detlev@die-offenbachs.de>
#

"""
Module implementing a tree view and associated model to show the test result
data.
"""

from PyQt6.QtCore import (
    pyqtSignal, pyqtSlot, Qt, QAbstractItemModel, QCoreApplication, QModelIndex
)
from PyQt6.QtWidgets import QTreeView

TopLevelId = 2 ** 32 - 1


class TestResultsModel(QAbstractItemModel):
    """
    Class implementing the item model containing the test data.
    """
    Headers = [
        QCoreApplication.translate("TestResultsModel", "Status"),
        QCoreApplication.translate("TestResultsModel", "Name"),
        QCoreApplication.translate("TestResultsModel", "Message"),
        QCoreApplication.translate("TestResultsModel", "Duration (ms)"),
    ]
    
    def __init__(self, parent=None):
        """
        Constructor
        
        @param parent reference to the parent object (defaults to None)
        @type QObject (optional)
        """
        super().__init__(parent)
        
        self.__testResults = []
    
    def headerData(self, section, orientation,
                   role=Qt.ItemDataRole.DisplayRole):
        """
        Public method to get the header string for the various sections.
        
        @param section section number
        @type int
        @param orientation orientation of the header
        @type Qt.Orientation
        @param role data role (defaults to Qt.ItemDataRole.DisplayRole)
        @type Qt.ItemDataRole (optional)
        @return header string of the section
        @rtype str
        """
        if (
            orientation == Qt.Orientation.Horizontal and
            role == Qt.ItemDataRole.DisplayRole
        ):
            return TestResultsModel.Headers[section]
        else:
            return None
    
    def rowCount(self, parent=QModelIndex()):
        """
        Public method to get the number of row for a given parent index.
        
        @param parent index of the parent item (defaults to QModelIndex())
        @type QModelIndex (optional)
        @return number of rows
        @rtype int
        """
        if not parent.isValid():
            return len(self.__testResults)
        
        if parent.internalId() == TopLevelId and parent.column() == 0:
            return len(self.__testResults[parent.row()].extra)
        
        return 0

    def columnCount(self, parent=QModelIndex()):
        """
        Public method to get the number of columns.
        
        @param parent index of the parent item (defaults to QModelIndex())
        @type QModelIndex (optional)
        @return number of columns
        @rtype int
        """
        if not parent.isValid():
            return len(TestResultsModel.Headers)
        else:
            return 1
    
    def clear(self):
        """
        Public method to clear the model data.
        """
        self.beginResetModel()
        self.__testResults.clear()
        self.endResetModel()


class TestResultsTreeView(QTreeView):
    """
    Class implementing a tree view to show the test result data.
    
    @signal goto(str, int) emitted to go to the position given by file name
        and line number
    """
    goto = pyqtSignal(str, int)
    
    def __init__(self, parent=None):
        """
        Constructor
        
        @param parent reference to the parent widget (defaults to None)
        @type QWidget (optional)
        """
        super().__init__(parent)
        
        self.setItemsExpandable(True)
        self.setExpandsOnDoubleClick(False)
        self.setSortingEnabled(True)
        
        self.header().setDefaultAlignment(Qt.AlignmentFlag.AlignCenter)
        self.header().setSortIndicatorShown(False)
        
        # connect signals and slots
        self.doubleClicked.connect(self.__gotoTestDefinition)
        
        self.header().sortIndicatorChanged.connect(self.sortByColumn)
        self.header().sortIndicatorChanged.connect(
            lambda column, order: self.header().setSortIndicatorShown(True))
    
    @pyqtSlot(QModelIndex)
    def __gotoTestDefinition(self, index):
        """
        Private slot to show the test definition.
        
        @param index index for the double-clicked item
        @type QModelIndex
        """
        # TODO: not implemented yet
        pass

#
# eflag: noqa = M822

eric ide

mercurial