Skip to content

Commit 3aef427

Browse files
committed
fix #307, explicit error when bind is None
1 parent b202734 commit 3aef427

File tree

5 files changed

+37
-5
lines changed

5 files changed

+37
-5
lines changed

gino/api.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
from .crud import CRUDModel
99
from .declarative import declarative_base, declared_attr
10+
from .exceptions import UninitializedError
1011
from .schema import GinoSchemaVisitor, patch_schema
1112
from . import json_support
1213

@@ -361,6 +362,9 @@ def bind(self):
361362
:class:`~sqlalchemy.engine.url.URL`).
362363
363364
"""
365+
if self._bind is None:
366+
return _PlaceHolder(
367+
UninitializedError('Gino engine is not initialized.'))
364368
return self._bind
365369

366370
# noinspection PyMethodOverriding,PyAttributeOutsideInit
@@ -483,3 +487,20 @@ def transaction(self, *args, **kwargs):
483487
484488
"""
485489
return self.bind.transaction(*args, **kwargs)
490+
491+
492+
class _PlaceHolder:
493+
__slots__ = '_exception'
494+
495+
def __init__(self, exception):
496+
self._exception = exception
497+
498+
def __getattribute__(self, item):
499+
if item == '_exception':
500+
return super().__getattribute__(item)
501+
raise self._exception
502+
503+
def __setattr__(self, key, value):
504+
if key == '_exception':
505+
return super().__setattr__(key, value)
506+
raise UninitializedError(self._exception)

gino/exceptions.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,7 @@ class GinoException(Exception):
44

55
class NoSuchRowError(GinoException):
66
pass
7+
8+
9+
class UninitializedError(GinoException):
10+
pass

tests/test_bind.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import random
22

3+
from gino.exceptions import UninitializedError
34
import pytest
45

56
from .models import db, PG_URL, User
@@ -30,8 +31,11 @@ async def test_unbind(asyncpg_pool):
3031
await test_create(None)
3132
await db.pop_bind().close()
3233
db.bind = None
33-
with pytest.raises(AttributeError):
34+
with pytest.raises(UninitializedError):
3435
await test_create(None)
36+
# test proper exception when engine is not initialized
37+
with pytest.raises(UninitializedError):
38+
db.bind.first = lambda x: 1
3539

3640

3741
async def test_db_api(bind, random_name):

tests/test_engine.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@
33
from datetime import datetime
44

55
import asyncpg
6+
from asyncpg.exceptions import InvalidCatalogNameError
7+
from gino import UninitializedError
68
import pytest
7-
import sqlalchemy as sa
89
from sqlalchemy.exc import ObjectNotExecutableError
9-
from asyncpg.exceptions import InvalidCatalogNameError
10+
import sqlalchemy as sa
1011

1112
from .models import db, User, PG_URL, qsize
1213

@@ -181,7 +182,8 @@ async def test_async_metadata():
181182
db_ = await gino.Gino(PG_URL)
182183
assert isinstance((await db_.scalar('select now()')), datetime)
183184
await db_.pop_bind().close()
184-
assert db.bind is None
185+
with pytest.raises(UninitializedError):
186+
db.bind.first()
185187

186188

187189
# noinspection PyUnreachableCode

tests/test_iterate.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from gino import UninitializedError
12
import pytest
23

34
from .models import db, User
@@ -74,7 +75,7 @@ async def test_bind(bind, names):
7475
async def test_basic(engine, names):
7576
result = set()
7677
async with engine.transaction() as tx:
77-
with pytest.raises(AttributeError, match='iterate'):
78+
with pytest.raises(UninitializedError):
7879
await db.iterate(User.query)
7980
result = set()
8081
async for u in tx.connection.iterate(User.query):

0 commit comments

Comments
 (0)