Skip to content

Commit fd0d2df

Browse files
authored
Merge pull request #365 from wwwjfy/alias-loader
add tests to demonstrate queries using alias
2 parents 1b89db8 + be1f37c commit fd0d2df

File tree

2 files changed

+49
-3
lines changed

2 files changed

+49
-3
lines changed

gino/loader.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
from sqlalchemy import select
44
from sqlalchemy.schema import Column
5+
from sqlalchemy.sql.elements import Label
56

67
from .declarative import Model
78

@@ -19,6 +20,8 @@ def get(cls, value):
1920
rv = AliasLoader(value)
2021
elif isinstance(value, Column):
2122
rv = ColumnLoader(value)
23+
elif isinstance(value, Label):
24+
rv = ColumnLoader(value.name)
2225
elif isinstance(value, tuple):
2326
rv = TupleLoader(value)
2427
elif callable(value):

tests/test_loader.py

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import random
22
from datetime import datetime
33

4-
import pytest
54
from async_generator import yield_, async_generator
5+
import pytest
6+
from sqlalchemy import select
7+
from sqlalchemy.sql.functions import count
68

79
from gino.loader import AliasLoader
810
from .models import db, User, Team, Company
@@ -12,11 +14,11 @@
1214

1315
@pytest.fixture
1416
@async_generator
15-
async def user(bind, random_name):
17+
async def user(bind):
1618
c = await Company.create()
1719
t1 = await Team.create(company_id=c.id)
1820
t2 = await Team.create(company_id=c.id, parent_id=t1.id)
19-
u = await User.create(nickname=random_name, team_id=t2.id)
21+
u = await User.create(team_id=t2.id)
2022
u.team = t2
2123
t2.parent = t1
2224
t2.company = c
@@ -161,6 +163,47 @@ async def test_alias_loader_columns(user):
161163
assert u.id is not None
162164

163165

166+
async def test_multiple_models_in_one_query(bind):
167+
for _ in range(3):
168+
await User.create()
169+
170+
ua1 = User.alias()
171+
ua2 = User.alias()
172+
join_query = select([ua1, ua2]).where(ua1.id < ua2.id)
173+
result = await join_query.gino.load((ua1.load('id'), ua2.load('id'))).all()
174+
assert len(result) == 3
175+
for u1, u2 in result:
176+
assert u1.id is not None
177+
assert u2.id is not None
178+
assert u1.id < u2.id
179+
180+
181+
async def test_loader_with_aggregation(user):
182+
count_col = count().label('count')
183+
user_count = select(
184+
[User.team_id, count_col]
185+
).group_by(
186+
User.team_id
187+
).alias()
188+
query = Team.outerjoin(user_count).select()
189+
result = await query.gino.load(
190+
(Team.id, Team.name, user_count.columns.team_id, count_col)
191+
).all()
192+
assert len(result) == 2
193+
# team 1 doesn't have users, team 2 has 1 user
194+
# third and forth columns are None for team 1
195+
for team_id, team_name, user_team_id, user_count in result:
196+
if team_id == user.team_id:
197+
assert team_name == user.team.name
198+
assert user_team_id == user.team_id
199+
assert user_count == 1
200+
else:
201+
assert team_id is not None
202+
assert team_name is not None
203+
assert user_team_id is None
204+
assert user_count is None
205+
206+
164207
async def test_adjacency_list_query_builder(user):
165208
group = Team.alias()
166209
u = await User.load(team=Team.load(parent=group.on(

0 commit comments

Comments
 (0)