Skip to content

Commit

Permalink
Test constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
edgarrmondragon committed Aug 9, 2022
1 parent 2feb9c2 commit 01acd00
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 14 deletions.
6 changes: 3 additions & 3 deletions docs/deprecations.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

The following features and APIs are deprecated and will be removed in a future release. Please update your code to use the recommended alternatives.

## Passing a schema of type string or `pathlib.Path` to the `Stream` constructor
## Passing a schema of string to the `Stream` constructor

The `schema` argument of the `Stream`, `RESTStream` and `GraphQLStream` constructors will not accept a `pathlib.Path` object in a future release. If you want to use a JSON schema file that is distributed with your tap, you can use `singer_sdk.helpers.util.get_package_files` to load it:
The `schema` argument of the `Stream`, `RESTStream` and `GraphQLStream` constructors will not accept a string object in a future release. If you want to load a JSON schema file that is distributed with your tap, you can use `singer_sdk.helpers.util.get_package_files` to load it:

```python
from singer_sdk.helpers.util import get_package_files
Expand All @@ -29,4 +29,4 @@ class MyTap(Tap):
To convert your `schemas/` directory to an importable Python package as shown above, simply add an empty `__init__.py` file to it.
```

The [`Stream.schema_filepath`](singer_sdk.Stream.schema_filepath) property will continue to work with `pathlib.Path` objects, but it's recommended to use `singer_sdk.helpers.util.get_package_files` as well.
The `schema` argument and [`Stream.schema_filepath`](singer_sdk.Stream.schema_filepath) property will continue to work with `pathlib.Path` objects, but it's recommended to use `singer_sdk.helpers.util.get_package_files` as well.
21 changes: 10 additions & 11 deletions singer_sdk/streams/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,19 +116,18 @@ def __init__(
self.child_streams: list[Stream] = []
if schema:
if isinstance(schema, (PathLike, str)):
if not Path(schema).is_file():
raise FileNotFoundError(
f"Could not find schema file '{self.schema_filepath}'."
if isinstance(schema, str):
warn(
"Passing a string filepath is deprecated. "
+ "Use singer_sdk.helpers.util.get_package instead.",
DeprecationWarning,
)
warn(
"Passing a string or pathlib.Path schema filepath is deprecated. "
+ "Use importlib.resources.files() instead.",
DeprecationWarning,
)
self._schema_filepath = Path(schema)
else:
self._schema_filepath = schema

self._schema_filepath = Path(schema)
elif isinstance(schema, Traversable):
self._schema_filepath = schema
if not self._schema_filepath.is_file():
raise FileNotFoundError(f"Could not find schema file '{schema}'.")
elif isinstance(schema, dict):
self._schema = schema
elif isinstance(schema, Schema):
Expand Down
26 changes: 26 additions & 0 deletions tests/core/test_streams.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Stream tests."""

import logging
import os
from typing import Any, Dict, Iterable, List, Optional, cast

import pendulum
Expand All @@ -9,6 +10,7 @@

from singer_sdk.helpers._classproperty import classproperty
from singer_sdk.helpers.jsonpath import _compile_jsonpath
from singer_sdk.helpers.util import get_package_files
from singer_sdk.streams.core import (
REPLICATION_FULL_TABLE,
REPLICATION_INCREMENTAL,
Expand Down Expand Up @@ -113,6 +115,30 @@ def stream(tap: SimpleTestTap) -> SimpleTestStream:
return cast(SimpleTestStream, tap.load_streams()[0])


def test_constructor(tap):
with pytest.warns(DeprecationWarning):
filepath = os.path.join(
__file__,
os.pardir,
"resources",
"schema.json",
)
RestTestStream(
tap,
name="test",
schema=os.path.abspath(filepath),
)

import tests.core.resources as test_resources

stream = RestTestStream(
tap,
name="test",
schema=get_package_files(test_resources).joinpath("schema.json"),
)
assert stream.schema_filepath.exists()


def test_stream_apply_catalog(tap: SimpleTestTap, stream: SimpleTestStream):
"""Applying a catalog to a stream should overwrite fields."""
assert stream.primary_keys == []
Expand Down

0 comments on commit 01acd00

Please sign in to comment.