Added last query
This commit is contained in:
parent
82a9e3a23a
commit
e7863a92e0
@ -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.element_at import element_at_query, element_at_or_default_query
|
||||||
from .._query.first import first_or_default_query, first_query
|
from .._query.first import first_or_default_query, first_query
|
||||||
from .._query.for_each import for_each_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.order_by import order_by_query, order_by_descending_query
|
||||||
from .._query.single import single_query, single_or_default_query
|
from .._query.single import single_query, single_or_default_query
|
||||||
from .._query.where import where_query
|
from .._query.where import where_query
|
||||||
@ -48,6 +49,12 @@ class Iterable(IterableABC):
|
|||||||
def element_at_or_default(self, index: int) -> Optional[any]:
|
def element_at_or_default(self, index: int) -> Optional[any]:
|
||||||
return element_at_or_default_query(self, index)
|
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:
|
def first(self) -> any:
|
||||||
return first_query(self)
|
return first_query(self)
|
||||||
|
|
||||||
|
24
src/cpl_query/_query/last.py
Normal file
24
src/cpl_query/_query/last.py
Normal 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]
|
@ -32,6 +32,12 @@ class IterableABC(ABC, list):
|
|||||||
@abstractmethod
|
@abstractmethod
|
||||||
def element_at_or_default(self, index: int) -> Optional[any]: pass
|
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
|
@abstractmethod
|
||||||
def first(self) -> any: pass
|
def first(self) -> any: pass
|
||||||
|
|
||||||
|
@ -100,8 +100,31 @@ class QueryTest(unittest.TestCase):
|
|||||||
self.assertEqual(self._tests[index], self._tests.element_at_or_default(index))
|
self.assertEqual(self._tests[index], self._tests.element_at_or_default(index))
|
||||||
self.assertIsNone(self._tests.element_at_or_default(len(self._tests)))
|
self.assertIsNone(self._tests.element_at_or_default(len(self._tests)))
|
||||||
|
|
||||||
test = [1, 2, 3]
|
def test_last(self):
|
||||||
t = test[100]
|
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):
|
def test_first(self):
|
||||||
results = []
|
results = []
|
||||||
@ -113,7 +136,7 @@ class QueryTest(unittest.TestCase):
|
|||||||
s_res = self._tests.where(lambda u: u.address.nr == 10).first()
|
s_res = self._tests.where(lambda u: u.address.nr == 10).first()
|
||||||
|
|
||||||
self.assertEqual(len(res), len(results))
|
self.assertEqual(len(res), len(results))
|
||||||
self.assertIsNotNone(s_res)
|
self.assertEqual(res[0], s_res)
|
||||||
|
|
||||||
def test_first_or_default(self):
|
def test_first_or_default(self):
|
||||||
results = []
|
results = []
|
||||||
@ -126,7 +149,7 @@ class QueryTest(unittest.TestCase):
|
|||||||
sn_res = self._tests.where(lambda u: u.address.nr == 11).first_or_default()
|
sn_res = self._tests.where(lambda u: u.address.nr == 11).first_or_default()
|
||||||
|
|
||||||
self.assertEqual(len(res), len(results))
|
self.assertEqual(len(res), len(results))
|
||||||
self.assertIsNotNone(s_res)
|
self.assertEqual(res[0], s_res)
|
||||||
self.assertIsNone(sn_res)
|
self.assertIsNone(sn_res)
|
||||||
|
|
||||||
def test_for_each(self):
|
def test_for_each(self):
|
||||||
|
Loading…
Reference in New Issue
Block a user