|
1 | 1 | from unittest import mock
|
2 | 2 |
|
3 | 3 | import pytest
|
| 4 | +import sqlalchemy.exc |
| 5 | +import sqlalchemy.orm.exc |
4 | 6 |
|
5 | 7 | from graphene import (Dynamic, Field, GlobalID, Int, List, Node, NonNull,
|
6 | 8 | ObjectType, Schema, String)
|
7 | 9 | from graphene.relay import Connection
|
8 | 10 |
|
| 11 | +from .. import utils |
9 | 12 | from ..converter import convert_sqlalchemy_composite
|
10 | 13 | from ..fields import (SQLAlchemyConnectionField,
|
11 | 14 | UnsortedSQLAlchemyConnectionField, createConnectionField,
|
@@ -492,3 +495,65 @@ class Meta:
|
492 | 495 | def test_deprecated_createConnectionField():
|
493 | 496 | with pytest.warns(DeprecationWarning):
|
494 | 497 | createConnectionField(None)
|
| 498 | + |
| 499 | + |
| 500 | +@mock.patch(utils.__name__ + '.class_mapper') |
| 501 | +def test_unique_errors_propagate(class_mapper_mock): |
| 502 | + # Define unique error to detect |
| 503 | + class UniqueError(Exception): |
| 504 | + pass |
| 505 | + |
| 506 | + # Mock class_mapper effect |
| 507 | + class_mapper_mock.side_effect = UniqueError |
| 508 | + |
| 509 | + # Make sure that errors are propagated from class_mapper when instantiating new classes |
| 510 | + error = None |
| 511 | + try: |
| 512 | + class ArticleOne(SQLAlchemyObjectType): |
| 513 | + class Meta(object): |
| 514 | + model = Article |
| 515 | + except UniqueError as e: |
| 516 | + error = e |
| 517 | + |
| 518 | + # Check that an error occured, and that it was the unique error we gave |
| 519 | + assert error is not None |
| 520 | + assert isinstance(error, UniqueError) |
| 521 | + |
| 522 | + |
| 523 | +@mock.patch(utils.__name__ + '.class_mapper') |
| 524 | +def test_argument_errors_propagate(class_mapper_mock): |
| 525 | + # Mock class_mapper effect |
| 526 | + class_mapper_mock.side_effect = sqlalchemy.exc.ArgumentError |
| 527 | + |
| 528 | + # Make sure that errors are propagated from class_mapper when instantiating new classes |
| 529 | + error = None |
| 530 | + try: |
| 531 | + class ArticleTwo(SQLAlchemyObjectType): |
| 532 | + class Meta(object): |
| 533 | + model = Article |
| 534 | + except sqlalchemy.exc.ArgumentError as e: |
| 535 | + error = e |
| 536 | + |
| 537 | + # Check that an error occured, and that it was the unique error we gave |
| 538 | + assert error is not None |
| 539 | + assert isinstance(error, sqlalchemy.exc.ArgumentError) |
| 540 | + |
| 541 | + |
| 542 | +@mock.patch(utils.__name__ + '.class_mapper') |
| 543 | +def test_unmapped_errors_reformat(class_mapper_mock): |
| 544 | + # Mock class_mapper effect |
| 545 | + class_mapper_mock.side_effect = sqlalchemy.orm.exc.UnmappedClassError(object) |
| 546 | + |
| 547 | + # Make sure that errors are propagated from class_mapper when instantiating new classes |
| 548 | + error = None |
| 549 | + try: |
| 550 | + class ArticleThree(SQLAlchemyObjectType): |
| 551 | + class Meta(object): |
| 552 | + model = Article |
| 553 | + except ValueError as e: |
| 554 | + error = e |
| 555 | + |
| 556 | + # Check that an error occured, and that it was the unique error we gave |
| 557 | + assert error is not None |
| 558 | + assert isinstance(error, ValueError) |
| 559 | + assert "You need to pass a valid SQLAlchemy Model" in str(error) |
0 commit comments