Example #1
0
    def setUp(self):
        """
        Adds the created instances to the tests' contexts.
        For more information see unittest's :meth:`~unittest.TestCase.setUp` method.

        """

        self.string_input_definition = StringInputDefinitionFactory()
 def test_fix_output_path_with_non_absolute_path_value(self):
     definition = StringInputDefinitionFactory(is_output_path=True,
                                               max_length=500)
     string_input = StringInputFactory(definition=definition,
                                       value="some/relative/file/path")
     expected = str(string_input.default_output_directory /
                    string_input.value)
     self.assertEqual(string_input.value, expected)
 def test_fix_output_path_with_empty_value_and_no_definition_default(self):
     definition = StringInputDefinitionFactory(is_output_path=True,
                                               max_length=500,
                                               default="")
     string_input = StringInputFactory(definition=definition, value="")
     expected = str(string_input.default_output_directory /
                    string_input.definition.key)
     self.assertEqual(string_input.value, expected)
 def test_invalid_choice_raises_validation_error(self):
     choices = ["AAA", "BBB", "CCC"]
     definition = StringInputDefinitionFactory(min_length=3,
                                               max_length=3,
                                               choices=choices,
                                               default="AAA")
     with self.assertRaises(ValidationError):
         StringInputFactory(definition=definition, value="DDD")
    def base_input_definitions(self, create, extracted, **kwargs):
        if create:
            if extracted:
                # A list of base_input_definitions was passed in, use it
                for base_input_definition in extracted:
                    self.base_input_definitions.add(base_input_definition)
            else:
                self.base_input_definitions.add(
                    BooleanInputDefinitionFactory())
                self.base_input_definitions.add(FileInputDefinitionFactory())
                self.base_input_definitions.add(FloatInputDefinitionFactory())
                self.base_input_definitions.add(
                    IntegerInputDefinitionFactory())
                self.base_input_definitions.add(ListInputDefinitionFactory())
                self.base_input_definitions.add(StringInputDefinitionFactory())

            # Validate that at least one definition is required
            if not self.base_input_definitions.filter(required=True):
                definition = self.base_input_definitions.first()
                definition.required = True
                definition.save()
Example #6
0
class StringInputDefinitionTestCase(TestCase):
    """
    Tests for the
    :class:`~django_analyses.models.input.definitions.string_input_definition.StringInputDefinition`
    model.
    
    """

    def setUp(self):
        """
        Adds the created instances to the tests' contexts.
        For more information see unittest's :meth:`~unittest.TestCase.setUp` method.

        """

        self.string_input_definition = StringInputDefinitionFactory()

    ##########
    #  Meta  #
    ##########

    def test_input_class_attribute(self):
        """
        Test the `input_class` attribute is set to
        :class:`~django_analyses.models.input.types.string_input.StringInput`

        """

        self.assertEqual(StringInputDefinition.input_class, StringInput)

    ##########
    # Fields #
    ##########

    def test_default_blank_and_null(self):
        """
        Tests that the *default* field may be blank or null.

        """

        field = self.string_input_definition._meta.get_field("default")
        self.assertTrue(field.blank)
        self.assertTrue(field.null)

    def test_default_max_length(self):
        """
        Tests the *default* field's max_length value.

        """

        field = self.string_input_definition._meta.get_field("default")
        self.assertEqual(field.max_length, 500)

    def test_min_length_blank_and_null(self):
        """
        Tests that the *min_length* field may be blank or null.

        """

        field = self.string_input_definition._meta.get_field("min_length")
        self.assertTrue(field.blank)
        self.assertTrue(field.null)

    def test_max_length_blank_and_null(self):
        """
        Tests that the *max_length* field may be blank or null.

        """

        field = self.string_input_definition._meta.get_field("max_length")
        self.assertTrue(field.blank)
        self.assertTrue(field.null)

    def test_choices_blank_and_null(self):
        """
        Tests that the *choices* field may be blank or null.

        """

        field = self.string_input_definition._meta.get_field("choices")
        self.assertTrue(field.blank)
        self.assertTrue(field.null)

    def test_is_output_path_blank_and_null(self):
        """
        Tests that the *is_output_path* field may be blank or null.

        """

        field = self.string_input_definition._meta.get_field("is_output_path")
        self.assertFalse(field.blank)
        self.assertFalse(field.null)

    def test_is_output_path_default_value(self):
        """
        Tests the *is_output_path* field's default value.

        """

        field = self.string_input_definition._meta.get_field("is_output_path")
        self.assertFalse(field.default)

    ###########
    # Methods #
    ###########

    def test_get_type(self):
        """
        Tests the
        :meth:`~django_analyses.models.input.definitions.string_input_definition.StringInputDefinition.get_type`
        method returns the expected value.

        """

        value = self.string_input_definition.get_type()
        self.assertEqual(value, InputDefinitions.STR)

    def test_validation_without_default_and_without_choices(self):
        """
        Tests the
        :meth:`~django_analyses.models.input.definitions.string_input_definition.StringInputDefinition.validate`
        methods does not raise a ValidationError if the `choices` and
        `default` fields are null.

        """

        self.string_input_definition.default = None
        self.assertIsNone(self.string_input_definition.validate())

    def test_validation_with_default_and_without_choices(self):
        """
        Tests the
        :meth:`~django_analyses.models.input.definitions.string_input_definition.StringInputDefinition.validate`
        methods does not raise a ValidationError if the `choices` field
        is null when `default` is set.

        """

        self.assertIsNotNone(self.string_input_definition.default)
        self.assertIsNone(self.string_input_definition.validate())

    def test_validation_with_default_from_choices(self):
        """
        Tests the
        :meth:`~django_analyses.models.input.definitions.string_input_definition.StringInputDefinition.validate`
        methods does not raise a ValidationError if the `choices` field
        is set and contains the `default` value.

        """

        choices = ["a", "b", "c"]
        choices.append(self.string_input_definition.default)
        self.string_input_definition.choices = choices
        self.assertIsNone(self.string_input_definition.validate())

    def test_validation_with_default_not_from_choices(self):
        """
        Tests the
        :meth:`~django_analyses.models.input.definitions.string_input_definition.StringInputDefinition.validate`
        methods raises a ValidationError if the `choices` field is set
        and does not contains the `default` value.

        """

        choices = ["a", "b", "c"]
        self.string_input_definition.choices = choices
        with self.assertRaises(ValidationError):
            self.string_input_definition.validate()
 def test_fix_output_path_with_absolute_path_value(self):
     definition = StringInputDefinitionFactory(is_output_path=True,
                                               max_length=500)
     value = "/some/absolute/file/path"
     string_input = StringInputFactory(definition=definition, value=value)
     self.assertEqual(string_input.value, value)