-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Confirm drop by prefix, add --continue #140
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -86,13 +86,25 @@ def get_study_prefix(self) -> Optional[str]: | |
""" | ||
return self._study_config.get("study_prefix") | ||
|
||
def get_sql_file_list(self) -> Optional[StrList]: | ||
def get_sql_file_list(self, continue_from: str = None) -> Optional[StrList]: | ||
"""Reads the contents of the sql_config array from the manifest | ||
|
||
:returns: An array of sql files from the manifest, or None if not found. | ||
""" | ||
sql_config = self._study_config.get("sql_config", {}) | ||
return sql_config.get("file_names", []) | ||
sql_files = sql_config.get("file_names", []) | ||
if continue_from: | ||
match_found = False | ||
for file in sql_files: | ||
print(continue_from.replace(".sql", "")) | ||
print(file.replace(".sql", "")) | ||
dogversioning marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if continue_from.replace(".sql", "") == file.replace(".sql", ""): | ||
sql_files = sql_files[sql_files.index(file) :] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: you could save yourself an There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ooh, i dig it |
||
match_found = True | ||
break | ||
if not match_found: | ||
sys.exit(f"No tables matching '{continue_from}' found") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You ready for this Python nonsense? Just do i.e.:
Alternatively, you could also do Or keep it! Whatever works for your sense of code cleanliness. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. taking the |
||
return sql_files | ||
|
||
def get_table_builder_file_list(self) -> Optional[StrList]: | ||
"""Reads the contents of the table_builder_config array from the manifest | ||
|
@@ -190,6 +202,13 @@ def clean_study( | |
): | ||
view_table_list.remove(view_table) | ||
# We want to only show a progress bar if we are :not: printing SQL lines | ||
if prefix: | ||
print("The following views/tables were selected by prefix:") | ||
for view_table in view_table_list: | ||
print(f" {view_table[0]}") | ||
confirm = input("Remove these tables? (Y/N)") | ||
dogversioning marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if confirm.lower() not in ("y", "yes"): | ||
sys.exit("Table cleaning aborted") | ||
with get_progress_bar(disable=verbose) as progress: | ||
task = progress.add_task( | ||
f"Removing {display_prefix} study artifacts...", | ||
|
@@ -314,7 +333,9 @@ def run_single_table_builder( | |
name = f"{name}.py" | ||
self._load_and_execute_builder(name, cursor, schema, verbose, drop_table=True) | ||
|
||
def build_study(self, cursor: object, verbose: bool = False) -> List: | ||
def build_study( | ||
self, cursor: object, verbose: bool = False, continue_from: str = None | ||
) -> List: | ||
"""Creates tables in the schema by iterating through the sql_config.file_names | ||
|
||
:param cursor: A PEP-249 compatible cursor object | ||
|
@@ -323,7 +344,7 @@ def build_study(self, cursor: object, verbose: bool = False) -> List: | |
:returns: loaded queries (for unit testing only) | ||
""" | ||
queries = [] | ||
for file in self.get_sql_file_list(): | ||
for file in self.get_sql_file_list(continue_from): | ||
for query in parse_sql(load_text(f"{self._study_path}/{file}")): | ||
queries.append([query, file]) | ||
if len(queries) == 0: | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
""" tests for study parser against mocks in test_data """ | ||
import builtins | ||
import pathlib | ||
from contextlib import nullcontext as does_not_raise | ||
from unittest import mock | ||
|
@@ -74,37 +75,38 @@ def test_manifest_data(manifest_key, raises): | |
|
||
|
||
@pytest.mark.parametrize( | ||
"schema,verbose,prefix,query_res,raises", | ||
"schema,verbose,prefix,confirm,query_res,raises", | ||
[ | ||
("schema", True, None, "study_valid__table", does_not_raise()), | ||
("schema", False, None, "study_valid__table", does_not_raise()), | ||
("schema", None, None, "study_valid__table", does_not_raise()), | ||
(None, True, None, [], pytest.raises(ValueError)), | ||
("schema", None, None, "study_valid__etl_table", does_not_raise()), | ||
("schema", None, None, "study_valid__nlp_table", does_not_raise()), | ||
("schema", None, None, "study_valid__lib_table", does_not_raise()), | ||
("schema", None, None, "study_valid__lib", does_not_raise()), | ||
("schema", None, "foo", "foo_table", does_not_raise()), | ||
("schema", True, None, None, "study_valid__table", does_not_raise()), | ||
("schema", False, None, None, "study_valid__table", does_not_raise()), | ||
("schema", None, None, None, "study_valid__table", does_not_raise()), | ||
(None, True, None, None, [], pytest.raises(ValueError)), | ||
("schema", None, None, None, "study_valid__etl_table", does_not_raise()), | ||
("schema", None, None, None, "study_valid__nlp_table", does_not_raise()), | ||
("schema", None, None, None, "study_valid__lib_table", does_not_raise()), | ||
("schema", None, None, None, "study_valid__lib", does_not_raise()), | ||
("schema", None, "foo", "y", "foo_table", does_not_raise()), | ||
("schema", None, "foo", "n", "foo_table", pytest.raises(SystemExit)), | ||
], | ||
) | ||
@mock.patch("cumulus_library.helper.query_console_output") | ||
def test_clean_study(mock_output, schema, verbose, prefix, query_res, raises): | ||
def test_clean_study(mock_output, schema, verbose, prefix, confirm, query_res, raises): | ||
with raises: | ||
mock_cursor = mock.MagicMock() | ||
mock_cursor.__iter__.return_value = [[query_res]] | ||
parser = StudyManifestParser("./tests/test_data/study_valid/") | ||
tables = parser.clean_study(mock_cursor, schema, verbose, prefix=prefix) | ||
with mock.patch.object(builtins, "input", lambda _: confirm): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fun |
||
mock_cursor = mock.MagicMock() | ||
mock_cursor.__iter__.return_value = [[query_res]] | ||
parser = StudyManifestParser("./tests/test_data/study_valid/") | ||
tables = parser.clean_study(mock_cursor, schema, verbose, prefix=prefix) | ||
|
||
if "study_valid__table" not in query_res and prefix is None: | ||
print("wha") | ||
assert not tables | ||
else: | ||
assert tables == [[query_res, "VIEW"]] | ||
if prefix is not None: | ||
assert prefix in mock_cursor.execute.call_args.args[0] | ||
if "study_valid__table" not in query_res and prefix is None: | ||
assert not tables | ||
else: | ||
assert "study_valid__" in mock_cursor.execute.call_args.args[0] | ||
assert mock_output.is_called() | ||
assert tables == [[query_res, "VIEW"]] | ||
if prefix is not None: | ||
assert prefix in mock_cursor.execute.call_args.args[0] | ||
else: | ||
assert "study_valid__" in mock_cursor.execute.call_args.args[0] | ||
assert mock_output.is_called() | ||
|
||
|
||
@pytest.mark.parametrize( | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why the parens?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
copy pasted from elsewhere - why i did that in the first place,
¯\_(ツ)_/¯