Added contains query

This commit is contained in:
Sven Heidemann 2021-07-27 10:55:23 +02:00
parent 5e5d86cf94
commit f8bd86692e
4 changed files with 16 additions and 0 deletions

View File

@ -5,6 +5,7 @@ from cpl_query.extension.ordered_iterable_abc import OrderedIterableABC
from .._query.all_query import all_query from .._query.all_query import all_query
from .._query.any_query import any_query from .._query.any_query import any_query
from .._query.avg_query import avg_query from .._query.avg_query import avg_query
from .._query.contains_query import contains_query
from .._query.first_query import first_or_default_query, first_query from .._query.first_query import first_or_default_query, first_query
from .._query.for_each_query import for_each_query from .._query.for_each_query import for_each_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
@ -27,6 +28,9 @@ class Iterable(IterableABC):
def average(self, t: type, func: Callable) -> Union[int, float, complex]: def average(self, t: type, func: Callable) -> Union[int, float, complex]:
return avg_query(self, t, func) return avg_query(self, t, func)
def contains(self, value: object) -> bool:
return contains_query(self, value)
def first(self) -> any: def first(self) -> any:
return first_query(self) return first_query(self)

View File

@ -0,0 +1,5 @@
from cpl_query.extension.iterable_abc import IterableABC
def contains_query(_list: IterableABC, value: object) -> bool:
return value in _list

View File

@ -17,6 +17,9 @@ class IterableABC(ABC, list):
@abstractmethod @abstractmethod
def average(self, t: type, func: Callable) -> Union[int, float, complex]: pass def average(self, t: type, func: Callable) -> Union[int, float, complex]: pass
@abstractmethod
def contains(self, value: object) -> bool: pass
@abstractmethod @abstractmethod
def first(self) -> any: pass def first(self) -> any: pass

View File

@ -79,6 +79,10 @@ class QueryTest(unittest.TestCase):
self.assertRaises(InvalidTypeException, invalid) self.assertRaises(InvalidTypeException, invalid)
self.assertRaises(WrongTypeException, wrong) self.assertRaises(WrongTypeException, wrong)
def test_contains(self):
self.assertTrue(self._tests.contains(self._t_user))
self.assertFalse(self._tests.contains(User("Test", None)))
def test_first(self): def test_first(self):
results = [] results = []
for user in self._tests: for user in self._tests: