mirror of
https://github.com/simonw/sqlite-utils.git
synced 2026-09-27 20:34:12 +02:00
Validate column names in analyze-columns, closes #548
This commit is contained in:
parent
6027f3ea69
commit
e8c5b042e4
2 changed files with 46 additions and 0 deletions
|
|
@ -2672,11 +2672,20 @@ def _analyze(db, tables, columns, save, common_limit=10, no_most=False, no_least
|
||||||
tables = db.table_names()
|
tables = db.table_names()
|
||||||
todo = []
|
todo = []
|
||||||
table_counts = {}
|
table_counts = {}
|
||||||
|
seen_columns = set()
|
||||||
for table in tables:
|
for table in tables:
|
||||||
table_counts[table] = db[table].count
|
table_counts[table] = db[table].count
|
||||||
for column in db[table].columns:
|
for column in db[table].columns:
|
||||||
if not columns or column.name in columns:
|
if not columns or column.name in columns:
|
||||||
todo.append((table, column.name))
|
todo.append((table, column.name))
|
||||||
|
seen_columns.add(column.name)
|
||||||
|
# Check the user didn't specify a column that doesn't exist
|
||||||
|
if columns and (set(columns) - seen_columns):
|
||||||
|
raise click.ClickException(
|
||||||
|
"These columns were not found: {}".format(
|
||||||
|
", ".join(sorted(set(columns) - seen_columns))
|
||||||
|
)
|
||||||
|
)
|
||||||
# Now we now how many we need to do
|
# Now we now how many we need to do
|
||||||
for i, (table, column) in enumerate(todo):
|
for i, (table, column) in enumerate(todo):
|
||||||
column_details = db[table].analyze_column(
|
column_details = db[table].analyze_column(
|
||||||
|
|
|
||||||
|
|
@ -281,3 +281,40 @@ def test_analyze_table_column_all_nulls(big_db_to_analyze_path):
|
||||||
"\n"
|
"\n"
|
||||||
" Distinct values: 0\n\n"
|
" Distinct values: 0\n\n"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
"args,expected_error",
|
||||||
|
(
|
||||||
|
(["-c", "bad_column"], "These columns were not found: bad_column\n"),
|
||||||
|
(["one", "-c", "age"], "These columns were not found: age\n"),
|
||||||
|
(["two", "-c", "age"], None),
|
||||||
|
(
|
||||||
|
["one", "-c", "age", "--column", "bad"],
|
||||||
|
"These columns were not found: age, bad\n",
|
||||||
|
),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
def test_analyze_table_validate_columns(tmpdir, args, expected_error):
|
||||||
|
path = str(tmpdir / "test_validate_columns.db")
|
||||||
|
db = Database(path)
|
||||||
|
db["one"].insert(
|
||||||
|
{
|
||||||
|
"id": 1,
|
||||||
|
"name": "one",
|
||||||
|
}
|
||||||
|
)
|
||||||
|
db["two"].insert(
|
||||||
|
{
|
||||||
|
"id": 1,
|
||||||
|
"age": 5,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
result = CliRunner().invoke(
|
||||||
|
cli.cli,
|
||||||
|
["analyze-tables", path] + args,
|
||||||
|
catch_exceptions=False,
|
||||||
|
)
|
||||||
|
assert result.exit_code == (1 if expected_error else 0)
|
||||||
|
if expected_error:
|
||||||
|
assert expected_error in result.output
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue