Skip to content

Commit

Permalink
fix(import_datasources): --sync flag works correctly (#18046)
Browse files Browse the repository at this point in the history
* Added code that properly accepts the -s flag on the import-datasources cli command. Also added unit tests for all of the edge cases (with both metrics & columns, with just columns, and with just metrics)

* Files were reformated using the 'pre-commit run --all-files' command

* added '*args: Any' back into v0.py as it did not need to be removed. Removing it might cause headaches for someone trying to work on this particular piece of code in the future

* Fixed the merge conflict as the cli.py was moved to another directory

* Modified my created unit tests to work with the new format of uni tests since the merge

* Modified my created unit tests to work with the new format of uni tests since the merge

* Fixed errors which were encountered while using the unit tests
  • Loading branch information
cccs-Dustin committed Jan 25, 2022
1 parent f018c82 commit 2dd64f9
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 1 deletion.
4 changes: 3 additions & 1 deletion superset/cli/importexport.py
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,9 @@ def import_datasources(path: str, sync: str, recursive: bool) -> None:
with open(path_) as file:
contents[path_.name] = file.read()
try:
ImportDatasetsCommand(contents, sync_columns, sync_metrics).run()
ImportDatasetsCommand(
contents, sync_columns=sync_columns, sync_metrics=sync_metrics
).run()
except Exception: # pylint: disable=broad-except
logger.exception("Error when importing dataset")
sys.exit(1)
Expand Down
105 changes: 105 additions & 0 deletions tests/integration_tests/cli_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,111 @@ def test_import_datasets_versioned_export(import_datasets_command, app_context,
import_datasets_command.assert_called_with(expected_contents, overwrite=True)


@mock.patch.dict(
"superset.config.DEFAULT_FEATURE_FLAGS", {"VERSIONED_EXPORT": False}, clear=True
)
@mock.patch("superset.datasets.commands.importers.v0.ImportDatasetsCommand")
def test_import_datasets_sync_argument_columns_metrics(
import_datasets_command, app_context, fs
):
"""
Test that the --sync command line argument syncs dataset in superset
with YAML file. Using both columns and metrics with the --sync flag
"""
# pylint: disable=reimported, redefined-outer-name
import superset.cli.importexport # noqa: F811

# reload to define export_datasets correctly based on the
# feature flags
importlib.reload(superset.cli.importexport)

# write YAML file
with open("dataset.yaml", "w") as fp:
fp.write("hello: world")

runner = app.test_cli_runner()
response = runner.invoke(
superset.cli.importexport.import_datasources,
["-p", "dataset.yaml", "-s", "metrics,columns"],
)

assert response.exit_code == 0
expected_contents = {"dataset.yaml": "hello: world"}
import_datasets_command.assert_called_with(
expected_contents, sync_columns=True, sync_metrics=True,
)


@mock.patch.dict(
"superset.config.DEFAULT_FEATURE_FLAGS", {"VERSIONED_EXPORT": False}, clear=True
)
@mock.patch("superset.datasets.commands.importers.v0.ImportDatasetsCommand")
def test_import_datasets_sync_argument_columns(
import_datasets_command, app_context, fs
):
"""
Test that the --sync command line argument syncs dataset in superset
with YAML file. Using only columns with the --sync flag
"""
# pylint: disable=reimported, redefined-outer-name
import superset.cli.importexport # noqa: F811

# reload to define export_datasets correctly based on the
# feature flags
importlib.reload(superset.cli.importexport)

# write YAML file
with open("dataset.yaml", "w") as fp:
fp.write("hello: world")

runner = app.test_cli_runner()
response = runner.invoke(
superset.cli.importexport.import_datasources,
["-p", "dataset.yaml", "-s", "columns"],
)

assert response.exit_code == 0
expected_contents = {"dataset.yaml": "hello: world"}
import_datasets_command.assert_called_with(
expected_contents, sync_columns=True, sync_metrics=False,
)


@mock.patch.dict(
"superset.config.DEFAULT_FEATURE_FLAGS", {"VERSIONED_EXPORT": False}, clear=True
)
@mock.patch("superset.datasets.commands.importers.v0.ImportDatasetsCommand")
def test_import_datasets_sync_argument_metrics(
import_datasets_command, app_context, fs
):
"""
Test that the --sync command line argument syncs dataset in superset
with YAML file. Using only metrics with the --sync flag
"""
# pylint: disable=reimported, redefined-outer-name
import superset.cli.importexport # noqa: F811

# reload to define export_datasets correctly based on the
# feature flags
importlib.reload(superset.cli.importexport)

# write YAML file
with open("dataset.yaml", "w") as fp:
fp.write("hello: world")

runner = app.test_cli_runner()
response = runner.invoke(
superset.cli.importexport.import_datasources,
["-p", "dataset.yaml", "-s", "metrics"],
)

assert response.exit_code == 0
expected_contents = {"dataset.yaml": "hello: world"}
import_datasets_command.assert_called_with(
expected_contents, sync_columns=False, sync_metrics=True,
)


@mock.patch.dict(
"superset.cli.lib.feature_flags", {"VERSIONED_EXPORT": True}, clear=True
)
Expand Down

0 comments on commit 2dd64f9

Please sign in to comment.