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
14 changes: 10 additions & 4 deletions piccolo/testing/model_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,10 +173,16 @@ def _randomize_attribute(cls, column: Column) -> Any:
random_value = RandomBuilder.next_datetime(tz_aware=tz_aware)
elif column.value_type == list:
length = RandomBuilder.next_int(maximum=10)
base_type = cast(Array, column).base_column.value_type
random_value = [
cls.__DEFAULT_MAPPER[base_type]() for _ in range(length)
]
if column._meta.choices:
random_value = [
RandomBuilder.next_enum(column._meta.choices)
for _ in range(length)
]
else:
base_type = cast(Array, column).base_column.value_type
random_value = [
cls.__DEFAULT_MAPPER[base_type]() for _ in range(length)
]
elif column._meta.choices:
random_value = RandomBuilder.next_enum(column._meta.choices)
else:
Expand Down
16 changes: 16 additions & 0 deletions tests/testing/test_model_builder.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import asyncio
import enum
import json
import unittest

Expand Down Expand Up @@ -30,9 +31,14 @@


class TableWithArrayField(Table):
class Choices(enum.Enum):
a = "a"
b = "b"

strings = Array(Varchar(30))
integers = Array(Integer())
floats = Array(Real())
choices = Array(Varchar(), choices=Choices)


class TableWithDecimal(Table):
Expand Down Expand Up @@ -104,6 +110,16 @@ def test_choices(self):
["s", "l", "m"],
)

def test_array_choices(self):
"""
Make sure that ``ModelBuilder`` generates arrays where each array
element is a valid choice.
"""
instance = ModelBuilder.build_sync(TableWithArrayField)
for value in instance.choices:
# Will raise an exception if the enum value isn't found:
TableWithArrayField.Choices[value]

def test_datetime(self):
"""
Make sure that ``ModelBuilder`` generates timezone aware datetime
Expand Down