def test_get_default_value_for_key_raises_key_error_when_rule_not_defined():
    # Arrange
    validation_dict = {
        'x': ValidationRule({'classification': 'Required', 'default': '7'}),
        'y': ValidationRule({'classification': 'Optional', 'default': 'banana'})
    }
    test_ruleset = ValidationRuleset(validation_dict)
    # Act & Assert
    with pytest.raises(KeyError):
        test_ruleset.get_default_value_for_key('z')
def test_get_default_value_for_key_returns_value_when_defined():
    # Arrange
    validation_dict = {
        'x': ValidationRule({'classification': 'Required', 'default': '7'}),
        'y': ValidationRule({'classification': 'Optional'})
    }
    test_ruleset = ValidationRuleset(validation_dict)
    # Act
    result = test_ruleset.get_default_value_for_key('x')
    # Assert
    assert result == '7'
def test_get_keys_for_language_returns_empty_list_when_none_match():
    # Arrange
    validation_dict = {
        'x': ValidationRule({
            'classification': 'Conditional',
            'conditional_input_name': 'language',
            'conditional_input_value': 'Other'
        }),
        'z': ValidationRule({'classification': 'Required', 'default': '7'})
    }
    test_ruleset = ValidationRuleset(validation_dict)
    # Act
    result = test_ruleset.get_keys_for_language('Test')
    # Assert
    assert result == []
def test_get_keys_of_optional_inputs_returns_empty_list_when_none_optional():
    # Arrange
    validation_dict = {
        'x': ValidationRule({
            'classification': 'Conditional',
            'conditional_input_name': 'language',
            'conditional_input_value': 'Other'
        }),
        'y': ValidationRule({'classification': 'Fixed', 'default': 'banana'}),
        'z': ValidationRule({'classification': 'Required', 'default': '7'})
    }
    test_ruleset = ValidationRuleset(validation_dict)
    # Act
    result = test_ruleset.get_keys_of_optional_inputs()
    # Assert
    assert result == []
    def test_validate_request_keys_alerts_absence_of_language_specific_input(
            self):
        # Arrange
        self.mock_validation_ruleset.get_keys_of_required_inputs.return_value = [
            'name', 'language'
        ]
        self.mock_validation_ruleset.get_keys_for_language.return_value = [
            'example_key'
        ]
        request_body = {
            'name': 'example-spark-job',
            'language': 'example-language'
        }
        self.mock_validation_ruleset.get_validation_rule_for_key.return_value = \
            ValidationRule({"classification": "Required", "options": ['example-language']})

        # Act
        result = self.test_service.validate_request_keys(request_body)
        # Assert
        self.mock_validation_ruleset.get_keys_of_required_inputs.assert_called_once(
        )
        self.mock_validation_ruleset.get_keys_for_language.assert_called_once_with(
            'example-language')
        assert result.is_valid is False
        assert result.message == 'The following errors were found:\nMissing required input "example_key"\n'
Exemple #6
0
def test_validate_label_accepts_valid_labels(label):
    # Arrange
    validation_rule = ValidationRule({'classification': 'Required'})
    # Act
    validation_result = argument_validator.validate("label", label,
                                                    validation_rule)
    assert validation_result.is_valid is True
 def test_validate_request_keys_confirms_presence_of_language_specific_input(
         self):
     # Arrange
     self.mock_validation_ruleset.get_keys_of_required_inputs.return_value = [
         'name', 'language'
     ]
     self.mock_validation_ruleset.get_keys_for_language.return_value = [
         'example_key'
     ]
     request_body = {
         'name': 'example-spark-job',
         'language': 'example-language',
         'example_key': 'example-value'
     }
     self.mock_validation_ruleset.get_validation_rule_for_key.return_value = \
         ValidationRule({"classification": "Required", "options": ['example-language']})
     # Act
     result = self.test_service.validate_request_keys(request_body)
     # Assert
     self.mock_validation_ruleset.get_keys_of_required_inputs.assert_called_once(
     )
     self.mock_validation_ruleset.get_keys_for_language.assert_called_once_with(
         'example-language')
     self.mock_validation_ruleset.get_keys_of_optional_inputs.assert_called_once(
     )
     assert result.is_valid is True
     assert result.message == 'All input keys provided are valid\n'
def test_get_keys_of_required_inputs_returns_required_names_from_mixture():
    # Arrange
    validation_dict = {
        'w': ValidationRule({
            'classification': 'Conditional',
            'conditional_input_name': 'language',
            'conditional_input_value': 'Other'
        }),
        'x': ValidationRule({'classification': 'Fixed', 'default': 'banana'}),
        'y': ValidationRule({'classification': 'Required', 'default': '7'}),
        'z': ValidationRule({'classification': 'Optional', 'default': '1.2.3'})
    }
    test_ruleset = ValidationRuleset(validation_dict)
    # Act
    result = test_ruleset.get_keys_of_required_inputs()
    # Assert
    assert result == ['y']
