Skip to content

Commit 21a3b89

Browse files
author
Connor Brinton
committed
Don't suppress SQLAlchemy errors when mapping classes
1 parent 33d5b74 commit 21a3b89

File tree

2 files changed

+38
-3
lines changed

2 files changed

+38
-3
lines changed

Diff for: graphene_sqlalchemy/tests/test_types.py

+33
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import six
55
from promise import Promise
66

7+
from .. import utils
78
from ..registry import Registry
89
from ..types import SQLAlchemyObjectType, SQLAlchemyObjectTypeOptions
910
from .models import Article, Reporter
@@ -181,3 +182,35 @@ class Meta:
181182
resolver, TestConnection, ReporterWithCustomOptions, None, None
182183
)
183184
assert result is not None
185+
186+
187+
def test_errors_propagated():
188+
# Get current `class_mapper` value
189+
old_class_mapper = utils.class_mapper
190+
191+
try:
192+
# Define unique error to detect
193+
class UniqueError(Exception):
194+
pass
195+
196+
# Redefine `class_mapper` in utils
197+
def new_class_mapper(*args, **kwargs):
198+
raise UniqueError()
199+
utils.class_mapper = new_class_mapper
200+
201+
# Make sure that errors are propagated from class_mapper when instantiating classes
202+
error = None
203+
try:
204+
class Tree(SQLAlchemyObjectType):
205+
class Meta(object):
206+
model = Article
207+
208+
except UniqueError as e:
209+
error = e
210+
211+
# Check that an error occured, and that it was a SQLAlchemy error
212+
assert error is not None
213+
assert isinstance(error, UniqueError)
214+
finally:
215+
# Restore original class mapper
216+
utils.class_mapper = old_class_mapper

Diff for: graphene_sqlalchemy/types.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -103,9 +103,11 @@ def __init_subclass_with_meta__(
103103
_meta=None,
104104
**options
105105
):
106-
assert is_mapped_class(model), (
107-
"You need to pass a valid SQLAlchemy Model in " '{}.Meta, received "{}".'
108-
).format(cls.__name__, model)
106+
# Make sure model is a valid SQLAlchemy model
107+
if not is_mapped_class(model):
108+
raise ValueError(
109+
"You need to pass a valid SQLAlchemy Model in " '{}.Meta, received "{}".'.format(cls.__name__, model)
110+
)
109111

110112
if not registry:
111113
registry = get_global_registry()

0 commit comments

Comments
 (0)