2021.10.3 #35

Merged
edraft merged 31 commits from 2021.10.3 into 2021.10 2021-08-02 14:36:46 +02:00
4 changed files with 64 additions and 4 deletions
Showing only changes of commit e7863a92e0 - Show all commits

View File

@ -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)

View File

@ -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]

View File

@ -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

View File

@ -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):