Skip to content

Commit d07561f

Browse files
author
Alessio Fabiani
authored
Merge pull request #20 from gioscarda/fix_inputs_tests
[Fixes #10] Add missing tests about input parameters
2 parents beac9c0 + cc885f2 commit d07561f

File tree

3 files changed

+160
-6
lines changed

3 files changed

+160
-6
lines changed

.gitignore

+8
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ RemoteWPS1.sln
88
/Include
99
/Lib/
1010
/Scripts/
11+
.eggs/
1112

1213
/RemoteWPS.pyproj
1314
/RemoteWPS.sln
@@ -18,3 +19,10 @@ RemoteWPS1.sln
1819
/json_out.json
1920
/json_out_1.json
2021
/json_out_2.json
22+
/copy_of_source_json.json
23+
24+
# Testing output
25+
.coverage
26+
27+
# IDE
28+
.idea

requirements.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ certifi==2019.9.11
77
cffi==1.11.5
88
chardet==3.0.4
99
colorama==0.3.6
10+
coverage==4.5.4
1011
cryptography==2.3.1
1112
docutils==0.15.2
1213
enum34==1.1.6
@@ -19,7 +20,6 @@ lazy-object-proxy==1.2.1
1920
mccabe==0.4.0
2021
paramiko==2.4.1
2122
pep8==1.7.1
22-
pkg-resources==0.0.0
2323
pkginfo==1.5.0.1
2424
psutil==5.4.7
2525
pyasn1==0.4.4

test/test_computation_job_inputs.py

+151-5
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from wpsremote import computational_job_input_action_cmd_param
1616
from wpsremote import computational_job_input_action_create_json_file
1717
from wpsremote import computational_job_input_action_update_json_file
18+
from wpsremote import computational_job_input_action_copyfile
1819
from wpsremote import mockutils
1920

2021
__author__ = "Alessio Fabiani"
@@ -60,15 +61,46 @@ def test_two_params(self):
6061
self.assertEquals(p1.get_value_string(), "1")
6162
self.assertEquals(p2.get_value_string(), "abc")
6263

64+
def test_set_float_param(self):
65+
p1 = computation_job_param.ComputationJobParam("mypar", "float", "par title", "par descr")
66+
p1.set_value("1.37")
67+
self.assertEquals(p1.get_value_string(), "1.37")
68+
69+
def test_set_wrong_float_param(self):
70+
p1 = computation_job_param.ComputationJobParam("mypar", "float", "par title", "par descr")
71+
self.assertRaises(TypeError, lambda: p1.set_value("not float"))
72+
73+
def test_set_url_param(self):
74+
p1 = computation_job_param.ComputationJobParam("mypar", "url", "par title", "par descr")
75+
p1.set_value("https://www.google.com/")
76+
self.assertEquals(p1.get_value_string(), "https://www.google.com/")
77+
78+
def test_set_wrong_url_param(self):
79+
p1 = computation_job_param.ComputationJobParam("mypar", "url", "par title", "par descr")
80+
self.assertRaises(Exception, lambda: p1.set_value("https://www.google.com/%"))
81+
82+
def test_set_datetime_param(self):
83+
p1 = computation_job_param.ComputationJobParam(
84+
"mypar", "datetime", "par title", "par descr",
85+
formatter="%Y-%m-%d"
86+
)
87+
p1.set_value("2019-10-21")
88+
self.assertEquals(p1.get_value_string(), "2019-10-21")
89+
90+
def test_set_wrong_datetime_param(self):
91+
p1 = computation_job_param.ComputationJobParam(
92+
"mypar", "datetime", "par title", "par descr"
93+
)
94+
self.assertIsNone(p1.get_value_string())
95+
6396
def test_cmd_line_action(self):
6497
inputs = computation_job_inputs.ComputationJobInputs()
6598
inputs.add_input(computation_job_param.ComputationJobParam("mypar1", "int", "par 1", "par descr 1"))
6699

67100
actions = computational_job_input_actions.ComputationalJobInputActions()
68101
actions.add_actions(
69-
computational_job_input_action_cmd_param.ComputationalJobInputActionCmdParam(
70-
"mypar1", "--name=value"))
71-
102+
computational_job_input_action_cmd_param.ComputationalJobInputActionCmdParam(
103+
"mypar1", "--name=value"))
72104
inputs.set_values({"mypar1": "1"})
73105
actions.execute(inputs)
74106

@@ -194,6 +226,22 @@ class = param
194226

195227
self.assertEquals(actions.get_cmd_line(), "--mypar1=1")
196228

