diff options
-rw-r--r-- | datamaps/__init__.py | 2 | ||||
-rw-r--r-- | datamaps/main.py | 15 | ||||
-rw-r--r-- | datamaps/tests/test_cli.py | 33 |
3 files changed, 42 insertions, 8 deletions
diff --git a/datamaps/__init__.py b/datamaps/__init__.py index 976498a..92192ee 100644 --- a/datamaps/__init__.py +++ b/datamaps/__init__.py @@ -1 +1 @@ -__version__ = "1.0.3" +__version__ = "1.0.4" diff --git a/datamaps/main.py b/datamaps/main.py index 407727d..3f38166 100644 --- a/datamaps/main.py +++ b/datamaps/main.py @@ -145,7 +145,12 @@ def templates(to_master, datamap): # @click.argument("datamap") # @click.argument("blank") @click.argument("master", metavar="FILE_PATH") -def master(master): +@click.option( + "--datamap", + "-d", + help="Path to datamap file", +) +def master(master, datamap): """Export data from a Master file Export data from master file whose path is FILE_PATH to a series of @@ -154,7 +159,10 @@ def master(master): input_dir = engine_config.PLATFORM_DOCS_DIR / "input" blank_fn = engine_config.config_parser["DEFAULT"]["blank file name"] - datamap_fn = engine_config.config_parser["DEFAULT"]["datamap file name"] + if datamap: + datamap_fn = datamap + else: + datamap_fn = engine_config.config_parser["DEFAULT"]["datamap file name"] blank = input_dir / blank_fn datamap = input_dir / datamap_fn @@ -179,6 +187,9 @@ def master(master): except MissingLineError as e: logger.critical(e) sys.exit(1) + except DatamapNotCSVException as e: + logger.critical(e) + sys.exit(1) be_logger.info("Export complete.") diff --git a/datamaps/tests/test_cli.py b/datamaps/tests/test_cli.py index 827d690..135f280 100644 --- a/datamaps/tests/test_cli.py +++ b/datamaps/tests/test_cli.py @@ -6,7 +6,7 @@ from pathlib import Path import pytest from click.testing import CliRunner -from datamaps.main import _import +from datamaps.main import _import, export def _copy_resources_to_input(config, directory): @@ -19,7 +19,7 @@ def _copy_resources_to_input(config, directory): ) -def test_can_select_datamap_when_importing(mock_config, resource_dir, caplog): +def test_import_with_alternative_datamap(mock_config, resource_dir, caplog): """ This should fail with exit code one as we are not providing a valid datamap file. :param mock_config: @@ -33,10 +33,11 @@ def test_can_select_datamap_when_importing(mock_config, resource_dir, caplog): _alt_datamap_file = os.path.join(mock_config.PLATFORM_DOCS_DIR, "input", "datamap_alternate.csv") result = runner.invoke(_import, ["templates", "-m", "-d", _alt_datamap_file]) assert result.exit_code == 0 - assert "Reading datamap \\tmp\\Documents\\datamaps\\input\\datamap_alternate.csv" in [x[2] for x in caplog.record_tuples] + assert "Reading datamap \\tmp\\Documents\\datamaps\\input\\datamap_alternate.csv" in [x[2] for x in + caplog.record_tuples] -def test_wrong_datamap_given(mock_config, resource_dir): +def test_import_with_wrong_datamap(mock_config, resource_dir, caplog): """ This should fail with exit code one as we are not providing a valid datamap file. :param mock_config: @@ -46,8 +47,30 @@ def test_wrong_datamap_given(mock_config, resource_dir): runner = CliRunner() mock_config.initialise() _copy_resources_to_input(mock_config, resource_dir) - result = runner.invoke(_import, ["templates", "-m", "-d", "C:/tmp/non-existant-file.txt"]) + result = runner.invoke(_import, ["templates", "-m", "-d", "C:/tmp/non-existent-file.txt"]) assert result.exit_code == 1 + assert "Given datamap file is not in CSV format." in [x[2] for x in caplog.record_tuples] + + +def test_export_with_alternative_datamap(mock_config, resource_dir, caplog): + runner = CliRunner() + mock_config.initialise() + _copy_resources_to_input(mock_config, resource_dir) + _master_file = os.path.join(mock_config.PLATFORM_DOCS_DIR, "input", "master.xlsx") + result = runner.invoke(export, ["master", _master_file, "-d", "C:/tmp/Desktop/test.txt"]) + assert result.exit_code == 1 + assert "Given datamap file is not in CSV format." in [x[2] for x in caplog.record_tuples] + + +def test_export_with_alternative_datamap_not_csv(mock_config, resource_dir, caplog): + runner = CliRunner() + mock_config.initialise() + _copy_resources_to_input(mock_config, resource_dir) + _master_file = os.path.join(mock_config.PLATFORM_DOCS_DIR, "input", "master.xlsx") + _alt_datamap_file = os.path.join(mock_config.PLATFORM_DOCS_DIR, "input", "datamap_alternate.csv") + _ = runner.invoke(export, ["master", _master_file, "-d", _alt_datamap_file]) + assert "Reading datamap \\tmp\\Documents\\datamaps\\input\\datamap_alternate.csv" in [x[2] for x in + caplog.record_tuples] @pytest.mark.skip("Not currently passing - need to investigate") |