-
Notifications
You must be signed in to change notification settings - Fork 8
/
test_qgis_bot.py
153 lines (126 loc) · 4.97 KB
/
test_qgis_bot.py
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
148
149
150
151
152
153
# Copyright (C) 2021-2022 pytest-qgis Contributors.
#
#
# This file is part of pytest-qgis.
#
# pytest-qgis 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.
#
# pytest-qgis is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with pytest-qgis. If not, see <https://www.gnu.org/licenses/>.
#
from typing import TYPE_CHECKING
import pytest
from qgis.core import QgsFieldConstraints, QgsGeometry
from qgis.gui import QgsAttributeDialog
if TYPE_CHECKING:
from pytest_qgis.qgis_bot import QgisBot
from qgis.core import QgsVectorLayer
from qgis.gui import QgisInterface
@pytest.fixture()
def layer_with_soft_constraint(layer_points: "QgsVectorLayer") -> "QgsVectorLayer":
"""setup the layer"""
# Set not-null constraint with SOFT strength
fields = layer_points.fields()
field_idx = fields.indexOf("text_field")
layer_points.setFieldConstraint(
field_idx,
QgsFieldConstraints.Constraint.ConstraintNotNull,
QgsFieldConstraints.ConstraintStrengthSoft,
)
layer_points.startEditing()
return layer_points
@pytest.fixture()
def layer_with_hard_constraint(layer_points: "QgsVectorLayer") -> "QgsVectorLayer":
"""setup the layer"""
# Set not-null constraint with SOFT strength
fields = layer_points.fields()
field_idx = fields.indexOf("text_field")
layer_points.setFieldConstraint(
field_idx,
QgsFieldConstraints.Constraint.ConstraintNotNull,
QgsFieldConstraints.ConstraintStrengthHard,
)
layer_points.startEditing()
return layer_points
def test_feature_gets_created_with_check_box_false_when_not_raising_from_warnings(
layer_with_soft_constraint: "QgsVectorLayer", qgis_bot: "QgisBot"
):
feature_count_before = layer_with_soft_constraint.featureCount()
feature = qgis_bot.create_feature_with_attribute_dialog(
layer_with_soft_constraint,
QgsGeometry.fromWkt("POINT(0,0)"),
raise_from_warnings=False,
)
assert layer_with_soft_constraint.featureCount() == feature_count_before + 1
# With normal way of creating, it would be NULL.
assert feature["bool_field"] is False
def test_should_raise_valueerror_on_soft_constraint_break_when_asked(
layer_with_soft_constraint: "QgsVectorLayer", qgis_bot: "QgisBot"
):
with pytest.raises(ValueError, match="value is NULL"):
qgis_bot.create_feature_with_attribute_dialog(
layer_with_soft_constraint,
QgsGeometry.fromWkt("POINT(0,0)"),
raise_from_warnings=True,
)
def test_feature_gets_created_with_check_box_false_when_not_raising_from_errors(
layer_with_hard_constraint: "QgsVectorLayer", qgis_bot: "QgisBot"
):
feature_count_before = layer_with_hard_constraint.featureCount()
feature = qgis_bot.create_feature_with_attribute_dialog(
layer_with_hard_constraint,
QgsGeometry.fromWkt("POINT(0,0)"),
raise_from_errors=False,
)
assert layer_with_hard_constraint.featureCount() == feature_count_before + 1
# With normal way of creating, it would be NULL.
assert feature["bool_field"] is False
def test_should_raise_valueerror_on_hard_constraint_break_when_asked(
layer_with_hard_constraint: "QgsVectorLayer", qgis_bot: "QgisBot"
):
with pytest.raises(ValueError, match="value is NULL"):
qgis_bot.create_feature_with_attribute_dialog(
layer_with_hard_constraint,
QgsGeometry.fromWkt("POINT(0,0)"),
raise_from_errors=True,
)
def test_create_simple_feature_with_attribute_dialog(
layer_points: "QgsVectorLayer", qgis_bot: "QgisBot"
):
layer = layer_points
count = layer.featureCount()
layer.startEditing()
feat = qgis_bot.create_feature_with_attribute_dialog(
layer, QgsGeometry.fromWkt("POINT(0,0)")
)
assert layer.featureCount() == count + 1
assert feat["bool_field"] is False
def test_get_qgs_attribute_dialog_widgets_by_name(
qgis_iface: "QgisInterface", layer_points: "QgsVectorLayer", qgis_bot: "QgisBot"
):
dialog = QgsAttributeDialog(
layer_points,
layer_points.getFeature(1),
False,
qgis_iface.mainWindow(),
True,
)
widgets_by_name = qgis_bot.get_qgs_attribute_dialog_widgets_by_name(dialog)
assert {
name: widget.__class__.__name__ for name, widget in widgets_by_name.items()
} == {
"bool_field": "QCheckBox",
"date_field": "QDateTimeEdit",
"datetime_field": "QDateTimeEdit",
"decimal_field": "QgsFilterLineEdit",
"fid": "QgsFilterLineEdit",
"text_field": "QgsFilterLineEdit",
}