|
12 | 12 | DateTimeField,
|
13 | 13 | DateField,
|
14 | 14 | TimeField,
|
| 15 | + ListField, |
15 | 16 | )
|
16 | 17 |
|
17 |
| -from drf_excel.fields import XLSXField, XLSXNumberField, XLSXDateField |
| 18 | +from drf_excel.fields import XLSXField, XLSXNumberField, XLSXDateField, XLSXListField |
18 | 19 | from drf_excel.utilities import XLSXStyle
|
19 | 20 |
|
20 | 21 |
|
@@ -304,3 +305,50 @@ def test_cell_date_custom_format(
|
304 | 305 | assert isinstance(cell, Cell)
|
305 | 306 | assert cell.value == dt.date(2017, 10, 25)
|
306 | 307 | assert cell.number_format == "dd/mm/yyyy"
|
| 308 | + |
| 309 | + |
| 310 | +class TestXLSXListField: |
| 311 | + def test_cell_default_separator(self, style: XLSXStyle, worksheet: Worksheet): |
| 312 | + f = XLSXListField( |
| 313 | + list_sep=None, |
| 314 | + key="things", |
| 315 | + value=["foo", "bar", "baz"], |
| 316 | + field=ListField(), |
| 317 | + style=style, |
| 318 | + mapping=None, |
| 319 | + cell_style=style, |
| 320 | + ) |
| 321 | + assert f.original_value == f.value == ["foo", "bar", "baz"] |
| 322 | + cell = f.cell(worksheet, 1, 1) |
| 323 | + assert isinstance(cell, Cell) |
| 324 | + assert cell.value == "foo, bar, baz" |
| 325 | + |
| 326 | + def test_cell_custom_separator(self, style: XLSXStyle, worksheet: Worksheet): |
| 327 | + f = XLSXListField( |
| 328 | + list_sep=";", |
| 329 | + key="things", |
| 330 | + value=[ "john", "doe", "[email protected]"], |
| 331 | + field=ListField(), |
| 332 | + style=style, |
| 333 | + mapping=None, |
| 334 | + cell_style=style, |
| 335 | + ) |
| 336 | + assert f. original_value == f. value == [ "john", "doe", "[email protected]"] |
| 337 | + cell = f.cell(worksheet, 1, 1) |
| 338 | + assert isinstance(cell, Cell) |
| 339 | + assert cell. value == "john;doe;[email protected]" |
| 340 | + |
| 341 | + def test_cell_complex_types(self, style: XLSXStyle, worksheet: Worksheet): |
| 342 | + f = XLSXListField( |
| 343 | + list_sep=None, |
| 344 | + key="objs", |
| 345 | + value=[{"a": 1}, {"b": 2}], |
| 346 | + field=ListField(), |
| 347 | + style=style, |
| 348 | + mapping=None, |
| 349 | + cell_style=style, |
| 350 | + ) |
| 351 | + assert f.original_value == f.value == [{"a": 1}, {"b": 2}] |
| 352 | + cell = f.cell(worksheet, 1, 1) |
| 353 | + assert isinstance(cell, Cell) |
| 354 | + assert cell.value == '[{"a": 1}, {"b": 2}]' |
0 commit comments