229+
def test_read_param_from_dict(self):
230+
input_dict = {
231+
"mypar1": {
232+
"class": "param",
233+
"type": "int",
234+
"description": "mypar descr"
235+
}
236+
}
237+
inputs = computation_job_inputs.ComputationJobInputs.create_from_dict(input_dict)
238+
actions = computational_job_input_action_cmd_param.ComputationalJobInputActionCmdParam(
239+
"mypar1", "--name=value"
240+
)
241+
inputs.set_values({"mypar1": "1"})
242+
actions.set_inputs(inputs)
243+
self.assertEquals(actions.get_cmd_line(), "--mypar1=1")
244+
197245
def test_read_action_from_ini(self):
198246
ini_text = '''[Action1]
199247
input_ref = mypar1
@@ -218,6 +266,24 @@ class = cmdline
218266

219267
self.assertEquals(actions.get_cmd_line(), "--mypar1=1")
220268

269+
def test_read_action_from_dict(self):
270+
input_dict = {
271+
"Action1": {
272+
"input_ref": "mypar1",
273+
"class": "cmdline",
274+
"template": "--name=value"
275+
}
276+
}
277+
inputs = computation_job_inputs.ComputationJobInputs()
278+
inputs.add_input(
279+
computation_job_param.ComputationJobParam("mypar1", "int", "par 1", "par descr")
280+
)
281+
actions = computational_job_input_actions. \
282+
ComputationalJobInputActions.create_from_dict(input_dict)
283+
inputs.set_values({"mypar1": "1"})
284+
actions.execute(inputs)
285+
self.assertEquals(actions.get_cmd_line(), "--mypar1=1")
286+
221287
def test_2_cmdpar_from_ini(self):
222288

223289
ini_text = '''[Input1]
@@ -258,6 +324,39 @@ class = cmdline
258324

259325
self.assertEquals(actions.get_cmd_line(), '-w . oaaOnDemand')
260326

327+
def test_2_cmdpar_from_dict(self):
328+
inputs_dict = {
329+
"workingdir": {
330+
"class": "param",
331+
"type": "string",
332+
"description": "OAA process working directory"
333+
},
334+
"etlExecutionType": {
335+
"class": "const",
336+
"type": "string",
337+
"description": "parameter to choose what type of execution is performed by etl.py script",
338+
"value": "oaaOnDemand"
339+
}
340+
}
341+
actions_dict = {
342+
"Action1": {
343+
"input_ref": "workingdir",
344+
"class": "cmdline",
345+
"alias": "w",
346+
"template": "-name value"
347+
},
348+
"Action2": {
349+
"input_ref": "etlExecutionType",
350+
"class": "cmdline",
351+
"template": "value",
352+
}
353+
}
354+
inputs = computation_job_inputs.ComputationJobInputs.create_from_dict(inputs_dict)
355+
actions = computational_job_input_actions.ComputationalJobInputActions.create_from_dict(actions_dict)
356+
inputs.set_values({'workingdir': '.'})
357+
actions.execute(inputs)
358+
self.assertEquals(actions.get_cmd_line(), '-w . oaaOnDemand')
359+
261360
def test_cmdpar_order(self):
262361

263362
ini_text = '''[Input2]
@@ -280,8 +379,7 @@ class = param
280379
class = cmdline
281380
input_ref = workingdir
282381
alias = w
283-
template = -name value
284-
'''
382+
template = -name value'''
285383

286384
config = ConfigParser.ConfigParser()
287385
import StringIO
@@ -300,6 +398,39 @@ class = cmdline
300398

301399
self.assertEquals(actions.get_cmd_line(), '-w . --k=2.4')
302400

401+
def test_cmdpar_order_from_dict(self):
402+
inputs_dict = {
403+
"coeff": {
404+
"class": "param",
405+
"type": "float",
406+
"description": "this is a coeff"
407+
},
408+
"workingdir": {
409+
"class": "param",
410+
"type": "string",
411+
"description": "process working directory"
412+
}
413+
}
414+
actions_dict = {
415+
"Action1": {
416+
"input_ref": "workingdir",
417+
"class": "cmdline",
418+
"alias": "w",
419+
"template": "-name value"
420+
},
421+
"Action2": {
422+
"input_ref": "coeff",
423+
"class": "cmdline",
424+
"alias": "k",
425+
"template": "--name=value",
426+
}
427+
}
428+
inputs = computation_job_inputs.ComputationJobInputs.create_from_dict(inputs_dict)
429+
actions = computational_job_input_actions.ComputationalJobInputActions.create_from_dict(actions_dict)
430+
inputs.set_values({'workingdir': '.', 'coeff': 2.4})
431+
actions.execute(inputs)
432+
self.assertEquals(actions.get_cmd_line(), '-w . --k=2.4')
433+
303434
def test_update_json_list_in_file(self):
304435
target_json_file = path.path("./json_out.json")
305436
if target_json_file.exists():
@@ -346,6 +477,21 @@ def test_update_json_list_in_file_two_times(self):
346477
self.assertTrue(75.5, j['Config']['latLim'][0])
347478
self.assertTrue(76.5, j['Config']['latLim'][1])
348479

480+
def test_copyfile(self):
481+
target_json_file = path.path("./copy_of_source_json.json")
482+
if target_json_file.exists():
483+
target_json_file.remove()
484+
source_template_json_file = path.path(r"./src/wpsremote/xmpp_data/test/CMREOAA_MainConfigFile_template.json")
485+
action = computational_job_input_action_copyfile.ComputationalJobInputActionCopyFile(
486+
source=source_template_json_file, target=target_json_file
487+
)
488+
action.set_inputs("test")
489+
self.assertTrue(target_json_file.exists())
490+
target_json_text = target_json_file.text()
491+
source_json_text = source_template_json_file.text()
492+
self.assertEqual(target_json_text, source_json_text)
493+
target_json_file.remove()
494+
349495
json_text1 = '''{
350496
"Asset": {
351497
"Pfa": 0.001,

0 commit comments

Comments
 (0)