File: grass_algorithm_postgreraster_test.py

package info (click to toggle)
qgis 3.44.8%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,237,544 kB
  • sloc: cpp: 1,668,420; python: 381,688; xml: 23,474; perl: 4,078; sh: 3,733; ansic: 2,799; sql: 2,137; yacc: 1,068; lex: 577; javascript: 540; lisp: 411; makefile: 155
file content (147 lines) | stat: -rw-r--r-- 4,329 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
"""QGIS Unit tests for Grass Algorithm with postgreraster provider

.. note:: This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
"""

__author__ = "Jan Caha"
__date__ = "01/08/2025"
__copyright__ = "Copyright 2025, The QGIS Project"


import os
import tempfile
from shutil import rmtree

import unittest
from qgis.testing import start_app, QgisTestCase
from qgis.core import (
    QgsApplication,
    QgsRasterLayer,
    QgsDataSourceUri,
    QgsAuthMethodConfig,
    QgsProcessingContext,
    QgsProcessingFeedback,
)
from qgis import processing
from grassprovider.grass_provider import GrassProvider
from grassprovider.grass_utils import GrassUtils

QGIS_AUTH_DB_DIR_PATH = tempfile.mkdtemp()
os.environ["QGIS_AUTH_DB_DIR_PATH"] = QGIS_AUTH_DB_DIR_PATH

start_app()


class TestProcessingGrassAlgsPostgreRasterProvider(QgisTestCase):

    @classmethod
    def setUpClass(cls):
        super().setUpClass()
        cls.provider = GrassProvider()
        QgsApplication.processingRegistry().addProvider(cls.provider)
        cls.cleanup_paths = []

        assert GrassUtils.installedVersion()

    @classmethod
    def tearDownClass(cls):
        """Run after all tests"""
        rmtree(QGIS_AUTH_DB_DIR_PATH)
        del os.environ["QGIS_AUTH_DB_DIR_PATH"]
        super().tearDownClass()

    def test_algorithm_r_buffer(self):
        """
        Test grass algorithm r.buffer with postgreraster provider
        """

        context = QgsProcessingContext()
        feedback = QgsProcessingFeedback()

        rl = QgsRasterLayer(
            "dbname='mydb' host=localhost port=5432 user='asdf' password='42'"
            " sslmode=disable table=some_table schema=some_schema column=rast sql=pk = 2",
            "pg_layer",
            "postgresraster",
        )

        alg = QgsApplication.processingRegistry().algorithmById("grass:r.buffer")

        self.assertTrue(alg)

        params = {
            "input": rl,
            "distances": "100,200,500",
            "units": 0,
            "-z": False,
            "output": "TEMPORARY_OUTPUT",
            "GRASS_REGION_PARAMETER": None,
            "GRASS_REGION_CELLSIZE_PARAMETER": 0,
            "GRASS_RASTER_FORMAT_OPT": "",
            "GRASS_RASTER_FORMAT_META": "",
        }

        can_run, _ = alg.checkParameterValues(parameters=params, context=context)

        self.assertTrue(can_run)

        res = alg.run(params, context, feedback)

        # should be tuple
        self.assertTrue(res)
        # should be true if algorithm run successfully
        self.assertTrue(res[1])
        # should be dict with output keys
        self.assertTrue(isinstance(res[0], dict))
        # should be path to result file
        self.assertTrue(isinstance(res[0]["output"], str))

    def test_algorithm_r_info(self):
        """
        Test grass algorithm r.info with postgreraster provider
        """

        context = QgsProcessingContext()
        feedback = QgsProcessingFeedback()

        rl = QgsRasterLayer(
            "dbname='mydb' host=localhost port=5432 user='asdf' password='42'"
            " sslmode=disable table=some_table schema=some_schema column=rast sql=pk = 2",
            "pg_layer",
            "postgresraster",
        )

        alg = QgsApplication.processingRegistry().algorithmById("grass:r.info")

        params = {
            "map": rl,
            "-r": False,
            "-g": False,
            "-h": False,
            "-e": False,
            "html": "./report.html",
            "GRASS_REGION_PARAMETER": None,
            "GRASS_REGION_CELLSIZE_PARAMETER": 0,
        }

        can_run, _ = alg.checkParameterValues(parameters=params, context=context)

        self.assertTrue(can_run)

        res = alg.run(params, context, feedback)

        # should be tuple
        self.assertTrue(res)
        # should be true if algorithm run successfully
        self.assertTrue(res[1])
        # should be dict with output keys
        self.assertTrue(isinstance(res[0], dict))
        # should be path to result file
        self.assertTrue(isinstance(res[0]["html"], str))


if __name__ == "__main__":
    unittest.main()