mirror of
https://github.com/simonw/sqlite-utils.git
synced 2026-07-24 09:54:31 +02:00
'sqlite-utils add-column name type' command, closes #15
This commit is contained in:
parent
05a85b358f
commit
0bc49e938e
5 changed files with 80 additions and 8 deletions
|
|
@ -6,6 +6,8 @@ import os
|
|||
import pytest
|
||||
import sqlite3
|
||||
|
||||
from .utils import collapse_whitespace
|
||||
|
||||
|
||||
CREATE_TABLES = """
|
||||
create table Gosh (c1 text, c2 text, c3 text);
|
||||
|
|
@ -170,6 +172,44 @@ def test_create_index(db_path):
|
|||
)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"col_name,col_type,expected_schema",
|
||||
(
|
||||
("text", "TEXT", "CREATE TABLE [dogs] ( [name] TEXT , [text] TEXT)"),
|
||||
(
|
||||
"integer",
|
||||
"INTEGER",
|
||||
"CREATE TABLE [dogs] ( [name] TEXT , [integer] INTEGER)",
|
||||
),
|
||||
("float", "FLOAT", "CREATE TABLE [dogs] ( [name] TEXT , [float] FLOAT)"),
|
||||
("blob", "blob", "CREATE TABLE [dogs] ( [name] TEXT , [blob] BLOB)"),
|
||||
),
|
||||
)
|
||||
def test_add_column(db_path, col_name, col_type, expected_schema):
|
||||
db = Database(db_path)
|
||||
db.create_table("dogs", {"name": str})
|
||||
assert "CREATE TABLE [dogs] ( [name] TEXT )" == collapse_whitespace(
|
||||
db["dogs"].schema
|
||||
)
|
||||
assert (
|
||||
0
|
||||
== CliRunner()
|
||||
.invoke(cli.cli, ["add-column", db_path, "dogs", col_name, col_type])
|
||||
.exit_code
|
||||
)
|
||||
assert expected_schema == collapse_whitespace(db["dogs"].schema)
|
||||
|
||||
|
||||
def test_add_column_error_invalid_type(db_path):
|
||||
db = Database(db_path)
|
||||
db.create_table("dogs", {"name": str})
|
||||
result = CliRunner().invoke(
|
||||
cli.cli, ["add-column", db_path, "dogs", "blah", "badtype"]
|
||||
)
|
||||
assert 0 != result.exit_code
|
||||
assert 'Invalid value for "col_type"' in result.output
|
||||
|
||||
|
||||
def test_enable_fts(db_path):
|
||||
assert None == Database(db_path)["Gosh"].detect_fts()
|
||||
result = CliRunner().invoke(
|
||||
|
|
|
|||
|
|
@ -4,9 +4,10 @@ import datetime
|
|||
import json
|
||||
import pathlib
|
||||
import pytest
|
||||
import re
|
||||
import sqlite3
|
||||
|
||||
from .utils import collapse_whitespace
|
||||
|
||||
try:
|
||||
import pandas as pd
|
||||
except ImportError:
|
||||
|
|
@ -364,10 +365,3 @@ def test_create_table_numpy(fresh_db):
|
|||
"np.uint8": 8,
|
||||
}
|
||||
] == list(fresh_db["types"].rows)
|
||||
|
||||
|
||||
COLLAPSE_RE = re.compile("\s+")
|
||||
|
||||
|
||||
def collapse_whitespace(s):
|
||||
return COLLAPSE_RE.sub(" ", s)
|
||||
|
|
|
|||
7
tests/utils.py
Normal file
7
tests/utils.py
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
import re
|
||||
|
||||
COLLAPSE_RE = re.compile(r"\s+")
|
||||
|
||||
|
||||
def collapse_whitespace(s):
|
||||
return COLLAPSE_RE.sub(" ", s)
|
||||
Loading…
Add table
Add a link
Reference in a new issue