-
Notifications
You must be signed in to change notification settings - Fork 200
/
Copy pathtest_code.py
276 lines (228 loc) · 11.5 KB
/
test_code.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
# -*- coding: utf-8 -*-
###########################################################################
# Copyright (c), The AiiDA team. All rights reserved. #
# This file is part of the AiiDA code. #
# #
# The code is hosted on GitHub at https://github.com/aiidateam/aiida-core #
# For further information on the license, see the LICENSE.txt file #
# For further information please visit http://www.aiida.net #
###########################################################################
"""Tests for the 'verdi code' command."""
import os
import subprocess as sp
from click.testing import CliRunner
from aiida.backends.testbase import AiidaTestCase
from aiida.cmdline.commands.cmd_code import (setup_code, delete, hide, reveal, relabel, code_list, show, code_duplicate)
from aiida.common.exceptions import NotExistent
from aiida import orm
class TestVerdiCodeSetup(AiidaTestCase):
"""Tests for the 'verdi code setup' command."""
@classmethod
def setUpClass(cls, *args, **kwargs):
super().setUpClass(*args, **kwargs)
orm.Computer(
name='comp', hostname='localhost', transport_type='local', scheduler_type='direct', workdir='/tmp/aiida'
).store()
def setUp(self):
self.comp = orm.Computer.objects.get(name='comp')
self.cli_runner = CliRunner()
self.this_folder = os.path.dirname(__file__)
self.this_file = os.path.basename(__file__)
def test_help(self):
self.cli_runner.invoke(setup_code, ['--help'], catch_exceptions=False)
def test_reachable(self):
output = sp.check_output(['verdi', 'code', 'setup', '--help'])
self.assertIn(b'Usage:', output)
def test_interactive_remote(self):
"""Test interactive remote code setup."""
from aiida.orm import Code
os.environ['VISUAL'] = 'sleep 1; vim -cwq'
os.environ['EDITOR'] = 'sleep 1; vim -cwq'
label = 'interactive_remote'
user_input = '\n'.join([label, 'description', 'arithmetic.add', 'yes', self.comp.name, '/remote/abs/path'])
result = self.cli_runner.invoke(setup_code, input=user_input)
self.assertClickResultNoException(result)
self.assertIsInstance(Code.get_from_string('{}@{}'.format(label, self.comp.name)), Code)
def test_interactive_upload(self):
"""Test interactive code setup."""
from aiida.orm import Code
os.environ['VISUAL'] = 'sleep 1; vim -cwq'
os.environ['EDITOR'] = 'sleep 1; vim -cwq'
label = 'interactive_upload'
user_input = '\n'.join([label, 'description', 'arithmetic.add', 'no', self.this_folder, self.this_file])
result = self.cli_runner.invoke(setup_code, input=user_input)
self.assertIsNone(result.exception, result.output)
self.assertIsInstance(Code.get_from_string('{}'.format(label)), Code)
def test_noninteractive_remote(self):
"""Test non-interactive remote code setup."""
from aiida.orm import Code
label = 'noninteractive_remote'
options = [
'--non-interactive', '--label={}'.format(label), '--description=description',
'--input-plugin=arithmetic.add', '--on-computer', '--computer={}'.format(self.comp.name),
'--remote-abs-path=/remote/abs/path'
]
result = self.cli_runner.invoke(setup_code, options)
self.assertClickResultNoException(result)
self.assertIsInstance(Code.get_from_string('{}@{}'.format(label, self.comp.name)), Code)
def test_noninteractive_upload(self):
"""Test non-interactive code setup."""
from aiida.orm import Code
label = 'noninteractive_upload'
options = [
'--non-interactive', '--label={}'.format(label), '--description=description',
'--input-plugin=arithmetic.add', '--store-in-db', '--code-folder={}'.format(self.this_folder),
'--code-rel-path={}'.format(self.this_file)
]
result = self.cli_runner.invoke(setup_code, options)
self.assertClickResultNoException(result)
self.assertIsInstance(Code.get_from_string('{}'.format(label)), Code)
def test_from_config(self):
"""Test setting up a code from a config file"""
from aiida.orm import Code
import tempfile
label = 'noninteractive_config'
with tempfile.NamedTemporaryFile('w') as handle:
handle.write(
"""---
label: {l}
input_plugin: arithmetic.add
computer: {c}
remote_abs_path: /remote/abs/path
""".format(l=label, c=self.comp.name)
)
handle.flush()
result = self.cli_runner.invoke(
setup_code,
['--non-interactive', '--config', os.path.realpath(handle.name)]
)
self.assertClickResultNoException(result)
self.assertIsInstance(Code.get_from_string('{}'.format(label)), Code)
def test_mixed(self):
"""Test mixed (interactive/from config) code setup."""
from aiida.orm import Code
label = 'mixed_remote'
options = ['--description=description', '--on-computer', '--remote-abs-path=/remote/abs/path']
user_input = '\n'.join([label, 'arithmetic.add', self.comp.name])
result = self.cli_runner.invoke(setup_code, options, input=user_input)
self.assertClickResultNoException(result)
self.assertIsInstance(Code.get_from_string('{}@{}'.format(label, self.comp.name)), Code)
class TestVerdiCodeCommands(AiidaTestCase):
"""Testing verdi code commands.
Testing everything besides `code setup`."""
@classmethod
def setUpClass(cls, *args, **kwargs):
super().setUpClass(*args, **kwargs)
orm.Computer(
name='comp', hostname='localhost', transport_type='local', scheduler_type='direct', workdir='/tmp/aiida'
).store()
def setUp(self):
self.comp = orm.Computer.objects.get(name='comp')
try:
code = orm.Code.get_from_string('code')
except NotExistent:
code = orm.Code(
input_plugin_name='arithmetic.add',
remote_computer_exec=[self.comp, '/remote/abs/path'],
)
code.label = 'code'
code.description = 'desc'
code.store()
self.code = code
self.cli_runner = CliRunner()
def test_hide_one(self):
result = self.cli_runner.invoke(hide, [str(self.code.pk)])
self.assertIsNone(result.exception, result.output)
self.assertTrue(self.code.hidden)
def test_reveal_one(self):
result = self.cli_runner.invoke(reveal, [str(self.code.pk)])
self.assertIsNone(result.exception, result.output)
self.assertFalse(self.code.hidden)
def test_relabel_code(self):
"""Test force code relabeling."""
result = self.cli_runner.invoke(relabel, [str(self.code.pk), 'new_code'])
self.assertIsNone(result.exception, result.output)
from aiida.orm import load_node
new_code = load_node(self.code.pk)
self.assertEqual(new_code.label, 'new_code')
def test_relabel_code_full(self):
self.cli_runner.invoke(relabel, [str(self.code.pk), 'new_code@comp'])
from aiida.orm import load_node
new_code = load_node(self.code.pk)
self.assertEqual(new_code.label, 'new_code')
def test_relabel_code_full_bad(self):
result = self.cli_runner.invoke(relabel, [str(self.code.pk), 'new_code@otherstuff'])
self.assertIsNotNone(result.exception)
def test_code_delete_one_force(self):
"""Test force code deletion."""
result = self.cli_runner.invoke(delete, [str(self.code.pk), '--force'])
self.assertIsNone(result.exception, result.output)
with self.assertRaises(NotExistent):
from aiida.orm import Code
Code.get_from_string('code')
def test_code_list(self):
"""Test code list command."""
# set up second code 'code2'
from aiida.orm import Code
try:
code = Code.get_from_string('code2')
except NotExistent:
code = Code(
input_plugin_name='templatereplacer',
remote_computer_exec=[self.comp, '/remote/abs/path'],
)
code.label = 'code2'
code.store()
options = ['-A', '-a', '-o', '--input-plugin=arithmetic.add', '--computer={}'.format(self.comp.name)]
result = self.cli_runner.invoke(code_list, options)
self.assertIsNone(result.exception, result.output)
self.assertTrue(str(self.code.pk) in result.output, 'PK of first code should be included')
self.assertTrue('code2' not in result.output, 'label of second code should not be included')
self.assertTrue('comp' in result.output, 'computer name should be included')
self.assertNotIn(result.output, '# No codes found matching the specified criteria.')
def test_code_list_hide(self):
"""Test that hidden codes are shown (or not) properly."""
self.code.hide()
options = ['-A']
result = self.cli_runner.invoke(code_list, options)
self.assertIsNone(result.exception, result.output)
self.assertTrue(self.code.full_label not in result.output, 'code should be hidden')
options = ['-a']
result = self.cli_runner.invoke(code_list, options)
self.assertIsNone(result.exception, result.output)
self.assertTrue(self.code.full_label in result.output, 'code should be shown')
def test_code_show(self):
result = self.cli_runner.invoke(show, [str(self.code.pk)])
self.assertIsNone(result.exception, result.output)
self.assertTrue(str(self.code.pk) in result.output)
def test_code_duplicate_interactive(self):
"""Test code duplication interacgtive."""
os.environ['VISUAL'] = 'sleep 1; vim -cwq'
os.environ['EDITOR'] = 'sleep 1; vim -cwq'
label = 'code_duplicate_interactive'
user_input = label + '\n\n\n\n\n\n'
result = self.cli_runner.invoke(code_duplicate, [str(self.code.pk)], input=user_input, catch_exceptions=False)
self.assertIsNone(result.exception, result.output)
from aiida.orm import Code
new_code = Code.get_from_string(label)
self.assertEqual(self.code.description, new_code.description)
self.assertEqual(self.code.get_prepend_text(), new_code.get_prepend_text())
self.assertEqual(self.code.get_append_text(), new_code.get_append_text())
def test_code_duplicate_non_interactive(self):
"""Test code duplication non-interacgtive."""
label = 'code_duplicate_noninteractive'
result = self.cli_runner.invoke(code_duplicate, ['--non-interactive', '--label=' + label, str(self.code.pk)])
self.assertIsNone(result.exception, result.output)
from aiida.orm import Code
new_code = Code.get_from_string(label)
self.assertEqual(self.code.description, new_code.description)
self.assertEqual(self.code.get_prepend_text(), new_code.get_prepend_text())
self.assertEqual(self.code.get_append_text(), new_code.get_append_text())
self.assertEqual(self.code.get_input_plugin_name(), new_code.get_input_plugin_name())
class TestVerdiCodeNoCodes(AiidaTestCase):
"""Test functionality when no codes been set up."""
def setUp(self):
self.cli_runner = CliRunner()
def test_code_list_no_codes_error_message(self):
result = self.cli_runner.invoke(code_list)
self.assertEqual(1, result.output.count('# No codes found matching the specified criteria.'))