def assert_required_fields(self, field_names, parent_getter,
                               field_parent_getter):
        """
        Helper function to assert required fields
        """
        for key in field_names:
            field = field_parent_getter(
                ProfileFilledOutSerializer().fields)[key]
            # skip fields that are generated, read only, or that tie to other serializers which are tested elsewhere
            if isinstance(
                    field,
                (ListSerializer, SerializerMethodField, ReadOnlyField)):
                continue
            elif field.read_only is True:
                continue

            clone = deepcopy(self.data)
            parent_getter(clone)[key] = None
            with self.assertRaises(ValidationError) as ex:
                ProfileFilledOutSerializer(data=clone).is_valid(
                    raise_exception=True)
            assert parent_getter(ex.exception.args[0]) == {
                key: ['This field may not be null.']
            }

            if isinstance(field, CharField):
                # test blank string too
                parent_getter(clone)[key] = ""
                with self.assertRaises(ValidationError) as ex:
                    ProfileFilledOutSerializer(data=clone).is_valid(
                        raise_exception=True)
                assert parent_getter(ex.exception.args[0]) == {
                    key: ['This field may not be blank.']
                }
예제 #2
0
 def test_success(self):
     """
     Test a successful validation
     """
     self.data["image"] = self.profile.image
     ProfileFilledOutSerializer(data=self.data).is_valid(
         raise_exception=True)
 def test_work_history_end_date(self):
     """
     Make sure end_date can be set to null on filled out profiles
     """
     self.data['work_history'][0]['end_date'] = None
     # No exception should be raised by the next line
     ProfileFilledOutSerializer(data=self.data).is_valid(
         raise_exception=True)
예제 #4
0
 def setUp(self):
     """
     Create a profile and social auth
     """
     super().setUp()
     serializer = ProfileFilledOutSerializer(self.profile)
     self.data = serializer.data
     self.profile.refresh_from_db()
 def test_filled_out_false(self):
     """
     filled_out cannot be set to false
     """
     self.data['filled_out'] = False
     with self.assertRaises(ValidationError) as ex:
         ProfileFilledOutSerializer(data=self.data).is_valid(
             raise_exception=True)
     assert ex.exception.args[0] == {
         'non_field_errors': ['filled_out cannot be set to false']
     }
예제 #6
0
    def assert_required_fields(self, field_names, parent_getter,
                               field_parent_getter):
        """
        Helper function to assert required fields
        """
        for key in field_names:
            field = field_parent_getter(
                ProfileFilledOutSerializer().fields)[key]
            is_generated = isinstance(
                field, (ListSerializer, SerializerMethodField, ReadOnlyField))
            is_skippable = (field.read_only or field.allow_null
                            or field.allow_blank)
            # skip fields that are skippable, generated, read only, or that tie
            #  to other serializers which are tested elsewhere.
            if is_generated or is_skippable:
                continue

            clone = deepcopy(self.data)
            clone["image"] = self.profile.image
            clone["image_small"] = self.profile.image_small
            clone["image_medium"] = self.profile.image_medium
            parent_getter(clone)[key] = None
            with self.assertRaises(ValidationError) as ex:
                ProfileFilledOutSerializer(data=clone).is_valid(
                    raise_exception=True)
            assert parent_getter(ex.exception.args[0]) == {
                key: ['This field may not be null.']
            }

            if isinstance(field, CharField):
                # test blank string too
                parent_getter(clone)[key] = ""
                with self.assertRaises(ValidationError) as ex:
                    ProfileFilledOutSerializer(data=clone).is_valid(
                        raise_exception=True)
                assert parent_getter(ex.exception.args[0]) == {
                    key: ['This field may not be blank.']
                }
 def setUp(self):
     """
     Create a profile and social auth
     """
     serializer = ProfileFilledOutSerializer(self.profile)
     self.data = serializer.to_representation(self.profile)