Skip to content

Commit

Permalink
Use a custom test suite
Browse files Browse the repository at this point in the history
  • Loading branch information
edgarrmondragon committed Jul 24, 2024
1 parent f1bcf3c commit cbbf56d
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 41 deletions.
24 changes: 23 additions & 1 deletion tests/test_core.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,35 @@
"""Tests standard target features using the built-in SDK tests library."""

from pathlib import Path
from typing import Any, Dict

from singer_sdk.testing import get_target_test_class
from singer_sdk.testing.suites import TestSuite
from singer_sdk.testing.templates import TargetFileTestTemplate

from target_csv.target import TargetCSV

SAMPLE_CONFIG: Dict[str, Any] = {
"escape_character": '"',
}

TestTargetCSV = get_target_test_class(TargetCSV, config=SAMPLE_CONFIG)

class MultipleStreamsTest(TargetFileTestTemplate):
name = "users_and_employees"

@property
def singer_filepath(self) -> Path:
current_dir = Path(__file__).resolve().parent
return current_dir / "data_files" / f"{self.name}.singer"


TestTargetCSV = get_target_test_class(
TargetCSV,
config=SAMPLE_CONFIG,
custom_suites=[
TestSuite(
kind="target",
tests=[MultipleStreamsTest],
)
],
)
40 changes: 0 additions & 40 deletions tests/test_csv.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
"""CSV-related tests."""

import io
from pathlib import Path
from typing import Any, Dict, List, Tuple
from contextlib import redirect_stdout
import shutil

import pytest

from target_csv.serialization import read_csv, write_csv
from target_csv.target import TargetCSV

SAMPLE_DATASETS: List[Tuple[Dict, List[Dict[str, Any]]]] = [
(
Expand Down Expand Up @@ -44,28 +40,6 @@
]


def singer_file_to_target(file_name, target) -> None:
"""Singer file to Target, emulates a tap run.
Equivalent to running cat file_path | target-name --config config.json.
Note that this function loads all lines into memory, so it is
not good very large files.
Args:
file_name: name to file in .tests/data_files to be sent into target
target: Target to pass data from file_path into..
"""
file_path = Path(__file__).parent / Path("./data_files") / Path(file_name)
buf = io.StringIO()
with redirect_stdout(buf):
with open(file_path) as f:
for line in f:
print(line.rstrip("\r\n")) # File endings are here,
# and print adds another line ending so we need to remove one.
buf.seek(0)
target.listen(buf)


@pytest.fixture
def output_dir() -> Path:
result = Path("./.output/")
Expand Down Expand Up @@ -113,17 +87,3 @@ def test_csv_roundtrip(output_filepath) -> None:
for key in orig_record.keys():
# Note: Results are stringified during serialization
assert str(orig_record[key]) == new_record[key]


def test_csv_multiple_streams(output_dir) -> None:
"""Tests syncing multiple streams. 30 times in a row to catch race condition.
Further discussion in #129.
"""
file_name = "users_and_employees.singer"
for i in range(30):
path = Path(output_dir / "test_csv_multiple_streams" / f"{i}")
shutil.rmtree(path, ignore_errors=True)
singer_file_to_target(
file_name=file_name, target=TargetCSV(config={"output_path": str(path)})
)

0 comments on commit cbbf56d

Please sign in to comment.