2021-07-25 19:15:02 +02:00
|
|
|
import string
|
|
|
|
import unittest
|
|
|
|
from random import randint
|
|
|
|
|
2021-08-05 14:21:42 +02:00
|
|
|
from cpl_core.utils import String
|
2022-09-13 19:33:26 +02:00
|
|
|
from cpl_query.enumerable.enumerable import Enumerable
|
|
|
|
from cpl_query.exceptions import InvalidTypeException, ArgumentNoneException, IndexOutOfRangeException
|
2022-05-26 15:53:23 +02:00
|
|
|
from unittests_query.models import User, Address
|
2021-07-25 19:15:02 +02:00
|
|
|
|
|
|
|
|
2022-09-13 19:33:26 +02:00
|
|
|
class EnumerableQueryTestCase(unittest.TestCase):
|
2021-07-25 19:15:02 +02:00
|
|
|
|
|
|
|
def setUp(self) -> None:
|
2022-09-13 19:33:26 +02:00
|
|
|
self._tests = Enumerable(User)
|
2021-07-25 19:15:02 +02:00
|
|
|
self._t_user = User(
|
|
|
|
'Test user',
|
|
|
|
Address(
|
|
|
|
'teststr.',
|
|
|
|
15
|
|
|
|
)
|
|
|
|
)
|
2022-09-13 19:33:26 +02:00
|
|
|
self._t_user2 = User(
|
|
|
|
'Test user',
|
|
|
|
Address(
|
|
|
|
'teststr.',
|
|
|
|
14
|
|
|
|
)
|
|
|
|
)
|
2021-07-25 19:15:02 +02:00
|
|
|
|
|
|
|
self._generate_test_data()
|
|
|
|
|
|
|
|
def _generate_test_data(self):
|
|
|
|
for i in range(0, 100):
|
|
|
|
user = User(
|
2021-07-26 15:21:57 +02:00
|
|
|
String.random_string(string.ascii_letters, 8).lower(),
|
2021-07-25 19:15:02 +02:00
|
|
|
Address(
|
2021-07-26 15:21:57 +02:00
|
|
|
String.random_string(string.ascii_letters, 10).lower(),
|
2021-07-27 11:57:42 +02:00
|
|
|
randint(1, 10)
|
2021-07-25 19:15:02 +02:00
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2022-09-13 19:33:26 +02:00
|
|
|
self._tests.add(user)
|
2021-07-25 19:15:02 +02:00
|
|
|
|
2022-09-13 19:33:26 +02:00
|
|
|
self._tests.add(self._t_user)
|
|
|
|
self._tests.add(self._t_user2)
|
2021-07-25 19:15:02 +02:00
|
|
|
|
|
|
|
def test_any(self):
|
|
|
|
results = []
|
|
|
|
for user in self._tests:
|
|
|
|
if user.address.nr == 10:
|
|
|
|
results.append(user)
|
|
|
|
|
2021-07-26 15:32:28 +02:00
|
|
|
res = self._tests.any(lambda u: u.address.nr == 10)
|
|
|
|
n_res = self._tests.any(lambda u: u.address.nr == 100)
|
2021-07-25 19:15:02 +02:00
|
|
|
|
|
|
|
self.assertTrue(res)
|
|
|
|
self.assertFalse(n_res)
|
|
|
|
|
2021-07-27 09:15:19 +02:00
|
|
|
def test_all(self):
|
|
|
|
results = []
|
|
|
|
for user in self._tests:
|
|
|
|
if user.address.nr == 10:
|
|
|
|
results.append(user)
|
|
|
|
|
|
|
|
res = self._tests.all(lambda u: u.address is not None)
|
|
|
|
n_res = self._tests.all(lambda u: u.address.nr == 100)
|
|
|
|
|
|
|
|
self.assertTrue(res)
|
|
|
|
self.assertFalse(n_res)
|
|
|
|
|
2021-07-27 09:26:30 +02:00
|
|
|
def test_avg(self):
|
|
|
|
avg = 0
|
|
|
|
for user in self._tests:
|
|
|
|
avg += user.address.nr
|
|
|
|
|
|
|
|
avg = avg / len(self._tests)
|
2021-07-27 12:35:14 +02:00
|
|
|
res = self._tests.average(lambda u: u.address.nr)
|
2021-07-27 09:26:30 +02:00
|
|
|
|
2021-07-27 12:35:14 +02:00
|
|
|
self.assertEqual(avg, res)
|
2021-07-27 09:26:30 +02:00
|
|
|
|
2021-07-27 13:00:06 +02:00
|
|
|
def invalid():
|
2022-09-13 19:33:26 +02:00
|
|
|
tests = Enumerable(str, ['hello', 'world'])
|
2021-07-27 13:00:06 +02:00
|
|
|
e_res = tests.average()
|
2021-07-27 09:26:30 +02:00
|
|
|
|
2021-07-27 13:00:06 +02:00
|
|
|
self.assertRaises(InvalidTypeException, invalid)
|
2021-07-27 09:26:30 +02:00
|
|
|
|
2022-09-13 19:33:26 +02:00
|
|
|
tests = Enumerable(int, list(range(0, 100)))
|
2021-07-27 12:35:14 +02:00
|
|
|
self.assertEqual(sum(tests) / len(tests), tests.average())
|
|
|
|
|
|
|
|
def wrong2():
|
2022-09-13 19:33:26 +02:00
|
|
|
tests2 = Enumerable(int, values=list(range(0, 100)))
|
2021-07-27 12:35:14 +02:00
|
|
|
e_res = tests2.average(lambda u: u.address.nr)
|
|
|
|
|
|
|
|
self.assertRaises(AttributeError, wrong2)
|
|
|
|
|
2021-07-27 10:55:23 +02:00
|
|
|
def test_contains(self):
|
|
|
|
self.assertTrue(self._tests.contains(self._t_user))
|
|
|
|
self.assertFalse(self._tests.contains(User("Test", None)))
|
|
|
|
|
2021-07-27 11:01:43 +02:00
|
|
|
def test_count(self):
|
|
|
|
self.assertEqual(len(self._tests), self._tests.count())
|
|
|
|
self.assertEqual(1, self._tests.count(lambda u: u == self._t_user))
|
|
|
|
|
2021-07-27 11:20:35 +02:00
|
|
|
def test_distinct(self):
|
|
|
|
res = self._tests.distinct(lambda u: u.address.nr).where(lambda u: u.address.nr == 5)
|
|
|
|
self.assertEqual(1, len(res))
|
|
|
|
|
2021-07-27 11:29:52 +02:00
|
|
|
def test_element_at(self):
|
|
|
|
index = randint(0, len(self._tests) - 1)
|
2022-09-13 19:33:26 +02:00
|
|
|
self.assertEqual(self._tests.element_at(index), self._tests.element_at(index))
|
|
|
|
|
|
|
|
def invalid():
|
|
|
|
result = self._tests.element_at(len(self._tests))
|
|
|
|
|
|
|
|
self.assertRaises(IndexOutOfRangeException, invalid)
|
2021-07-27 11:29:52 +02:00
|
|
|
|
|
|
|
def test_element_at_or_default(self):
|
|
|
|
index = randint(0, len(self._tests) - 1)
|
2022-09-13 19:33:26 +02:00
|
|
|
self.assertEqual(self._tests.element_at(index), self._tests.element_at_or_default(index))
|
2021-07-27 11:29:52 +02:00
|
|
|
self.assertIsNone(self._tests.element_at_or_default(len(self._tests)))
|
|
|
|
|
2021-07-27 11:43:45 +02:00
|
|
|
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))
|
2022-09-13 19:33:26 +02:00
|
|
|
self.assertEqual(res.element_at(len(res) - 1), s_res)
|
2021-07-27 11:43:45 +02:00
|
|
|
|
|
|
|
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))
|
2022-09-13 19:33:26 +02:00
|
|
|
self.assertEqual(res.element_at(len(res) - 1), s_res)
|
2021-07-27 11:43:45 +02:00
|
|
|
self.assertIsNone(sn_res)
|
2021-07-27 11:40:24 +02:00
|
|
|
|
2021-07-25 19:15:02 +02:00
|
|
|
def test_first(self):
|
|
|
|
results = []
|
|
|
|
for user in self._tests:
|
|
|
|
if user.address.nr == 10:
|
|
|
|
results.append(user)
|
|
|
|
|
2021-07-26 15:32:28 +02:00
|
|
|
res = self._tests.where(lambda u: u.address.nr == 10)
|
|
|
|
s_res = self._tests.where(lambda u: u.address.nr == 10).first()
|
2021-07-25 19:15:02 +02:00
|
|
|
|
|
|
|
self.assertEqual(len(res), len(results))
|
2022-09-13 19:33:26 +02:00
|
|
|
self.assertEqual(res.element_at(0), s_res)
|
2021-07-25 19:15:02 +02:00
|
|
|
|
|
|
|
def test_first_or_default(self):
|
|
|
|
results = []
|
|
|
|
for user in self._tests:
|
|
|
|
if user.address.nr == 10:
|
|
|
|
results.append(user)
|
|
|
|
|
2021-07-26 15:32:28 +02:00
|
|
|
res = self._tests.where(lambda u: u.address.nr == 10)
|
|
|
|
s_res = self._tests.where(lambda u: u.address.nr == 10).first_or_default()
|
|
|
|
sn_res = self._tests.where(lambda u: u.address.nr == 11).first_or_default()
|
2021-07-25 19:15:02 +02:00
|
|
|
|
|
|
|
self.assertEqual(len(res), len(results))
|
2022-09-13 19:33:26 +02:00
|
|
|
self.assertEqual(res.element_at(0), s_res)
|
2021-07-25 19:15:02 +02:00
|
|
|
self.assertIsNone(sn_res)
|
|
|
|
|
|
|
|
def test_for_each(self):
|
|
|
|
users = []
|
2021-08-02 14:09:42 +02:00
|
|
|
self._tests.for_each(lambda user: (
|
2022-09-12 17:58:48 +02:00
|
|
|
users.append(user)
|
2021-07-25 19:15:02 +02:00
|
|
|
)
|
2022-09-12 17:58:48 +02:00
|
|
|
)
|
2021-07-25 19:15:02 +02:00
|
|
|
|
|
|
|
self.assertEqual(len(users), len(self._tests))
|
|
|
|
|
2021-07-27 11:57:42 +02:00
|
|
|
def test_max(self):
|
2021-07-27 12:42:33 +02:00
|
|
|
res = self._tests.max(lambda u: u.address.nr)
|
2022-09-13 19:33:26 +02:00
|
|
|
self.assertEqual(res, self._t_user.address.nr)
|
2021-07-27 11:57:42 +02:00
|
|
|
|
2022-09-13 19:33:26 +02:00
|
|
|
tests = Enumerable(values=list(range(0, 100)))
|
2021-07-27 12:48:22 +02:00
|
|
|
self.assertEqual(99, tests.max())
|
|
|
|
|
2021-07-27 13:00:06 +02:00
|
|
|
def invalid():
|
2022-09-13 19:33:26 +02:00
|
|
|
tests = Enumerable(str, ['hello', 'world'])
|
2021-07-27 13:00:06 +02:00
|
|
|
e_res = tests.average()
|
2021-07-27 12:48:22 +02:00
|
|
|
|
2021-07-27 13:00:06 +02:00
|
|
|
self.assertRaises(InvalidTypeException, invalid)
|
2021-07-27 12:48:22 +02:00
|
|
|
|
2021-07-27 11:57:42 +02:00
|
|
|
def test_min(self):
|
2021-07-27 12:42:33 +02:00
|
|
|
res = self._tests.min(lambda u: u.address.nr)
|
2021-07-27 11:57:42 +02:00
|
|
|
self.assertEqual(1, res)
|
|
|
|
|
2022-09-13 19:33:26 +02:00
|
|
|
tests = Enumerable(values=list(range(0, 100)))
|
2021-07-27 12:48:22 +02:00
|
|
|
self.assertEqual(0, tests.min())
|
|
|
|
|
2021-07-27 13:00:06 +02:00
|
|
|
def invalid():
|
2022-09-13 19:33:26 +02:00
|
|
|
tests = Enumerable(str, ['hello', 'world'])
|
2021-07-27 13:00:06 +02:00
|
|
|
e_res = tests.average()
|
2021-07-27 12:48:22 +02:00
|
|
|
|
2021-07-27 13:00:06 +02:00
|
|
|
self.assertRaises(InvalidTypeException, invalid)
|
2021-07-27 12:48:22 +02:00
|
|
|
|
2021-07-26 15:21:57 +02:00
|
|
|
def test_order_by(self):
|
|
|
|
res = self._tests.order_by(lambda user: user.address.street)
|
2022-09-13 19:33:26 +02:00
|
|
|
res2 = self._tests.order_by(lambda user: user.address.nr).to_list()
|
|
|
|
s_res = self._tests.to_list()
|
2021-07-26 15:21:57 +02:00
|
|
|
s_res.sort(key=lambda user: user.address.street)
|
2022-09-13 19:33:26 +02:00
|
|
|
self.assertEqual(res.to_list(), s_res)
|
2021-07-26 15:21:57 +02:00
|
|
|
|
2022-09-13 19:33:26 +02:00
|
|
|
s_res = self._tests.to_list()
|
2021-07-26 15:21:57 +02:00
|
|
|
s_res.sort(key=lambda user: user.address.nr)
|
|
|
|
self.assertEqual(res2, s_res)
|
|
|
|
|
2021-07-27 13:57:42 +02:00
|
|
|
self.assertEqual(self._t_user, res.where(lambda u: u.address.nr == self._t_user.address.nr).single())
|
|
|
|
|
2021-07-26 15:21:57 +02:00
|
|
|
def test_order_by_descending(self):
|
2022-09-13 19:33:26 +02:00
|
|
|
res = self._tests.order_by_descending(lambda user: user.address.street).to_list()
|
|
|
|
res2 = self._tests.order_by_descending(lambda user: user.address.nr).to_list()
|
|
|
|
s_res = self._tests.to_list()
|
2021-07-26 15:21:57 +02:00
|
|
|
s_res.sort(key=lambda user: user.address.street, reverse=True)
|
|
|
|
|
|
|
|
self.assertEqual(res, s_res)
|
2022-09-13 19:33:26 +02:00
|
|
|
s_res = self._tests.to_list()
|
2021-07-26 15:21:57 +02:00
|
|
|
s_res.sort(key=lambda user: user.address.nr, reverse=True)
|
|
|
|
self.assertEqual(res2, s_res)
|
|
|
|
|
|
|
|
def test_then_by(self):
|
2022-09-13 19:33:26 +02:00
|
|
|
res = self._tests.order_by(lambda user: user.address.street).then_by(lambda user: user.address.nr).to_list()
|
2021-07-26 15:21:57 +02:00
|
|
|
|
2022-09-13 19:33:26 +02:00
|
|
|
s_res = self._tests.to_list()
|
|
|
|
s_res.sort(key=lambda user: (user.address.street, user.address.nr))
|
2021-07-26 15:21:57 +02:00
|
|
|
|
|
|
|
self.assertEqual(res, s_res)
|
|
|
|
|
|
|
|
def test_then_by_descending(self):
|
2022-09-13 19:33:26 +02:00
|
|
|
res = self._tests.order_by_descending(lambda user: user.address.street).then_by_descending(lambda user: user.address.nr).to_list()
|
2021-07-26 15:21:57 +02:00
|
|
|
|
2022-09-13 19:33:26 +02:00
|
|
|
s_res = self._tests.to_list()
|
|
|
|
s_res.sort(key=lambda user: (user.address.street, user.address.nr), reverse=True)
|
2021-07-26 15:21:57 +02:00
|
|
|
|
|
|
|
self.assertEqual(res, s_res)
|
|
|
|
|
2021-07-27 12:54:49 +02:00
|
|
|
def test_reverse(self):
|
2022-09-13 19:33:26 +02:00
|
|
|
res = self._tests.reverse().to_list()
|
2021-07-27 12:54:49 +02:00
|
|
|
l_res = self._tests.to_list()
|
|
|
|
l_res.reverse()
|
|
|
|
|
2022-09-13 19:33:26 +02:00
|
|
|
self.assertEqual(res, l_res)
|
2021-07-27 12:54:49 +02:00
|
|
|
|
2022-09-12 17:58:48 +02:00
|
|
|
def test_select(self):
|
2022-09-13 19:33:26 +02:00
|
|
|
range_list = Enumerable(int, range(0, 100))
|
2022-09-12 17:58:48 +02:00
|
|
|
selected_range = range_list.select(lambda x: x + 1)
|
|
|
|
|
|
|
|
modulo_range = []
|
|
|
|
for x in range(0, 100):
|
|
|
|
if x % 2 == 0:
|
|
|
|
modulo_range.append(x)
|
|
|
|
self.assertEqual(selected_range.to_list(), list(range(1, 101)))
|
|
|
|
self.assertEqual(range_list.where(lambda x: x % 2 == 0).to_list(), modulo_range)
|
|
|
|
|
|
|
|
def test_select_many(self):
|
2022-09-13 19:33:26 +02:00
|
|
|
range_list = Enumerable(int, list(range(0, 100)))
|
2022-09-12 17:58:48 +02:00
|
|
|
selected_range = range_list.select(lambda x: [x, x])
|
|
|
|
|
2022-09-13 19:33:26 +02:00
|
|
|
self.assertEqual(selected_range.to_list(), [[x, x] for x in range(0, 100)])
|
2022-09-12 17:58:48 +02:00
|
|
|
self.assertEqual(selected_range.select_many(lambda x: x).to_list(), [_x for _l in [2 * [x] for x in range(0, 100)] for _x in _l])
|
|
|
|
|
|
|
|
class TestClass:
|
|
|
|
def __init__(self, i, is_sub=False):
|
|
|
|
self.i = i
|
|
|
|
if is_sub:
|
|
|
|
return
|
|
|
|
self.elements = [TestClass(x, True) for x in range(0, 10)]
|
|
|
|
|
2022-09-13 19:33:26 +02:00
|
|
|
elements = Enumerable(TestClass, [TestClass(i) for i in range(0, 100)])
|
2022-09-12 17:58:48 +02:00
|
|
|
selected_elements = elements.select_many(lambda x: x.elements).select(lambda x: x.i)
|
|
|
|
self.assertEqual(selected_elements.where(lambda x: x == 0).count(), 100)
|
|
|
|
|
2021-07-25 19:15:02 +02:00
|
|
|
def test_single(self):
|
2021-07-26 15:32:28 +02:00
|
|
|
res = self._tests.where(lambda u: u.address.nr == self._t_user.address.nr)
|
|
|
|
s_res = self._tests.where(lambda u: u.address.nr == self._t_user.address.nr).single()
|
2021-07-25 19:15:02 +02:00
|
|
|
|
|
|
|
self.assertEqual(len(res), 1)
|
|
|
|
self.assertEqual(self._t_user, s_res)
|
|
|
|
|
|
|
|
def test_single_or_default(self):
|
2021-07-26 15:32:28 +02:00
|
|
|
res = self._tests.where(lambda u: u.address.nr == self._t_user.address.nr)
|
|
|
|
s_res = self._tests.where(lambda u: u.address.nr == self._t_user.address.nr).single_or_default()
|
|
|
|
sn_res = self._tests.where(lambda u: u.address.nr == self._t_user.address.nr + 1).single_or_default()
|
2021-07-25 19:15:02 +02:00
|
|
|
|
|
|
|
self.assertEqual(len(res), 1)
|
|
|
|
self.assertEqual(self._t_user, s_res)
|
|
|
|
self.assertIsNone(sn_res)
|
|
|
|
|
2021-07-27 13:57:42 +02:00
|
|
|
def test_skip(self):
|
2022-09-13 19:33:26 +02:00
|
|
|
skipped = self._tests.skip(5).to_list()
|
2021-07-27 13:57:42 +02:00
|
|
|
|
2022-09-13 19:33:26 +02:00
|
|
|
self.assertEqual(len(skipped), len(self._tests) - 5)
|
|
|
|
self.assertEqual(skipped, self._tests.to_list()[5:])
|
2021-07-27 13:57:42 +02:00
|
|
|
|
|
|
|
def test_skip_last(self):
|
|
|
|
skipped = self._tests.skip_last(5)
|
|
|
|
|
2022-09-13 19:33:26 +02:00
|
|
|
self.assertEqual(skipped.count(), len(self._tests) - 5)
|
|
|
|
self.assertEqual(skipped.to_list(), self._tests.to_list()[:-5])
|
|
|
|
self.assertEqual(skipped.last(), self._tests.to_list()[:-5][len(self._tests.to_list()[:-5]) - 1])
|
2021-07-27 13:57:42 +02:00
|
|
|
|
|
|
|
def test_sum(self):
|
|
|
|
res = self._tests.sum(lambda u: u.address.nr)
|
|
|
|
|
|
|
|
s_res = 0
|
|
|
|
for user in self._tests:
|
|
|
|
s_res += user.address.nr
|
|
|
|
|
|
|
|
self.assertEqual(s_res, res)
|
|
|
|
|
2022-09-13 19:33:26 +02:00
|
|
|
tests = Enumerable(values=list(range(0, 100)))
|
2021-07-27 13:57:42 +02:00
|
|
|
self.assertEqual(0, tests.min())
|
|
|
|
|
|
|
|
def invalid():
|
2022-09-13 19:33:26 +02:00
|
|
|
tests2 = Enumerable(str, ['hello', 'world'])
|
2021-07-27 13:57:42 +02:00
|
|
|
e_res = tests2.average()
|
|
|
|
|
|
|
|
self.assertRaises(InvalidTypeException, invalid)
|
|
|
|
|
|
|
|
def test_take(self):
|
|
|
|
skipped = self._tests.take(5)
|
|
|
|
|
2022-09-13 19:33:26 +02:00
|
|
|
self.assertEqual(skipped.count(), 5)
|
|
|
|
self.assertEqual(skipped.to_list(), self._tests.to_list()[:5])
|
2021-07-27 13:57:42 +02:00
|
|
|
|
|
|
|
def test_take_last(self):
|
|
|
|
skipped = self._tests.take_last(5)
|
|
|
|
|
2022-09-13 19:33:26 +02:00
|
|
|
self.assertEqual(skipped.count(), 5)
|
|
|
|
self.assertEqual(skipped.to_list(), self._tests.to_list()[-5:])
|
|
|
|
self.assertEqual(skipped.last(), self._tests.to_list()[len(self._tests) - 1])
|
2021-07-27 13:57:42 +02:00
|
|
|
|
2021-07-25 19:15:02 +02:00
|
|
|
def test_where(self):
|
|
|
|
results = []
|
|
|
|
for user in self._tests:
|
|
|
|
if user.address.nr == 5:
|
|
|
|
results.append(user)
|
|
|
|
|
2021-07-26 15:32:28 +02:00
|
|
|
res = self._tests.where(lambda u: u.address.nr == 5)
|
2021-07-25 19:15:02 +02:00
|
|
|
self.assertEqual(len(results), len(res))
|
2021-07-27 09:41:51 +02:00
|
|
|
|
|
|
|
def ex():
|
|
|
|
e_res = self._tests.where(None)
|
|
|
|
|
|
|
|
self.assertRaises(ArgumentNoneException, ex)
|