22
22
QgsField ,
23
23
QgsFields
24
24
)
25
- from qgis .PyQt .QtCore import QDir , QTemporaryFile
25
+ from qgis .PyQt .QtCore import QDir , QTemporaryFile , QUuid
26
26
27
27
from qgis .testing import (start_app ,
28
28
unittest
@@ -70,26 +70,26 @@ def testAddAction(self):
70
70
""" Test adding actions """
71
71
72
72
# should be empty to start with
73
- self .assertEqual (self .manager .listActions (), [])
73
+ self .assertEqual (self .manager .actions (), [])
74
74
75
75
# add an action
76
76
action1 = QgsAction (QgsAction .GenericPython , 'Test Action' , 'i=1' )
77
77
self .manager .addAction (action1 )
78
- self .assertEqual (len (self .manager .listActions ()), 1 )
79
- self .assertEqual (self .manager .listActions ()[0 ].type (), QgsAction .GenericPython )
80
- self .assertEqual (self .manager .listActions ()[0 ].name (), 'Test Action' )
81
- self .assertEqual (self .manager .listActions ()[0 ].command (), 'i=1' )
78
+ self .assertEqual (len (self .manager .actions ()), 1 )
79
+ self .assertEqual (self .manager .actions ()[0 ].type (), QgsAction .GenericPython )
80
+ self .assertEqual (self .manager .actions ()[0 ].name (), 'Test Action' )
81
+ self .assertEqual (self .manager .actions ()[0 ].command (), 'i=1' )
82
82
83
83
# add another action
84
84
action2 = QgsAction (QgsAction .Windows , 'Test Action2' , 'i=2' )
85
85
self .manager .addAction (action2 )
86
- self .assertEqual (len (self .manager .listActions ()), 2 )
86
+ self .assertEqual (len (self .manager .actions ()), 2 )
87
87
self .assertEqual (self .manager .action (action2 .id ()).type (), QgsAction .Windows )
88
88
self .assertEqual (self .manager .action (action2 .id ()).name (), 'Test Action2' )
89
89
self .assertEqual (self .manager .action (action2 .id ()).command (), 'i=2' )
90
90
91
91
id3 = self .manager .addAction (QgsAction .Generic , 'Test Action3' , 'i=3' )
92
- self .assertEqual (len (self .manager .listActions ()), 3 )
92
+ self .assertEqual (len (self .manager .actions ()), 3 )
93
93
self .assertEqual (self .manager .action (id3 ).type (), QgsAction .Generic )
94
94
self .assertEqual (self .manager .action (id3 ).name (), 'Test Action3' )
95
95
self .assertEqual (self .manager .action (id3 ).command (), 'i=3' )
@@ -102,75 +102,52 @@ def testRemoveActions(self):
102
102
103
103
# clear the manager and check that it's empty
104
104
self .manager .clearActions ()
105
- self .assertEqual (self .manager .size (), 0 )
106
- self .assertEqual (self .manager .listActions (), [])
105
+ self .assertEqual (self .manager .actions (), [])
107
106
108
107
# add some actions
109
108
id1 = self .manager .addAction (QgsAction .GenericPython , 'test_action' , 'i=1' )
110
109
id2 = self .manager .addAction (QgsAction .GenericPython , 'test_action2' , 'i=2' )
111
110
id3 = self .manager .addAction (QgsAction .GenericPython , 'test_action3' , 'i=3' )
112
111
113
112
# remove non-existant action
114
- self .manager .removeAction (5 )
113
+ self .manager .removeAction (QUuid . createUuid () )
115
114
116
115
# remove them one by one
117
- self .manager .removeAction (1 )
118
- self .assertEqual (self .manager .size (), 2 )
119
- self .assertEqual (self .manager .listActions ()[0 ].name (), 'test_action' )
120
- self .assertEqual (self .manager .listActions ()[1 ].name (), 'test_action3' )
121
- self .manager .removeAction (0 )
122
- self .assertEqual (self .manager .size (), 1 )
123
- self .assertEqual (self .manager .listActions ()[0 ].name (), 'test_action3' )
124
- self .manager .removeAction (0 )
125
- self .assertEqual (self .manager .size (), 0 )
126
-
127
- def testRetrieveAction (self ):
128
- """ test retrieving actions """
129
- self .manager .clearActions ()
130
-
131
- # test that exceptions are thrown when retrieving bad indices
132
-
133
- with self .assertRaises (KeyError ):
134
- self .manager [0 ]
135
-
136
- with self .assertRaises (KeyError ):
137
- self .manager .at (0 )
138
-
139
- self .manager .addAction (QgsAction .GenericPython , 'test_action' , 'i=1' )
140
-
141
- with self .assertRaises (KeyError ):
142
- self .manager [- 1 ]
143
-
144
- with self .assertRaises (KeyError ):
145
- self .manager .at (- 1 )
146
-
147
- with self .assertRaises (KeyError ):
148
- self .manager [5 ]
149
-
150
- with self .assertRaises (KeyError ):
151
- self .manager .at (5 )
116
+ self .manager .removeAction (id2 )
117
+ self .assertEqual (len (self .manager .actions ()), 2 )
118
+ self .assertEqual (self .manager .action (id1 ).name (), 'test_action' )
119
+ self .assertEqual (self .manager .action (id3 ).name (), 'test_action3' )
120
+ self .manager .removeAction (id1 )
121
+ self .assertEqual (len (self .manager .actions ()), 1 )
122
+ self .assertEqual (self .manager .action (id3 ).name (), 'test_action3' )
123
+ self .manager .removeAction (id3 )
124
+ self .assertEqual (len (self .manager .actions ()), 0 )
152
125
153
126
def testDefaultAction (self ):
154
127
""" test default action for layer"""
155
128
156
129
self .manager .clearActions ()
157
- self .manager .addAction (QgsAction .GenericPython , 'test_action' , 'i=1' )
158
- self .manager .addAction (QgsAction .GenericPython , 'test_action2' , 'i=2' )
130
+ action1 = QgsAction (QgsAction .GenericPython , 'test_action' , '' , 'i=1' , False , actionScopes = {'Feature' })
131
+ self .manager .addAction (action1 )
132
+ action2 = QgsAction (QgsAction .GenericPython , 'test_action2' , 'i=2' )
133
+ self .manager .addAction (action2 )
159
134
160
135
# initially should be not set
161
- self .assertEqual (self .manager .defaultAction (), - 1 )
136
+ self .assertFalse (self .manager .defaultAction ('Feature' ). isValid () )
162
137
163
138
# set bad default action
164
- self .manager .setDefaultAction (10 )
165
- self .assertEqual (self .manager .defaultAction (), - 1 )
139
+ self .manager .setDefaultAction ('Feature' , QUuid . createUuid () )
140
+ self .assertFalse (self .manager .defaultAction ('Feature' ). isValid () )
166
141
167
142
# set good default action
168
- self .manager .setDefaultAction (1 )
169
- self .assertEqual (self .manager .defaultAction (), 1 )
143
+ self .manager .setDefaultAction ('Feature' , action1 .id ())
144
+ self .assertTrue (self .manager .defaultAction ('Feature' ).isValid ())
145
+ self .assertEquals (self .manager .defaultAction ('Feature' ).id (), action1 .id ())
146
+ self .assertNotEquals (self .manager .defaultAction ('Feature' ).id (), action2 .id ())
170
147
171
148
# if default action is removed, should be reset to -1
172
149
self .manager .clearActions ()
173
- self .assertEqual (self .manager .defaultAction (), - 1 )
150
+ self .assertFalse (self .manager .defaultAction ('Feature' ). isValid () )
174
151
175
152
def check_action_result (self , temp_file ):
176
153
with open (temp_file , 'r' ) as result :
@@ -185,7 +162,7 @@ def testDoAction(self):
185
162
186
163
# simple action
187
164
temp_file = self .get_temp_filename ()
188
- self .manager .addAction (QgsAction .Unix , 'test_action' , self .create_action (temp_file , 'test output' ))
165
+ id1 = self .manager .addAction (QgsAction .Unix , 'test_action' , self .create_action (temp_file , 'test output' ))
189
166
190
167
fields = QgsFields ()
191
168
fields .append (QgsField ('my_field' ))
@@ -195,28 +172,29 @@ def testDoAction(self):
195
172
f .setAttributes ([5 , 'val' ])
196
173
197
174
c = QgsExpressionContext ()
198
- self .manager .doAction (0 , f , c )
175
+ self .manager .doAction (id1 , f , c )
199
176
time .sleep (0.5 )
200
177
201
- self .assertEqual (self .check_action_result (temp_file ), 'test output' )
178
+ self .assertEquals (self .check_action_result (temp_file ), 'test output' )
202
179
203
180
# action with substitutions
204
181
temp_file = self .get_temp_filename ()
205
- self .manager .addAction (QgsAction .Unix , 'test_action' , self .create_action (temp_file , 'test [% $id %] output [% @layer_name %]' ))
206
- self .manager .doAction (1 , f , c )
182
+ id2 = self .manager .addAction (QgsAction .Unix , 'test_action' , self .create_action (temp_file , 'test [% $id %] output [% @layer_name %]' ))
183
+ self .manager .doAction (id2 , f , c )
207
184
time .sleep (0.5 )
208
185
209
- self .assertEqual (self .check_action_result (temp_file ), 'test 1 output test_layer' )
186
+ self .assertEquals (self .check_action_result (temp_file ), 'test 1 output test_layer' )
210
187
211
188
# test doAction using field variant
212
189
temp_file = self .get_temp_filename ()
213
- self .manager .addAction (QgsAction .Unix , 'test_action' , self .create_action (temp_file , 'test [% @current_field %]' ))
214
- self .manager .doActionFeature (2 , f , 0 )
190
+ id3 = self .manager .addAction (QgsAction .Unix , 'test_action' ,
191
+ self .create_action (temp_file , 'test : [% @field_index %] : [% @field_name %] : [% @field_value%]' ))
192
+ self .manager .doActionFeature (id3 , f , 0 )
215
193
time .sleep (0.5 )
216
- self .assertEqual (self .check_action_result (temp_file ), 'test 5' )
217
- self .manager .doActionFeature (2 , f , 1 )
194
+ self .assertEquals (self .check_action_result (temp_file ), 'test : 0 : my_field : 5' )
195
+ self .manager .doActionFeature (id3 , f , 1 )
218
196
time .sleep (0.5 )
219
- self .assertEqual (self .check_action_result (temp_file ), 'test val' )
197
+ self .assertEquals (self .check_action_result (temp_file ), 'test : 1 : my_other_field : val' )
220
198
221
199
if __name__ == '__main__' :
222
200
unittest .main ()
0 commit comments