From c6c2951beb3df10bc803b6fbc507d885d8f80af1 Mon Sep 17 00:00:00 2001 From: Joe Numainville Date: Wed, 10 Apr 2024 14:57:44 -0500 Subject: [PATCH] fix props --- .../ui/src/deephaven/ui/_internal/utils.py | 2 +- .../ui/components/spectrum/date_picker.py | 8 ++ .../ui/test/deephaven/ui/test_date_picker.py | 79 +++++++++++++++++++ plugins/ui/test/deephaven/ui/test_utils.py | 44 +++++++++++ 4 files changed, 132 insertions(+), 1 deletion(-) create mode 100644 plugins/ui/src/deephaven/ui/components/spectrum/date_picker.py create mode 100644 plugins/ui/test/deephaven/ui/test_date_picker.py diff --git a/plugins/ui/src/deephaven/ui/_internal/utils.py b/plugins/ui/src/deephaven/ui/_internal/utils.py index 98746041b..53d012517 100644 --- a/plugins/ui/src/deephaven/ui/_internal/utils.py +++ b/plugins/ui/src/deephaven/ui/_internal/utils.py @@ -210,7 +210,7 @@ def create_props(args: dict[str, Any]) -> tuple[tuple[Any], dict[str, Any]]: Returns: A tuple of children and props """ - children, props = args.pop("children"), args.pop("props") + children, props = args.pop("children", tuple()), args.pop("props", {}) props.update(args) return children, props diff --git a/plugins/ui/src/deephaven/ui/components/spectrum/date_picker.py b/plugins/ui/src/deephaven/ui/components/spectrum/date_picker.py new file mode 100644 index 000000000..3c8be378d --- /dev/null +++ b/plugins/ui/src/deephaven/ui/components/spectrum/date_picker.py @@ -0,0 +1,8 @@ +from typing import Literal + +PageBehavior = Literal["single", "visible"] +HourCycle = Literal[12, 24] +ValidationBehavior = Literal["aria", "native"] +Alignment = Literal["start", "end"] +NecessityIndicator = Literal["label", "icon"] +ValidationState = Literal["valid", "invalid"] diff --git a/plugins/ui/test/deephaven/ui/test_date_picker.py b/plugins/ui/test/deephaven/ui/test_date_picker.py new file mode 100644 index 000000000..2055e81e1 --- /dev/null +++ b/plugins/ui/test/deephaven/ui/test_date_picker.py @@ -0,0 +1,79 @@ +import unittest + +from .BaseTest import BaseTestCase + + +class DatePickerTest(BaseTestCase): + def test_convert_date_props(self): + from deephaven.time import to_j_instant, to_j_zdt, to_j_local_date + from deephaven.ui.components.date_picker import _convert_date_picker_props + from deephaven.ui._internal.utils import get_jclass_name + + def verify_is_local_date(date): + self.assertEqual(get_jclass_name(date), "java.time.LocalDate") + + def verify_is_instant(date): + self.assertEqual(get_jclass_name(date), "java.time.Instant") + + def verify_is_zdt(date): + self.assertEqual(get_jclass_name(date), "java.time.ZonedDateTime") + + def empty_on_change(): + pass + + props1 = { + "placeholder_value": "2021-01-01", + "value": "2021-01-01 UTC", + "default_value": "2021-01-01 ET", + "unavailable_dates": [to_j_instant("2021-01-01 UTC")], + "min_value": to_j_zdt("2021-01-01 ET"), + "max_value": to_j_local_date("2021-01-01"), + } + + props2 = { + "value": to_j_local_date("2021-01-01"), + "default_value": to_j_zdt("2021-01-01 ET"), + "placeholder_value": to_j_instant("2021-01-01 UTC"), + "on_change": verify_is_local_date, + } + + props3 = { + "default_value": to_j_instant("2021-01-01 UTC"), + "placeholder_value": to_j_zdt("2021-01-01 ET"), + "on_change": verify_is_instant, + } + + props4 = { + "placeholder_value": to_j_zdt("2021-01-01 ET"), + "on_change": verify_is_zdt, + } + + props5 = {"on_change": verify_is_instant} + + props6 = {"on_change": empty_on_change} + + _convert_date_picker_props(props1) + _convert_date_picker_props(props2) + _convert_date_picker_props(props3) + _convert_date_picker_props(props4) + _convert_date_picker_props(props5) + _convert_date_picker_props(props6) + + verify_is_local_date(props1["max_value"]) + verify_is_zdt(props1["min_value"]) + verify_is_instant(props1["unavailable_dates"][0]) + verify_is_instant(props1["value"]) + verify_is_instant(props1["default_value"]) + verify_is_local_date(props1["placeholder_value"]) + + props2["on_change"]("2021-01-01") + props3["on_change"]("2021-01-01 UTC") + props4["on_change"]("2021-01-01 ET") + props5["on_change"]("2021-01-01 UTC") + + # pass an Instant but it should be dropped with no error + props6["on_change"]("2021-01-01 UTC") + + +if __name__ == "__main__": + unittest.main() diff --git a/plugins/ui/test/deephaven/ui/test_utils.py b/plugins/ui/test/deephaven/ui/test_utils.py index f67f2bb80..d98bca7e0 100644 --- a/plugins/ui/test/deephaven/ui/test_utils.py +++ b/plugins/ui/test/deephaven/ui/test_utils.py @@ -217,6 +217,50 @@ def test_func_with_all_args(a, /, b, *args, c=1, **kwargs): # Test that wrapping a function without a signature doesn't throw an error wrapped = wrap_callable(print) + def test_create_props(self): + from deephaven.ui._internal.utils import create_props + + children1, props1 = create_props( + { + "foo": "bar", + "baz": 42, + "fizz": "buzz", + } + ) + + self.assertEqual(children1, tuple()) + self.assertDictEqual( + props1, + { + "foo": "bar", + "baz": 42, + "fizz": "buzz", + }, + ) + + children2, props2 = create_props( + { + "children": ["item1", "item2"], + "test": "value", + "props": { + "foo": "bar", + "baz": 42, + "fizz": "buzz", + }, + } + ) + + self.assertEqual(children2, ["item1", "item2"]) + self.assertDictEqual( + props2, + { + "foo": "bar", + "baz": 42, + "fizz": "buzz", + "test": "value", + }, + ) + if __name__ == "__main__": unittest.main()