-
Notifications
You must be signed in to change notification settings - Fork 4.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
88 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
airbyte-integrations/connectors/destination-deepset/unit_tests/test_writer.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
# Copyright (c) 2024 Airbyte, Inc., all rights reserved. | ||
|
||
from __future__ import annotations | ||
|
||
import secrets | ||
from typing import TYPE_CHECKING, Any | ||
from uuid import UUID, uuid4 | ||
|
||
import pytest | ||
from airbyte_cdk.models import AirbyteLogMessage, AirbyteTraceMessage, Level, TraceType, Type | ||
from destination_deepset.api import APIError, DeepsetCloudApi | ||
from destination_deepset.models import DeepsetCloudConfig, DeepsetCloudFile | ||
from destination_deepset.writer import DeepsetCloudFileWriter | ||
|
||
if TYPE_CHECKING: | ||
from collections.abc import Mapping | ||
|
||
|
||
@pytest.fixture() | ||
def config_dict() -> Mapping[str, Any]: | ||
return { | ||
"api_key": secrets.token_urlsafe(16), | ||
"base_url": "https://api.dev.cloud.dpst.dev", | ||
"workspace": "airbyte-test", | ||
"retries": 5, | ||
} | ||
|
||
|
||
def test_factory(config_dict: Mapping[str, Any]) -> None: | ||
writer = DeepsetCloudFileWriter.factory(config_dict) | ||
|
||
assert isinstance(writer, DeepsetCloudFileWriter) | ||
assert isinstance(writer.client, DeepsetCloudApi) | ||
assert isinstance(writer.client.config, DeepsetCloudConfig) | ||
|
||
config = writer.client.config | ||
assert config.api_key == config_dict["api_key"] | ||
assert config.base_url == config_dict["base_url"] | ||
assert config.workspace == config_dict["workspace"] | ||
assert config.retries == config_dict["retries"] | ||
|
||
|
||
def test_write_happy_path( | ||
monkeypatch: pytest.MonkeyPatch, config_dict: Mapping[str, Any], file: DeepsetCloudFile | ||
) -> None: | ||
writer = DeepsetCloudFileWriter.factory(config_dict) | ||
|
||
def upload(file: DeepsetCloudFile) -> UUID: | ||
return uuid4() | ||
|
||
monkeypatch.setattr(writer.client, "upload", upload) | ||
|
||
message = writer.write(file) | ||
assert message.type == Type.LOG | ||
assert isinstance(message.log, AirbyteLogMessage) | ||
assert message.log.level == Level.INFO | ||
|
||
|
||
def test_write_returns_trace_message_if_error( | ||
monkeypatch: pytest.MonkeyPatch, config_dict: Mapping[str, Any], file: DeepsetCloudFile | ||
) -> None: | ||
writer = DeepsetCloudFileWriter.factory(config_dict) | ||
|
||
def upload(_: ...) -> UUID: | ||
raise APIError | ||
|
||
monkeypatch.setattr(writer.client, "upload", upload) | ||
|
||
message = writer.write(file) | ||
assert message.type == Type.TRACE | ||
assert isinstance(message.trace, AirbyteTraceMessage) | ||
assert message.trace.type == TraceType.ERROR |