Skip to content

Commit

Permalink
fix: Force stream selection in tests (#1698)
Browse files Browse the repository at this point in the history
Co-authored-by: Ken Payne <[email protected]>
  • Loading branch information
edgarrmondragon and Ken Payne authored Jun 19, 2023
1 parent 5a5f43a commit 3e7f5b3
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 1 deletion.
11 changes: 10 additions & 1 deletion singer_sdk/streams/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,6 @@ def get_starting_timestamp(self, context: dict | None) -> datetime.datetime | No

return t.cast(datetime.datetime, pendulum.parse(value))

@final
@property
def selected(self) -> bool:
"""Check if stream is selected.
Expand All @@ -279,6 +278,16 @@ def selected(self) -> bool:
"""
return self.mask.get((), True)

@selected.setter
def selected(self, value: bool | None) -> None:
"""Set stream selection.
Args:
value: True if the stream is selected.
"""
self.metadata.root.selected = value
self._mask = self.metadata.resolve_selection()

@final
@property
def has_selected_descendents(self) -> bool:
Expand Down
4 changes: 4 additions & 0 deletions singer_sdk/tap_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,9 @@ def run_sync_dry_run(
# Initialize streams' record limits before beginning the sync test.
stream.ABORT_AT_RECORD_COUNT = dry_run_record_limit

# Force selection of streams.
stream.selected = True

for stream in streams:
if stream.parent_stream_type:
self.logger.debug(
Expand All @@ -267,6 +270,7 @@ def run_sync_dry_run(
def write_schemas(self) -> None:
"""Write a SCHEMA message for all known streams to STDOUT."""
for stream in self.streams.values():
stream.selected = True
stream._write_schema_message()

# Stream detection:
Expand Down
22 changes: 22 additions & 0 deletions tests/samples/test_tap_countries.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
import typing as t
from contextlib import redirect_stdout

import pytest
from click.testing import CliRunner

from samples.sample_tap_countries.countries_tap import SampleTapCountries
from singer_sdk.helpers._catalog import (
get_selected_schema,
Expand All @@ -17,6 +20,11 @@
from singer_sdk.testing import get_tap_test_class
from singer_sdk.testing.config import SuiteConfig

if t.TYPE_CHECKING:
from pathlib import Path

from pytest_snapshot.plugin import Snapshot

SAMPLE_CONFIG = {}
SAMPLE_CONFIG_BAD = {"not": "correct"}

Expand Down Expand Up @@ -136,3 +144,17 @@ def tally_messages(messages: list) -> t.Counter:
assert counter["BATCH", "countries"] == 1

assert counter[("STATE",)] == 3


@pytest.mark.snapshot()
def test_write_schema(
snapshot: Snapshot,
snapshot_dir: Path,
):
snapshot.snapshot_dir = snapshot_dir.joinpath("countries_write_schemas")

runner = CliRunner(mix_stderr=False)
result = runner.invoke(SampleTapCountries.cli, ["--test", "schema"])

snapshot_name = "countries_write_schemas"
snapshot.assert_match(result.stdout, snapshot_name)
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{"type": "SCHEMA", "stream": "continents", "schema": {"properties": {"code": {"type": ["null", "string"]}, "name": {"type": ["null", "string"]}}, "type": "object"}, "key_properties": ["code"]}
{"type": "SCHEMA", "stream": "countries", "schema": {"properties": {"code": {"type": ["string", "null"]}, "name": {"type": ["string", "null"]}, "native": {"type": ["string", "null"]}, "phone": {"type": ["string", "null"]}, "capital": {"type": ["string", "null"]}, "currency": {"type": ["string", "null"]}, "emoji": {"type": ["string", "null"]}, "continent": {"properties": {"code": {"type": ["string", "null"]}, "name": {"type": ["string", "null"]}}, "type": ["object", "null"]}, "languages": {"items": {"properties": {"code": {"type": ["string", "null"]}, "name": {"type": ["string", "null"]}}, "type": "object"}, "type": ["array", "null"]}}, "type": "object"}, "key_properties": ["code"]}

0 comments on commit 3e7f5b3

Please sign in to comment.