From f23d8dd66b210315b05783ba98216cac5021d080 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adolfo=20G=C3=B3mez=20Garc=C3=ADa?= Date: Sun, 30 Jun 2024 14:30:56 +0200 Subject: [PATCH] Added "sorted_by" parameter to filter helpers --- server/src/tests/utils/__init__.py | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/server/src/tests/utils/__init__.py b/server/src/tests/utils/__init__.py index 6bfc45291..d3522bae2 100644 --- a/server/src/tests/utils/__init__.py +++ b/server/src/tests/utils/__init__.py @@ -173,19 +173,25 @@ def search_item_by_attr(lst: list[T], attribute: str, value: typing.Any, **kwarg return item raise ValueError(f'Item with {attribute}=="{value}" not found in list {str(lst)[:100]}') -def filter_list_by_attr(lst: list[T], attribute: str, value: typing.Any, **kwargs: typing.Any) -> list[T]: +def filter_list_by_attr(lst: list[T], attribute: str, value: typing.Any,*, sorted_by: str = '', **kwargs: typing.Any) -> list[T]: """ Returns a list of items from a list of items kwargs are not used, just to let use it as partial on fixtures """ - return [item for item in lst if getattr(item, attribute) == value or value is None] + values = [item for item in lst if getattr(item, attribute) == value or value is None] + if sorted_by: + values.sort(key=lambda x: getattr(x, sorted_by)) + return values -def filter_list_by_attr_list(lst: list[T], attribute: str, values: list[typing.Any], **kwargs: typing.Any) -> list[T]: +def filter_list_by_attr_list(lst: list[T], attribute: str, values: list[typing.Any], *, sorted_by: str = '', **kwargs: typing.Any) -> list[T]: """ Returns a list of items from a list of items kwargs are not used, just to let use it as partial on fixtures """ - return [item for item in lst if getattr(item, attribute) in values] + values = [item for item in lst if getattr(item, attribute) in values] + if sorted_by: + values.sort(key=lambda x: getattr(x, sorted_by)) + return values def check_userinterface_values(obj: ui.UserInterface, values: ui.gui.ValuesDictType) -> None: """