diff --git a/superset/cli/importexport.py b/superset/cli/importexport.py index 21167bc69f4..8ca86939f20 100755 --- a/superset/cli/importexport.py +++ b/superset/cli/importexport.py @@ -358,7 +358,9 @@ else: 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 baae90e2c5c..3f4725640e3 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 )