~mapclient.devs/mapclient/stable

« back to all changes in this revision

Viewing changes to tests/tools_tests/wizard.py

  • Committer: musculoskeletal
  • Date: 2014-06-25 05:38:05 UTC
  • mfrom: (1.6.35 testing)
  • Revision ID: musculoskeletal@bioeng1033-20140625053805-jkqhi5oq74vmlntl
Merging testing into stable.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
'''
2
 
MAP Client, a program to generate detailed musculoskeletal models for OpenSim.
3
 
    Copyright (C) 2012  University of Auckland
4
 
    
5
 
This file is part of MAP Client. (http://launchpad.net/mapclient)
6
 
 
7
 
    MAP Client is free software: you can redistribute it and/or modify
8
 
    it under the terms of the GNU General Public License as published by
9
 
    the Free Software Foundation, either version 3 of the License, or
10
 
    (at your option) any later version.
11
 
 
12
 
    MAP Client is distributed in the hope that it will be useful,
13
 
    but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 
    GNU General Public License for more details.
16
 
 
17
 
    You should have received a copy of the GNU General Public License
18
 
    along with MAP Client.  If not, see <http://www.gnu.org/licenses/>..
19
 
'''
20
 
import os, unittest
21
 
from PySide import QtGui
22
 
 
23
 
from tools.pluginwizard import wizarddialog
24
 
from tools.pluginwizard.skeleton import SkeletonOptions, Skeleton
25
 
 
26
 
if QtGui.qApp == None: QtGui.QApplication([])
27
 
import widgets.resources_rc
28
 
 
29
 
import alltests
30
 
 
31
 
test_path = os.path.join(os.path.dirname(alltests.__file__), 'test_resources', 'wizard_test')
32
 
 
33
 
PLUGIN_WRITE_TO_DIRECTORY = test_path
34
 
PLUGIN_PACKAGE_NAME = 'abcdalphastep'
35
 
PLUGIN_NAME = 'Abcd Alpha'
36
 
PLUGIN_IMAGE_FILE = os.path.join(test_path, 'logo.png')
37
 
CATEGORY = 'Viewer'
38
 
AUTHOR_NAME = 'Prince of Persia'
39
 
 
40
 
class WizardTestCase(unittest.TestCase):
41
 
 
42
 
 
43
 
    def assertIn(self, a, b, *args, **kwargs):
44
 
        ''''Python < v2.7 compatibility.  Assert "a" in "b"'''
45
 
        try:
46
 
            f = super(WizardTestCase, self).assertIn
47
 
        except AttributeError:
48
 
            self.assertTrue(a in b, *args, **kwargs)
49
 
        else:
50
 
            f(a, b, *args, **kwargs)
51
 
 
52
 
    def assertNotIn(self, a, b, *args, **kwargs):
53
 
        ''''Python < v2.7 compatibility.  Assert "a" NOT in "b"'''
54
 
        try:
55
 
            f = super(WizardTestCase, self).assertNotIn
56
 
        except AttributeError:
57
 
            self.assertFalse(a in b, *args, **kwargs)
58
 
        else:
59
 
            f(a, b, *args, **kwargs)
60
 
 
61
 
    def _doCleanUp(self, package_name):
62
 
        rmdir = os.path.join(PLUGIN_WRITE_TO_DIRECTORY, package_name)
63
 
        if os.path.exists(rmdir):
64
 
            directories = []
65
 
            for package_dir, _, files in os.walk(rmdir):
66
 
                for file_ in files:
67
 
                    src_file = os.path.join(package_dir, file_)
68
 
                    os.remove(src_file)
69
 
 
70
 
                directories.append(package_dir)
71
 
 
72
 
            directories.reverse()
73
 
            for directory in directories:
74
 
                os.rmdir(directory)
75
 
 
76
 
 
77
 
    def tearDown(self):
78
 
        pass
79
 
 
80
 
 
81
 
#     def testRunWizard(self):
82
 
#         '''
83
 
#         Visual test for wizard, uncomment to manually test.
84
 
#         '''
85
 
#         dlg = wizarddialog.WizardDialog()
86
 
#         result = dlg.exec_()
87
 
#         self.assertTrue(result == dlg.Accepted or result == dlg.Rejected)
88
 
 
89
 
    def testWizard(self):
90
 
        dlg = wizarddialog.WizardDialog()
91
 
 
92
 
        p1 = dlg.page(1)
93
 
        p1._ui.nameLineEdit.setText(PLUGIN_NAME)
