Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
153 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import pytest | ||
|
||
from .models import db, User | ||
|
||
pytestmark = pytest.mark.asyncio | ||
|
||
|
||
@pytest.fixture | ||
async def names(sa_engine): | ||
rv = {'11', '22', '33'} | ||
sa_engine.execute(User.__table__.insert(), | ||
[dict(nickname=name) for name in rv]) | ||
yield rv | ||
sa_engine.execute('DELETE FROM gino_users') | ||
|
||
|
||
# noinspection PyUnusedLocal,PyShadowingNames | ||
async def test_bind(bind, names): | ||
with pytest.raises(ValueError, match='No Connection in context'): | ||
async for u in User.query.gino.iterate(): | ||
assert False, 'Should not reach here' | ||
with pytest.raises(ValueError, match='No Connection in context'): | ||
await User.query.gino.iterate() | ||
with pytest.raises(ValueError, match='No Connection in context'): | ||
await db.iterate(User.query) | ||
|
||
result = set() | ||
async with bind.transaction(): | ||
async for u in User.query.gino.iterate(): | ||
result.add(u.nickname) | ||
assert names == result | ||
|
||
result = set() | ||
async with bind.transaction(): | ||
async for u in db.iterate(User.query): | ||
result.add(u.nickname) | ||
assert names == result | ||
|
||
result = set() | ||
async with bind.transaction(): | ||
cursor = await User.query.gino.iterate() | ||
result.add((await cursor.next()).nickname) | ||
assert names != result | ||
result.update([u.nickname for u in await cursor.many(1)]) | ||
assert names != result | ||
result.update([u.nickname for u in await cursor.many(2)]) | ||
assert names == result | ||
result.update([u.nickname for u in await cursor.many(2)]) | ||
assert names == result | ||
assert await cursor.next() is None | ||
|
||
with pytest.raises(ValueError, match='too many multiparams'): | ||
async with bind.transaction(): | ||
await db.iterate(User.insert().returning(User.nickname), [ | ||
dict(nickname='444'), | ||
dict(nickname='555'), | ||
dict(nickname='666'), | ||
]) | ||
|
||
|
||
# noinspection PyUnusedLocal,PyShadowingNames | ||
async def test_basic(engine, names): | ||
result = set() | ||
async with engine.transaction() as tx: | ||
with pytest.raises(ValueError, match='No Connection in context'): | ||
async for u in User.query.gino.iterate(): | ||
assert False, 'Should not reach here' | ||
with pytest.raises(ValueError, match='No Connection in context'): | ||
await db.iterate(User.query) | ||
result = set() | ||
async for u in User.query.gino.iterate(connection=tx.connection): | ||
result.add(u.nickname) | ||
assert names == result | ||
|
||
result = set() | ||
cursor = await User.query.gino.iterate(connection=tx.connection) | ||
result.update([u.nickname for u in await cursor.many(2)]) | ||
assert names != result | ||
result.update([u.nickname for u in await cursor.many(2)]) | ||
assert names == result |