From 1c366183fc1dd880363956cc17a6d6159cecbd05 Mon Sep 17 00:00:00 2001 From: Matthew Lemon Date: Sun, 17 Jan 2021 16:37:27 +0000 Subject: added flag to change input directory for import templates command --- datamaps/main.py | 5 +++-- datamaps/tests/conftest.py | 8 ++++++++ datamaps/tests/test_cli.py | 24 ++++++++++++++++++++++++ 3 files changed, 35 insertions(+), 2 deletions(-) (limited to 'datamaps') diff --git a/datamaps/main.py b/datamaps/main.py index 7b7c670..1bde2a6 100644 --- a/datamaps/main.py +++ b/datamaps/main.py @@ -143,7 +143,8 @@ def show_file(): help="Set row limit to prevent spurious Excel files with hidden rows being processed." "Default is 500.", ) -def templates(to_master, datamap, rowlimit): +@click.option("--inputdir", help="Path to input directory", metavar="INPUT_DIRECTORY_PATH") +def templates(to_master, datamap, rowlimit, inputdir): """Import data to a master file from a collection of populated templates. BASICS @@ -171,7 +172,7 @@ def templates(to_master, datamap, rowlimit): if to_master: try: engine_cli.import_and_create_master( - echo_funcs=output_funcs, datamap=datamap, rowlimit=rowlimit + echo_funcs=output_funcs, datamap=datamap, rowlimit=rowlimit, inputdir=inputdir ) except MalFormedCSVHeaderException as e: click.echo( diff --git a/datamaps/tests/conftest.py b/datamaps/tests/conftest.py index aa59301..4faed8c 100644 --- a/datamaps/tests/conftest.py +++ b/datamaps/tests/conftest.py @@ -1,5 +1,7 @@ import os import shutil +import tempfile + from pathlib import Path import pytest @@ -12,6 +14,12 @@ def master() -> Path: return Path.cwd() / "datamaps" / "tests" / "resources" / "master.xlsx" +@pytest.fixture +def temp_input_dir() -> Path: + pth = tempfile.mkdtemp() + yield pth + + @pytest.fixture def mock_config(monkeypatch): monkeypatch.setattr(Config, "PLATFORM_DOCS_DIR", Path("/tmp/Documents/datamaps")) diff --git a/datamaps/tests/test_cli.py b/datamaps/tests/test_cli.py index aef977c..a3a832f 100644 --- a/datamaps/tests/test_cli.py +++ b/datamaps/tests/test_cli.py @@ -20,6 +20,30 @@ def _copy_resources_to_input(config, directory): ) +def test_import_from_user_defined_source_dir( + mock_config, resource_dir, caplog, temp_input_dir +): + runner = CliRunner() + mock_config.initialise() + caplog.set_level(logging.INFO) + for fl in os.listdir(resource_dir): + shutil.copy( + Path.cwd() / "datamaps" / "tests" / "resources" / fl, temp_input_dir + ) + result = runner.invoke(_import, ["templates", "-m", "--inputdir", temp_input_dir]) + assert result.exit_code == 0 + output = [x[2] for x in caplog.record_tuples] + found = False + for o in output: + if f"Reading datamap {temp_input_dir}/" in o: + found = True + break + if found: + assert True + else: + assert False, "Cannot find correct string" + + @pytest.mark.parametrize( "rowlimit,expected,exit_code", [ -- cgit v1.2.3