sh_cpl/src/cpl_query/tests/query_test.py

188 lines
5.9 KiB
Python
Raw Normal View History

2021-07-25 19:15:02 +02:00
import string
import unittest
from random import randint
from cpl.utils import String
2021-07-27 09:41:51 +02:00
from cpl_query.exceptions import InvalidTypeException, WrongTypeException, ArgumentNoneException
2021-07-25 19:15:02 +02:00
from cpl_query.extension.list import List
from cpl_query.tests.models import User, Address
class QueryTest(unittest.TestCase):
def setUp(self) -> None:
2021-07-26 15:21:57 +02:00
self._tests = List(User)
2021-07-25 19:15:02 +02:00
self._t_user = User(
'Test user',
Address(
'teststr.',
15
)
)
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-25 19:15:02 +02:00
randint(0, 10)
)
)
self._tests.append(user)
self._tests.append(self._t_user)
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)
res = self._tests.average(int, lambda u: u.address.nr)
self.assertEqual(res, avg)
2021-07-27 09:41:51 +02:00
def invalid():
e_res = self._tests.average(str, lambda u: u.address.nr)
2021-07-27 09:26:30 +02:00
2021-07-27 09:41:51 +02:00
def wrong():
e_res = self._tests.average(int, lambda u: u.address.street)
2021-07-27 09:26:30 +02:00
2021-07-27 09:41:51 +02:00
self.assertRaises(InvalidTypeException, invalid)
self.assertRaises(WrongTypeException, wrong)
2021-07-27 09:26:30 +02:00
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-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))
self.assertIsNotNone(s_res)
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))
self.assertIsNotNone(s_res)
self.assertIsNone(sn_res)
def test_for_each(self):
users = []
self._tests.for_each(
lambda user: (
users.append(user)
)
)
self.assertEqual(len(users), len(self._tests))
2021-07-26 15:21:57 +02:00
def test_order_by(self):
res = self._tests.order_by(lambda user: user.address.street)
res2 = self._tests.order_by(lambda user: user.address.nr)
s_res = self._tests
s_res.sort(key=lambda user: user.address.street)
self.assertEqual(res, s_res)
s_res.sort(key=lambda user: user.address.nr)
self.assertEqual(res2, s_res)
def test_order_by_descending(self):
res = self._tests.order_by_descending(lambda user: user.address.street)
res2 = self._tests.order_by_descending(lambda user: user.address.nr)
s_res = self._tests
s_res.sort(key=lambda user: user.address.street, reverse=True)
self.assertEqual(res, s_res)
s_res.sort(key=lambda user: user.address.nr, reverse=True)
self.assertEqual(res2, s_res)
def test_then_by(self):
res = self._tests.order_by(lambda user: user.address.street[0]).then_by(lambda user: user.address.nr)
s_res = self._tests
s_res.sort(key=lambda user: (user.address.street[0], user.address.nr))
self.assertEqual(res, s_res)
def test_then_by_descending(self):
2021-07-26 15:32:28 +02:00
res = self._tests.order_by_descending(lambda user: user.address.street[0]).then_by_descending(
lambda user: user.address.nr)
2021-07-26 15:21:57 +02:00
s_res = self._tests
s_res.sort(key=lambda user: (user.address.street[0], user.address.nr), reverse=True)
self.assertEqual(res, s_res)
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)
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)