|
6 | 6 | import orjson |
7 | 7 |
|
8 | 8 | from airbyte_cdk import Connector |
| 9 | +from airbyte_cdk.models import ( |
| 10 | + Status, |
| 11 | +) |
9 | 12 | from airbyte_cdk.sources.abstract_source import AbstractSource |
10 | 13 | from airbyte_cdk.sources.declarative.declarative_source import DeclarativeSource |
11 | 14 | from airbyte_cdk.test import entrypoint_wrapper |
@@ -59,6 +62,7 @@ def run_test_job( |
59 | 62 |
|
60 | 63 | catalog_path: Path | None = None |
61 | 64 | if verb not in ["discover", "check"]: |
| 65 | + # We need a catalog for read. |
62 | 66 | if catalog: |
63 | 67 | # Write the catalog to a temp json file and pass the path to the file as an argument. |
64 | 68 | catalog_path = ( |
@@ -89,7 +93,36 @@ def run_test_job( |
89 | 93 | ) |
90 | 94 | ) |
91 | 95 |
|
92 | | - if test_instance.expect_exception and not result.errors: |
93 | | - raise AssertionError("Expected exception but got none.") # noqa: TRY003 |
| 96 | + if verb == "check": |
| 97 | + # Check is expected to fail gracefully without an exception. |
| 98 | + # Instead, we assert that we have a CONNECTION_STATUS message with |
| 99 | + # a failure status. |
| 100 | + assert not result.errors, "Expected no errors from check. Got:\n" + "\n".join( |
| 101 | + [str(error) for error in result.errors] |
| 102 | + ) |
| 103 | + assert len(result.connection_status_messages) == 1, ( |
| 104 | + "Expected exactly one CONNECTION_STATUS message. Got " |
| 105 | + f"{len(result.connection_status_messages)}:\n" |
| 106 | + + "\n".join(result.connection_status_messages) |
| 107 | + ) |
| 108 | + if test_instance.expect_exception: |
| 109 | + assert result.connection_status_messages[0].connectionStatus.status == Status.FAILED, ( |
| 110 | + "Expected CONNECTION_STATUS message to be FAILED. Got: \n" |
| 111 | + + "\n".join([str(result.connection_status_messages)]) |
| 112 | + ) |
| 113 | + return result |
| 114 | + |
| 115 | + # For all other verbs, we assert check that an exception is raised (or not). |
| 116 | + if test_instance.expect_exception: |
| 117 | + if not result.errors: |
| 118 | + raise AssertionError("Expected exception but got none.") |
| 119 | + |
| 120 | + return result |
| 121 | + if result.errors: |
| 122 | + raise AssertionError( |
| 123 | + "\n\n".join( |
| 124 | + [str(err.trace.error).replace("\\n", "\n") for err in result.errors], |
| 125 | + ) |
| 126 | + ) |
94 | 127 |
|
95 | 128 | return result |
0 commit comments