Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,19 @@
GeneratedField = None


def serialize_enum_value(value):
try:
json.dumps(value)
except TypeError:
value = str(value)
return value


class FieldFactory:
@staticmethod
def _build_enum_values(field: Field) -> Optional[List[str]]:
if field.choices:
return [c[0] for c in field.choices] # type: ignore
return [serialize_enum_value(c[0]) for c in field.choices] # type: ignore
return None

@staticmethod
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from datetime import datetime
from unittest import TestCase
from unittest.mock import patch

Expand Down Expand Up @@ -52,6 +53,20 @@ def test_build_should_handle_enums(self):
self.assertEqual(field_schema["enum_values"], [1, 2, 3])
self.assertEqual(field_schema["column_type"], PrimitiveType.ENUM)

def test_build_should_use_str_on_enum_values_when_value_is_not_json_serializable(self):
"""enums"""
now = datetime.now()
choices = [
(datetime(1916, 1, 1, 1, 1, 1), "WW1"),
(datetime(1941, 2, 2, 2, 2, 2), "WW2"),
(now, "NOW"),
]
field = models.DateField(choices=choices)
field_schema = FieldFactory.build(field)

self.assertEqual(field_schema["enum_values"], ["1916-01-01 01:01:01", "1941-02-02 02:02:02", str(now)])
self.assertEqual(field_schema["column_type"], PrimitiveType.ENUM)

def test_build_should_handle_read_only_for_auto_increment_pk(self):
"""readonly"""
field = models.AutoField(primary_key=True)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def validate_value_for_operator(condition_tree: ConditionTreeLeaf, column_schema

error_msg = (
f'The given value attribute "{value}" (type: {value_type}) has an unexpected value '
'for the given operator "{condition_tree.operator}."'
f'for the given operator "{condition_tree.operator}."'
)
allowed_types = MAP_ALLOWED_TYPES_FOR_OPERATOR.get(condition_tree.operator, [])
if not allowed_types:
Expand Down