def test_list_attr_inputs(self, mock_get_operation_instance_data): ''' Test the case where inputs are of a list type (e.g. a list of strings) Check that it all validates as expected ''' # first test one where we expect an empty list-- no resources # are used or created: f = os.path.join( TESTDIR, 'valid_op_with_list_inputs.json' ) d = read_operation_json(f) mock_get_operation_instance_data.return_value = d l1 = ['https://foo.com/bar', 'https://foo.com/baz'] l2 = ['abc', 'def'] inputs = { 'link_list': l1, 'regular_string_list': l2 } ops = OperationDbModel.objects.all() op = ops[0] result = validate_operation_inputs(self.regular_user_1, inputs, op, None) self.assertIsNone(result['optional_input']) self.assertCountEqual(result['link_list'].get_value(), l1) self.assertCountEqual(result['regular_string_list'].get_value(), l2)
def test_optional_without_default_becomes_none(self, mock_get_operation_instance_data): ''' Generally, Operations with optional inputs should have defaults. However, if that is violated, the "input" should be assigned to be None ''' f = os.path.join( TESTDIR, 'optional_without_default.json' ) d = read_operation_json(f) mock_get_operation_instance_data.return_value = d # the only input is optional, so this is technically fine. sample_inputs = {} #with self.assertRaises(ValidationError): final_inputs = validate_operation_inputs(self.regular_user_1, sample_inputs, self.db_op, self.workspace) self.assertIsNone(final_inputs['optional_int_type'])
def test_optional_value_overridden(self, mock_get_operation_instance_data): ''' Test that the optional parameter is overridden when given ''' f = os.path.join( TESTDIR, 'required_without_default.json' ) d = read_operation_json(f) mock_get_operation_instance_data.return_value = d sample_inputs = { 'required_int_type': 22, 'optional_int_type': 33 } final_inputs = validate_operation_inputs(self.regular_user_1, sample_inputs, self.db_op, self.workspace) self.assertEqual(final_inputs['required_int_type'].submitted_value, 22) self.assertEqual(final_inputs['optional_int_type'].submitted_value, 33)
def test_optional_boolean_value_filled_by_default(self, mock_get_operation_instance_data): ''' Test that a missing optional boolean parameter gets the default value ''' f = os.path.join( TESTDIR, 'valid_op_with_default_bool.json' ) d = read_operation_json(f) mock_get_operation_instance_data.return_value = d # one input was optional, one required. An empty payload # qualifies as a problem since it's missing the required key sample_inputs = {} final_inputs = validate_operation_inputs(self.regular_user_1, sample_inputs, self.db_op, self.workspace) self.assertEqual(final_inputs['some_boolean'].submitted_value, False) expected_default = d['inputs']['some_boolean']['spec']['default'] self.assertEqual( final_inputs['some_boolean'].submitted_value, expected_default)