94
 
        p1._ui.iconLineEdit.setText(PLUGIN_IMAGE_FILE)
95
 
        p2 = dlg.page(2)
96
 
        p2._ui.portTableWidget.insertRow(0)
97
 
        cb = QtGui.QComboBox()
98
 
        cb.addItems(['provides', 'uses'])
99
 
        p2._ui.portTableWidget.setCellWidget(0, 0, cb)
100
 
        p2._ui.portTableWidget.setItem(0, 1, QtGui.QTableWidgetItem('http://my.example.org/1.0/workflowstep#octopus'))
101
 
        p3 = dlg.page(3)
102
 
        p3._ui.configTableWidget.setItem(0, 1, QtGui.QTableWidgetItem('xxx'))
103
 
        p4 = dlg.page(4)
104
 
        p4._ui.authorNameLineEdit.setText(AUTHOR_NAME)
105
 
        p4._ui.categoryLineEdit.setText(CATEGORY)
106
 
        p5 = dlg.page(5)
107
 
        p5._ui.directoryLineEdit.setText(PLUGIN_WRITE_TO_DIRECTORY)
108
 
 
109
 
        dlg.accept()
110
 
 
111
 
        options = dlg.getOptions()
112
 
        self.assertEqual(PLUGIN_NAME, options.getName())
113
 
        self.assertEqual(PLUGIN_IMAGE_FILE, options.getImageFile())
114
 
        self.assertEqual(PLUGIN_PACKAGE_NAME, options.getPackageName())
115
 
        self.assertEqual(PLUGIN_WRITE_TO_DIRECTORY, options.getOutputDirectory())
116
 
        self.assertEqual(1, options.portCount())
117
 
        self.assertEqual([u'http://physiomeproject.org/workflow/1.0/rdf-schema#provides', u'http://my.example.org/1.0/workflowstep#octopus'], options.getPort(0))
118
 
        self.assertEqual(1, options.configCount())
119
 
        self.assertEqual([u'identifier', u''], options.getConfig(0))
120
 
        self.assertEqual(CATEGORY, options.getCategory())
121
 
        self.assertEqual(AUTHOR_NAME, options.getAuthorName())
122
 
 
123
 
    def testSkeleton1(self):
124
 
 
125
 
        local_package_name = PLUGIN_PACKAGE_NAME.replace('step', str(1) + 'step')
126
 
 
127
 
        options = SkeletonOptions()
128
 
        options.setImageFile(PLUGIN_IMAGE_FILE)
129
 
        options.setName(PLUGIN_NAME + str(1))
130
 
        options.setPackageName(local_package_name)
131
 
        options.setOutputDirectory(PLUGIN_WRITE_TO_DIRECTORY)
132
 
        options.addPort('http://physiomeproject.org/workflow/1.0/rdf-schema#uses', 'object')
133
 
        options.addPort('http://physiomeproject.org/workflow/1.0/rdf-schema#provides', 'http://my.example.org/1.0/workflowstep#octopus')
134
 
        options.addConfig('identifier', '')
135
 
        options.setAuthorName(AUTHOR_NAME)
136
 
        options.setCategory(CATEGORY)
137
 
 
138
 
        s = Skeleton(options)
139
 
        s.write()
140
 
 
141
 
        package_dir = os.path.join(PLUGIN_WRITE_TO_DIRECTORY, local_package_name)
142
 
        self.assertTrue(os.path.exists(package_dir))
143
 
        package_init_file = os.path.join(package_dir, '__init__.py')
144
 
        self.assertTrue(os.path.exists(package_init_file))
145
 
        init_contents = open(package_init_file).read()
146
 
        self.assertIn(AUTHOR_NAME, init_contents)
147
 
 
148
 
        step_file = os.path.join(package_dir, local_package_name, 'step.py')
149
 
        self.assertTrue(os.path.exists(step_file))
150
 
 
151
 
        file_contents = open(step_file).read()
152
 
        self.assertIn('octopus', file_contents)
153
 
        self.assertIn('http://physiomeproject.org/workflow/1.0/rdf-schema#provides', file_contents)
154
 
        self.assertIn('return self._config[', file_contents)
155
 
        self.assertIn('] = identifier', file_contents)
156
 
        self.assertIn('self._category = \'' + CATEGORY + '\'', file_contents)
157
 
        self.assertNotIn('{setidentifiercontent}', file_contents)
158
 
        self.assertNotIn('{serializecontent}', file_contents)
159
 
        self.assertNotIn('{serializesetvalues}', file_contents)
