|
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 |
|
| 10 | +from .. import utils |
7 | 11 | from ..registry import Registry
|
8 | 12 | from ..types import SQLAlchemyObjectType, SQLAlchemyObjectTypeOptions
|
9 | 13 | from .models import Article, Reporter
|
@@ -181,3 +185,65 @@ class Meta:
|
181 | 185 | resolver, TestConnection, ReporterWithCustomOptions, None, None
|
182 | 186 | )
|
183 | 187 | assert result is not None
|
| 188 | + |
| 189 | + |
| 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 |
| 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 |
| 239 | + try: |
| 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