dragon-fire-fly / developer_matcher

2 stars 1 forks source link

[Test Failure] Create invalid project post #49

Closed dragon-fire-fly closed 1 year ago

dragon-fire-fly commented 1 year ago

Which file is being tested? app_home/views.py

What is being tested? test => def test_create_project_post_invalid(): The 'create project' post method is being tested with an invalid title being given.

Describe the test failure The custom form validation is failing because it expects "title" to be present in order to check for duplicates and profanities.

Screenshots image

Additional context in forms.py

def validate_project_name(data):
    """Checks that a project name is not already taken and does not
    contain profanities"""

    if isinstance(data, str):
        project_name = data
    else:
        project_name = data.cleaned_data["title"]
        if data.instance:
            if data.instance.title == project_name:
                return

    # Check project name for profanity and do not allow if present
    if profanity.contains_profanity(project_name):
        raise ValidationError("profanity")
    else:
        # Check if project name already taken and return error if so
        try:
            taken_project_name = Project.objects.get(title=project_name)
            raise ValidationError("duplicate_name")
        except Project.DoesNotExist:
            return
dragon-fire-fly commented 1 year ago

The code in forms.py was amended to use the get method, returning the title, if present, else None. If None is returned, the code raises a validation error.

def validate_project_name(data):
    """Checks that a project name is not already taken and does not
    contain profanities"""

    if isinstance(data, str):
        project_name = data
    else:
        project_name = data.cleaned_data.get("title", None)
        if not project_name:
            raise ValidationError("No title given")
        else:
            if data.instance:
                if data.instance.title == project_name:
                    return