-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(floorist): RHICOMPL-2984 Bugfix for empty prefix floorplan
- Fix a bug for when floorplan file does not provide prefix - Add helper function for generating directory names - Add helper function for validating Floorplan files - Add unit test coverage wip done
- Loading branch information
1 parent
ae2b896
commit a0049ab
Showing
8 changed files
with
126 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,3 +16,4 @@ python_requires = >=3.6, <4 | |
[options.extras_require] | ||
test = | ||
pytest | ||
pytest-mock |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
from datetime import date | ||
|
||
|
||
def generate_name(bucket_name, prefix=None): | ||
|
||
file_name = date.today().strftime('year_created=%Y/month_created=%-m/day_created=%-d') | ||
parts = ["s3:/", bucket_name, file_name] | ||
if prefix: | ||
parts.insert(2, prefix) | ||
|
||
return '/'.join(parts) | ||
|
||
def validate_floorplan_entry(query, prefix): | ||
if not query: | ||
raise ValueError("Query cannot be empty!") | ||
elif not prefix: | ||
raise ValueError("Prefix cannot be empty!") | ||
else: | ||
return True |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
- query: SELECT * FROM (VALUES (1, 'one'), (2, 'two'), (3, 'three')) AS t (num,letter); | ||
prefix: some-prefix |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import uuid | ||
|
||
from floorist.floorist import main | ||
from floorist.config import Config | ||
from pandas import DataFrame | ||
|
||
|
||
def test_floorplan_without_prefix_raises_exception_keeps_reading_other_floorplans(mocker): | ||
|
||
mocker.patch('floorist.floorist.open') | ||
mocker.patch('floorist.floorist.logging') | ||
|
||
config_mock = mocker.patch('floorist.floorist.get_config') | ||
config_mock.return_value = Config(bucket_name='foo') | ||
|
||
awswrangler_mock = mocker.patch('floorist.floorist.wr') | ||
|
||
connection_engine_mock = mocker.patch('floorist.floorist.create_engine') | ||
connection_mock = connection_engine_mock().connect().execution_options() | ||
|
||
exit_mock = mocker.patch('floorist.floorist.exit') | ||
|
||
safe_load_mock = mocker.patch('floorist.floorist.yaml.safe_load') | ||
safe_load_mock.return_value = [{'query': "a query", 'prefix': None}, {'query': 'another-query', 'prefix': 'a prefix'}] | ||
|
||
pandas_mock = mocker.patch('floorist.floorist.pd') | ||
data_stub = DataFrame({ | ||
'ID': [uuid.uuid4(), uuid.uuid4(), uuid.uuid4()], | ||
'columnA': ["foo", "bar", "baz"] | ||
}) | ||
pandas_mock.read_sql.return_value = [data_stub] | ||
|
||
main() | ||
|
||
pandas_mock.read_sql.assert_called_once_with("another-query", connection_mock, chunksize=1000) | ||
data_stub.equals(awswrangler_mock.s3.to_parquet.call_args[0]) | ||
exit_mock.assert_called_once_with(1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
from floorist.helpers import generate_name | ||
from floorist.helpers import validate_floorplan_entry | ||
from datetime import date | ||
|
||
import pytest | ||
|
||
|
||
def test_name_without_prefix(): | ||
bucket_name = "my_bucket" | ||
actual_name = generate_name("my_bucket") | ||
name = date.today().strftime('year_created=%Y/month_created=%-m/day_created=%-d') | ||
expected_name = f"s3://{bucket_name}/{name}" | ||
|
||
assert actual_name == expected_name | ||
|
||
|
||
def test_name_with_prefix(): | ||
bucket_name = "my_bucket" | ||
prefix = "some-prefix" | ||
actual_name = generate_name(bucket_name, prefix) | ||
name = date.today().strftime('year_created=%Y/month_created=%-m/day_created=%-d') | ||
expected_name = f"s3://{bucket_name}/{prefix}/{name}" | ||
|
||
assert actual_name == expected_name | ||
|
||
@pytest.mark.parametrize("query,prefix", [(None, "prefix"), (None, None), ("query", None)]) | ||
def test_validate_floorplan_entry_captures_invalid_data(query,prefix): | ||
with pytest.raises(ValueError) as excinfo: | ||
validate_floorplan_entry(query,prefix) | ||
|
||
if (not prefix and not query) or not query: | ||
assert "Query cannot be empty!" in str(excinfo.value) | ||
elif not prefix: | ||
assert "Prefix cannot be empty" in str(excinfo.value) | ||
|
||
def test_validate_floorplan_entry_checks_valid_data(): | ||
assert validate_floorplan_entry("query", "prefix") |