diff --git a/.gitignore b/.gitignore index 54f752e..c11410e 100644 --- a/.gitignore +++ b/.gitignore @@ -8,6 +8,7 @@ RemoteWPS1.sln /Include /Lib/ /Scripts/ +.eggs/ /RemoteWPS.pyproj /RemoteWPS.sln @@ -18,3 +19,10 @@ RemoteWPS1.sln /json_out.json /json_out_1.json /json_out_2.json +/copy_of_source_json.json + +# Testing output +.coverage + +# IDE +.idea \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index 8a91465..694975d 100644 --- a/requirements.txt +++ b/requirements.txt @@ -7,6 +7,7 @@ certifi==2019.9.11 cffi==1.11.5 chardet==3.0.4 colorama==0.3.6 +coverage==4.5.4 cryptography==2.3.1 docutils==0.15.2 enum34==1.1.6 @@ -19,7 +20,6 @@ lazy-object-proxy==1.2.1 mccabe==0.4.0 paramiko==2.4.1 pep8==1.7.1 -pkg-resources==0.0.0 pkginfo==1.5.0.1 psutil==5.4.7 pyasn1==0.4.4 diff --git a/test/test_computation_job_inputs.py b/test/test_computation_job_inputs.py index 61f06a3..8e53d3d 100644 --- a/test/test_computation_job_inputs.py +++ b/test/test_computation_job_inputs.py @@ -15,6 +15,7 @@ from wpsremote import computational_job_input_action_cmd_param from wpsremote import computational_job_input_action_create_json_file from wpsremote import computational_job_input_action_update_json_file +from wpsremote import computational_job_input_action_copyfile from wpsremote import mockutils __author__ = "Alessio Fabiani" @@ -60,15 +61,46 @@ def test_two_params(self): self.assertEquals(p1.get_value_string(), "1") self.assertEquals(p2.get_value_string(), "abc") + def test_set_float_param(self): + p1 = computation_job_param.ComputationJobParam("mypar", "float", "par title", "par descr") + p1.set_value("1.37") + self.assertEquals(p1.get_value_string(), "1.37") + + def test_set_wrong_float_param(self): + p1 = computation_job_param.ComputationJobParam("mypar", "float", "par title", "par descr") + self.assertRaises(TypeError, lambda: p1.set_value("not float")) + + def test_set_url_param(self): + p1 = computation_job_param.ComputationJobParam("mypar", "url", "par title", "par descr") + p1.set_value("https://www.google.com/") + self.assertEquals(p1.get_value_string(), "https://www.google.com/") + + def test_set_wrong_url_param(self): + p1 = computation_job_param.ComputationJobParam("mypar", "url", "par title", "par descr") + self.assertRaises(Exception, lambda: p1.set_value("https://www.google.com/%")) + + def test_set_datetime_param(self): + p1 = computation_job_param.ComputationJobParam( + "mypar", "datetime", "par title", "par descr", + formatter="%Y-%m-%d" + ) + p1.set_value("2019-10-21") + self.assertEquals(p1.get_value_string(), "2019-10-21") + + def test_set_wrong_datetime_param(self): + p1 = computation_job_param.ComputationJobParam( + "mypar", "datetime", "par title", "par descr" + ) + self.assertIsNone(p1.get_value_string()) + def test_cmd_line_action(self): inputs = computation_job_inputs.ComputationJobInputs() inputs.add_input(computation_job_param.ComputationJobParam("mypar1", "int", "par 1", "par descr 1")) actions = computational_job_input_actions.ComputationalJobInputActions() actions.add_actions( - computational_job_input_action_cmd_param.ComputationalJobInputActionCmdParam( - "mypar1", "--name=value")) - + computational_job_input_action_cmd_param.ComputationalJobInputActionCmdParam( + "mypar1", "--name=value")) inputs.set_values({"mypar1": "1"}) actions.execute(inputs) @@ -194,6 +226,22 @@ class = param self.assertEquals(actions.get_cmd_line(), "--mypar1=1") + def test_read_param_from_dict(self): + input_dict = { + "mypar1": { + "class": "param", + "type": "int", + "description": "mypar descr" + } + } + inputs = computation_job_inputs.ComputationJobInputs.create_from_dict(input_dict) + actions = computational_job_input_action_cmd_param.ComputationalJobInputActionCmdParam( + "mypar1", "--name=value" + ) + inputs.set_values({"mypar1": "1"}) + actions.set_inputs(inputs) + self.assertEquals(actions.get_cmd_line(), "--mypar1=1") + def test_read_action_from_ini(self): ini_text = '''[Action1] input_ref = mypar1 @@ -218,6 +266,24 @@ class = cmdline self.assertEquals(actions.get_cmd_line(), "--mypar1=1") + def test_read_action_from_dict(self): + input_dict = { + "Action1": { + "input_ref": "mypar1", + "class": "cmdline", + "template": "--name=value" + } + } + inputs = computation_job_inputs.ComputationJobInputs() + inputs.add_input( + computation_job_param.ComputationJobParam("mypar1", "int", "par 1", "par descr") + ) + actions = computational_job_input_actions. \ + ComputationalJobInputActions.create_from_dict(input_dict) + inputs.set_values({"mypar1": "1"}) + actions.execute(inputs) + self.assertEquals(actions.get_cmd_line(), "--mypar1=1") + def test_2_cmdpar_from_ini(self): ini_text = '''[Input1] @@ -258,6 +324,39 @@ class = cmdline self.assertEquals(actions.get_cmd_line(), '-w . oaaOnDemand') + def test_2_cmdpar_from_dict(self): + inputs_dict = { + "workingdir": { + "class": "param", + "type": "string", + "description": "OAA process working directory" + }, + "etlExecutionType": { + "class": "const", + "type": "string", + "description": "parameter to choose what type of execution is performed by etl.py script", + "value": "oaaOnDemand" + } + } + actions_dict = { + "Action1": { + "input_ref": "workingdir", + "class": "cmdline", + "alias": "w", + "template": "-name value" + }, + "Action2": { + "input_ref": "etlExecutionType", + "class": "cmdline", + "template": "value", + } + } + inputs = computation_job_inputs.ComputationJobInputs.create_from_dict(inputs_dict) + actions = computational_job_input_actions.ComputationalJobInputActions.create_from_dict(actions_dict) + inputs.set_values({'workingdir': '.'}) + actions.execute(inputs) + self.assertEquals(actions.get_cmd_line(), '-w . oaaOnDemand') + def test_cmdpar_order(self): ini_text = '''[Input2] @@ -280,8 +379,7 @@ class = param class = cmdline input_ref = workingdir alias = w -template = -name value -''' +template = -name value''' config = ConfigParser.ConfigParser() import StringIO @@ -300,6 +398,39 @@ class = cmdline self.assertEquals(actions.get_cmd_line(), '-w . --k=2.4') + def test_cmdpar_order_from_dict(self): + inputs_dict = { + "coeff": { + "class": "param", + "type": "float", + "description": "this is a coeff" + }, + "workingdir": { + "class": "param", + "type": "string", + "description": "process working directory" + } + } + actions_dict = { + "Action1": { + "input_ref": "workingdir", + "class": "cmdline", + "alias": "w", + "template": "-name value" + }, + "Action2": { + "input_ref": "coeff", + "class": "cmdline", + "alias": "k", + "template": "--name=value", + } + } + inputs = computation_job_inputs.ComputationJobInputs.create_from_dict(inputs_dict) + actions = computational_job_input_actions.ComputationalJobInputActions.create_from_dict(actions_dict) + inputs.set_values({'workingdir': '.', 'coeff': 2.4}) + actions.execute(inputs) + self.assertEquals(actions.get_cmd_line(), '-w . --k=2.4') + def test_update_json_list_in_file(self): target_json_file = path.path("./json_out.json") if target_json_file.exists(): @@ -346,6 +477,21 @@ def test_update_json_list_in_file_two_times(self): self.assertTrue(75.5, j['Config']['latLim'][0]) self.assertTrue(76.5, j['Config']['latLim'][1]) + def test_copyfile(self): + target_json_file = path.path("./copy_of_source_json.json") + if target_json_file.exists(): + target_json_file.remove() + source_template_json_file = path.path(r"./src/wpsremote/xmpp_data/test/CMREOAA_MainConfigFile_template.json") + action = computational_job_input_action_copyfile.ComputationalJobInputActionCopyFile( + source=source_template_json_file, target=target_json_file + ) + action.set_inputs("test") + self.assertTrue(target_json_file.exists()) + target_json_text = target_json_file.text() + source_json_text = source_template_json_file.text() + self.assertEqual(target_json_text, source_json_text) + target_json_file.remove() + json_text1 = '''{ "Asset": { "Pfa": 0.001,