Esempio n. 1
0
    def check_algorithm(self, name, defs):
        """
        Will run an algorithm definition and check if it generates the expected result
        :param name: The identifier name used in the test output heading
        :param defs: A python dict containing a test algorithm definition
        """
        QgsProject.instance().removeAllMapLayers()

        params = self.load_params(defs['params'])

        if defs['algorithm'].startswith('script:'):
            filePath = os.path.join(
                processingTestDataPath(), 'scripts',
                '{}.py'.format(defs['algorithm'][len('script:'):]))
            alg = ScriptAlgorithm(filePath)
        else:
            alg = QgsApplication.processingRegistry().algorithmById(
                defs['algorithm'])

        if isinstance(params, list):
            for param in zip(alg.parameters, params):
                param[0].setValue(param[1])
        else:
            for k, p in list(params.items()):
                alg.setParameterValue(k, p)

        for r, p in list(defs['results'].items()):
            alg.setOutputValue(r, self.load_result_param(p))

        expectFailure = False
        if 'expectedFailure' in defs:
            exec(('\n'.join(defs['expectedFailure'][:-1])), globals(),
                 locals())
            expectFailure = eval(defs['expectedFailure'][-1])

        # ignore user setting for invalid geometry handling
        context = QgsProcessingContext()
        context.setProject(QgsProject.instance())

        if expectFailure:
            try:
                alg.execute(context)
                self.check_results(alg.getOutputValuesAsDictionary(),
                                   defs['params'], defs['results'])
            except Exception:
                pass
            else:
                raise _UnexpectedSuccess
        else:
            alg.execute(context)
            self.check_results(alg.getOutputValuesAsDictionary(),
                               defs['params'], defs['results'])
Esempio n. 2
0
    def check_algorithm(self, name, defs):
        """
        Will run an algorithm definition and check if it generates the expected result
        :param name: The identifier name used in the test output heading
        :param defs: A python dict containing a test algorithm definition
        """
        QgsProject.instance().removeAllMapLayers()

        params = self.load_params(defs['params'])

        if defs['algorithm'].startswith('script:'):
            filePath = os.path.join(processingTestDataPath(), 'scripts', '{}.py'.format(defs['algorithm'][len('script:'):]))
            alg = ScriptAlgorithm(filePath)
        else:
            alg = QgsApplication.processingRegistry().algorithmById(defs['algorithm'])

        if isinstance(params, list):
            for param in zip(alg.parameters, params):
                param[0].setValue(param[1])
        else:
            for k, p in list(params.items()):
                alg.setParameterValue(k, p)

        for r, p in list(defs['results'].items()):
            alg.setOutputValue(r, self.load_result_param(p))

        expectFailure = False
        if 'expectedFailure' in defs:
            exec(('\n'.join(defs['expectedFailure'][:-1])), globals(), locals())
            expectFailure = eval(defs['expectedFailure'][-1])

        if expectFailure:
            try:
                alg.execute()
                self.check_results(alg.getOutputValuesAsDictionary(), defs['params'], defs['results'])
            except Exception:
                pass
            else:
                raise _UnexpectedSuccess
        else:
            alg.execute()
            self.check_results(alg.getOutputValuesAsDictionary(), defs['params'], defs['results'])