Skip to content

Commit

Permalink
init: Make tables a positional argument.
Browse files Browse the repository at this point in the history
Fixes #92
  • Loading branch information
craigds committed Jun 15, 2020
1 parent 0fc13cc commit 815db3a
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 73 deletions.
45 changes: 14 additions & 31 deletions sno/init.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,9 +195,7 @@ def prompt_for_table(self, prompt):
else:
self.print_table_list()
if get_input_mode() == InputMode.NO_INPUT:
raise NotFound(
"No table specified", exit_code=NO_TABLE, param_hint="--table"
)
raise NotFound("No table specified", exit_code=NO_TABLE)
t_choices = click.Choice(choices=table_list)
t_default = table_list[0] if len(table_list) == 1 else None
return click.prompt(
Expand All @@ -213,9 +211,7 @@ def __str__(self):
def check_table(self, table_name):
if table_name not in self.get_tables():
raise NotFound(
f"Table '{table_name}' not found",
exit_code=NO_TABLE,
param_hint="--table",
f"Table '{table_name}' not found", exit_code=NO_TABLE,
)

def __enter__(self):
Expand Down Expand Up @@ -541,17 +537,8 @@ def get_table_names_map(tables):
@click.command("import")
@click.pass_context
@click.argument("source")
@click.option(
"tables",
"--table",
"-t",
multiple=True,
help=(
"Which table to import (can specify more than one). "
"If not specified, this will be selected interactively"
),
cls=MutexOption,
exclusive_with=["do_list", "all_tables"],
@click.argument(
"tables", nargs=-1,
)
@click.option(
"--all-tables",
Expand Down Expand Up @@ -587,7 +574,7 @@ def get_table_names_map(tables):
"--output-format", "-o", type=click.Choice(["text", "json"]), default="text",
)
def import_table(
ctx, source, tables, all_tables, message, do_list, output_format, version
ctx, all_tables, message, do_list, output_format, version, source, tables,
):
"""
Import data into a repository.
Expand Down Expand Up @@ -639,23 +626,14 @@ def import_table(

@click.command()
@click.pass_context
@click.argument(
"tables", nargs=-1,
)
@click.option(
"--import",
"import_from",
help='Import from data: "FORMAT:PATH" eg. "GPKG:my.gpkg"',
)
@click.option(
"tables",
"--table",
"-t",
multiple=True,
help=(
"Which table to import (can specify more than one). "
"If not specified, this will be selected interactively"
),
cls=MutexOption,
exclusive_with=["do_list", "all_tables"],
)
@click.option(
"--all-tables",
"-a",
Expand Down Expand Up @@ -689,7 +667,7 @@ def import_table(
hidden=True,
)
def init(
ctx, import_from, tables, all_tables, do_checkout, message, directory, version
ctx, all_tables, do_checkout, message, directory, version, import_from, tables
):
"""
Initialise a new repository and optionally import data
Expand All @@ -699,6 +677,11 @@ def init(
To show available tables in the import data, use
$ sno init --import=GPKG:my.gpkg
"""
if tables and not import_from:
raise click.UsageError(
f"Table names cannot be specified without --import. Did you mean `--path PATH`?"
)

if import_from:
check_git_user(repo=None)
source_loader = OgrImporter.open(import_from, None)
Expand Down
3 changes: 1 addition & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -305,8 +305,7 @@ def _data_imported(
"import",
f"GPKG:{data / source_gpkg}",
f"--version={version}",
f"--table={table}",
"mytable",
f"{table}:mytable",
]
)
assert r.exit_code == 0, r
Expand Down
4 changes: 2 additions & 2 deletions tests/scripts/e2e-1.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,11 @@ $SQLITE=(Join-Path $SNO_PREFIX 'sqlite3.exe')
New-Item -ItemType Directory -Path "${TMP_PATH}\test"
Push-Location "${TMP_PATH}\test"
try {
Exec { sno init . }
Exec { sno init --path . }
Exec { sno -v config --local 'user.name' 'Sno E2E Test 1' }
Exec { sno -v config --local 'user.email' 'sno-e2e-test-1@email.invalid' }
Exec { sno -v config --local 'core.pager' false }
Exec { sno import "GPKG:${TEST_GPKG}" "--table=mylayer" }
Exec { sno import "GPKG:${TEST_GPKG}" "mylayer" }

Exec { sno log }
Exec { sno checkout }
Expand Down
4 changes: 2 additions & 2 deletions tests/scripts/e2e-1.sh
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ mkdir "${TMP_PATH}/test"
cd "${TMP_PATH}/test"
set -x

sno init .
sno init --path .
sno config user.name "Sno E2E Test 1"
sno config user.email "sno-e2e-test-1@email.invalid"
sno import "GPKG:${TEST_GPKG}" --table=mylayer
sno import "GPKG:${TEST_GPKG}" mylayer

sno log
sno checkout
Expand Down
2 changes: 1 addition & 1 deletion tests/test_e2e.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def test_e2e(
assert r.exit_code == 0

# import data
r = cli_runner.invoke(["import", f"GPKG:{data / gpkg}", "--table", table])
r = cli_runner.invoke(["import", f"GPKG:{data / gpkg}", table])
assert r.exit_code == 0

# check there's a commit
Expand Down
34 changes: 10 additions & 24 deletions tests/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ def test_init_import_table_with_prompt_with_no_input(
" census2016_sdhca_ot_ced_short - census2016_sdhca_ot_ced_short"
in r.stdout
)
assert "Invalid value for --table: No table specified" in r.stderr
assert "No table specified" in r.stderr


def test_init_import_table_ogr_types(data_archive_readonly, tmp_path, cli_runner):
Expand Down Expand Up @@ -250,9 +250,7 @@ def test_init_import(
repo_path.mkdir()

with chdir(repo_path):
r = cli_runner.invoke(
["init", "--import", f"gpkg:{data / gpkg}", f"--table={table}"]
)
r = cli_runner.invoke(["init", "--import", f"gpkg:{data / gpkg}", table])
assert r.exit_code == 0, r
assert (repo_path / "HEAD").exists()

Expand Down Expand Up @@ -293,13 +291,7 @@ def test_init_import_name_clash(data_archive, cli_runner, geopackage):
""" Import the GeoPackage into a Sno repository of the same name, and checkout a working copy of the same name. """
with data_archive("gpkg-editing") as data:
r = cli_runner.invoke(
[
"init",
"--import",
f"GPKG:editing.gpkg",
"--table=editing",
"--path=editing",
]
["init", "--import", f"GPKG:editing.gpkg", "editing", "--path=editing",]
)
repo_path = data / "editing"

Expand Down Expand Up @@ -355,21 +347,15 @@ def test_init_import_errors(data_archive, tmp_path, chdir, cli_runner):
assert "Couldn't find 'thingz.gpkg'" in r.stderr

r = cli_runner.invoke(
["init", "--import", f"gpkg:{data/gpkg}", f"--table=no-existey"]
["init", "--import", f"gpkg:{data/gpkg}", "no-existey"]
)
assert r.exit_code == NO_TABLE, r
assert "Invalid value for --table: Table 'no-existey' not found" in r.stderr
assert "Table 'no-existey' not found" in r.stderr

# not empty
(repo_path / "a.file").touch()
r = cli_runner.invoke(
[
"init",
"--import",
f"gpkg:{data/gpkg}",
f"--table={table}",
f'--path={repo_path}',
]
["init", "--import", f"gpkg:{data/gpkg}", table, f'--path={repo_path}',]
)
assert r.exit_code == INVALID_OPERATION, r
assert "isn't empty" in r.stderr
Expand Down Expand Up @@ -451,7 +437,7 @@ def test_init_import_alt_names(data_archive, tmp_path, cli_runner, chdir, geopac
[
"import",
f"GPKG:{source_path / source_gpkg}",
f"--table={source_table}:{import_path}",
f"{source_table}:{import_path}",
]
)
assert r.exit_code == 0, r
Expand Down Expand Up @@ -506,7 +492,7 @@ def test_init_import_home_resolve(
[
"import",
"GPKG:~/nz-pa-points-topo-150k.gpkg",
"--table=nz_pa_points_topo_150k",
"nz_pa_points_topo_150k",
]
)
assert r.exit_code == 0, r
Expand All @@ -530,7 +516,7 @@ def test_import_existing_wc(
[
"import",
f"GPKG:{source_path / 'nz-waca-adjustments.gpkg'}",
f"--table={H.POLYGONS.LAYER}",
H.POLYGONS.LAYER,
]
)
assert r.exit_code == 0, r
Expand Down Expand Up @@ -568,7 +554,7 @@ def test_import_existing_wc(
[
"import",
f"GPKG:{source_path / 'nz-waca-adjustments.gpkg'}",
f"--table={H.POLYGONS.LAYER}:waca2",
f"{H.POLYGONS.LAYER}:waca2",
]
)
assert r.exit_code == 0, r
Expand Down
16 changes: 5 additions & 11 deletions tests/test_structure.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ def _benchmark_import(*args, **kwargs):
"import",
str(data / source_gpkg),
f"--version={import_version}",
f'--table={table}',
table,
]
)
assert r.exit_code == 0, r
Expand Down Expand Up @@ -394,13 +394,7 @@ def test_import_from_non_gpkg(
gpkg_repo_path = tmp_path / "gpkg"
gpkg_repo_path.mkdir()
r = cli_runner.invoke(
[
"init",
"--import",
data / source_gpkg,
f"--table={table}",
f"--path={gpkg_repo_path}",
]
["init", "--import", data / source_gpkg, table, f"--path={gpkg_repo_path}",]
)
assert r.exit_code == 0, r

Expand Down Expand Up @@ -432,7 +426,7 @@ def test_import_from_non_gpkg(
"import",
str(source_filename),
f"--version={import_version}",
f"--table=data:{table}",
f"data:{table}",
]
)
assert r.exit_code == 0, r
Expand Down Expand Up @@ -666,7 +660,7 @@ def test_import_multiple(
"import",
f"GPKG:{data / source_gpkg}",
f"--version={import_version}",
f"--table={table}",
table,
]
)
assert r.exit_code == 0, r
Expand All @@ -685,7 +679,7 @@ def test_import_multiple(
"import",
f"GPKG:{data / source_gpkg}",
f"--version={import_version}",
f"--table={table}",
table,
]
)

Expand Down

0 comments on commit 815db3a

Please sign in to comment.