Skip to content

Commit

Permalink
add motherduck tests (passing)
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronsteers committed Aug 26, 2023
1 parent 6907b06 commit 6c69415
Showing 1 changed file with 44 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@
# Copyright (c) 2023 Airbyte, Inc., all rights reserved.
#

from __future__ import annotations

import json
import os
from pathlib import Path
import random
import string
import tempfile
Expand All @@ -27,23 +30,48 @@
)
from destination_duckdb import DestinationDuckdb

SECRETS_CONFIG_PATH = "secrets/config.json"

@pytest.fixture(autouse=True)
def disable_destination_modification(monkeypatch, request):
if "disable_autouse" in request.keywords:
def pytest_generate_tests(metafunc):
if "config" not in metafunc.fixturenames:
return

configs: list[str] = ["local_file_config"]
if Path(SECRETS_CONFIG_PATH).is_file():
configs.append("motherduck_config")
else:
monkeypatch.setattr(DestinationDuckdb, "_get_destination_path", lambda _, x: x)
print(f"Skipping MotherDuck tests because config file not found at: {SECRETS_CONFIG_PATH}")

# for test_name in ["test_check_succeeds", "test_write"]:
metafunc.parametrize("config", configs, indirect=True)

@pytest.fixture(scope="module")
def local_file_config() -> Dict[str, str]:

@pytest.fixture
def config(request) -> Dict[str, str]:
# create a file "myfile" in "mydir" in temp directory
tmp_dir = tempfile.TemporaryDirectory()
test = os.path.join(str(tmp_dir.name), "test.duckdb")
if request.param == "local_file_config":
tmp_dir = tempfile.TemporaryDirectory()
test = os.path.join(str(tmp_dir.name), "test.duckdb")
yield {"destination_path": test}

# f1.write_text("text to myfile")
yield {"destination_path": test}
elif request.param == "motherduck_config":
yield json.loads(Path(SECRETS_CONFIG_PATH).read_text())

else:
raise ValueError(f"Unknown config type: {request.param}")


@pytest.fixture
def invalid_config() -> Dict[str, str]:
return {"destination_path": "/destination.duckdb"}


@pytest.fixture(autouse=True)
def disable_destination_modification(monkeypatch, request):
if "disable_autouse" in request.keywords:
return
else:
monkeypatch.setattr(DestinationDuckdb, "_get_destination_path", lambda _, x: x)


@pytest.fixture(scope="module")
Expand Down Expand Up @@ -71,11 +99,6 @@ def configured_catalogue(test_table_name: str, table_schema: str) -> ConfiguredA
return ConfiguredAirbyteCatalog(streams=[append_stream])


@pytest.fixture
def invalid_config() -> Dict[str, str]:
return {"destination_path": "/destination.duckdb"}


@pytest.fixture
def airbyte_message1(test_table_name: str):
return AirbyteMessage(
Expand All @@ -101,18 +124,17 @@ def airbyte_message3():
return AirbyteMessage(type=Type.STATE, state=AirbyteStateMessage(data={"state": "1"}))


@pytest.mark.parametrize("config", ["invalid_config"])
@pytest.mark.disable_autouse
def test_check_fails(config, request):
config = request.getfixturevalue(config)
def test_check_fails(invalid_config, request):
destination = DestinationDuckdb()
status = destination.check(logger=MagicMock(), config=config)
status = destination.check(logger=MagicMock(), config=invalid_config)
assert status.status == Status.FAILED


@pytest.mark.parametrize("config", ["local_file_config"])
def test_check_succeeds(config, request):
config = request.getfixturevalue(config)
def test_check_succeeds(
config: dict[str, str],
request,
):
destination = DestinationDuckdb()
status = destination.check(logger=MagicMock(), config=config)
assert status.status == Status.SUCCEEDED
Expand All @@ -122,7 +144,6 @@ def _state(data: Dict[str, Any]) -> AirbyteMessage:
return AirbyteMessage(type=Type.STATE, state=AirbyteStateMessage(data=data))


@pytest.mark.parametrize("config", ["local_file_config"])
def test_write(
config: Dict[str, str],
request,
Expand All @@ -132,7 +153,6 @@ def test_write(
airbyte_message3: AirbyteMessage,
test_table_name: str,
):
config = request.getfixturevalue(config)
destination = DestinationDuckdb()
generator = destination.write(config, configured_catalogue, [airbyte_message1, airbyte_message2, airbyte_message3])

Expand Down

0 comments on commit 6c69415

Please sign in to comment.