Skip to content

Commit

Permalink
add tests for fields & fix EnumField init error (#12) ☃️
Browse files Browse the repository at this point in the history
* add tests for fields

* fix error when non-enum is passed into EnumField

* update changelog

* add EnumField fix to changelog

* improve test for invalid FieldList input

* remove redundant test

* improve test for invalid FormField input

* improve test for invalid FormFieldList input

* make empty values passed to FormField return {}

* remove f-string test

* add more true values to BooleanFieldTests

* uncomment and fix remaining tests in FormFieldListTests

* remove unused imports

* improve error message for invalid values passed to enum

* update changelog
  • Loading branch information
pawl authored May 5, 2020
1 parent 3571d30 commit 05948be
Show file tree
Hide file tree
Showing 6 changed files with 439 additions and 10 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@

## 0.8.0 : TBD

- **Maintenance**: Add tests for fields
- **Change**: Remove DeclarativeFieldsMetaclass and import from Django instead.
- **Change**: Msgpack dependency is no longer required.
- **Change**: Empty values passed into a FormField now return {} rather than None.
- **Fix**: Throw a more user friendly error when passing non-Enums or invalid values to EnumField.

## 0.7.1 : 13.04.2020

Expand Down
12 changes: 6 additions & 6 deletions django_api_forms/fields.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import typing

from enum import Enum

from django.core.exceptions import ValidationError
from django.forms import Field

from django.utils.translation import gettext_lazy as _

from .exceptions import RequestValidationError
Expand Down Expand Up @@ -82,7 +80,7 @@ def form(self):

def to_python(self, value) -> typing.Union[typing.Dict, None]:
if not value:
return None
return {}

form = self._form(value)
if form.is_valid():
Expand Down Expand Up @@ -125,8 +123,10 @@ class EnumField(Field):
}

def __init__(self, enum: typing.Type, **kwargs):
if not issubclass(enum, Enum):
raise RuntimeError("Invalid Field type passed into FieldList!")
# isinstance(enum, type) prevents "TypeError: issubclass() arg 1 must be a class"
# based on: https://github.com/samuelcolvin/pydantic/blob/v0.32.x/pydantic/utils.py#L260-L261
if not (isinstance(enum, type) and issubclass(enum, Enum)):
raise RuntimeError("Invalid Enum type passed into EnumField!")

self.enum = enum

Expand All @@ -137,7 +137,7 @@ def to_python(self, value) -> typing.Union[typing.Type[Enum], None]:
try:
return self.enum(value)
except ValueError:
raise ValidationError(f"Invalid enum value {value} passed to {type(self.enum)}")
raise ValidationError(f"Invalid enum value {value} passed to {self.enum}")
return None


Expand Down
2 changes: 0 additions & 2 deletions django_api_forms/forms.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import copy
import json
from collections import OrderedDict
from typing import Union

from django.core.exceptions import NON_FIELD_ERRORS, ValidationError
from django.forms import Field
from django.forms.forms import DeclarativeFieldsMetaclass
from django.utils.translation import gettext as _

Expand Down
2 changes: 2 additions & 0 deletions pytest.ini
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
[pytest]
DJANGO_SETTINGS_MODULE = tests.settings
log_cli = 1
log_cli_level = INFO
Loading

0 comments on commit 05948be

Please sign in to comment.