Skip to content

Commit 213022f

Browse files
committed
tests: move util function tests to a new test file
1 parent 2fe126e commit 213022f

2 files changed

Lines changed: 113 additions & 60 deletions

File tree

tests/test_smart_query.py

Lines changed: 0 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
from sqlactive import JOINED, SELECT_IN, SUBQUERY
1212
from sqlactive.conn import DBConnection
1313
from sqlactive.exceptions import (
14-
InvalidJoinMethodError,
1514
NoColumnOrHybridPropertyError,
1615
NoFilterableError,
1716
NoSortableError,
@@ -483,40 +482,6 @@ async def test_eager_expr(self):
483482
self.assertEqual('Bob28', expected_users[0]['username'])
484483
self.assertEqual(4, expected_users[0]['posts'][0]['rating'])
485484

486-
def test_flatten_filter_keys(self):
487-
"""Test for ``_flatten_filter_keys`` function."""
488-
logger.info('Testing "_flatten_filter_keys" function...')
489-
filter_keys = list(
490-
SmartQueryMixin._flatten_filter_keys(
491-
{
492-
or_: {
493-
'id__gt': 1000,
494-
and_: {
495-
'id__lt': 500,
496-
'related___property__in': (1, 2, 3),
497-
},
498-
}
499-
}
500-
)
501-
)
502-
self.assertCountEqual(
503-
['id__gt', 'id__lt', 'related___property__in'], filter_keys
504-
)
505-
filter_keys = list(
506-
SmartQueryMixin._flatten_filter_keys(
507-
[{'id__lt': 500}, {'related___property__in': (1, 2, 3)}]
508-
)
509-
)
510-
self.assertCountEqual(
511-
['id__lt', 'related___property__in'], filter_keys
512-
)
513-
with self.assertRaises(TypeError):
514-
filter_keys = list(
515-
SmartQueryMixin._flatten_filter_keys(
516-
{or_: {'id__gt': 1000}, and_: True}
517-
)
518-
)
519-
520485
def test_make_aliases_from_attrs(self):
521486
"""Test for ``_make_aliases_from_attrs`` function."""
522487
logger.info('Testing "_make_aliases_from_attrs" function...')
@@ -644,28 +609,3 @@ def test_group_query(self):
644609
root_cls=Post,
645610
aliases=aliases,
646611
)
647-
648-
async def test_eager_expr_from_schema(self):
649-
"""Test for ``_eager_expr_from_schema`` function."""
650-
logger.info('Testing "_eager_expr_from_schema" function...')
651-
schema = {
652-
Post.user: JOINED,
653-
Post.comments: (SUBQUERY, {Comment.user: SELECT_IN}),
654-
}
655-
eager_expr = SmartQueryMixin._eager_expr_from_schema(schema)
656-
post1 = await Post.options(*eager_expr).limit(1).unique_one()
657-
self.assertEqual('Bob Williams', post1.user.name)
658-
self.assertEqual('Bob Williams', post1.comments[0].user.name)
659-
660-
schema = {Post.user: JOINED, Post.comments: {Comment.user: SELECT_IN}}
661-
eager_expr = SmartQueryMixin._eager_expr_from_schema(schema)
662-
post2 = await Post.options(*eager_expr).limit(1).unique_one()
663-
self.assertEqual('Bob Williams', post2.user.name)
664-
self.assertEqual('Bob Williams', post2.comments[0].user.name)
665-
666-
with self.assertRaises(InvalidJoinMethodError):
667-
schema = {
668-
Post.user: JOINED,
669-
Post.comments: (SUBQUERY, {Comment.user: 'UNKNOWN'}),
670-
}
671-
SmartQueryMixin._eager_expr_from_schema(schema)