160
 
 
161
 
 
162
 
        resources_file = os.path.join(package_dir, local_package_name, 'resources_rc.py')
163
 
        self.assertTrue(os.path.exists(resources_file))
164
 
 
165
 
        config_file = os.path.join(package_dir, local_package_name, 'configuredialog.py')
166
 
        self.assertTrue(os.path.exists(config_file))
167
 
 
168
 
        config_contents = open(config_file).read()
169
 
        self.assertIn('validate', config_contents)
170
 
 
171
 
        self._doCleanUp(local_package_name)  # Move this to the start of the test if you want to see the output
172
 
 
173
 
    def testSkeleton2(self):
174
 
 
175
 
        local_package_name = PLUGIN_PACKAGE_NAME.replace('step', str(2) + 'step')
176
 
 
177
 
        options = SkeletonOptions()
178
 
        options.setImageFile(PLUGIN_IMAGE_FILE)
179
 
        options.setName(PLUGIN_NAME + str(2))
180
 
        options.setPackageName(local_package_name)
181
 
        options.setOutputDirectory(PLUGIN_WRITE_TO_DIRECTORY)
182
 
        options.addPort('http://physiomeproject.org/workflow/1.0/rdf-schema#uses', 'object')
183
 
        options.addPort('http://physiomeproject.org/workflow/1.0/rdf-schema#provides', 'number')
184
 
        options.addPort('http://physiomeproject.org/workflow/1.0/rdf-schema#provides', 'http://my.example.org/1.0/workflowstep#octopus')
185
 
        options.addPort('http://physiomeproject.org/workflow/1.0/rdf-schema#uses', 'int')
186
 
 
187
 
        s = Skeleton(options)
188
 
        s.write()
189
 
 
190
 
        package_dir = os.path.join(PLUGIN_WRITE_TO_DIRECTORY, local_package_name)
191
 
        self.assertTrue(os.path.exists(package_dir))
192
 
        package_init_file = os.path.join(package_dir, '__init__.py')
193
 
        self.assertTrue(os.path.exists(package_init_file))
194
 
        step_file = os.path.join(package_dir, local_package_name, 'step.py')
195
 
        self.assertTrue(os.path.exists(step_file))
196
 
 
197
 
        file_contents = open(step_file).read()
198
 
        self.assertIn('octopus', file_contents)
199
 
        self.assertIn('http://physiomeproject.org/workflow/1.0/rdf-schema#provides', file_contents)
200
 
        self.assertIn('# TODO: The string must be replaced with', file_contents)
201
 
        self.assertIn('# TODO: Must actually set the step', file_contents)
202
 
        self.assertNotIn('{setidentifiercontent}', file_contents)
203
 
        self.assertNotIn('{serializecontent}', file_contents)
204
 
        self.assertNotIn('{serializesetvalues}', file_contents)
205
 
 
206
 
        resources_file = os.path.join(package_dir, local_package_name, 'resources_rc.py')
207
 
        self.assertTrue(os.path.exists(resources_file))
208
 
 
209
 
        self._doCleanUp(local_package_name)  # Move this to the start of the test if you want to see the output
210
 
 
211
 
    def testSkeleton3(self):
212
 
 
213
 
        local_package_name = PLUGIN_PACKAGE_NAME.replace('step', str(3) + 'step')
214
 
 
215
 
        options = SkeletonOptions()
216
 
        options.setImageFile(PLUGIN_IMAGE_FILE)
217
 
        options.setName(PLUGIN_NAME + str(3))
218
 
        options.setPackageName(local_package_name)
219
 
        options.setOutputDirectory(PLUGIN_WRITE_TO_DIRECTORY)
220
 
        options.addPort('http://physiomeproject.org/workflow/1.0/rdf-schema#uses', 'object')
221
 
        options.addPort('http://physiomeproject.org/workflow/1.0/rdf-schema#provides', 'number')
222
 
        options.addPort('http://physiomeproject.org/workflow/1.0/rdf-schema#provides', 'http://my.example.org/1.0/workflowstep#octopus')
223
 
        options.addPort('http://physiomeproject.org/workflow/1.0/rdf-schema#uses', 'int')
224
 
        options.addConfig('identifier', '')
225
 
        options.addConfig('Cabbage', 'Brown')
226
 
        options.addConfig('Path', '')
227
 
        options.addConfig('Carrot', 'tis a long way down')
228
 
 
229
 
        s = Skeleton(options)
230
 
        s.write()
