Skip to content

Commit a3d6b98

Browse files
committed
Refined tests for QgsProject::transactionGroup()
1 parent 798408b commit a3d6b98

File tree

3 files changed

+109
-10
lines changed

3 files changed

+109
-10
lines changed

tests/src/python/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,7 @@ ENDIF (WITH_DESKTOP)
228228

229229
IF (ENABLE_PGTEST)
230230
ADD_PYTHON_TEST(PyQgsPostgresProvider test_provider_postgres.py)
231+
ADD_PYTHON_TEST(PyQgsPostgresTransaction test_qgspostgrestransaction.py)
231232
ADD_PYTHON_TEST(PyQgsRelationEditWidget test_qgsrelationeditwidget.py)
232233
ADD_PYTHON_TEST(PyQgsVectorLayerTools test_qgsvectorlayertools.py)
233234
ADD_PYTHON_TEST(PyQgsProjectStoragePostgres test_project_storage_postgres.py)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
# -*- coding: utf-8 -*-
2+
"""QGIS Unit tests for postgres transaction groups.
3+
4+
.. note:: This program is free software; you can redistribute it and/or modify
5+
it under the terms of the GNU General Public License as published by
6+
the Free Software Foundation; either version 2 of the License, or
7+
(at your option) any later version.
8+
"""
9+
__author__ = 'Nyall Dawson'
10+
__date__ = '11/06/2018'
11+
__copyright__ = 'Copyright 2018, The QGIS Project'
12+
# This will get replaced with a git SHA1 when you do a git archive
13+
__revision__ = '$Format:%H$'
14+
15+
import qgis # NOQA
16+
17+
import os
18+
19+
from qgis.core import (
20+
QgsVectorLayer,
21+
QgsProject,
22+
QgsTransaction,
23+
QgsDataSourceUri
24+
)
25+
26+
from qgis.testing import start_app, unittest
27+
28+
start_app()
29+
30+
31+
class TestQgsPostgresTransaction(unittest.TestCase):
32+
33+
@classmethod
34+
def setUpClass(cls):
35+
"""
36+
Setup the involved layers and relations for a n:m relation
37+
:return:
38+
"""
39+
cls.dbconn = 'dbname=\'qgis_test\''
40+
if 'QGIS_PGTEST_DB' in os.environ:
41+
cls.dbconn = os.environ['QGIS_PGTEST_DB']
42+
# Create test layer
43+
cls.vl_b = QgsVectorLayer(cls.dbconn + ' sslmode=disable key=\'pk\' table="qgis_test"."books" sql=', 'books', 'postgres')
44+
cls.vl_a = QgsVectorLayer(cls.dbconn + ' sslmode=disable key=\'pk\' table="qgis_test"."authors" sql=', 'authors', 'postgres')
45+
46+
QgsProject.instance().addMapLayer(cls.vl_b)
47+
QgsProject.instance().addMapLayer(cls.vl_a)
48+
49+
cls.relMgr = QgsProject.instance().relationManager()
50+
51+
assert(cls.vl_a.isValid())
52+
assert(cls.vl_b.isValid())
53+
54+
def startTransaction(self):
55+
"""
56+
Start a new transaction and set all layers into transaction mode.
57+
58+
:return: None
59+
"""
60+
lyrs = [self.vl_a, self.vl_b]
61+
62+
self.transaction = QgsTransaction.create(lyrs)
63+
self.transaction.begin()
64+
for l in lyrs:
65+
l.startEditing()
66+
67+
def rollbackTransaction(self):
68+
"""
69+
Rollback all changes done in this transaction.
70+
We always rollback and never commit to have the database in a pristine
71+
state at the end of each test.
72+
73+
:return: None
74+
"""
75+
lyrs = [self.vl_a, self.vl_b]
76+
for l in lyrs:
77+
l.commitChanges()
78+
self.transaction.rollback()
79+
80+
def test_transactionsGroup(self):
81+
conn_string = QgsDataSourceUri(self.vl_b.source()).connectionInfo()
82+
83+
# No transaction group.
84+
QgsProject.instance().setAutoTransaction(False)
85+
noTg = QgsProject.instance().transactionGroup("postgres", conn_string)
86+
self.assertIsNone(noTg)
87+
88+
# start transaction - no auto transaction
89+
self.startTransaction()
90+
noTg = QgsProject.instance().transactionGroup("postgres", conn_string)
91+
self.assertIsNone(noTg)
92+
self.rollbackTransaction()
93+
94+
# with auto transactions
95+
QgsProject.instance().setAutoTransaction(True)
96+
self.startTransaction()
97+
noTg = QgsProject.instance().transactionGroup("postgres", conn_string)
98+
self.assertIsNotNone(noTg)
99+
self.rollbackTransaction()
100+
101+
# bad provider key
102+
self.startTransaction()
103+
noTg = QgsProject.instance().transactionGroup("xxxpostgres", conn_string)
104+
self.assertIsNone(noTg)
105+
self.rollbackTransaction()
106+
107+
if __name__ == '__main__':
108+
unittest.main()

tests/src/python/test_qgsproject.py

-10
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,6 @@ def __init__(self, methodName):
5454
unittest.TestCase.__init__(self, methodName)
5555
self.messageCaught = False
5656

57-
def setUpClass(cls):
58-
cls.dbconn = 'dbname=\'qgis_test\''
59-
if 'QGIS_PGTEST_DB' in os.environ:
60-
cls.dbconn = os.environ['QGIS_PGTEST_DB']
61-
6257
def test_makeKeyTokens_(self):
6358
# see http://www.w3.org/TR/REC-xml/#d0e804 for a list of valid characters
6459

@@ -695,11 +690,6 @@ def testTakeLayer(self):
695690
self.assertTrue(l1.isValid())
696691

697692
def test_transactionsGroup(self):
698-
# No transaction group.
699-
QgsProject.instance().setAutoTransaction(False)
700-
noTg = QgsProject.instance().transactionGroup("provider-key", "database-connection-string")
701-
self.assertIsNone(noTg)
702-
703693
# Undefined transaction group (wrong provider key).
704694
QgsProject.instance().setAutoTransaction(True)
705695
noTg = QgsProject.instance().transactionGroup("provider-key", "database-connection-string")

0 commit comments

Comments
 (0)