mirror of
https://github.com/simonw/sqlite-utils.git
synced 2026-08-14 05:14:22 +02:00
Use db.table() and db.view() in tests, closes #838
This commit is contained in:
parent
43d5d3331f
commit
38fe466700
43 changed files with 1321 additions and 1254 deletions
|
|
@ -72,13 +72,13 @@ def test_views(db_path):
|
|||
|
||||
|
||||
def test_tables_fts4(db_path):
|
||||
Database(db_path)["Gosh"].enable_fts(["c2"], fts_version="FTS4")
|
||||
Database(db_path).table("Gosh").enable_fts(["c2"], fts_version="FTS4")
|
||||
result = CliRunner().invoke(cli.cli, ["tables", "--fts4", db_path])
|
||||
assert '[{"table": "Gosh_fts"}]' == result.output.strip()
|
||||
|
||||
|
||||
def test_tables_fts5(db_path):
|
||||
Database(db_path)["Gosh"].enable_fts(["c2"], fts_version="FTS5")
|
||||
Database(db_path).table("Gosh").enable_fts(["c2"], fts_version="FTS5")
|
||||
result = CliRunner().invoke(cli.cli, ["tables", "--fts5", db_path])
|
||||
assert '[{"table": "Gosh_fts"}]' == result.output.strip()
|
||||
|
||||
|
|
@ -86,7 +86,7 @@ def test_tables_fts5(db_path):
|
|||
def test_tables_counts_and_columns(db_path):
|
||||
db = Database(db_path)
|
||||
with db.conn:
|
||||
db["lots"].insert_all([{"id": i, "age": i + 1} for i in range(30)])
|
||||
db.table("lots").insert_all([{"id": i, "age": i + 1} for i in range(30)])
|
||||
result = CliRunner().invoke(cli.cli, ["tables", "--counts", "--columns", db_path])
|
||||
assert (
|
||||
'[{"table": "Gosh", "count": 0, "columns": ["c1", "c2", "c3"]},\n'
|
||||
|
|
@ -121,7 +121,7 @@ def test_tables_counts_and_columns(db_path):
|
|||
def test_tables_counts_and_columns_csv(db_path, format, expected):
|
||||
db = Database(db_path)
|
||||
with db.conn:
|
||||
db["lots"].insert_all([{"id": i, "age": i + 1} for i in range(30)])
|
||||
db.table("lots").insert_all([{"id": i, "age": i + 1} for i in range(30)])
|
||||
result = CliRunner().invoke(
|
||||
cli.cli, ["tables", "--counts", "--columns", format, db_path]
|
||||
)
|
||||
|
|
@ -131,7 +131,7 @@ def test_tables_counts_and_columns_csv(db_path, format, expected):
|
|||
def test_tables_schema(db_path):
|
||||
db = Database(db_path)
|
||||
with db.conn:
|
||||
db["lots"].insert_all([{"id": i, "age": i + 1} for i in range(30)])
|
||||
db.table("lots").insert_all([{"id": i, "age": i + 1} for i in range(30)])
|
||||
result = CliRunner().invoke(cli.cli, ["tables", "--schema", db_path])
|
||||
assert (
|
||||
'[{"table": "Gosh", "schema": "CREATE TABLE Gosh (c1 text, c2 text, c3 text)"},\n'
|
||||
|
|
@ -183,7 +183,7 @@ def test_tables_schema(db_path):
|
|||
def test_output_table(db_path, options, expected):
|
||||
db = Database(db_path)
|
||||
with db.conn:
|
||||
db["rows"].insert_all(
|
||||
db.table("rows").insert_all(
|
||||
[
|
||||
{
|
||||
"c1": f"verb{i}",
|
||||
|
|
@ -207,7 +207,7 @@ def test_output_table_no_headers(db_path, fmt_option):
|
|||
# tabulate formats and the column names were always printed.
|
||||
db = Database(db_path)
|
||||
with db.conn:
|
||||
db["dogs"].insert_all(
|
||||
db.table("dogs").insert_all(
|
||||
[
|
||||
{"id": 1, "name": "Cleo", "age": 4},
|
||||
{"id": 2, "name": "Pancakes", "age": 2},
|
||||
|
|
@ -244,14 +244,14 @@ def test_output_table_no_headers(db_path, fmt_option):
|
|||
|
||||
def test_create_index(db_path):
|
||||
db = Database(db_path)
|
||||
assert [] == db["Gosh"].indexes
|
||||
assert [] == db.table("Gosh").indexes
|
||||
result = CliRunner().invoke(cli.cli, ["create-index", db_path, "Gosh", "c1"])
|
||||
assert result.exit_code == 0
|
||||
assert [
|
||||
Index(
|
||||
seq=0, name="idx_Gosh_c1", unique=0, origin="c", partial=0, columns=["c1"]
|
||||
)
|
||||
] == db["Gosh"].indexes
|
||||
] == db.table("Gosh").indexes
|
||||
# Try with a custom name
|
||||
result = CliRunner().invoke(
|
||||
cli.cli, ["create-index", db_path, "Gosh", "c2", "--name", "blah"]
|
||||
|
|
@ -262,7 +262,7 @@ def test_create_index(db_path):
|
|||
Index(
|
||||
seq=1, name="idx_Gosh_c1", unique=0, origin="c", partial=0, columns=["c1"]
|
||||
),
|
||||
] == db["Gosh"].indexes
|
||||
] == db.table("Gosh").indexes
|
||||
# Try a two-column unique index
|
||||
create_index_unique_args = [
|
||||
"create-index",
|
||||
|
|
@ -283,7 +283,7 @@ def test_create_index(db_path):
|
|||
partial=0,
|
||||
columns=["c1", "c2"],
|
||||
)
|
||||
] == db["Gosh2"].indexes
|
||||
] == db.table("Gosh2").indexes
|
||||
# Trying to create the same index should fail
|
||||
assert CliRunner().invoke(cli.cli, create_index_unique_args).exit_code != 0
|
||||
# ... unless we use --if-not-exists or --ignore
|
||||
|
|
@ -296,11 +296,11 @@ def test_create_index(db_path):
|
|||
|
||||
def test_drop_index(db_path):
|
||||
db = Database(db_path)
|
||||
db["Gosh"].create_index(["c1"])
|
||||
assert [index.name for index in db["Gosh"].indexes] == ["idx_Gosh_c1"]
|
||||
db.table("Gosh").create_index(["c1"])
|
||||
assert [index.name for index in db.table("Gosh").indexes] == ["idx_Gosh_c1"]
|
||||
result = CliRunner().invoke(cli.cli, ["drop-index", db_path, "Gosh", "idx_Gosh_c1"])
|
||||
assert result.exit_code == 0
|
||||
assert db["Gosh"].indexes == []
|
||||
assert db.table("Gosh").indexes == []
|
||||
|
||||
result = CliRunner().invoke(cli.cli, ["drop-index", db_path, "Gosh", "idx_Gosh_c1"])
|
||||
assert result.exit_code == 1
|
||||
|
|
@ -315,7 +315,7 @@ def test_drop_index(db_path):
|
|||
def test_create_index_analyze(db_path):
|
||||
db = Database(db_path)
|
||||
assert "sqlite_stat1" not in db.table_names()
|
||||
assert [] == db["Gosh"].indexes
|
||||
assert [] == db.table("Gosh").indexes
|
||||
result = CliRunner().invoke(
|
||||
cli.cli, ["create-index", db_path, "Gosh", "c1", "--analyze"]
|
||||
)
|
||||
|
|
@ -325,7 +325,7 @@ def test_create_index_analyze(db_path):
|
|||
|
||||
def test_create_index_desc(db_path):
|
||||
db = Database(db_path)
|
||||
assert [] == db["Gosh"].indexes
|
||||
assert [] == db.table("Gosh").indexes
|
||||
result = CliRunner().invoke(cli.cli, ["create-index", db_path, "Gosh", "--", "-c1"])
|
||||
assert result.exit_code == 0
|
||||
assert (
|
||||
|
|
@ -361,12 +361,12 @@ def test_create_index_desc(db_path):
|
|||
def test_add_column(db_path, col_name, col_type, expected_schema):
|
||||
db = Database(db_path)
|
||||
db.create_table("dogs", {"name": str})
|
||||
assert db["dogs"].schema == 'CREATE TABLE "dogs" (\n "name" TEXT\n)'
|
||||
assert db.table("dogs").schema == 'CREATE TABLE "dogs" (\n "name" TEXT\n)'
|
||||
args = ["add-column", db_path, "dogs", col_name]
|
||||
if col_type is not None:
|
||||
args.append(col_type)
|
||||
assert CliRunner().invoke(cli.cli, args).exit_code == 0
|
||||
assert db["dogs"].schema == expected_schema
|
||||
assert db.table("dogs").schema == expected_schema
|
||||
|
||||
|
||||
@pytest.mark.parametrize("ignore", (True, False))
|
||||
|
|
@ -385,7 +385,7 @@ def test_add_column_ignore(db_path, ignore):
|
|||
def test_add_column_not_null_default(db_path):
|
||||
db = Database(db_path)
|
||||
db.create_table("dogs", {"name": str})
|
||||
assert db["dogs"].schema == 'CREATE TABLE "dogs" (\n "name" TEXT\n)'
|
||||
assert db.table("dogs").schema == 'CREATE TABLE "dogs" (\n "name" TEXT\n)'
|
||||
args = [
|
||||
"add-column",
|
||||
db_path,
|
||||
|
|
@ -395,7 +395,7 @@ def test_add_column_not_null_default(db_path):
|
|||
"dogs'dawg",
|
||||
]
|
||||
assert CliRunner().invoke(cli.cli, args).exit_code == 0
|
||||
assert db["dogs"].schema == (
|
||||
assert db.table("dogs").schema == (
|
||||
'CREATE TABLE "dogs" (\n'
|
||||
' "name" TEXT\n'
|
||||
", \"nickname\" TEXT NOT NULL DEFAULT 'dogs''dawg')"
|
||||
|
|
@ -415,10 +415,10 @@ def test_add_column_not_null_default(db_path):
|
|||
)
|
||||
def test_add_foreign_key(db_path, args, assert_message):
|
||||
db = Database(db_path)
|
||||
db["authors"].insert_all(
|
||||
db.table("authors").insert_all(
|
||||
[{"id": 1, "name": "Sally"}, {"id": 2, "name": "Asheesh"}], pk="id"
|
||||
)
|
||||
db["books"].insert_all(
|
||||
db.table("books").insert_all(
|
||||
[
|
||||
{"title": "Hedgehogs of the world", "author_id": 1},
|
||||
{"title": "How to train your wolf", "author_id": 2},
|
||||
|
|
@ -431,7 +431,7 @@ def test_add_foreign_key(db_path, args, assert_message):
|
|||
ForeignKey(
|
||||
table="books", column="author_id", other_table="authors", other_column="id"
|
||||
)
|
||||
] == db["books"].foreign_keys
|
||||
] == db.table("books").foreign_keys
|
||||
|
||||
# Error if we try to add it twice:
|
||||
result = CliRunner().invoke(
|
||||
|
|
@ -460,14 +460,14 @@ def test_add_foreign_key(db_path, args, assert_message):
|
|||
|
||||
def test_add_column_foreign_key(db_path):
|
||||
db = Database(db_path)
|
||||
db["authors"].insert({"id": 1, "name": "Sally"}, pk="id")
|
||||
db["books"].insert({"title": "Hedgehogs of the world"})
|
||||
db.table("authors").insert({"id": 1, "name": "Sally"}, pk="id")
|
||||
db.table("books").insert({"title": "Hedgehogs of the world"})
|
||||
# Add an author_id foreign key column to the books table
|
||||
result = CliRunner().invoke(
|
||||
cli.cli, ["add-column", db_path, "books", "author_id", "--fk", "authors"]
|
||||
)
|
||||
assert result.exit_code == 0, result.output
|
||||
assert db["books"].schema == (
|
||||
assert db.table("books").schema == (
|
||||
'CREATE TABLE "books" (\n'
|
||||
' "title" TEXT,\n'
|
||||
' "author_id" INTEGER REFERENCES "authors"("id")\n'
|
||||
|
|
@ -488,7 +488,7 @@ def test_add_column_foreign_key(db_path):
|
|||
],
|
||||
)
|
||||
assert result.exit_code == 0, result.output
|
||||
assert db["books"].schema == (
|
||||
assert db.table("books").schema == (
|
||||
'CREATE TABLE "books" (\n'
|
||||
' "title" TEXT,\n'
|
||||
' "author_id" INTEGER REFERENCES "authors"("id"),\n'
|
||||
|
|
@ -505,7 +505,7 @@ def test_add_column_foreign_key(db_path):
|
|||
|
||||
def test_suggest_alter_if_column_missing(db_path):
|
||||
db = Database(db_path)
|
||||
db["authors"].insert({"id": 1, "name": "Sally"}, pk="id")
|
||||
db.table("authors").insert({"id": 1, "name": "Sally"}, pk="id")
|
||||
result = CliRunner().invoke(
|
||||
cli.cli,
|
||||
["insert", db_path, "authors", "-"],
|
||||
|
|
@ -521,27 +521,27 @@ def test_suggest_alter_if_column_missing(db_path):
|
|||
def test_index_foreign_keys(db_path):
|
||||
test_add_column_foreign_key(db_path)
|
||||
db = Database(db_path)
|
||||
assert [] == db["books"].indexes
|
||||
assert [] == db.table("books").indexes
|
||||
result = CliRunner().invoke(cli.cli, ["index-foreign-keys", db_path])
|
||||
assert result.exit_code == 0
|
||||
assert [["author_id"], ["author_name_ref"]] == [
|
||||
i.columns for i in db["books"].indexes
|
||||
i.columns for i in db.table("books").indexes
|
||||
]
|
||||
|
||||
|
||||
def test_enable_fts(db_path):
|
||||
db = Database(db_path)
|
||||
assert db["Gosh"].detect_fts() is None
|
||||
assert db.table("Gosh").detect_fts() is None
|
||||
result = CliRunner().invoke(
|
||||
cli.cli, ["enable-fts", db_path, "Gosh", "c1", "--fts4"]
|
||||
)
|
||||
assert result.exit_code == 0
|
||||
assert "Gosh_fts" == db["Gosh"].detect_fts()
|
||||
assert "Gosh_fts" == db.table("Gosh").detect_fts()
|
||||
|
||||
# Table names with restricted chars are handled correctly.
|
||||
# colons and dots are restricted characters for table names.
|
||||
db["http://example.com"].create({"c1": str, "c2": str, "c3": str})
|
||||
assert db["http://example.com"].detect_fts() is None
|
||||
db.table("http://example.com").create({"c1": str, "c2": str, "c3": str})
|
||||
assert db.table("http://example.com").detect_fts() is None
|
||||
result = CliRunner().invoke(
|
||||
cli.cli,
|
||||
[
|
||||
|
|
@ -555,7 +555,7 @@ def test_enable_fts(db_path):
|
|||
],
|
||||
)
|
||||
assert result.exit_code == 0
|
||||
assert "http://example.com_fts" == db["http://example.com"].detect_fts()
|
||||
assert "http://example.com_fts" == db.table("http://example.com").detect_fts()
|
||||
# Check tokenize was set to porter
|
||||
assert (
|
||||
'CREATE VIRTUAL TABLE "http://example.com_fts" USING FTS4 (\n'
|
||||
|
|
@ -563,19 +563,19 @@ def test_enable_fts(db_path):
|
|||
" tokenize='porter',\n"
|
||||
' content="http://example.com"'
|
||||
"\n)"
|
||||
) == db["http://example.com_fts"].schema
|
||||
db["http://example.com"].drop()
|
||||
) == db.table("http://example.com_fts").schema
|
||||
db.table("http://example.com").drop()
|
||||
|
||||
|
||||
def test_enable_fts_replace(db_path):
|
||||
db = Database(db_path)
|
||||
assert db["Gosh"].detect_fts() is None
|
||||
assert db.table("Gosh").detect_fts() is None
|
||||
result = CliRunner().invoke(
|
||||
cli.cli, ["enable-fts", db_path, "Gosh", "c1", "--fts4"]
|
||||
)
|
||||
assert result.exit_code == 0
|
||||
assert "Gosh_fts" == db["Gosh"].detect_fts()
|
||||
assert db["Gosh_fts"].columns_dict == {"c1": str}
|
||||
assert "Gosh_fts" == db.table("Gosh").detect_fts()
|
||||
assert db.table("Gosh_fts").columns_dict == {"c1": str}
|
||||
|
||||
# This should throw an error
|
||||
result2 = CliRunner().invoke(
|
||||
|
|
@ -589,11 +589,11 @@ def test_enable_fts_replace(db_path):
|
|||
cli.cli, ["enable-fts", db_path, "Gosh", "c2", "--fts4", "--replace"]
|
||||
)
|
||||
assert result3.exit_code == 0
|
||||
assert db["Gosh_fts"].columns_dict == {"c2": str}
|
||||
assert db.table("Gosh_fts").columns_dict == {"c2": str}
|
||||
|
||||
|
||||
def test_enable_fts_with_triggers(db_path):
|
||||
Database(db_path)["Gosh"].insert_all([{"c1": "baz"}])
|
||||
Database(db_path).table("Gosh").insert_all([{"c1": "baz"}])
|
||||
exit_code = (
|
||||
CliRunner()
|
||||
.invoke(
|
||||
|
|
@ -612,12 +612,12 @@ def test_enable_fts_with_triggers(db_path):
|
|||
)
|
||||
|
||||
assert [("baz",)] == search("baz")
|
||||
Database(db_path)["Gosh"].insert_all([{"c1": "martha"}])
|
||||
Database(db_path).table("Gosh").insert_all([{"c1": "martha"}])
|
||||
assert [("martha",)] == search("martha")
|
||||
|
||||
|
||||
def test_populate_fts(db_path):
|
||||
Database(db_path)["Gosh"].insert_all([{"c1": "baz"}])
|
||||
Database(db_path).table("Gosh").insert_all([{"c1": "baz"}])
|
||||
exit_code = (
|
||||
CliRunner()
|
||||
.invoke(cli.cli, ["enable-fts", db_path, "Gosh", "c1", "--fts4"])
|
||||
|
|
@ -633,7 +633,7 @@ def test_populate_fts(db_path):
|
|||
)
|
||||
|
||||
assert [("baz",)] == search("baz")
|
||||
Database(db_path)["Gosh"].insert_all([{"c1": "martha"}])
|
||||
Database(db_path).table("Gosh").insert_all([{"c1": "martha"}])
|
||||
assert [] == search("martha")
|
||||
exit_code = (
|
||||
CliRunner().invoke(cli.cli, ["populate-fts", db_path, "Gosh", "c1"]).exit_code
|
||||
|
|
@ -645,7 +645,7 @@ def test_populate_fts(db_path):
|
|||
def test_disable_fts(db_path):
|
||||
db = Database(db_path)
|
||||
assert {"Gosh", "Gosh2"} == set(db.table_names())
|
||||
db["Gosh"].enable_fts(["c1"], create_triggers=True)
|
||||
db.table("Gosh").enable_fts(["c1"], create_triggers=True)
|
||||
assert {
|
||||
"Gosh_fts",
|
||||
"Gosh_fts_idx",
|
||||
|
|
@ -677,7 +677,7 @@ def test_optimize(db_path, tables):
|
|||
db = Database(db_path)
|
||||
with db.conn:
|
||||
for table in ("Gosh", "Gosh2"):
|
||||
db[table].insert_all(
|
||||
db.table(table).insert_all(
|
||||
[
|
||||
{
|
||||
"c1": f"verb{i}",
|
||||
|
|
@ -687,8 +687,8 @@ def test_optimize(db_path, tables):
|
|||
for i in range(10000)
|
||||
]
|
||||
)
|
||||
db["Gosh"].enable_fts(["c1", "c2", "c3"], fts_version="FTS4")
|
||||
db["Gosh2"].enable_fts(["c1", "c2", "c3"], fts_version="FTS5")
|
||||
db.table("Gosh").enable_fts(["c1", "c2", "c3"], fts_version="FTS4")
|
||||
db.table("Gosh2").enable_fts(["c1", "c2", "c3"], fts_version="FTS5")
|
||||
size_before_optimize = os.stat(db_path).st_size
|
||||
result = CliRunner().invoke(cli.cli, ["optimize", db_path] + tables)
|
||||
assert result.exit_code == 0
|
||||
|
|
@ -713,22 +713,22 @@ def test_rebuild_fts_fixes_docsize_error(db_path):
|
|||
for i in range(10000)
|
||||
]
|
||||
with db.conn:
|
||||
db["fts5_table"].insert_all(records, pk="c1")
|
||||
db["fts5_table"].enable_fts(
|
||||
db.table("fts5_table").insert_all(records, pk="c1")
|
||||
db.table("fts5_table").enable_fts(
|
||||
["c1", "c2", "c3"], fts_version="FTS5", create_triggers=True
|
||||
)
|
||||
# Search should work
|
||||
assert list(db["fts5_table"].search("verb1"))
|
||||
assert list(db.table("fts5_table").search("verb1"))
|
||||
# Replicate docsize error from this issue for FTS5
|
||||
# https://github.com/simonw/sqlite-utils/issues/149
|
||||
assert db["fts5_table_fts_docsize"].count == 10000
|
||||
db["fts5_table"].insert_all(records, replace=True)
|
||||
assert db["fts5_table"].count == 10000
|
||||
assert db["fts5_table_fts_docsize"].count == 20000
|
||||
assert db.table("fts5_table_fts_docsize").count == 10000
|
||||
db.table("fts5_table").insert_all(records, replace=True)
|
||||
assert db.table("fts5_table").count == 10000
|
||||
assert db.table("fts5_table_fts_docsize").count == 20000
|
||||
# Running rebuild-fts should fix this
|
||||
result = CliRunner().invoke(cli.cli, ["rebuild-fts", db_path, "fts5_table"])
|
||||
assert result.exit_code == 0
|
||||
assert db["fts5_table_fts_docsize"].count == 10000
|
||||
assert db.table("fts5_table_fts_docsize").count == 10000
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
|
|
@ -741,7 +741,7 @@ def test_rebuild_fts_fixes_docsize_error(db_path):
|
|||
def test_query_csv(db_path, format, expected):
|
||||
db = Database(db_path)
|
||||
with db.conn:
|
||||
db["dogs"].insert_all(
|
||||
db.table("dogs").insert_all(
|
||||
[
|
||||
{"id": 1, "age": 4, "name": "Cleo"},
|
||||
{"id": 2, "age": 2, "name": "Pancakes"},
|
||||
|
|
@ -793,7 +793,7 @@ _one_query = "select id, name, age from dogs where id = 1"
|
|||
def test_query_json(db_path, sql, args, expected):
|
||||
db = Database(db_path)
|
||||
with db.conn:
|
||||
db["dogs"].insert_all(
|
||||
db.table("dogs").insert_all(
|
||||
[
|
||||
{"id": 1, "age": 4, "name": "Cleo"},
|
||||
{"id": 2, "age": 2, "name": "Pancakes"},
|
||||
|
|
@ -807,7 +807,7 @@ def test_query_sql_from_stdin(db_path):
|
|||
# https://github.com/simonw/sqlite-utils/issues/765
|
||||
db = Database(db_path)
|
||||
with db.conn:
|
||||
db["dogs"].insert_all(
|
||||
db.table("dogs").insert_all(
|
||||
[
|
||||
{"id": 1, "age": 4, "name": "Cleo"},
|
||||
{"id": 2, "age": 2, "name": "Pancakes"},
|
||||
|
|
@ -1004,7 +1004,7 @@ LOREM_IPSUM_COMPRESSED = (
|
|||
def test_query_json_binary(db_path):
|
||||
db = Database(db_path)
|
||||
with db.conn:
|
||||
db["files"].insert(
|
||||
db.table("files").insert(
|
||||
{
|
||||
"name": "lorem.txt",
|
||||
"sz": 16984,
|
||||
|
|
@ -1059,7 +1059,7 @@ def test_query_params(db_path, sql, params, expected):
|
|||
def test_query_json_with_json_cols(db_path):
|
||||
db = Database(db_path)
|
||||
with db.conn:
|
||||
db["dogs"].insert(
|
||||
db.table("dogs").insert(
|
||||
{
|
||||
"id": 1,
|
||||
"name": "Cleo",
|
||||
|
|
@ -1088,7 +1088,7 @@ def test_query_json_with_json_cols(db_path):
|
|||
def test_query_json_unicode_not_escaped_by_default(db_path):
|
||||
db = Database(db_path)
|
||||
with db.conn:
|
||||
db["text"].insert({"id": 1, "text": "Japanese 日本語"}, pk="id")
|
||||
db.table("text").insert({"id": 1, "text": "Japanese 日本語"}, pk="id")
|
||||
result = CliRunner().invoke(cli.cli, [db_path, "select id, text from text"])
|
||||
assert result.exit_code == 0
|
||||
assert result.output.strip() == '[{"id": 1, "text": "Japanese 日本語"}]'
|
||||
|
|
@ -1102,7 +1102,7 @@ def test_query_json_unicode_not_escaped_by_default(db_path):
|
|||
def test_query_json_ascii_option(db_path, command):
|
||||
db = Database(db_path)
|
||||
with db.conn:
|
||||
db["text"].insert({"id": 1, "text": "Japanese 日本語"}, pk="id")
|
||||
db.table("text").insert({"id": 1, "text": "Japanese 日本語"}, pk="id")
|
||||
if command == "query":
|
||||
args = [db_path, "select id, text from text", "--ascii"]
|
||||
else:
|
||||
|
|
@ -1118,7 +1118,7 @@ def test_query_json_ascii_option(db_path, command):
|
|||
[(b"\x00\x0fbinary", True), ("this is text", False), (1, False), (1.5, False)],
|
||||
)
|
||||
def test_query_raw(db_path, content, is_binary):
|
||||
Database(db_path)["files"].insert({"content": content})
|
||||
Database(db_path).table("files").insert({"content": content})
|
||||
result = CliRunner().invoke(
|
||||
cli.cli, [db_path, "select content from files", "--raw"]
|
||||
)
|
||||
|
|
@ -1133,7 +1133,7 @@ def test_query_raw(db_path, content, is_binary):
|
|||
[(b"\x00\x0fbinary", True), ("this is text", False), (1, False), (1.5, False)],
|
||||
)
|
||||
def test_query_raw_lines(db_path, content, is_binary):
|
||||
Database(db_path)["files"].insert_all({"content": content} for _ in range(3))
|
||||
Database(db_path).table("files").insert_all({"content": content} for _ in range(3))
|
||||
result = CliRunner().invoke(
|
||||
cli.cli, [db_path, "select content from files", "--raw-lines"]
|
||||
)
|
||||
|
|
@ -1215,7 +1215,7 @@ def test_query_memory_does_not_create_file(tmpdir):
|
|||
def test_rows(db_path, args, expected):
|
||||
db = Database(db_path)
|
||||
with db.conn:
|
||||
db["dogs"].insert_all(
|
||||
db.table("dogs").insert_all(
|
||||
[
|
||||
{"id": 1, "age": 4, "name": "Cleo"},
|
||||
{"id": 2, "age": 2, "name": "Pancakes"},
|
||||
|
|
@ -1240,7 +1240,7 @@ def test_upsert(db_path, tmpdir):
|
|||
catch_exceptions=False,
|
||||
)
|
||||
assert result.exit_code == 0, result.output
|
||||
assert 2 == db["dogs"].count
|
||||
assert 2 == db.table("dogs").count
|
||||
# Now run the upsert to update just their ages
|
||||
upsert_dogs = [
|
||||
{"id": 1, "age": 5},
|
||||
|
|
@ -1295,8 +1295,8 @@ def test_upsert_pk_inferred_from_existing_table(db_path, tmpdir):
|
|||
|
||||
def test_upsert_analyze(db_path, tmpdir):
|
||||
db = Database(db_path)
|
||||
db["rows"].insert({"id": 1, "foo": "x", "n": 3}, pk="id")
|
||||
db["rows"].create_index(["n"])
|
||||
db.table("rows").insert({"id": 1, "foo": "x", "n": 3}, pk="id")
|
||||
db.table("rows").create_index(["n"])
|
||||
assert "sqlite_stat1" not in db.table_names()
|
||||
result = CliRunner().invoke(
|
||||
cli.cli,
|
||||
|
|
@ -1310,7 +1310,7 @@ def test_upsert_analyze(db_path, tmpdir):
|
|||
def test_upsert_flatten(tmpdir):
|
||||
db_path = str(tmpdir / "flat.db")
|
||||
db = Database(db_path)
|
||||
db["upsert_me"].insert({"id": 1, "name": "Example"}, pk="id")
|
||||
db.table("upsert_me").insert({"id": 1, "name": "Example"}, pk="id")
|
||||
result = CliRunner().invoke(
|
||||
cli.cli,
|
||||
["upsert", db_path, "upsert_me", "-", "--flatten", "--pk", "id", "--alter"],
|
||||
|
|
@ -1424,7 +1424,7 @@ def test_create_table(args, schema):
|
|||
)
|
||||
assert result.exit_code == 0
|
||||
db = Database("test.db")
|
||||
assert schema == db["t"].schema
|
||||
assert schema == db.table("t").schema
|
||||
|
||||
|
||||
def test_create_table_foreign_key():
|
||||
|
|
@ -1459,21 +1459,21 @@ def test_create_table_foreign_key():
|
|||
' "id" INTEGER PRIMARY KEY,\n'
|
||||
' "name" TEXT\n'
|
||||
")"
|
||||
) == db["authors"].schema
|
||||
) == db.table("authors").schema
|
||||
assert (
|
||||
'CREATE TABLE "books" (\n'
|
||||
' "id" INTEGER PRIMARY KEY,\n'
|
||||
' "title" TEXT,\n'
|
||||
' "author_id" INTEGER REFERENCES "authors"("id")\n'
|
||||
")"
|
||||
) == db["books"].schema
|
||||
) == db.table("books").schema
|
||||
|
||||
|
||||
def test_create_table_error_if_table_exists():
|
||||
runner = CliRunner()
|
||||
with runner.isolated_filesystem():
|
||||
db = Database("test.db")
|
||||
db["dogs"].insert({"name": "Cleo"})
|
||||
db.table("dogs").insert({"name": "Cleo"})
|
||||
result = runner.invoke(
|
||||
cli.cli, ["create-table", "test.db", "dogs", "id", "integer"]
|
||||
)
|
||||
|
|
@ -1488,24 +1488,24 @@ def test_create_table_ignore():
|
|||
runner = CliRunner()
|
||||
with runner.isolated_filesystem():
|
||||
db = Database("test.db")
|
||||
db["dogs"].insert({"name": "Cleo"})
|
||||
db.table("dogs").insert({"name": "Cleo"})
|
||||
result = runner.invoke(
|
||||
cli.cli, ["create-table", "test.db", "dogs", "id", "integer", "--ignore"]
|
||||
)
|
||||
assert result.exit_code == 0
|
||||
assert 'CREATE TABLE "dogs" (\n "name" TEXT\n)' == db["dogs"].schema
|
||||
assert 'CREATE TABLE "dogs" (\n "name" TEXT\n)' == db.table("dogs").schema
|
||||
|
||||
|
||||
def test_create_table_replace():
|
||||
runner = CliRunner()
|
||||
with runner.isolated_filesystem():
|
||||
db = Database("test.db")
|
||||
db["dogs"].insert({"name": "Cleo"})
|
||||
db.table("dogs").insert({"name": "Cleo"})
|
||||
result = runner.invoke(
|
||||
cli.cli, ["create-table", "test.db", "dogs", "id", "integer", "--replace"]
|
||||
)
|
||||
assert result.exit_code == 0
|
||||
assert 'CREATE TABLE "dogs" (\n "id" INTEGER\n)' == db["dogs"].schema
|
||||
assert 'CREATE TABLE "dogs" (\n "id" INTEGER\n)' == db.table("dogs").schema
|
||||
|
||||
|
||||
def test_create_view():
|
||||
|
|
@ -1517,7 +1517,8 @@ def test_create_view():
|
|||
)
|
||||
assert result.exit_code == 0
|
||||
assert (
|
||||
'CREATE VIEW "version" AS select sqlite_version()' == db["version"].schema
|
||||
'CREATE VIEW "version" AS select sqlite_version()'
|
||||
== db.view("version").schema
|
||||
)
|
||||
|
||||
|
||||
|
|
@ -1554,7 +1555,7 @@ def test_create_view_ignore():
|
|||
assert result.exit_code == 0
|
||||
assert (
|
||||
'CREATE VIEW "version" AS select sqlite_version() + 1'
|
||||
== db["version"].schema
|
||||
== db.view("version").schema
|
||||
)
|
||||
|
||||
|
||||
|
|
@ -1575,7 +1576,8 @@ def test_create_view_replace():
|
|||
)
|
||||
assert result.exit_code == 0
|
||||
assert (
|
||||
'CREATE VIEW "version" AS select sqlite_version()' == db["version"].schema
|
||||
'CREATE VIEW "version" AS select sqlite_version()'
|
||||
== db.view("version").schema
|
||||
)
|
||||
|
||||
|
||||
|
|
@ -1583,7 +1585,7 @@ def test_drop_table():
|
|||
runner = CliRunner()
|
||||
with runner.isolated_filesystem():
|
||||
db = Database("test.db")
|
||||
db["t"].create({"pk": int}, pk="pk")
|
||||
db.table("t").create({"pk": int}, pk="pk")
|
||||
assert "t" in db.table_names()
|
||||
result = runner.invoke(
|
||||
cli.cli,
|
||||
|
|
@ -1601,7 +1603,7 @@ def test_drop_table_error():
|
|||
runner = CliRunner()
|
||||
with runner.isolated_filesystem():
|
||||
db = Database("test.db")
|
||||
db["t"].create({"pk": int}, pk="pk")
|
||||
db.table("t").create({"pk": int}, pk="pk")
|
||||
result = runner.invoke(
|
||||
cli.cli,
|
||||
[
|
||||
|
|
@ -1624,7 +1626,7 @@ def test_drop_table_on_view_errors():
|
|||
runner = CliRunner()
|
||||
with runner.isolated_filesystem():
|
||||
db = Database("test.db")
|
||||
db["t"].insert({"id": 1})
|
||||
db.table("t").insert({"id": 1})
|
||||
db.create_view("v", "select * from t")
|
||||
result = runner.invoke(cli.cli, ["drop-table", "test.db", "v"])
|
||||
assert result.exit_code == 1
|
||||
|
|
@ -1660,7 +1662,7 @@ def test_drop_view_on_table_errors():
|
|||
runner = CliRunner()
|
||||
with runner.isolated_filesystem():
|
||||
db = Database("test.db")
|
||||
db["t"].insert({"id": 1})
|
||||
db.table("t").insert({"id": 1})
|
||||
result = runner.invoke(cli.cli, ["drop-view", "test.db", "t"])
|
||||
assert result.exit_code == 1
|
||||
assert 'Error: "t" is a table, not a view - use drop-table to drop it' == (
|
||||
|
|
@ -1677,7 +1679,7 @@ def test_drop_view_error():
|
|||
runner = CliRunner()
|
||||
with runner.isolated_filesystem():
|
||||
db = Database("test.db")
|
||||
db["t"].create({"pk": int}, pk="pk")
|
||||
db.table("t").create({"pk": int}, pk="pk")
|
||||
result = runner.invoke(
|
||||
cli.cli,
|
||||
[
|
||||
|
|
@ -1702,7 +1704,7 @@ def test_enable_wal():
|
|||
with runner.isolated_filesystem():
|
||||
for dbname in dbs:
|
||||
db = Database(dbname)
|
||||
db["t"].create({"pk": int}, pk="pk")
|
||||
db.table("t").create({"pk": int}, pk="pk")
|
||||
assert db.journal_mode == "delete"
|
||||
result = runner.invoke(cli.cli, ["enable-wal"] + dbs, catch_exceptions=False)
|
||||
assert result.exit_code == 0
|
||||
|
|
@ -1717,7 +1719,7 @@ def test_disable_wal():
|
|||
with runner.isolated_filesystem():
|
||||
for dbname in dbs:
|
||||
db = Database(dbname)
|
||||
db["t"].create({"pk": int}, pk="pk")
|
||||
db.table("t").create({"pk": int}, pk="pk")
|
||||
db.enable_wal()
|
||||
assert db.journal_mode == "wal"
|
||||
result = runner.invoke(cli.cli, ["disable-wal"] + dbs)
|
||||
|
|
@ -1740,7 +1742,7 @@ def test_disable_wal():
|
|||
def test_query_update(db_path, args, expected):
|
||||
db = Database(db_path)
|
||||
with db.conn:
|
||||
db["dogs"].insert_all(
|
||||
db.table("dogs").insert_all(
|
||||
[
|
||||
{"id": 1, "age": 4, "name": "Cleo"},
|
||||
]
|
||||
|
|
@ -1756,11 +1758,13 @@ def test_query_update(db_path, args, expected):
|
|||
|
||||
def test_add_foreign_keys(db_path):
|
||||
db = Database(db_path)
|
||||
db["countries"].insert({"id": 7, "name": "Panama"}, pk="id")
|
||||
db["authors"].insert({"id": 3, "name": "Matilda", "country_id": 7}, pk="id")
|
||||
db["books"].insert({"id": 2, "title": "Wolf anatomy", "author_id": 3}, pk="id")
|
||||
assert db["authors"].foreign_keys == []
|
||||
assert db["books"].foreign_keys == []
|
||||
db.table("countries").insert({"id": 7, "name": "Panama"}, pk="id")
|
||||
db.table("authors").insert({"id": 3, "name": "Matilda", "country_id": 7}, pk="id")
|
||||
db.table("books").insert(
|
||||
{"id": 2, "title": "Wolf anatomy", "author_id": 3}, pk="id"
|
||||
)
|
||||
assert db.table("authors").foreign_keys == []
|
||||
assert db.table("books").foreign_keys == []
|
||||
result = CliRunner().invoke(
|
||||
cli.cli,
|
||||
[
|
||||
|
|
@ -1777,7 +1781,7 @@ def test_add_foreign_keys(db_path):
|
|||
],
|
||||
)
|
||||
assert result.exit_code == 0
|
||||
assert db["authors"].foreign_keys == [
|
||||
assert db.table("authors").foreign_keys == [
|
||||
ForeignKey(
|
||||
table="authors",
|
||||
column="country_id",
|
||||
|
|
@ -1785,7 +1789,7 @@ def test_add_foreign_keys(db_path):
|
|||
other_column="id",
|
||||
)
|
||||
]
|
||||
assert db["books"].foreign_keys == [
|
||||
assert db.table("books").foreign_keys == [
|
||||
ForeignKey(
|
||||
table="books", column="author_id", other_table="authors", other_column="id"
|
||||
)
|
||||
|
|
@ -1909,7 +1913,7 @@ def test_add_foreign_keys(db_path):
|
|||
def test_transform(db_path, args, expected_schema):
|
||||
db = Database(db_path)
|
||||
with db.conn:
|
||||
db["dogs"].insert(
|
||||
db.table("dogs").insert(
|
||||
{"id": 1, "age": 4, "name": "Cleo"},
|
||||
not_null={"age"},
|
||||
defaults={"age": 1},
|
||||
|
|
@ -1918,20 +1922,20 @@ def test_transform(db_path, args, expected_schema):
|
|||
result = CliRunner().invoke(cli.cli, ["transform", db_path, "dogs"] + args)
|
||||
print(result.output)
|
||||
assert result.exit_code == 0
|
||||
schema = db["dogs"].schema
|
||||
schema = db.table("dogs").schema
|
||||
assert schema == expected_schema
|
||||
|
||||
|
||||
def test_transform_sql(db_path):
|
||||
db = Database(db_path)
|
||||
with db.conn:
|
||||
db["dogs"].insert(
|
||||
db.table("dogs").insert(
|
||||
{"id": 1, "age": 4, "name": "Cleo"},
|
||||
not_null={"age"},
|
||||
defaults={"age": 1},
|
||||
pk="id",
|
||||
)
|
||||
original_schema = db["dogs"].schema
|
||||
original_schema = db.table("dogs").schema
|
||||
|
||||
result = CliRunner().invoke(
|
||||
cli.cli, ["transform", db_path, "dogs", "--drop", "name", "--sql"]
|
||||
|
|
@ -1942,7 +1946,7 @@ def test_transform_sql(db_path):
|
|||
assert '"age" INTEGER NOT NULL DEFAULT' in result.output
|
||||
assert 'DROP TABLE "dogs";' in result.output
|
||||
assert 'ALTER TABLE "dogs_new_' in result.output
|
||||
assert db["dogs"].schema == original_schema
|
||||
assert db.table("dogs").schema == original_schema
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
|
|
@ -1958,12 +1962,12 @@ def test_transform_strict_option(db_path, initial_strict, args, expected_strict)
|
|||
db = Database(db_path)
|
||||
if not db.supports_strict:
|
||||
pytest.skip("SQLite version does not support strict tables")
|
||||
db["dogs"].create({"id": int}, strict=initial_strict)
|
||||
db.table("dogs").create({"id": int}, strict=initial_strict)
|
||||
|
||||
result = CliRunner().invoke(cli.cli, ["transform", db_path, "dogs"] + args)
|
||||
|
||||
assert result.exit_code == 0, result.output
|
||||
assert db["dogs"].strict is expected_strict
|
||||
assert db.table("dogs").strict is expected_strict
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
|
|
@ -1977,20 +1981,20 @@ def test_transform_strict_option_sql(db_path, initial_strict, flag, sql_is_stric
|
|||
db = Database(db_path)
|
||||
if not db.supports_strict:
|
||||
pytest.skip("SQLite version does not support strict tables")
|
||||
db["dogs"].create({"id": int}, strict=initial_strict)
|
||||
db.table("dogs").create({"id": int}, strict=initial_strict)
|
||||
|
||||
result = CliRunner().invoke(cli.cli, ["transform", db_path, "dogs", flag, "--sql"])
|
||||
|
||||
assert result.exit_code == 0, result.output
|
||||
assert (") STRICT;" in result.output) is sql_is_strict
|
||||
assert db["dogs"].strict is initial_strict
|
||||
assert db.table("dogs").strict is initial_strict
|
||||
|
||||
|
||||
def test_transform_strict_option_with_invalid_data(db_path):
|
||||
db = Database(db_path)
|
||||
if not db.supports_strict:
|
||||
pytest.skip("SQLite version does not support strict tables")
|
||||
dogs = db["dogs"]
|
||||
dogs = db.table("dogs")
|
||||
dogs.create({"id": int})
|
||||
dogs.insert({"id": "not-an-integer"})
|
||||
|
||||
|
|
@ -2048,10 +2052,10 @@ def test_transform_add_or_drop_foreign_key(db_path, extra_args, expected_schema)
|
|||
db = Database(db_path)
|
||||
with db.conn:
|
||||
# Create table with three foreign keys so we can drop two of them
|
||||
db["continent"].insert({"id": 1, "name": "Europe"}, pk="id")
|
||||
db["country"].insert({"id": 1, "name": "France"}, pk="id")
|
||||
db["city"].insert({"id": 24, "name": "Paris"}, pk="id")
|
||||
db["places"].insert(
|
||||
db.table("continent").insert({"id": 1, "name": "Europe"}, pk="id")
|
||||
db.table("country").insert({"id": 1, "name": "France"}, pk="id")
|
||||
db.table("city").insert({"id": 24, "name": "Paris"}, pk="id")
|
||||
db.table("places").insert(
|
||||
{
|
||||
"id": 32,
|
||||
"name": "Caveau de la Huchette",
|
||||
|
|
@ -2072,7 +2076,7 @@ def test_transform_add_or_drop_foreign_key(db_path, extra_args, expected_schema)
|
|||
+ extra_args,
|
||||
)
|
||||
assert result.exit_code == 0
|
||||
schema = db["places"].schema
|
||||
schema = db.table("places").schema
|
||||
assert schema == expected_schema
|
||||
|
||||
|
||||
|
|
@ -2133,7 +2137,7 @@ _common_other_schema = (
|
|||
def test_extract(db_path, args, expected_table_schema, expected_other_schema):
|
||||
db = Database(db_path)
|
||||
with db.conn:
|
||||
db["trees"].insert(
|
||||
db.table("trees").insert(
|
||||
{"id": 1, "address": "4 Park Ave", "species": "Palm"},
|
||||
pk="id",
|
||||
)
|
||||
|
|
@ -2142,7 +2146,7 @@ def test_extract(db_path, args, expected_table_schema, expected_other_schema):
|
|||
)
|
||||
print(result.output)
|
||||
assert result.exit_code == 0
|
||||
schema = db["trees"].schema
|
||||
schema = db.table("trees").schema
|
||||
assert schema == expected_table_schema
|
||||
other_schema = next(
|
||||
t for t in db.tables if t.name not in ("trees", "Gosh", "Gosh2")
|
||||
|
|
@ -2190,7 +2194,7 @@ def test_insert_encoding(tmpdir):
|
|||
)
|
||||
assert good_result.exit_code == 0
|
||||
db = Database(db_path)
|
||||
assert list(db["places"].rows) == [
|
||||
assert list(db.table("places").rows) == [
|
||||
{
|
||||
"date": "2020-01-01",
|
||||
"name": "Barra da Lagoa",
|
||||
|
|
@ -2226,7 +2230,7 @@ def test_insert_encoding(tmpdir):
|
|||
def test_search(tmpdir, fts, extra_arg, expected):
|
||||
db_path = str(tmpdir / "test.db")
|
||||
db = Database(db_path)
|
||||
db["articles"].insert_all(
|
||||
db.table("articles").insert_all(
|
||||
[
|
||||
{"id": 1, "title": "Title the first"},
|
||||
{"id": 2, "title": "Title the second"},
|
||||
|
|
@ -2234,7 +2238,7 @@ def test_search(tmpdir, fts, extra_arg, expected):
|
|||
],
|
||||
pk="id",
|
||||
)
|
||||
db["articles"].enable_fts(["title"], fts_version=fts)
|
||||
db.table("articles").enable_fts(["title"], fts_version=fts)
|
||||
result = CliRunner().invoke(
|
||||
cli.cli,
|
||||
["search", db_path, "articles", "second"] + ([extra_arg] if extra_arg else []),
|
||||
|
|
@ -2247,7 +2251,7 @@ def test_search(tmpdir, fts, extra_arg, expected):
|
|||
def test_search_quote(tmpdir):
|
||||
db_path = str(tmpdir / "test.db")
|
||||
db = Database(db_path)
|
||||
db["creatures"].insert({"name": "dog."}).enable_fts(["name"])
|
||||
db.table("creatures").insert({"name": "dog."}).enable_fts(["name"])
|
||||
# Without --quote should return an error
|
||||
error_result = CliRunner().invoke(cli.cli, ["search", db_path, "creatures", 'dog"'])
|
||||
assert error_result.exit_code == 1
|
||||
|
|
@ -2355,11 +2359,11 @@ _TRIGGERS_EXPECTED = (
|
|||
def test_triggers(tmpdir, extra_args, expected):
|
||||
db_path = str(tmpdir / "test.db")
|
||||
db = Database(db_path)
|
||||
db["articles"].insert(
|
||||
db.table("articles").insert(
|
||||
{"id": 1, "title": "Title the first"},
|
||||
pk="id",
|
||||
)
|
||||
db["counter"].insert({"count": 1})
|
||||
db.table("counter").insert({"count": 1})
|
||||
db.conn.execute(textwrap.dedent("""
|
||||
CREATE TRIGGER blah AFTER INSERT ON articles
|
||||
BEGIN
|
||||
|
|
@ -2420,9 +2424,9 @@ def test_triggers(tmpdir, extra_args, expected):
|
|||
def test_schema(tmpdir, options, expected):
|
||||
db_path = str(tmpdir / "test.db")
|
||||
db = Database(db_path)
|
||||
db["dogs"].create({"id": int, "name": str})
|
||||
db["chickens"].create({"id": int, "name": str, "breed": str})
|
||||
db["chickens"].create_index(["breed"])
|
||||
db.table("dogs").create({"id": int, "name": str})
|
||||
db.table("chickens").create({"id": int, "name": str, "breed": str})
|
||||
db.table("chickens").create_index(["breed"])
|
||||
result = CliRunner().invoke(
|
||||
cli.cli,
|
||||
["schema", db_path] + options,
|
||||
|
|
@ -2446,7 +2450,7 @@ def test_long_csv_column_value(tmpdir):
|
|||
)
|
||||
assert result.exit_code == 0
|
||||
db = Database(db_path)
|
||||
rows = list(db["bigtable"].rows)
|
||||
rows = list(db.table("bigtable").rows)
|
||||
assert len(rows) == 1
|
||||
assert rows[0]["text"] == long_string
|
||||
|
||||
|
|
@ -2473,7 +2477,7 @@ def test_import_no_headers(tmpdir, args, tsv):
|
|||
)
|
||||
assert result.exit_code == 0, result.output
|
||||
db = Database(db_path)
|
||||
schema = db["creatures"].schema
|
||||
schema = db.table("creatures").schema
|
||||
assert schema == (
|
||||
'CREATE TABLE "creatures" (\n'
|
||||
' "untitled_1" TEXT,\n'
|
||||
|
|
@ -2481,7 +2485,7 @@ def test_import_no_headers(tmpdir, args, tsv):
|
|||
' "untitled_3" TEXT\n'
|
||||
")"
|
||||
)
|
||||
rows = list(db["creatures"].rows)
|
||||
rows = list(db.table("creatures").rows)
|
||||
assert rows == [
|
||||
{"untitled_1": "Cleo", "untitled_2": "Dog", "untitled_3": "5"},
|
||||
{"untitled_1": "Tracy", "untitled_2": "Spider", "untitled_3": "7"},
|
||||
|
|
@ -2493,10 +2497,10 @@ def test_attach(tmpdir):
|
|||
bar_path = str(tmpdir / "bar.db")
|
||||
db = Database(foo_path)
|
||||
with db.conn:
|
||||
db["foo"].insert({"id": 1, "text": "foo"})
|
||||
db.table("foo").insert({"id": 1, "text": "foo"})
|
||||
db2 = Database(bar_path)
|
||||
with db2.conn:
|
||||
db2["bar"].insert({"id": 1, "text": "bar"})
|
||||
db2.table("bar").insert({"id": 1, "text": "bar"})
|
||||
db.attach("bar", bar_path)
|
||||
sql = "select * from foo union all select * from bar.bar"
|
||||
result = CliRunner().invoke(
|
||||
|
|
@ -2557,7 +2561,7 @@ def test_insert_detect_types(tmpdir):
|
|||
)
|
||||
assert result.exit_code == 0
|
||||
db = Database(db_path)
|
||||
assert list(db["creatures"].rows) == [
|
||||
assert list(db.table("creatures").rows) == [
|
||||
{"name": "Cleo", "age": 6, "weight": 45.5},
|
||||
{"name": "Dori", "age": 1, "weight": 3.5},
|
||||
]
|
||||
|
|
@ -2589,7 +2593,7 @@ def test_upsert_detect_types(tmpdir):
|
|||
)
|
||||
assert result.exit_code == 0
|
||||
db = Database(db_path)
|
||||
assert list(db["creatures"].rows) == [
|
||||
assert list(db.table("creatures").rows) == [
|
||||
{"id": 1, "name": "Cleo", "age": 6, "weight": 45.5},
|
||||
{"id": 2, "name": "Dori", "age": 1, "weight": 3.5},
|
||||
]
|
||||
|
|
@ -2608,7 +2612,7 @@ def test_csv_detect_types_creates_real_columns(tmpdir):
|
|||
assert result.exit_code == 0
|
||||
db = Database(db_path)
|
||||
# Check that the schema uses REAL for the weight column
|
||||
assert db["creatures"].schema == (
|
||||
assert db.table("creatures").schema == (
|
||||
'CREATE TABLE "creatures" (\n'
|
||||
' "name" TEXT,\n'
|
||||
' "age" INTEGER,\n'
|
||||
|
|
@ -2630,11 +2634,11 @@ def test_insert_no_detect_types(tmpdir):
|
|||
assert result.exit_code == 0
|
||||
db = Database(db_path)
|
||||
# All columns should be TEXT when --no-detect-types is used
|
||||
assert list(db["creatures"].rows) == [
|
||||
assert list(db.table("creatures").rows) == [
|
||||
{"name": "Cleo", "age": "6", "weight": "45.5"},
|
||||
{"name": "Dori", "age": "1", "weight": "3.5"},
|
||||
]
|
||||
assert db["creatures"].schema == (
|
||||
assert db.table("creatures").schema == (
|
||||
'CREATE TABLE "creatures" (\n'
|
||||
' "name" TEXT,\n'
|
||||
' "age" TEXT,\n'
|
||||
|
|
@ -2665,11 +2669,11 @@ def test_upsert_no_detect_types(tmpdir):
|
|||
assert result.exit_code == 0
|
||||
db = Database(db_path)
|
||||
# All columns should be TEXT when --no-detect-types is used
|
||||
assert list(db["creatures"].rows) == [
|
||||
assert list(db.table("creatures").rows) == [
|
||||
{"id": "1", "name": "Cleo", "age": "6", "weight": "45.5"},
|
||||
{"id": "2", "name": "Dori", "age": "1", "weight": "3.5"},
|
||||
]
|
||||
assert db["creatures"].schema == (
|
||||
assert db.table("creatures").schema == (
|
||||
'CREATE TABLE "creatures" (\n'
|
||||
' "id" TEXT PRIMARY KEY,\n'
|
||||
' "name" TEXT,\n'
|
||||
|
|
@ -2751,20 +2755,20 @@ def test_create_database(tmpdir, enable_wal):
|
|||
def test_analyze(tmpdir, options, expected):
|
||||
db_path = str(tmpdir / "test.db")
|
||||
db = Database(db_path)
|
||||
db["one_index"].insert({"id": 1, "name": "Cleo"}, pk="id")
|
||||
db["one_index"].create_index(["name"])
|
||||
db["two_indexes"].insert({"id": 1, "name": "Cleo", "species": "dog"}, pk="id")
|
||||
db["two_indexes"].create_index(["name"])
|
||||
db["two_indexes"].create_index(["species"])
|
||||
db.table("one_index").insert({"id": 1, "name": "Cleo"}, pk="id")
|
||||
db.table("one_index").create_index(["name"])
|
||||
db.table("two_indexes").insert({"id": 1, "name": "Cleo", "species": "dog"}, pk="id")
|
||||
db.table("two_indexes").create_index(["name"])
|
||||
db.table("two_indexes").create_index(["species"])
|
||||
result = CliRunner().invoke(cli.cli, ["analyze", db_path] + options)
|
||||
assert result.exit_code == 0
|
||||
assert list(db["sqlite_stat1"].rows) == expected
|
||||
assert list(db.table("sqlite_stat1").rows) == expected
|
||||
|
||||
|
||||
def test_rename_table(tmpdir):
|
||||
db_path = str(tmpdir / "test.db")
|
||||
db = Database(db_path)
|
||||
db["one"].insert({"id": 1, "name": "Cleo"}, pk="id")
|
||||
db.table("one").insert({"id": 1, "name": "Cleo"}, pk="id")
|
||||
# First try a non-existent table
|
||||
result_error = CliRunner().invoke(
|
||||
cli.cli,
|
||||
|
|
@ -2782,7 +2786,7 @@ def test_rename_table(tmpdir):
|
|||
catch_exceptions=False,
|
||||
)
|
||||
assert result_error2.exit_code == 0
|
||||
previous_columns = db["one"].columns_dict
|
||||
previous_columns = db.table("one").columns_dict
|
||||
# Now try for a table that exists
|
||||
result = CliRunner().invoke(
|
||||
cli.cli,
|
||||
|
|
@ -2790,13 +2794,13 @@ def test_rename_table(tmpdir):
|
|||
catch_exceptions=False,
|
||||
)
|
||||
assert result.exit_code == 0
|
||||
assert db["two"].columns_dict == previous_columns
|
||||
assert db.table("two").columns_dict == previous_columns
|
||||
|
||||
|
||||
def test_duplicate_table(tmpdir):
|
||||
db_path = str(tmpdir / "test.db")
|
||||
db = Database(db_path)
|
||||
db["one"].insert({"id": 1, "name": "Cleo"}, pk="id")
|
||||
db.table("one").insert({"id": 1, "name": "Cleo"}, pk="id")
|
||||
# First try a non-existent table
|
||||
result_error = CliRunner().invoke(
|
||||
cli.cli,
|
||||
|
|
@ -2819,8 +2823,8 @@ def test_duplicate_table(tmpdir):
|
|||
catch_exceptions=False,
|
||||
)
|
||||
assert result.exit_code == 0
|
||||
assert db["one"].columns_dict == db["two"].columns_dict
|
||||
assert list(db["one"].rows) == list(db["two"].rows)
|
||||
assert db.table("one").columns_dict == db.table("two").columns_dict
|
||||
assert list(db.table("one").rows) == list(db.table("two").rows)
|
||||
|
||||
|
||||
@pytest.mark.skipif(not _has_compiled_ext(), reason="Requires compiled ext.c")
|
||||
|
|
@ -2863,9 +2867,9 @@ def test_create_table_strict(strict):
|
|||
+ (["--strict"] if strict else []),
|
||||
)
|
||||
assert result.exit_code == 0
|
||||
assert db["items"].strict == strict or not db.supports_strict
|
||||
assert db.table("items").strict == strict or not db.supports_strict
|
||||
# Should have a floating point column
|
||||
assert db["items"].columns_dict == {"id": int, "w": float}
|
||||
assert db.table("items").columns_dict == {"id": int, "w": float}
|
||||
|
||||
|
||||
@pytest.mark.parametrize("method", ("insert", "upsert"))
|
||||
|
|
@ -2880,12 +2884,12 @@ def test_insert_upsert_strict(tmpdir, method, strict):
|
|||
)
|
||||
assert result.exit_code == 0
|
||||
db = Database(db_path)
|
||||
assert db["items"].strict == strict or not db.supports_strict
|
||||
assert db.table("items").strict == strict or not db.supports_strict
|
||||
|
||||
|
||||
def test_extract_bad_column_clean_error(db_path):
|
||||
db = Database(db_path)
|
||||
db["trees"].insert({"id": 1, "species": "Palm"}, pk="id")
|
||||
db.table("trees").insert({"id": 1, "species": "Palm"}, pk="id")
|
||||
result = CliRunner().invoke(cli.cli, ["extract", db_path, "trees", "nope"])
|
||||
assert result.exit_code == 1
|
||||
assert result.exception is None or isinstance(result.exception, SystemExit)
|
||||
|
|
@ -2894,7 +2898,7 @@ def test_extract_bad_column_clean_error(db_path):
|
|||
|
||||
def test_extract_view_clean_error(db_path):
|
||||
db = Database(db_path)
|
||||
db["trees"].insert({"id": 1, "species": "Palm"}, pk="id")
|
||||
db.table("trees").insert({"id": 1, "species": "Palm"}, pk="id")
|
||||
db.create_view("v", "select * from trees")
|
||||
result = CliRunner().invoke(cli.cli, ["extract", db_path, "v", "species"])
|
||||
assert result.exit_code == 1
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue