Repository navigation
Expand file tree
/
Copy pathtest_streaming.py
More file actions
54 lines (43 loc) · 1.78 KB
/
Copy pathtest_streaming.py
File metadata and controls
54 lines (43 loc) · 1.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import pytest
from sqlalchemy import select
from sqlalchemy.exc import InvalidRequestError
from sqlalchemy.orm import Session, joinedload, selectinload, subqueryload
from demo_models import User, new_engine
from demo_queries import seed
@pytest.fixture
def engine():
engine = new_engine()
try:
with Session(engine) as session, session.begin():
seed(session)
yield engine
finally:
engine.dispose()
def test_partitions_preserve_all_names(engine):
with Session(engine) as session:
result = session.scalars(select(User).order_by(User.id).execution_options(yield_per=1))
names = []
for batch in result.partitions():
assert len(batch) == 1
names.extend(user.name for user in batch)
assert names == ["alice", "bob"]
def test_selectin_with_partitions_on_sqlite(engine):
with Session(engine) as session:
result = session.scalars(
select(User).options(selectinload(User.posts)).order_by(User.id)
.execution_options(yield_per=1)
)
observed = [(user.name, len(user.posts)) for batch in result.partitions() for user in batch]
assert observed == [("alice", 2), ("bob", 0)]
@pytest.mark.parametrize("loader", [joinedload, subqueryload])
def test_collection_loaders_reject_yield_per(engine, loader):
with Session(engine) as session:
with pytest.raises(InvalidRequestError):
session.scalars(
select(User).options(loader(User.posts)).execution_options(yield_per=1)
).all()
def test_unique_rejects_yield_per(engine):
with Session(engine) as session:
result = session.scalars(select(User).execution_options(yield_per=1))
with pytest.raises(InvalidRequestError):
list(result.unique())