|
1 | 1 | from collections import OrderedDict
|
2 | 2 | from graphene import Field, Int, Interface, ObjectType
|
3 | 3 | from graphene.relay import Node, is_node, Connection
|
| 4 | +import mock |
4 | 5 | import six
|
| 6 | +import sqlalchemy.exc |
| 7 | +import sqlalchemy.orm.exc |
5 | 8 | from promise import Promise
|
6 | 9 |
|
7 | 10 | from .. import utils
|
@@ -184,33 +187,63 @@ class Meta:
|
184 | 187 | assert result is not None
|
185 | 188 |
|
186 | 189 |
|
187 |
| -def test_errors_propagated(): |
188 |
| - # Get current `class_mapper` value |
189 |
| - old_class_mapper = utils.class_mapper |
| 190 | +@mock.patch(utils.__name__ + '.class_mapper') |
| 191 | +def test_unique_errors_propagate(class_mapper_mock): |
| 192 | + # Define unique error to detect |
| 193 | + class UniqueError(Exception): |
| 194 | + pass |
190 | 195 |
|
| 196 | + # Mock class_mapper effect |
| 197 | + class_mapper_mock.side_effect = UniqueError |
| 198 | + |
| 199 | + # Make sure that errors are propagated from class_mapper when instantiating new classes |
| 200 | + error = None |
| 201 | + try: |
| 202 | + class ArticleOne(SQLAlchemyObjectType): |
| 203 | + class Meta(object): |
| 204 | + model = Article |
| 205 | + except UniqueError as e: |
| 206 | + error = e |
| 207 | + |
| 208 | + # Check that an error occured, and that it was the unique error we gave |
| 209 | + assert error is not None |
| 210 | + assert isinstance(error, UniqueError) |
| 211 | + |
| 212 | + |
| 213 | +@mock.patch(utils.__name__ + '.class_mapper') |
| 214 | +def test_argument_errors_propagate(class_mapper_mock): |
| 215 | + # Mock class_mapper effect |
| 216 | + class_mapper_mock.side_effect = sqlalchemy.exc.ArgumentError |
| 217 | + |
| 218 | + # Make sure that errors are propagated from class_mapper when instantiating new classes |
| 219 | + error = None |
| 220 | + try: |
| 221 | + class ArticleTwo(SQLAlchemyObjectType): |
| 222 | + class Meta(object): |
| 223 | + model = Article |
| 224 | + except sqlalchemy.exc.ArgumentError as e: |
| 225 | + error = e |
| 226 | + |
| 227 | + # Check that an error occured, and that it was the unique error we gave |
| 228 | + assert error is not None |
| 229 | + assert isinstance(error, sqlalchemy.exc.ArgumentError) |
| 230 | + |
| 231 | + |
| 232 | +@mock.patch(utils.__name__ + '.class_mapper') |
| 233 | +def test_unmapped_errors_reformat(class_mapper_mock): |
| 234 | + # Mock class_mapper effect |
| 235 | + class_mapper_mock.side_effect = sqlalchemy.orm.exc.UnmappedClassError(object) |
| 236 | + |
| 237 | + # Make sure that errors are propagated from class_mapper when instantiating new classes |
| 238 | + error = None |
191 | 239 | 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 |
| 240 | + class ArticleThree(SQLAlchemyObjectType): |
| 241 | + class Meta(object): |
| 242 | + model = Article |
| 243 | + except ValueError as e: |
| 244 | + error = e |
| 245 | + |
| 246 | + # Check that an error occured, and that it was the unique error we gave |
| 247 | + assert error is not None |
| 248 | + assert isinstance(error, ValueError) |
| 249 | + assert "You need to pass a valid SQLAlchemy Model" in str(error) |
0 commit comments