From e7863a92e002205dffd481b73c4e22e4d63d1bd8 Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Tue, 27 Jul 2021 11:43:45 +0200 Subject: [PATCH] Added last query --- src/cpl_query/_extension/iterable.py | 7 ++++++ src/cpl_query/_query/last.py | 24 +++++++++++++++++++ src/cpl_query/extension/iterable_abc.py | 6 +++++ src/cpl_query/tests/query_test.py | 31 +++++++++++++++++++++---- 4 files changed, 64 insertions(+), 4 deletions(-) create mode 100644 src/cpl_query/_query/last.py diff --git a/src/cpl_query/_extension/iterable.py b/src/cpl_query/_extension/iterable.py index cb0f868e..091eae1a 100644 --- a/src/cpl_query/_extension/iterable.py +++ b/src/cpl_query/_extension/iterable.py @@ -11,6 +11,7 @@ from .._query.distinct import distinct_query from .._query.element_at import element_at_query, element_at_or_default_query from .._query.first import first_or_default_query, first_query from .._query.for_each import for_each_query +from .._query.last import last_query, last_or_default_query from .._query.order_by import order_by_query, order_by_descending_query from .._query.single import single_query, single_or_default_query from .._query.where import where_query @@ -48,6 +49,12 @@ class Iterable(IterableABC): def element_at_or_default(self, index: int) -> Optional[any]: return element_at_or_default_query(self, index) + def last(self) -> any: + return last_query(self) + + def last_or_default(self) -> Optional[any]: + return last_or_default_query(self) + def first(self) -> any: return first_query(self) diff --git a/src/cpl_query/_query/last.py b/src/cpl_query/_query/last.py new file mode 100644 index 00000000..20ff901a --- /dev/null +++ b/src/cpl_query/_query/last.py @@ -0,0 +1,24 @@ +from typing import Optional + +from cpl_query.exceptions import ArgumentNoneException, ExceptionArgument, IndexOutOfRangeException +from cpl_query.extension.iterable_abc import IterableABC + + +def last_query(_list: IterableABC) -> any: + if _list is None: + raise ArgumentNoneException(ExceptionArgument.list) + + if len(_list) == 0: + raise IndexOutOfRangeException() + + return _list[len(_list) - 1] + + +def last_or_default_query(_list: IterableABC) -> Optional[any]: + if _list is None: + raise ArgumentNoneException(ExceptionArgument.list) + + if len(_list) == 0: + return None + + return _list[len(_list) - 1] diff --git a/src/cpl_query/extension/iterable_abc.py b/src/cpl_query/extension/iterable_abc.py index 28cb188b..c9d0105b 100644 --- a/src/cpl_query/extension/iterable_abc.py +++ b/src/cpl_query/extension/iterable_abc.py @@ -32,6 +32,12 @@ class IterableABC(ABC, list): @abstractmethod def element_at_or_default(self, index: int) -> Optional[any]: pass + @abstractmethod + def last(self) -> any: pass + + @abstractmethod + def last_or_default(self) -> any: pass + @abstractmethod def first(self) -> any: pass diff --git a/src/cpl_query/tests/query_test.py b/src/cpl_query/tests/query_test.py index 0f6b3e17..64f5f23c 100644 --- a/src/cpl_query/tests/query_test.py +++ b/src/cpl_query/tests/query_test.py @@ -100,8 +100,31 @@ class QueryTest(unittest.TestCase): self.assertEqual(self._tests[index], self._tests.element_at_or_default(index)) self.assertIsNone(self._tests.element_at_or_default(len(self._tests))) - test = [1, 2, 3] - t = test[100] + def test_last(self): + results = [] + for user in self._tests: + if user.address.nr == 10: + results.append(user) + + res = self._tests.where(lambda u: u.address.nr == 10) + s_res = self._tests.where(lambda u: u.address.nr == 10).last() + + self.assertEqual(len(res), len(results)) + self.assertEqual(res[len(res) - 1], s_res) + + def test_last_or_default(self): + results = [] + for user in self._tests: + if user.address.nr == 10: + results.append(user) + + res = self._tests.where(lambda u: u.address.nr == 10) + s_res = self._tests.where(lambda u: u.address.nr == 10).last_or_default() + sn_res = self._tests.where(lambda u: u.address.nr == 11).last_or_default() + + self.assertEqual(len(res), len(results)) + self.assertEqual(res[len(res) - 1], s_res) + self.assertIsNone(sn_res) def test_first(self): results = [] @@ -113,7 +136,7 @@ class QueryTest(unittest.TestCase): s_res = self._tests.where(lambda u: u.address.nr == 10).first() self.assertEqual(len(res), len(results)) - self.assertIsNotNone(s_res) + self.assertEqual(res[0], s_res) def test_first_or_default(self): results = [] @@ -126,7 +149,7 @@ class QueryTest(unittest.TestCase): sn_res = self._tests.where(lambda u: u.address.nr == 11).first_or_default() self.assertEqual(len(res), len(results)) - self.assertIsNotNone(s_res) + self.assertEqual(res[0], s_res) self.assertIsNone(sn_res) def test_for_each(self):