231
 
 
232
 
        package_dir = os.path.join(PLUGIN_WRITE_TO_DIRECTORY, local_package_name)
233
 
        self.assertTrue(os.path.exists(package_dir))
234
 
        package_init_file = os.path.join(package_dir, '__init__.py')
235
 
        self.assertTrue(os.path.exists(package_init_file))
236
 
        step_file = os.path.join(package_dir, local_package_name, 'step.py')
237
 
        self.assertTrue(os.path.exists(step_file))
238
 
 
239
 
        file_contents = open(step_file).read()
240
 
        self.assertIn('octopus', file_contents)
241
 
        self.assertIn('http://physiomeproject.org/workflow/1.0/rdf-schema#provides', file_contents)
242
 
        self.assertIn('Cabbage', file_contents)
243
 
        self.assertIn('Carrot', file_contents)
244
 
        self.assertNotIn('{setidentifiercontent}', file_contents)
245
 
        self.assertNotIn('{serializecontent}', file_contents)
246
 
        self.assertNotIn('{serializesetvalues}', file_contents)
247
 
 
248
 
        resources_file = os.path.join(package_dir, local_package_name, 'resources_rc.py')
249
 
        self.assertTrue(os.path.exists(resources_file))
250
 
 
251
 
        config_file = os.path.join(package_dir, local_package_name, 'configuredialog.py')
252
 
        self.assertTrue(os.path.exists(config_file))
253
 
 
254
 
        self._doCleanUp(local_package_name)  # Move this to the start of the test if you want to see the output
255
 
 
256
 
    def testSkeleton4(self):
257
 
 
258
 
        local_package_name = PLUGIN_PACKAGE_NAME.replace('step', str(4) + 'step')
259
 
 
260
 
        options = SkeletonOptions()
261
 
        options.setImageFile(PLUGIN_IMAGE_FILE)
262
 
        options.setName(PLUGIN_NAME + str(4))
263
 
        options.setPackageName(local_package_name)
264
 
        options.setOutputDirectory(PLUGIN_WRITE_TO_DIRECTORY)
265
 
        options.addPort('http://physiomeproject.org/workflow/1.0/rdf-schema#uses', 'object')
266
 
        options.addPort('http://physiomeproject.org/workflow/1.0/rdf-schema#provides', 'number')
267
 
        options.addPort('http://physiomeproject.org/workflow/1.0/rdf-schema#provides', 'http://my.example.org/1.0/workflowstep#octopus')
268
 
        options.addPort('http://physiomeproject.org/workflow/1.0/rdf-schema#uses', 'int')
269
 
        options.addConfig('Cabbage', 'Brown')
270
 
        options.addConfig('Path', '')
271
 
        options.addConfig('Carrot', 'tis a long way down')
272
 
 
273
 
 
274
 
        s = Skeleton(options)
275
 
        s.write()
276
 
 
277
 
        package_dir = os.path.join(PLUGIN_WRITE_TO_DIRECTORY, local_package_name)
278
 
        self.assertTrue(os.path.exists(package_dir))
279
 
        package_init_file = os.path.join(package_dir, '__init__.py')
280
 
        self.assertTrue(os.path.exists(package_init_file))
281
 
        step_file = os.path.join(package_dir, local_package_name, 'step.py')
282
 
        self.assertTrue(os.path.exists(step_file))
283
 
 
284
 
        file_contents = open(step_file).read()
285
 
        self.assertIn('octopus', file_contents)
286
 
        self.assertIn('http://physiomeproject.org/workflow/1.0/rdf-schema#provides', file_contents)
287
 
        self.assertIn('Cabbage', file_contents)
288
 
        self.assertIn('Carrot', file_contents)
289
 
        self.assertNotIn('{setidentifiercontent}', file_contents)
290
 
        self.assertNotIn('{serializecontent}', file_contents)
291
 
        self.assertNotIn('{serializesetvalues}', file_contents)
292
 
 
293
 
        resources_file = os.path.join(package_dir, local_package_name, 'resources_rc.py')
294
 
        self.assertTrue(os.path.exists(resources_file))
295
 
 
296
 
        config_file = os.path.join(package_dir, local_package_name, 'configuredialog.py')
297
 
        self.assertTrue(os.path.exists(config_file))
298
 
 
299
 
        self._doCleanUp(local_package_name)  # Move this to the start of the test if you want to see the output
300
 
 
301
 
 
302
 
if __name__ == "__main__":
303
 
    # import sys;sys.argv = ['', 'Test.testName']
304
 
    unittest.main()