From 99a41f3b8bd0b1d0b7257edfb8bf2b8756da5c8b Mon Sep 17 00:00:00 2001 From: Andreas Motl Date: Tue, 19 Dec 2023 01:31:26 +0100 Subject: [PATCH] test: Add `test_sink` test cases, exercising recently added properties (#54) ## About - Add software tests missing from GH-50. - Improve documentation wrt. GH-53. --- .gitignore | 3 +++ README.md | 2 +- tests/test_sink.py | 49 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 53 insertions(+), 1 deletion(-) create mode 100644 tests/test_sink.py diff --git a/.gitignore b/.gitignore index 83694b8..ce7e455 100644 --- a/.gitignore +++ b/.gitignore @@ -57,6 +57,9 @@ coverage.xml .hypothesis/ .pytest_cache/ +# Test artefacts +*.csv + # Translations *.mo *.pot diff --git a/README.md b/README.md index 71abf32..9336351 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,7 @@ Built with the [Meltano SDK](https://sdk.meltano.com) for Singer Taps and Target | Setting | Required | Default | Description | |:-------------------------|:--------:|:-------:|:------------| -| output_path | False | None | Filesystem path where to store output files. By default, the current working directory will be used. | +| output_path | False | None | Filesystem path where to store output files. By default, the current working directory will be used. When specified, the output directory will be created automatically. | | file_naming_scheme | False | {stream_name}.csv | The scheme with which output files will be named. Naming scheme may leverage any of the following substitutions:
- `{stream_name}`
- `{datestamp}`
- `{timestamp}` | | datestamp_format | False | %Y-%m-%d | A python format string to use when outputting the `{datestamp}` string. For reference, see: https://docs.python.org/3/library/datetime.html#strftime-and-strptime-format-codes | | timestamp_format | False | %Y-%m-%d.T%H%M%S | A python format string to use when outputting the `{timestamp}` string. For reference, see: https://docs.python.org/3/library/datetime.html#strftime-and-strptime-format-codes | diff --git a/tests/test_sink.py b/tests/test_sink.py new file mode 100644 index 0000000..f087f4b --- /dev/null +++ b/tests/test_sink.py @@ -0,0 +1,49 @@ +from pathlib import Path + +import pytest + +from target_csv.sinks import CSVSink +from target_csv.target import TargetCSV + + +def test_sink_output_file_cwd(): + """Verify `output_file` property without defining an output path.""" + target = TargetCSV() + sink = CSVSink( + target=target, stream_name="foo", schema={"properties": {}}, key_properties=[] + ) + assert sink.output_file == Path("foo.csv") + + +@pytest.mark.parametrize( + "property_name", ["output_path", "destination_path", "output_path_prefix"] +) +def test_sink_output_file_with_path(tmp_path, property_name): + """Verify `output_file` property when defining an output path. + + The test is parameterized to iterate and verify all the possible properties + which define an output path. + """ + folder_path = tmp_path / "to/folder" + output_file = tmp_path / "to/folder/foo.csv" + target = TargetCSV(config={property_name: str(folder_path)}) + sink = CSVSink( + target=target, stream_name="foo", schema={"properties": {}}, key_properties=[] + ) + assert sink.output_file == output_file + + +def test_sink_output_file_with_path_deprecated(tmp_path): + """Verify `output_file` property with deprecated `output_path_prefix` property.""" + folder_path = tmp_path / "to/folder" + output_file = tmp_path / "to/folder/foo.csv" + target = TargetCSV(config={"output_path_prefix": str(folder_path)}) + sink = CSVSink( + target=target, stream_name="foo", schema={"properties": {}}, key_properties=[] + ) + with pytest.warns( + UserWarning, + match="The property `output_path_prefix` is deprecated, " + "please use `output_path`.", + ): + assert sink.output_file == output_file