Skip to content

Commit fcfe697

Browse files
committed
fix test handling for expected errors
1 parent a0f8ed5 commit fcfe697

File tree

3 files changed

+43
-3
lines changed

3 files changed

+43
-3
lines changed

airbyte_cdk/test/declarative/test_suites/source_base.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,10 @@ def test_basic_read(
5959
"discover",
6060
test_instance=instance,
6161
)
62-
assert discover_result.catalog, "Expected a non-empty catalog."
62+
if instance.expect_exception:
63+
assert discover_result.errors, "Expected exception but got none."
64+
return
65+
6366
configured_catalog = ConfiguredAirbyteCatalog(
6467
streams=[
6568
ConfiguredAirbyteStream(

airbyte_cdk/test/declarative/utils/job_runner.py

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
import orjson
77

88
from airbyte_cdk import Connector
9+
from airbyte_cdk.models import (
10+
Status,
11+
)
912
from airbyte_cdk.sources.abstract_source import AbstractSource
1013
from airbyte_cdk.sources.declarative.declarative_source import DeclarativeSource
1114
from airbyte_cdk.test import entrypoint_wrapper
@@ -59,6 +62,7 @@ def run_test_job(
5962

6063
catalog_path: Path | None = None
6164
if verb not in ["discover", "check"]:
65+
# We need a catalog for read.
6266
if catalog:
6367
# Write the catalog to a temp json file and pass the path to the file as an argument.
6468
catalog_path = (
@@ -89,7 +93,36 @@ def run_test_job(
8993
)
9094
)
9195

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+
)
94127

95128
return result

airbyte_cdk/test/entrypoint_wrapper.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,10 @@ def records(self) -> List[AirbyteMessage]:
8282
def state_messages(self) -> List[AirbyteMessage]:
8383
return self._get_message_by_types([Type.STATE])
8484

85+
@property
86+
def connection_status_messages(self) -> List[AirbyteMessage]:
87+
return self._get_message_by_types([Type.CONNECTION_STATUS])
88+
8589
@property
8690
def most_recent_state(self) -> Any:
8791
state_messages = self._get_message_by_types([Type.STATE])

0 commit comments

Comments
 (0)