Exemple #9
0
def test_validate_main_class_rejects_empty_strings_and_non_strings(main_class):
    # Arrange
    validation_rule = ValidationRule({'classification': 'Required'})
    # Act
    validation_result = argument_validator.validate("main_class", main_class,
                                                    validation_rule)
    # Assert
    assert validation_result.is_valid is False
Exemple #10
0
def test_validate_label_rejects_empty_strings_and_non_strings(label):
    # Arrange
    validation_rule = ValidationRule({'classification': 'Optional'})
    # Act
    validation_result = argument_validator.validate("label", label,
                                                    validation_rule)
    # Assert
    assert validation_result.is_valid is False
Exemple #11
0
def test_validate_name_validates_well_formatted_strings(name):
    # Arrange
    validation_rule = ValidationRule({'classification': 'Required'})
    # Act
    validation_result = argument_validator.validate("name", name,
                                                    validation_rule)
    # Assert
    assert validation_result.is_valid is True
Exemple #12
0
def test_validate_path_to_main_app_file_validates_non_empty_strings(path):
    # Arrange
    validation_rule = ValidationRule({'classification': 'Required'})
    # Act
    validation_result = argument_validator.validate("path_to_main_app_file",
                                                    path, validation_rule)
    # Assert
    assert validation_result.is_valid is True
Exemple #13
0
def test_validate_label_rejects_invalid_non_empty_labels(label):
    # Arrange
    validation_rule = ValidationRule({'classification': 'Required'})
    # Act
    validation_result = argument_validator.validate("label", label,
                                                    validation_rule)
    assert validation_result.is_valid is False
    assert validation_result.message == '"label" input must obey naming convention: ' \
                                        'see https://github.com/ukaea/piezo/wiki/WebAppUserGuide#submit-a-job'
Exemple #14
0
def test_validate_label_rejects_empty_strings(label):
    # Arrange
    validation_rule = ValidationRule({'classification': 'Required'})
    # Act
    validation_result = argument_validator.validate("label", label,
                                                    validation_rule)
    # Assert
    assert validation_result.is_valid is False
    assert validation_result.message == '"label" input cannot be empty'
Exemple #15
0
def test_validate_label_validates_label_with_63_characters():
    # Arrange
    validation_rule = ValidationRule({'classification': 'Required'})
    label = "abcdefghijklmnopqrstuvwxyabcdefghijklmnopqrstuvwxyabcdefghijklm"
    assert len(label) == 63
    # Act
    validation_result = argument_validator.validate("label", label,
                                                    validation_rule)
    assert validation_result.is_valid is True
Exemple #16
0
def test_validate_name_rejects_non_string_values(name):
    # Arrange
    validation_rule = ValidationRule({'classification': 'Required'})
    # Act
    validation_result = argument_validator.validate("name", name,
                                                    validation_rule)
    # Assert
    assert validation_result.is_valid is False
    assert validation_result.message == '"name" input must be a string'
Exemple #17
0
def test_validate_name_allows_29_character_name():
    # Arrange
    validation_rule = ValidationRule({'classification': 'Required'})
    name = 'abcdefghijklmnopqrstuvwxyzabc'
    assert len(name) == 29
    # Act
    validation_result = argument_validator.validate("name", name,
                                                    validation_rule)
    # Assert
    assert validation_result.is_valid is True
Exemple #18
0
def test_validate_name_rejects_poorly_formatted_strings(name):
    # Arrange
    validation_rule = ValidationRule({'classification': 'Required'})
    # Act
    validation_result = argument_validator.validate("name", name,
                                                    validation_rule)
    # Assert
    assert validation_result.is_valid is False
    assert validation_result.message == '"name" input must obey naming convention: '\
                                        'see https://github.com/ukaea/piezo/wiki/WebAppUserGuide#submit-a-job'
Exemple #19
0
def test_validate_label_rejects_label_with_64_characters():
    # Arrange
    validation_rule = ValidationRule({'classification': 'Required'})
    label = "abcdefghijklmnopqrstuvwxyabcdefghijklmnopqrstuvwxyabcdefghijklm" + "n"
    assert len(label) == 64
    # Act
    validation_result = argument_validator.validate("label", label,
                                                    validation_rule)
    assert validation_result.is_valid is False
    assert validation_result.message == '"label" input has a maximum length of 63 characters'
def test_get_keys_of_required_inputs_returns_single_required_rule():
    # Arrange
    validation_dict = {
        'x': ValidationRule({'classification': 'Required', 'default': '7'})
    }
    test_ruleset = ValidationRuleset(validation_dict)
    # Act
    result = test_ruleset.get_keys_of_required_inputs()
    # Assert
    assert result == ['x']
