From 2dd64f9a93bff7e96182bb27a547b21a0a6ef48e Mon Sep 17 00:00:00 2001 From: cccs-Dustin <96579982+cccs-Dustin@users.noreply.github.com> Date: Tue, 25 Jan 2022 09:53:44 -0500 Subject: [PATCH] fix(import_datasources): --sync flag works correctly (#18046) * 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 --- superset/cli/importexport.py | 4 +- tests/integration_tests/cli_tests.py | 105 +++++++++++++++++++++++++++ 2 files changed, 108 insertions(+), 1 deletion(-) diff --git a/superset/cli/importexport.py b/superset/cli/importexport.py index 21167bc69f47d..8ca86939f2065 100755 --- a/superset/cli/importexport.py +++ b/superset/cli/importexport.py @@ -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) diff --git a/tests/integration_tests/cli_tests.py b/tests/integration_tests/cli_tests.py index baae90e2c5c8e..3f4725640e3c3 100644 --- a/tests/integration_tests/cli_tests.py +++ b/tests/integration_tests/cli_tests.py @@ -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 )