Skip to content

Commit

Permalink
Merge branch 'audit-table' into audit-tree
Browse files Browse the repository at this point in the history
  • Loading branch information
freakboy3742 committed Jun 29, 2023
2 parents ea85630 + 80b9853 commit 95f6cc2
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 92 deletions.
13 changes: 7 additions & 6 deletions core/src/toga/widgets/table.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
from __future__ import annotations

import warnings
from typing import Any

from toga.handlers import wrapped_handler
from toga.sources import ListSource, Row
from toga.sources import ListSource, Row, Source
from toga.sources.accessors import build_accessors, to_accessor

from .base import Widget
Expand All @@ -15,7 +16,7 @@ def __init__(
headings: list[str] | None = None,
id=None,
style=None,
data: list | ListSource | None = None,
data: Any = None,
accessors: list[str] | None = None,
multiple_select: bool = False,
on_select: callable | None = None,
Expand Down Expand Up @@ -135,13 +136,13 @@ def data(self) -> ListSource:
return self._data

@data.setter
def data(self, data: list | ListSource | None):
def data(self, data: Any):
if data is None:
self._data = ListSource(accessors=self._accessors, data=[])
elif isinstance(data, (list, tuple)):
self._data = ListSource(accessors=self._accessors, data=data)
else:
elif isinstance(data, Source):
self._data = data
else:
self._data = ListSource(accessors=self._accessors, data=data)

self._data.add_listener(self._impl)
self._impl.change_source(source=self._data)
Expand Down
149 changes: 63 additions & 86 deletions core/tests/widgets/test_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ def test_create_with_values(source, on_select_handler, on_activate_handler):
assert table.on_activate._raw == on_activate_handler


def test_create_with_acessor_overrides():
def test_create_with_accessor_overrides():
"A Table can partially override accessors"
table = toga.Table(
["First", "Second"],
Expand Down Expand Up @@ -138,43 +138,59 @@ def test_focus_noop(table):
assert_action_not_performed(table, "focus")


def test_set_data_list(table, on_select_handler):
"Data can be set from a list of lists"

# The selection hasn't changed yet.
on_select_handler.assert_not_called()

# Change the data
table.data = [
["Alice", 123, "extra1"],
["Bob", 234, "extra2"],
["Charlie", 345, "extra3"],
]

# This triggered the select handler
on_select_handler.assert_called_once_with(table)

# A ListSource has been constructed
assert isinstance(table.data, ListSource)
assert len(table.data) == 3

# The accessors are mapped in order.
assert table.data[1].key == "Bob"
assert table.data[1].value == 234


def test_set_data_tuple(table, on_select_handler):
"Data can be set from a list of tuples"
@pytest.mark.parametrize(
"data, all_attributes, extra_attributes",
[
# List of lists
(
[
["Alice", 123, "extra1"],
["Bob", 234, "extra2"],
["Charlie", 345, "extra3"],
],
True,
False,
),
# List of tuples
(
[
("Alice", 123, "extra1"),
("Bob", 234, "extra2"),
("Charlie", 345, "extra3"),
],
True,
False,
),
# List of dictionaries
(
[
{"key": "Alice", "value": 123, "extra": "extra1"},
{"key": "Bob", "value": 234, "extra": "extra2"},
{"key": "Charlie", "value": 345, "extra": "extra3"},
],
True,
True,
),
# List of bare data
(
[
"Alice",
1234,
"Charlie",
],
False,
False,
),
],
)
def test_set_data(table, on_select_handler, data, all_attributes, extra_attributes):
"Data can be set from a variety of sources"

# The selection hasn't changed yet.
on_select_handler.assert_not_called()

# Change the data
table.data = [
("Alice", 123, "extra1"),
("Bob", 234, "extra2"),
("Charlie", 345, "extra3"),
]
table.data = data

# This triggered the select handler
on_select_handler.assert_called_once_with(table)
Expand All @@ -184,60 +200,21 @@ def test_set_data_tuple(table, on_select_handler):
assert len(table.data) == 3

# The accessors are mapped in order.
assert table.data[1].key == "Bob"
assert table.data[1].value == 234


def test_set_data_dict(table, on_select_handler):
"Data can be set from a list of dicts"

# The selection hasn't changed yet.
on_select_handler.assert_not_called()

# Change the data
table.data = [
{"key": "Alice", "value": 123, "extra": "extra1"},
{"key": "Bob", "value": 234, "extra": "extra2"},
{"key": "Charlie", "value": 345, "extra": "extra3"},
]

# This triggered the select handler
on_select_handler.assert_called_once_with(table)

# A ListSource has been constructed
assert isinstance(table.data, ListSource)
assert len(table.data) == 3

# The accessors are all available
assert table.data[1].key == "Bob"
assert table.data[1].value == 234
assert table.data[1].extra == "extra2"


def test_set_data_other(table, on_select_handler):
"Data can be set from a list of values"

# The selection hasn't changed yet.
on_select_handler.assert_not_called()

# Change the data
table.data = [
"Alice",
1234,
"other",
]

# This triggered the select handler
on_select_handler.assert_called_once_with(table)

# A ListSource has been constructed
assert isinstance(table.data, ListSource)
assert len(table.data) == 3

# The values are mapped to the first accessor.
assert table.data[0].key == "Alice"
assert table.data[1].key == 1234
assert table.data[2].key == "other"
assert table.data[2].key == "Charlie"

if all_attributes:
assert table.data[1].key == "Bob"
assert table.data[0].value == 123
assert table.data[1].value == 234
assert table.data[2].value == 345
else:
assert table.data[1].key == 1234

if extra_attributes:
assert table.data[0].extra == "extra1"
assert table.data[1].extra == "extra2"
assert table.data[2].extra == "extra3"


def test_single_selection(table, on_select_handler):
Expand Down

0 comments on commit 95f6cc2

Please sign in to comment.