Logo Search packages:      
Sourcecode: qm version File versions

def qm::test::web::web::DirPage::__init__ (   self,
  server,
  path 
)

Construct a 'DirPage'.

'server' -- The 'QMTestServer' creating this page.

'path' -- The label directory to display.

Reimplemented from qm::test::web::web::QMTestPage.

Definition at line 357 of file web.py.

00357                                     :
        """Construct a 'DirPage'.

        'server' -- The 'QMTestServer' creating this page.

        'path' -- The label directory to display."""
        
        # Initialize the base class.
        QMTestPage.__init__(self, "dir.dtml", server)

        self.path = path
        database = server.GetDatabase()
        self.subdir_ids = database.GetSubdirectories(path)
        self.subdir_ids = map(lambda l: database.JoinLabels(path, l),
                              self.subdir_ids)
        self.test_ids = database.GetTestIds(path, scan_subdirs=0)
        self.suite_ids = database.GetSuiteIds(path, scan_subdirs=0)
        # Do not show implicit suites.  Otherwise, there are two
        # entries for a directory: one as a subdirectory entry, and
        # the other as a test suite.
        self.suite_ids = filter(lambda s, d=database: \
                                    not d.GetSuite(s).IsImplicit(),
                                self.suite_ids)
        self.resource_ids = database.GetResourceIds(path, scan_subdirs=0)

        # Get the results to date.
        results_stream = server.GetResultsStream()
        # It is important that we ask for IsFinished before asking
        # for GetTestResults.  The stream could be finished between
        # the two calls, and it is better to show all the results but
        # claim they are incomplete than to show only some of the
        # results and claim they are complete.
        self.__is_finished = results_stream.IsFinished()
        self.test_results = results_stream.GetTestResults()
        self.expected_outcomes = server.GetExpectedOutcomes()

        # Make it easy for the DTML page to get at all the outcomes.
        self.outcomes = Result.outcomes + [self.EXPECTED]
        
        # Provide a menu choice to allow running all of the tests in
        # this directory.
        self.run_menu_items.append(("This Directory", "javascript:run_dir();"))


    def GetExpectationUrl(self, id, expectation):
        """Return the URL for setting the expectation associated with 'id'.

        'id' -- The name of a test.

        'expectation' -- The current expectation associated with the
        test, or 'None' if there is no associated expectation."""
        
        return qm.web.WebRequest("set-expectation",
                                 base=self.request,
                                 id=id,
                                 expectation=expectation or "None",
                                 url=self.request.AsUrl()).AsUrl()

    
    def GetRunUrl(self):
        """Return the URL for running this directory."""

        return qm.web.WebRequest("run-tests",
                                 base=self.request,
                                 ids=self.path) \
               .AsUrl()


    def GetTestResultsForDirectory(self, directory):
        """Return all of the test results for tests in 'directory'.

        'directory' -- A string giving the label for a directory.

        returns -- A sequence of 'Result' instances corresponding to
        results for tests from the indicated directory."""

        # If the directory is the root, just return all the results.
        if directory == "":
            return self.test_results.values()
        
        return filter(lambda r: self.__IsLabelInDirectory(r.GetId(),
                                                          directory),
                      self.test_results.values())
                      

    def GetResultsByOutcome(self, results):
        """Compute the tests in 'results' with each outcome.

        'results' -- A sequence of 'Result' instances.

        returns -- A dictionary mapping outcomes to the sequence of
        tests that have the indicated outcome in 'results'."""

        results_by_outcome = {}
        # At first, there are no results for any outcome.
        for o in self.outcomes:
            results_by_outcome[o] = []

        # Iterate through the results, adding each one to
        # 'results_by_outcome'.
        for r in results:
            results_by_outcome[r.GetOutcome()].append(r)

        return results_by_outcome


    def GetOutcomePercentages(self, results):
        """Compute the percentage (by outcome) of the 'results'.

        'results' -- A sequence of 'Result' instances.
        
        returns -- A dictionary mapping outcomes to the percentage (as
        a floating point number) of tests in 'results' that have
        that outcome."""

        # Compute the total number of tests for which results are
        # available.
        total = len(results)

        # Get the test results, organized by outcome.
        results = self.GetResultsByOutcome(results)

        # Compute the percentages.
        percentages = {}
        for o in self.outcomes:
            if total:
                percentages[o] = float(len(results[o])) / float(total)
            else:
                percentages[o] = 0.0

        return percentages
    

    def GetUnexpectedResultsByOutcome(self, results):
        """Compute the tests in 'results' with each outcome.

        'results' -- A sequence of 'Result' instances.

        returns -- A dictionary mapping outcomes to the results with
        that outcome -- and for which that outcome is unexpected.
        The (fake) outcome 'self.EXPECTED' is mapped to expected
        results."""

        results_by_outcome = {}
        # At first, there are no results for any outcome.
        for o in self.outcomes:
            results_by_outcome[o] = []

        for r in results:
            # See what outcome was expected.
            expectation = self.GetExpectation(r.GetId()) or Result.PASS
            # Update results_by_outcome.
            if r.GetOutcome() != expectation:
                results_by_outcome[r.GetOutcome()].append(r)
            else:
                results_by_outcome[self.EXPECTED].append(r)

        return results_by_outcome


    def GetUnexpectedOutcomePercentages(self, results):
        """Compute percentages of unexpected 'results'.

        'results' -- A sequence of 'Result' instances.
        
        returns -- A dictionary mapping the 'EXPECTATION_KINDS' to the
        percentage (as a floating point number) of tests in 'results'
        that have that expectation."""
        
        # Compute the total number of tests for which results are
        # available.
        total = len(results)

        # Get the test results, organized by outcome.
        results_by_outcome \
            = self.GetUnexpectedResultsByOutcome(results)
        
        # Compute the absolute number of tests in each category.
        percentages = {}
        percentages[self.POSITIVE_UNEXPECTED] \
            = len(results_by_outcome[Result.PASS]) 
        percentages[self.NEGATIVE_UNEXPECTED] \
            = (len(results_by_outcome[Result.FAIL]) 
               + len(results_by_outcome[Result.ERROR])
               + len(results_by_outcome[Result.UNTESTED]))
        percentages[self.EXPECTED] \
            = len(results_by_outcome[self.EXPECTED])

        # And the corresponding percentages.
        for e in self.EXPECTATION_KINDS:
            if percentages[e]:
                percentages[e] = float(percentages[e]) / float(total)
            else:
                percentages[e] = 0.0
                
        return percentages


    def CountUnexpected(self, results):
        """Count the unexpected 'results'.

        'results' -- A dictionary of the form returned by
        'GetUnexpectedResultsByOutcome'.

        returns -- The total number of unexpected results."""

        total = 0
        # Go through all the outcomes except 'EXPECTED'.
        for o in Result.outcomes:
            total += len(results[o])

        return total


    def GetTests(self, sort):
        """Return information about all of the tests.

        'sort' -- One of the 'SORT_KINDS' indicating how the results
        should be sorted.
        
        returns -- A sequence of '_TestInformation' instances
        corresponding to all of the tests in this diretory."""

        # There is no information yet.
        tests = []
        
        # Iterate through each of the tests.
        for id in self.test_ids:
            outcome = self.GetTestOutcome(id)
            expectation = self.GetExpectation(id)
            tests.append(self._TestInformation(id, outcome, expectation))

        if sort == self.SORT_NAME:
            # The tests are already sorted by name.
            pass
        elif sort == self.SORT_OUTCOME:
            # Sort the test by outcome; interesting outcomes come first.
            buckets = {}
            for o in Result.outcomes + [None]:
                buckets[o] = []
                
            # Go through the tests dropping each in the right bucket.
            for t in tests:
                buckets[t.outcome].append(t)
                
            # Combine the buckets.
            tests = []
            for o in Result.outcomes + [None]:
                tests += buckets[o]
        elif sort == self.SORT_EXPECTATION:
            # Sort the test by expectations; unexpected outcomes come
            # first.
            buckets = {}
            for o in ['UNEXPECTED', self.EXPECTED, None]:
                buckets[o] = []
                
            # Go through the tests dropping each in the right bucket.
            for t in tests:
                if (t.outcome == (t.expectation or Result.PASS)):
                    buckets[self.EXPECTED].append(t)
                elif t.outcome:
                    buckets['UNEXPECTED'].append(t)
                else:
                    buckets[None].append(t)
                
            # Combine the buckets.
            tests = []
            for o in ['UNEXPECTED', self.EXPECTED, None]:
                tests += buckets[o]
        else:
            # Ignore the sort request.  (We cannot assert that this case
            # never happens because users can type any URL they like
            # into their web browser.)
            pass

        return tests

    
    def GetTestOutcome(self, test_id):
        """Return the 'Result' for 'test_id'.

        'test_id' -- The name of the test whose result is requested.

        'result' -- The result associated with the 'test_id', or
        'None' if no result is available."""

        result = self.test_results.get(test_id)
        return result and result.GetOutcome()


    def GetDetailURL(self, test_id):
        """Return the detail URL for 'test_id'.

        'test_id' -- The name of the test.

        returns -- The URL that contains details about the 'test_id'."""

        return qm.web.WebRequest("show-result",
                                 base=self.request,
                                 id=test_id).AsUrl()


    def GetExpectation(self, test_id):
        """Return the expected outcome for 'test_id'.

        'test_id' -- The name of the test.
                
        returns -- A string giving the expected outcome for 'test_id',
        or 'None' if there is no expectation."""

        return self.expected_outcomes.get(test_id)


    def GetSortURL(self, sort):
        """Get the URL for this page, but sorted as indicated.

        'sort' -- One of the 'SORT_KINDS'.

        returns -- A URL indicating this page, but sorted as
        indicated."""

        return qm.web.WebRequest("show-dir",
                                 base=self.request,
                                 id=self.path,
                                 sort=sort).AsUrl()
    
    def IsFinished(self):
        """Returns true if tests are still running.

        returns -- True if the data on this page should be considered
        incomplete due to the fact that tests are still running."""

        return self.__is_finished


    def GetRefreshDelay(self):
        """Returns the number of seconds to wait before refreshing the page.

        returns -- The number of seconds to wait before refreshing this
        page.  A value of zero means that te page should never be
        refreshed.  This function is only called if 'IsFinished' returns
        true."""

        if len(self.test_results.items()) < 50:
            return 10
        else:
            return 30


    def __IsLabelInDirectory(self, id, directory):
        """Returns true if 'id' is in 'directory'.

        returns -- True if 'id' indicates a test contained in
        'directory', or one of its subdirectories."""

        while len(id) >= len(directory):
            if id == directory:
                return 1
            id = self.GetDatabase().SplitLabel(id)[0]
            
        return 0


    
class LoadContextPage(QMTestPage):


Generated by  Doxygen 1.6.0   Back to index