Exemple #21
0
def test_validate_language_rejects_non_valid_languages(language):
    # Arrange
    validation_rule = ValidationRule({
        'classification': 'Required',
        'options': ['Python', 'Scala']
    })
    # Act
    validation_result = argument_validator.validate("language", language,
                                                    validation_rule)
    # Assert
    assert validation_result.is_valid is False
Exemple #22
0
def test_validate_name_rejects_30_character_name():
    # Arrange
    validation_rule = ValidationRule({'classification': 'Required'})
    name = 'abcdefghijklmnopqrstuvwxyzabc' + 'd'
    assert len(name) == 30
    # Act
    validation_result = argument_validator.validate("name", name,
                                                    validation_rule)
    # Assert
    assert validation_result.is_valid is False
    assert validation_result.message == '"name" input must obey naming convention: ' \
                                        'see https://github.com/ukaea/piezo/wiki/WebAppUserGuide#submit-a-job'
Exemple #23
0
def test_validate_driver_memory_accepts_values_for_megabytes_as_string(memory):
    # Arrange
    validation_rule = ValidationRule({
        'classification': 'Optional',
        'default': '512m',
        'minimum': 512,
        'maximum': 2048
    })
    # Act
    validation_result = argument_validator.validate("driver_memory", memory,
                                                    validation_rule)
    # Assert
    assert validation_result.is_valid is True
Exemple #24
0
def test_validate_python_version_validates_string_2_or_3(version):
    # Arrange
    validation_rule = ValidationRule({
        'classification': 'Conditional',
        'options': ['2', '3'],
        'conditional_input_name': 'language',
        'conditional_input_value': 'Python'
    })
    # Act
    validation_result = argument_validator.validate("python_version", version,
                                                    validation_rule)
    # Assert
    assert validation_result.is_valid is True
Exemple #25
0
def test_validate_driver_cores_accepts_values_within_valid_range(cores):
    # Arrange
    validation_rule = ValidationRule({
        'classification': 'Optional',
        'default': 0.1,
        'minimum': 0.1,
        'maximum': 1
    })
    # Act
    validation_result = argument_validator.validate("driver_cores", cores,
                                                    validation_rule)
    # Assert
    assert validation_result.is_valid is True
Exemple #26
0
def test_validate_executor_memory_rejects_values_outside_valid_range(memory):
    # Arrange
    validation_rule = ValidationRule({
        'classification': 'Optional',
        'default': '512m',
        'minimum': 512,
        'maximum': 4096
    })
    # Act
    validation_result = argument_validator.validate("executor_memory", memory,
                                                    validation_rule)
    # Assert
    assert validation_result.is_valid is False
    assert validation_result.message == '"executor_memory" input must be in range [512m, 4096m]'
Exemple #27
0
def test_validate_executors_rejects_values_outside_valid_range(executors):
    # Arrange
    validation_rule = ValidationRule({
        'classification': 'Optional',
        'default': 1,
        'minimum': 1,
        'maximum': 10
    })
    # Act
    validation_result = argument_validator.validate("executors", executors,
                                                    validation_rule)
    # Assert
    assert validation_result.is_valid is False
    assert validation_result.message == '"executors" input must be in range [1, 10]'
Exemple #28
0
def test_validate_executors_rejects_badly_formatted_inputs(executors):
    # Arrange
    validation_rule = ValidationRule({
        'classification': 'Optional',
        'default': 1,
        'minimum': 1,
        'maximum': 10
    })
    # Act
    validation_result = argument_validator.validate("executors", executors,
                                                    validation_rule)
    # Assert
    assert validation_result.is_valid is False
    assert validation_result.message == '"executors" input must be an integer'
Exemple #29
0
def test_validate_executors_accepts_numerical_values_within_valid_range(
        executors):
    # Arrange
    validation_rule = ValidationRule({
        'classification': 'Optional',
        'default': 1,
        'minimum': 1,
        'maximum': 10
    })
    # Act
    validation_result = argument_validator.validate("executors", executors,
                                                    validation_rule)
    # Assert
    assert validation_result.is_valid is True
Exemple #30
0
def test_validate_driver_cores_rejects_values_outside_valid_range(cores):
    # Arrange
    validation_rule = ValidationRule({
        'classification': 'Optional',
        'default': 0.1,
        'minimum': 0.1,
        'maximum': 1
    })
    # Act
    validation_result = argument_validator.validate("driver_cores", cores,
                                                    validation_rule)
    # Assert
    assert validation_result.is_valid is False
    assert validation_result.message == '"driver_cores" input must be in range [0.1, 1]'