tests/test_utils.py

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
import asyncio
2+
import unittest
3+
4+
from sqlalchemy.sql import func, select
5+
from sqlalchemy.sql.operators import and_, or_
6+
7+
from sqlactive import JOINED, SELECT_IN, SUBQUERY
8+
from sqlactive.conn import DBConnection
9+
from sqlactive.utils import (
10+
eager_expr_from_schema,
11+
flatten_nested_filter_keys,
12+
get_query_root_cls,
13+
)
14+
15+
from ._logger import logger
16+
from ._models import BaseModel, Comment, Post
17+
from ._seed import Seed
18+
19+
20+
class TestSmartQueryMixin(unittest.IsolatedAsyncioTestCase):
21+
"""Tests for ``sqlactive.smart_query.SmartQueryMixin``."""
22+
23+
DB_URL = 'sqlite+aiosqlite://'
24+
25+
@classmethod
26+
def setUpClass(cls):
27+
logger.info('***** SmartQueryMixin tests *****')
28+
logger.info('Creating DB connection...')
29+
cls.conn = DBConnection(cls.DB_URL, echo=False)
30+
seed = Seed(cls.conn, BaseModel)
31+
asyncio.run(seed.run())
32+
33+
@classmethod
34+
def tearDownClass(cls):
35+
if hasattr(cls, 'conn'):
36+
logger.info('Closing DB connection...')
37+
asyncio.run(cls.conn.close(BaseModel))
38+
39+
def test_get_query_root_cls(self):
40+
"""Test for ``get_query_root_cls`` function."""
41+
logger.info('Testing "get_query_root_cls" function...')
42+
query = Post.query
43+
self.assertEqual(Post, get_query_root_cls(query))
44+
query = Post.query.join(Comment)
45+
self.assertEqual(Post, get_query_root_cls(query))
46+
query = select(Post.id)
47+
self.assertEqual(Post, get_query_root_cls(query))
48+
query = select(Post.id, Comment.id)
49+
self.assertEqual(Post, get_query_root_cls(query))
50+
query = select(func.count(Post.id))
51+
self.assertEqual(Post, get_query_root_cls(query))
52+
query = select(func.count('*'))
53+
self.assertIsNone(get_query_root_cls(query))
54+
query = select(func.count('*'))
55+
with self.assertRaises(ValueError):
56+
get_query_root_cls(query, raise_on_none=True)
57+
58+
def test_flatten_filter_keys(self):
59+
"""Test for ``flatten_filter_keys`` function."""
60+
logger.info('Testing "flatten_filter_keys" function...')
61+
filter_keys = list(
62+
flatten_nested_filter_keys(
63+
{
64+
or_: {
65+
'id__gt': 1000,
66+
and_: {
67+
'id__lt': 500,
68+
'related___property__in': (1, 2, 3),
69+
},
70+
}
71+
}
72+
)
73+
)
74+
self.assertCountEqual(
75+
['id__gt', 'id__lt', 'related___property__in'], filter_keys
76+
)
77+
filter_keys = list(
78+
flatten_nested_filter_keys(
79+
[{'id__lt': 500}, {'related___property__in': (1, 2, 3)}]
80+
)
81+
)
82+
self.assertCountEqual(
83+
['id__lt', 'related___property__in'], filter_keys
84+
)
85+
with self.assertRaises(TypeError):
86+
filter_keys = list(
87+
flatten_nested_filter_keys({or_: {'id__gt': 1000}, and_: True})
88+
)
89+
90+
async def test_eager_expr_from_schema(self):
91+
"""Test for ``eager_expr_from_schema`` function."""
92+
logger.info('Testing "eager_expr_from_schema" function...')
93+
schema = {
94+
Post.user: JOINED,
95+
Post.comments: (SUBQUERY, {Comment.user: SELECT_IN}),
96+
}
97+
eager_expr = eager_expr_from_schema(schema)
98+
post1 = await Post.options(*eager_expr).limit(1).unique_one()
99+
self.assertEqual('Bob Williams', post1.user.name)
100+
self.assertEqual('Bob Williams', post1.comments[0].user.name)
101+
102+
schema = {Post.user: JOINED, Post.comments: {Comment.user: SELECT_IN}}
103+
eager_expr = eager_expr_from_schema(schema)
104+
post2 = await Post.options(*eager_expr).limit(1).unique_one()
105+
self.assertEqual('Bob Williams', post2.user.name)
106+
self.assertEqual('Bob Williams', post2.comments[0].user.name)
107+
108+
with self.assertRaises(ValueError):
109+
schema = {
110+
Post.user: JOINED,
111+
Post.comments: (SUBQUERY, {Comment.user: 'UNKNOWN'}),
112+
}
113+
eager_expr_from_schema(schema)

0 commit comments

Comments
 (0)