mirror of
https://github.com/simonw/sqlite-utils.git
synced 2026-09-09 01:54:21 +02:00
Swapped the order of a bunch of pytest comparisons
When I wrote this I thought constant == value was a better assertion. I no longer think that.
This commit is contained in:
parent
b4735f794a
commit
d2bcdc00c6
10 changed files with 82 additions and 82 deletions
|
|
@ -237,7 +237,7 @@ def test_create_index(db_path):
|
||||||
)
|
)
|
||||||
] == db["Gosh2"].indexes
|
] == db["Gosh2"].indexes
|
||||||
# Trying to create the same index should fail
|
# Trying to create the same index should fail
|
||||||
assert 0 != CliRunner().invoke(cli.cli, create_index_unique_args).exit_code
|
assert CliRunner().invoke(cli.cli, create_index_unique_args).exit_code != 0
|
||||||
# ... unless we use --if-not-exists or --ignore
|
# ... unless we use --if-not-exists or --ignore
|
||||||
for option in ("--if-not-exists", "--ignore"):
|
for option in ("--if-not-exists", "--ignore"):
|
||||||
assert (
|
assert (
|
||||||
|
|
@ -349,7 +349,7 @@ def test_add_foreign_key(db_path, args, assert_message):
|
||||||
]
|
]
|
||||||
)
|
)
|
||||||
assert (
|
assert (
|
||||||
0 == CliRunner().invoke(cli.cli, ["add-foreign-key", db_path] + args).exit_code
|
CliRunner().invoke(cli.cli, ["add-foreign-key", db_path] + args).exit_code == 0
|
||||||
), assert_message
|
), assert_message
|
||||||
assert [
|
assert [
|
||||||
ForeignKey(
|
ForeignKey(
|
||||||
|
|
@ -1105,7 +1105,7 @@ def test_upsert_alter(db_path, tmpdir):
|
||||||
result = CliRunner().invoke(
|
result = CliRunner().invoke(
|
||||||
cli.cli, ["upsert", db_path, "dogs", json_path, "--pk", "id"]
|
cli.cli, ["upsert", db_path, "dogs", json_path, "--pk", "id"]
|
||||||
)
|
)
|
||||||
assert 1 == result.exit_code
|
assert result.exit_code == 1
|
||||||
assert (
|
assert (
|
||||||
"Error: no such column: age\n\n"
|
"Error: no such column: age\n\n"
|
||||||
"sql = UPDATE [dogs] SET [age] = ? WHERE [id] = ?\n"
|
"sql = UPDATE [dogs] SET [age] = ? WHERE [id] = ?\n"
|
||||||
|
|
@ -1116,9 +1116,9 @@ def test_upsert_alter(db_path, tmpdir):
|
||||||
cli.cli, ["upsert", db_path, "dogs", json_path, "--pk", "id", "--alter"]
|
cli.cli, ["upsert", db_path, "dogs", json_path, "--pk", "id", "--alter"]
|
||||||
)
|
)
|
||||||
assert result.exit_code == 0
|
assert result.exit_code == 0
|
||||||
assert [
|
assert list(db.query("select * from dogs order by id")) == [
|
||||||
{"id": 1, "name": "Cleo", "age": 5},
|
{"id": 1, "name": "Cleo", "age": 5},
|
||||||
] == list(db.query("select * from dogs order by id"))
|
]
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize(
|
@pytest.mark.parametrize(
|
||||||
|
|
@ -1240,7 +1240,7 @@ def test_create_table_error_if_table_exists():
|
||||||
result = runner.invoke(
|
result = runner.invoke(
|
||||||
cli.cli, ["create-table", "test.db", "dogs", "id", "integer"]
|
cli.cli, ["create-table", "test.db", "dogs", "id", "integer"]
|
||||||
)
|
)
|
||||||
assert 1 == result.exit_code
|
assert result.exit_code == 1
|
||||||
assert (
|
assert (
|
||||||
'Error: Table "dogs" already exists. Use --replace to delete and replace it.'
|
'Error: Table "dogs" already exists. Use --replace to delete and replace it.'
|
||||||
== result.output.strip()
|
== result.output.strip()
|
||||||
|
|
@ -1290,7 +1290,7 @@ def test_create_view_error_if_view_exists():
|
||||||
result = runner.invoke(
|
result = runner.invoke(
|
||||||
cli.cli, ["create-view", "test.db", "version", "select sqlite_version()"]
|
cli.cli, ["create-view", "test.db", "version", "select sqlite_version()"]
|
||||||
)
|
)
|
||||||
assert 1 == result.exit_code
|
assert result.exit_code == 1
|
||||||
assert (
|
assert (
|
||||||
'Error: View "version" already exists. Use --replace to delete and replace it.'
|
'Error: View "version" already exists. Use --replace to delete and replace it.'
|
||||||
== result.output.strip()
|
== result.output.strip()
|
||||||
|
|
@ -1368,7 +1368,7 @@ def test_drop_table_error():
|
||||||
"t2",
|
"t2",
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
assert 1 == result.exit_code
|
assert result.exit_code == 1
|
||||||
assert 'Error: Table "t2" does not exist' == result.output.strip()
|
assert 'Error: Table "t2" does not exist' == result.output.strip()
|
||||||
# Using --ignore suppresses that error
|
# Using --ignore suppresses that error
|
||||||
result = runner.invoke(
|
result = runner.invoke(
|
||||||
|
|
@ -1409,7 +1409,7 @@ def test_drop_view_error():
|
||||||
"t2",
|
"t2",
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
assert 1 == result.exit_code
|
assert result.exit_code == 1
|
||||||
assert 'Error: View "t2" does not exist' == result.output.strip()
|
assert 'Error: View "t2" does not exist' == result.output.strip()
|
||||||
# Using --ignore suppresses that error
|
# Using --ignore suppresses that error
|
||||||
result = runner.invoke(
|
result = runner.invoke(
|
||||||
|
|
|
||||||
|
|
@ -49,7 +49,7 @@ def test_convert_code(fresh_db_and_path, code):
|
||||||
result = CliRunner().invoke(
|
result = CliRunner().invoke(
|
||||||
cli.cli, ["convert", db_path, "t", "text", code], catch_exceptions=False
|
cli.cli, ["convert", db_path, "t", "text", code], catch_exceptions=False
|
||||||
)
|
)
|
||||||
assert 0 == result.exit_code, result.output
|
assert result.exit_code == 0, result.output
|
||||||
value = list(db["t"].rows)[0]["text"]
|
value = list(db["t"].rows)[0]["text"]
|
||||||
assert value == "Spooktober"
|
assert value == "Spooktober"
|
||||||
|
|
||||||
|
|
@ -67,7 +67,7 @@ def test_convert_code_errors(fresh_db_and_path, bad_code):
|
||||||
result = CliRunner().invoke(
|
result = CliRunner().invoke(
|
||||||
cli.cli, ["convert", db_path, "t", "text", bad_code], catch_exceptions=False
|
cli.cli, ["convert", db_path, "t", "text", bad_code], catch_exceptions=False
|
||||||
)
|
)
|
||||||
assert 1 == result.exit_code
|
assert result.exit_code == 1
|
||||||
assert result.output == "Error: Could not compile code\n"
|
assert result.output == "Error: Could not compile code\n"
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -85,7 +85,7 @@ def test_convert_import(test_db_and_path):
|
||||||
"re",
|
"re",
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
assert 0 == result.exit_code, result.output
|
assert result.exit_code == 0, result.output
|
||||||
assert [
|
assert [
|
||||||
{"id": 1, "dt": "5th OXXober 2019 12:04"},
|
{"id": 1, "dt": "5th OXXober 2019 12:04"},
|
||||||
{"id": 2, "dt": "6th OXXober 2019 00:05:06"},
|
{"id": 2, "dt": "6th OXXober 2019 00:05:06"},
|
||||||
|
|
@ -109,7 +109,7 @@ def test_convert_import_nested(fresh_db_and_path):
|
||||||
"xml.etree.ElementTree",
|
"xml.etree.ElementTree",
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
assert 0 == result.exit_code, result.output
|
assert result.exit_code == 0, result.output
|
||||||
assert [
|
assert [
|
||||||
{"xml": "Cleo"},
|
{"xml": "Cleo"},
|
||||||
] == list(db["example"].rows)
|
] == list(db["example"].rows)
|
||||||
|
|
@ -230,7 +230,7 @@ def test_convert_output_column(test_db_and_path, drop):
|
||||||
if drop:
|
if drop:
|
||||||
args += ["--drop"]
|
args += ["--drop"]
|
||||||
result = CliRunner().invoke(cli.cli, args)
|
result = CliRunner().invoke(cli.cli, args)
|
||||||
assert 0 == result.exit_code, result.output
|
assert result.exit_code == 0, result.output
|
||||||
expected = [
|
expected = [
|
||||||
{
|
{
|
||||||
"id": 1,
|
"id": 1,
|
||||||
|
|
@ -277,7 +277,7 @@ def test_convert_output_column_output_type(test_db_and_path, output_type, expect
|
||||||
cli.cli,
|
cli.cli,
|
||||||
args,
|
args,
|
||||||
)
|
)
|
||||||
assert 0 == result.exit_code, result.output
|
assert result.exit_code == 0, result.output
|
||||||
assert expected == list(db.execute("select id, new_id from example"))
|
assert expected == list(db.execute("select id, new_id from example"))
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -428,7 +428,7 @@ def test_recipe_jsonsplit(tmpdir, delimiter):
|
||||||
code = 'recipes.jsonsplit(value, delimiter="{}")'.format(delimiter)
|
code = 'recipes.jsonsplit(value, delimiter="{}")'.format(delimiter)
|
||||||
args = ["convert", db_path, "example", "tags", code]
|
args = ["convert", db_path, "example", "tags", code]
|
||||||
result = CliRunner().invoke(cli.cli, args)
|
result = CliRunner().invoke(cli.cli, args)
|
||||||
assert 0 == result.exit_code, result.output
|
assert result.exit_code == 0, result.output
|
||||||
assert list(db["example"].rows) == [
|
assert list(db["example"].rows) == [
|
||||||
{"id": 1, "tags": '["foo", "bar"]'},
|
{"id": 1, "tags": '["foo", "bar"]'},
|
||||||
{"id": 2, "tags": '["bar", "baz"]'},
|
{"id": 2, "tags": '["bar", "baz"]'},
|
||||||
|
|
@ -456,7 +456,7 @@ def test_recipe_jsonsplit_type(fresh_db_and_path, type, expected_array):
|
||||||
code = "recipes.jsonsplit(value, type={})".format(type)
|
code = "recipes.jsonsplit(value, type={})".format(type)
|
||||||
args = ["convert", db_path, "example", "records", code]
|
args = ["convert", db_path, "example", "records", code]
|
||||||
result = CliRunner().invoke(cli.cli, args)
|
result = CliRunner().invoke(cli.cli, args)
|
||||||
assert 0 == result.exit_code, result.output
|
assert result.exit_code == 0, result.output
|
||||||
assert json.loads(db["example"].get(1)["records"]) == expected_array
|
assert json.loads(db["example"].get(1)["records"]) == expected_array
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -474,7 +474,7 @@ def test_recipe_jsonsplit_output(fresh_db_and_path, drop):
|
||||||
if drop:
|
if drop:
|
||||||
args += ["--drop"]
|
args += ["--drop"]
|
||||||
result = CliRunner().invoke(cli.cli, args)
|
result = CliRunner().invoke(cli.cli, args)
|
||||||
assert 0 == result.exit_code, result.output
|
assert result.exit_code == 0, result.output
|
||||||
expected = {
|
expected = {
|
||||||
"id": 1,
|
"id": 1,
|
||||||
"records": "1,2,3",
|
"records": "1,2,3",
|
||||||
|
|
@ -568,7 +568,7 @@ def test_convert_where_multi(fresh_db_and_path):
|
||||||
"--multi",
|
"--multi",
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
assert 0 == result.exit_code, result.output
|
assert result.exit_code == 0, result.output
|
||||||
assert list(db["names"].rows) == [
|
assert list(db["names"].rows) == [
|
||||||
{"id": 1, "name": "Cleo", "upper": None},
|
{"id": 1, "name": "Cleo", "upper": None},
|
||||||
{"id": 2, "name": "Bants", "upper": "BANTS"},
|
{"id": 2, "name": "Bants", "upper": "BANTS"},
|
||||||
|
|
@ -589,7 +589,7 @@ def test_convert_code_standard_input(fresh_db_and_path):
|
||||||
],
|
],
|
||||||
input="value.upper()",
|
input="value.upper()",
|
||||||
)
|
)
|
||||||
assert 0 == result.exit_code, result.output
|
assert result.exit_code == 0, result.output
|
||||||
assert list(db["names"].rows) == [
|
assert list(db["names"].rows) == [
|
||||||
{"id": 1, "name": "CLEO"},
|
{"id": 1, "name": "CLEO"},
|
||||||
]
|
]
|
||||||
|
|
@ -602,7 +602,7 @@ def test_convert_hyphen_workaround(fresh_db_and_path):
|
||||||
cli.cli,
|
cli.cli,
|
||||||
["convert", db_path, "names", "name", '"-"'],
|
["convert", db_path, "names", "name", '"-"'],
|
||||||
)
|
)
|
||||||
assert 0 == result.exit_code, result.output
|
assert result.exit_code == 0, result.output
|
||||||
assert list(db["names"].rows) == [
|
assert list(db["names"].rows) == [
|
||||||
{"id": 1, "name": "-"},
|
{"id": 1, "name": "-"},
|
||||||
]
|
]
|
||||||
|
|
@ -622,7 +622,7 @@ def test_convert_initialization_pattern(fresh_db_and_path):
|
||||||
],
|
],
|
||||||
input="import random\nrandom.seed(1)\ndef convert(value): return random.randint(0, 100)",
|
input="import random\nrandom.seed(1)\ndef convert(value): return random.randint(0, 100)",
|
||||||
)
|
)
|
||||||
assert 0 == result.exit_code, result.output
|
assert result.exit_code == 0, result.output
|
||||||
assert list(db["names"].rows) == [
|
assert list(db["names"].rows) == [
|
||||||
{"id": 1, "name": "17"},
|
{"id": 1, "name": "17"},
|
||||||
]
|
]
|
||||||
|
|
@ -650,6 +650,6 @@ def test_convert_no_skip_false(fresh_db_and_path, no_skip_false, expected):
|
||||||
assert db["t"].get(1)["x"] == 0
|
assert db["t"].get(1)["x"] == 0
|
||||||
assert db["t"].get(2)["x"] == 1
|
assert db["t"].get(2)["x"] == 1
|
||||||
result = CliRunner().invoke(cli.cli, args, input="value + 1")
|
result = CliRunner().invoke(cli.cli, args, input="value + 1")
|
||||||
assert 0 == result.exit_code, result.output
|
assert result.exit_code == 0, result.output
|
||||||
assert db["t"].get(1)["x"] == expected
|
assert db["t"].get(1)["x"] == expected
|
||||||
assert db["t"].get(2)["x"] == 2
|
assert db["t"].get(2)["x"] == 2
|
||||||
|
|
|
||||||
|
|
@ -13,7 +13,7 @@ def test_insert_simple(tmpdir):
|
||||||
with open(json_path, "w") as fp:
|
with open(json_path, "w") as fp:
|
||||||
fp.write(json.dumps({"name": "Cleo", "age": 4}))
|
fp.write(json.dumps({"name": "Cleo", "age": 4}))
|
||||||
result = CliRunner().invoke(cli.cli, ["insert", db_path, "dogs", json_path])
|
result = CliRunner().invoke(cli.cli, ["insert", db_path, "dogs", json_path])
|
||||||
assert 0 == result.exit_code
|
assert result.exit_code == 0
|
||||||
assert [{"age": 4, "name": "Cleo"}] == list(
|
assert [{"age": 4, "name": "Cleo"}] == list(
|
||||||
Database(db_path).query("select * from dogs")
|
Database(db_path).query("select * from dogs")
|
||||||
)
|
)
|
||||||
|
|
@ -29,7 +29,7 @@ def test_insert_from_stdin(tmpdir):
|
||||||
["insert", db_path, "dogs", "-"],
|
["insert", db_path, "dogs", "-"],
|
||||||
input=json.dumps({"name": "Cleo", "age": 4}),
|
input=json.dumps({"name": "Cleo", "age": 4}),
|
||||||
)
|
)
|
||||||
assert 0 == result.exit_code
|
assert result.exit_code == 0
|
||||||
assert [{"age": 4, "name": "Cleo"}] == list(
|
assert [{"age": 4, "name": "Cleo"}] == list(
|
||||||
Database(db_path).query("select * from dogs")
|
Database(db_path).query("select * from dogs")
|
||||||
)
|
)
|
||||||
|
|
@ -84,7 +84,7 @@ def test_insert_with_primary_key(db_path, tmpdir):
|
||||||
result = CliRunner().invoke(
|
result = CliRunner().invoke(
|
||||||
cli.cli, ["insert", db_path, "dogs", json_path, "--pk", "id"]
|
cli.cli, ["insert", db_path, "dogs", json_path, "--pk", "id"]
|
||||||
)
|
)
|
||||||
assert 0 == result.exit_code
|
assert result.exit_code == 0
|
||||||
assert [{"id": 1, "age": 4, "name": "Cleo"}] == list(
|
assert [{"id": 1, "age": 4, "name": "Cleo"}] == list(
|
||||||
Database(db_path).query("select * from dogs")
|
Database(db_path).query("select * from dogs")
|
||||||
)
|
)
|
||||||
|
|
@ -100,7 +100,7 @@ def test_insert_multiple_with_primary_key(db_path, tmpdir):
|
||||||
result = CliRunner().invoke(
|
result = CliRunner().invoke(
|
||||||
cli.cli, ["insert", db_path, "dogs", json_path, "--pk", "id"]
|
cli.cli, ["insert", db_path, "dogs", json_path, "--pk", "id"]
|
||||||
)
|
)
|
||||||
assert 0 == result.exit_code
|
assert result.exit_code == 0
|
||||||
db = Database(db_path)
|
db = Database(db_path)
|
||||||
assert dogs == list(db.query("select * from dogs order by id"))
|
assert dogs == list(db.query("select * from dogs order by id"))
|
||||||
assert ["id"] == db["dogs"].pks
|
assert ["id"] == db["dogs"].pks
|
||||||
|
|
@ -117,7 +117,7 @@ def test_insert_multiple_with_compound_primary_key(db_path, tmpdir):
|
||||||
result = CliRunner().invoke(
|
result = CliRunner().invoke(
|
||||||
cli.cli, ["insert", db_path, "dogs", json_path, "--pk", "id", "--pk", "breed"]
|
cli.cli, ["insert", db_path, "dogs", json_path, "--pk", "id", "--pk", "breed"]
|
||||||
)
|
)
|
||||||
assert 0 == result.exit_code
|
assert result.exit_code == 0
|
||||||
db = Database(db_path)
|
db = Database(db_path)
|
||||||
assert dogs == list(db.query("select * from dogs order by breed, id"))
|
assert dogs == list(db.query("select * from dogs order by breed, id"))
|
||||||
assert {"breed", "id"} == set(db["dogs"].pks)
|
assert {"breed", "id"} == set(db["dogs"].pks)
|
||||||
|
|
@ -146,7 +146,7 @@ def test_insert_not_null_default(db_path, tmpdir):
|
||||||
+ ["--not-null", "name", "--not-null", "age"]
|
+ ["--not-null", "name", "--not-null", "age"]
|
||||||
+ ["--default", "score", "5", "--default", "age", "1"],
|
+ ["--default", "score", "5", "--default", "age", "1"],
|
||||||
)
|
)
|
||||||
assert 0 == result.exit_code
|
assert result.exit_code == 0
|
||||||
db = Database(db_path)
|
db = Database(db_path)
|
||||||
assert (
|
assert (
|
||||||
"CREATE TABLE [dogs] (\n"
|
"CREATE TABLE [dogs] (\n"
|
||||||
|
|
@ -163,7 +163,7 @@ def test_insert_binary_base64(db_path):
|
||||||
["insert", db_path, "files", "-"],
|
["insert", db_path, "files", "-"],
|
||||||
input=r'{"content": {"$base64": true, "encoded": "aGVsbG8="}}',
|
input=r'{"content": {"$base64": true, "encoded": "aGVsbG8="}}',
|
||||||
)
|
)
|
||||||
assert 0 == result.exit_code, result.output
|
assert result.exit_code == 0, result.output
|
||||||
db = Database(db_path)
|
db = Database(db_path)
|
||||||
actual = list(db.query("select content from files"))
|
actual = list(db.query("select content from files"))
|
||||||
assert actual == [{"content": b"hello"}]
|
assert actual == [{"content": b"hello"}]
|
||||||
|
|
@ -175,7 +175,7 @@ def test_insert_newline_delimited(db_path):
|
||||||
["insert", db_path, "from_json_nl", "-", "--nl"],
|
["insert", db_path, "from_json_nl", "-", "--nl"],
|
||||||
input='{"foo": "bar", "n": 1}\n\n{"foo": "baz", "n": 2}',
|
input='{"foo": "bar", "n": 1}\n\n{"foo": "baz", "n": 2}',
|
||||||
)
|
)
|
||||||
assert 0 == result.exit_code, result.output
|
assert result.exit_code == 0, result.output
|
||||||
db = Database(db_path)
|
db = Database(db_path)
|
||||||
assert [
|
assert [
|
||||||
{"foo": "bar", "n": 1},
|
{"foo": "bar", "n": 1},
|
||||||
|
|
@ -193,12 +193,12 @@ def test_insert_ignore(db_path, tmpdir):
|
||||||
result = CliRunner().invoke(
|
result = CliRunner().invoke(
|
||||||
cli.cli, ["insert", db_path, "dogs", json_path, "--pk", "id"]
|
cli.cli, ["insert", db_path, "dogs", json_path, "--pk", "id"]
|
||||||
)
|
)
|
||||||
assert 0 != result.exit_code, result.output
|
assert result.exit_code != 0, result.output
|
||||||
# If we use --ignore it should run OK
|
# If we use --ignore it should run OK
|
||||||
result = CliRunner().invoke(
|
result = CliRunner().invoke(
|
||||||
cli.cli, ["insert", db_path, "dogs", json_path, "--pk", "id", "--ignore"]
|
cli.cli, ["insert", db_path, "dogs", json_path, "--pk", "id", "--ignore"]
|
||||||
)
|
)
|
||||||
assert 0 == result.exit_code, result.output
|
assert result.exit_code == 0, result.output
|
||||||
# ... but it should actually have no effect
|
# ... but it should actually have no effect
|
||||||
assert [{"id": 1, "name": "Cleo"}] == list(db.query("select * from dogs"))
|
assert [{"id": 1, "name": "Cleo"}] == list(db.query("select * from dogs"))
|
||||||
|
|
||||||
|
|
@ -225,7 +225,7 @@ def test_insert_csv_tsv(content, options, db_path, tmpdir):
|
||||||
["insert", db_path, "data", file_path] + options,
|
["insert", db_path, "data", file_path] + options,
|
||||||
catch_exceptions=False,
|
catch_exceptions=False,
|
||||||
)
|
)
|
||||||
assert 0 == result.exit_code
|
assert result.exit_code == 0
|
||||||
assert [{"foo": "1", "bar": "2", "baz": "cat,dog"}] == list(db["data"].rows)
|
assert [{"foo": "1", "bar": "2", "baz": "cat,dog"}] == list(db["data"].rows)
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -266,7 +266,7 @@ def test_insert_stop_after(tmpdir, input, args):
|
||||||
["insert", db_path, "rows", "-", "--stop-after", "2"] + args,
|
["insert", db_path, "rows", "-", "--stop-after", "2"] + args,
|
||||||
input=input,
|
input=input,
|
||||||
)
|
)
|
||||||
assert 0 == result.exit_code
|
assert result.exit_code == 0
|
||||||
assert [{"name": "One"}, {"name": "Two"}] == list(
|
assert [{"name": "One"}, {"name": "Two"}] == list(
|
||||||
Database(db_path).query("select * from rows")
|
Database(db_path).query("select * from rows")
|
||||||
)
|
)
|
||||||
|
|
@ -288,7 +288,7 @@ def test_only_allow_one_of_nl_tsv_csv(options, db_path, tmpdir):
|
||||||
result = CliRunner().invoke(
|
result = CliRunner().invoke(
|
||||||
cli.cli, ["insert", db_path, "data", file_path] + options
|
cli.cli, ["insert", db_path, "data", file_path] + options
|
||||||
)
|
)
|
||||||
assert 0 != result.exit_code
|
assert result.exit_code != 0
|
||||||
assert "Error: Use just one of --nl, --csv or --tsv" == result.output.strip()
|
assert "Error: Use just one of --nl, --csv or --tsv" == result.output.strip()
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -296,7 +296,7 @@ def test_insert_replace(db_path, tmpdir):
|
||||||
test_insert_multiple_with_primary_key(db_path, tmpdir)
|
test_insert_multiple_with_primary_key(db_path, tmpdir)
|
||||||
json_path = str(tmpdir / "insert-replace.json")
|
json_path = str(tmpdir / "insert-replace.json")
|
||||||
db = Database(db_path)
|
db = Database(db_path)
|
||||||
assert 20 == db["dogs"].count
|
assert db["dogs"].count == 20
|
||||||
insert_replace_dogs = [
|
insert_replace_dogs = [
|
||||||
{"id": 1, "name": "Insert replaced 1", "age": 4},
|
{"id": 1, "name": "Insert replaced 1", "age": 4},
|
||||||
{"id": 2, "name": "Insert replaced 2", "age": 4},
|
{"id": 2, "name": "Insert replaced 2", "age": 4},
|
||||||
|
|
@ -307,8 +307,8 @@ def test_insert_replace(db_path, tmpdir):
|
||||||
result = CliRunner().invoke(
|
result = CliRunner().invoke(
|
||||||
cli.cli, ["insert", db_path, "dogs", json_path, "--pk", "id", "--replace"]
|
cli.cli, ["insert", db_path, "dogs", json_path, "--pk", "id", "--replace"]
|
||||||
)
|
)
|
||||||
assert 0 == result.exit_code, result.output
|
assert result.exit_code == 0, result.output
|
||||||
assert 21 == db["dogs"].count
|
assert db["dogs"].count == 21
|
||||||
assert (
|
assert (
|
||||||
list(db.query("select * from dogs where id in (1, 2, 21) order by id"))
|
list(db.query("select * from dogs where id in (1, 2, 21) order by id"))
|
||||||
== insert_replace_dogs
|
== insert_replace_dogs
|
||||||
|
|
@ -321,7 +321,7 @@ def test_insert_truncate(db_path):
|
||||||
["insert", db_path, "from_json_nl", "-", "--nl", "--batch-size=1"],
|
["insert", db_path, "from_json_nl", "-", "--nl", "--batch-size=1"],
|
||||||
input='{"foo": "bar", "n": 1}\n{"foo": "baz", "n": 2}',
|
input='{"foo": "bar", "n": 1}\n{"foo": "baz", "n": 2}',
|
||||||
)
|
)
|
||||||
assert 0 == result.exit_code, result.output
|
assert result.exit_code == 0, result.output
|
||||||
db = Database(db_path)
|
db = Database(db_path)
|
||||||
assert [
|
assert [
|
||||||
{"foo": "bar", "n": 1},
|
{"foo": "bar", "n": 1},
|
||||||
|
|
@ -341,7 +341,7 @@ def test_insert_truncate(db_path):
|
||||||
],
|
],
|
||||||
input='{"foo": "bam", "n": 3}\n{"foo": "bat", "n": 4}',
|
input='{"foo": "bam", "n": 3}\n{"foo": "bat", "n": 4}',
|
||||||
)
|
)
|
||||||
assert 0 == result.exit_code, result.output
|
assert result.exit_code == 0, result.output
|
||||||
assert [
|
assert [
|
||||||
{"foo": "bam", "n": 3},
|
{"foo": "bam", "n": 3},
|
||||||
{"foo": "bat", "n": 4},
|
{"foo": "bat", "n": 4},
|
||||||
|
|
@ -354,21 +354,21 @@ def test_insert_alter(db_path, tmpdir):
|
||||||
["insert", db_path, "from_json_nl", "-", "--nl"],
|
["insert", db_path, "from_json_nl", "-", "--nl"],
|
||||||
input='{"foo": "bar", "n": 1}\n{"foo": "baz", "n": 2}',
|
input='{"foo": "bar", "n": 1}\n{"foo": "baz", "n": 2}',
|
||||||
)
|
)
|
||||||
assert 0 == result.exit_code, result.output
|
assert result.exit_code == 0, result.output
|
||||||
# Should get an error with incorrect shaped additional data
|
# Should get an error with incorrect shaped additional data
|
||||||
result = CliRunner().invoke(
|
result = CliRunner().invoke(
|
||||||
cli.cli,
|
cli.cli,
|
||||||
["insert", db_path, "from_json_nl", "-", "--nl"],
|
["insert", db_path, "from_json_nl", "-", "--nl"],
|
||||||
input='{"foo": "bar", "baz": 5}',
|
input='{"foo": "bar", "baz": 5}',
|
||||||
)
|
)
|
||||||
assert 0 != result.exit_code, result.output
|
assert result.exit_code != 0, result.output
|
||||||
# If we run it again with --alter it should work correctly
|
# If we run it again with --alter it should work correctly
|
||||||
result = CliRunner().invoke(
|
result = CliRunner().invoke(
|
||||||
cli.cli,
|
cli.cli,
|
||||||
["insert", db_path, "from_json_nl", "-", "--nl", "--alter"],
|
["insert", db_path, "from_json_nl", "-", "--nl", "--alter"],
|
||||||
input='{"foo": "bar", "baz": 5}',
|
input='{"foo": "bar", "baz": 5}',
|
||||||
)
|
)
|
||||||
assert 0 == result.exit_code, result.output
|
assert result.exit_code == 0, result.output
|
||||||
# Soundness check the database itself
|
# Soundness check the database itself
|
||||||
db = Database(db_path)
|
db = Database(db_path)
|
||||||
assert {"foo": str, "n": int, "baz": int} == db["from_json_nl"].columns_dict
|
assert {"foo": str, "n": int, "baz": int} == db["from_json_nl"].columns_dict
|
||||||
|
|
@ -389,7 +389,7 @@ def test_insert_analyze(db_path):
|
||||||
["insert", db_path, "rows", "-", "--nl", "--analyze"],
|
["insert", db_path, "rows", "-", "--nl", "--analyze"],
|
||||||
input='{"foo": "bar", "n": 1}\n{"foo": "baz", "n": 2}',
|
input='{"foo": "bar", "n": 1}\n{"foo": "baz", "n": 2}',
|
||||||
)
|
)
|
||||||
assert 0 == result.exit_code, result.output
|
assert result.exit_code == 0, result.output
|
||||||
assert "sqlite_stat1" in db.table_names()
|
assert "sqlite_stat1" in db.table_names()
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -399,7 +399,7 @@ def test_insert_lines(db_path):
|
||||||
["insert", db_path, "from_lines", "-", "--lines"],
|
["insert", db_path, "from_lines", "-", "--lines"],
|
||||||
input='First line\nSecond line\n{"foo": "baz"}',
|
input='First line\nSecond line\n{"foo": "baz"}',
|
||||||
)
|
)
|
||||||
assert 0 == result.exit_code, result.output
|
assert result.exit_code == 0, result.output
|
||||||
db = Database(db_path)
|
db = Database(db_path)
|
||||||
assert [
|
assert [
|
||||||
{"line": "First line"},
|
{"line": "First line"},
|
||||||
|
|
@ -414,7 +414,7 @@ def test_insert_text(db_path):
|
||||||
["insert", db_path, "from_text", "-", "--text"],
|
["insert", db_path, "from_text", "-", "--text"],
|
||||||
input='First line\nSecond line\n{"foo": "baz"}',
|
input='First line\nSecond line\n{"foo": "baz"}',
|
||||||
)
|
)
|
||||||
assert 0 == result.exit_code, result.output
|
assert result.exit_code == 0, result.output
|
||||||
db = Database(db_path)
|
db = Database(db_path)
|
||||||
assert [{"text": 'First line\nSecond line\n{"foo": "baz"}'}] == list(
|
assert [{"text": 'First line\nSecond line\n{"foo": "baz"}'}] == list(
|
||||||
db.query("select text from from_text")
|
db.query("select text from from_text")
|
||||||
|
|
|
||||||
|
|
@ -151,8 +151,8 @@ def test_create_table_from_example(fresh_db, example, expected_columns):
|
||||||
assert people_table.last_rowid is None
|
assert people_table.last_rowid is None
|
||||||
assert people_table.last_pk is None
|
assert people_table.last_pk is None
|
||||||
people_table.insert(example)
|
people_table.insert(example)
|
||||||
assert 1 == people_table.last_rowid
|
assert people_table.last_rowid == 1
|
||||||
assert 1 == people_table.last_pk
|
assert people_table.last_pk == 1
|
||||||
assert ["people"] == fresh_db.table_names()
|
assert ["people"] == fresh_db.table_names()
|
||||||
assert expected_columns == [
|
assert expected_columns == [
|
||||||
{"name": col.name, "type": col.type} for col in fresh_db["people"].columns
|
{"name": col.name, "type": col.type} for col in fresh_db["people"].columns
|
||||||
|
|
@ -680,7 +680,7 @@ def test_bulk_insert_more_than_999_values(fresh_db):
|
||||||
),
|
),
|
||||||
pk="id",
|
pk="id",
|
||||||
)
|
)
|
||||||
assert 100 == fresh_db["big"].count
|
assert fresh_db["big"].count == 100
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize(
|
@pytest.mark.parametrize(
|
||||||
|
|
@ -791,7 +791,7 @@ def test_create_index_if_not_exists(fresh_db):
|
||||||
dogs.insert({"name": "Cleo", "twitter": "cleopaws", "age": 3, "is_good_dog": True})
|
dogs.insert({"name": "Cleo", "twitter": "cleopaws", "age": 3, "is_good_dog": True})
|
||||||
assert [] == dogs.indexes
|
assert [] == dogs.indexes
|
||||||
dogs.create_index(["name"])
|
dogs.create_index(["name"])
|
||||||
assert 1 == len(dogs.indexes)
|
assert len(dogs.indexes) == 1
|
||||||
with pytest.raises(Exception, match="index idx_dogs_name already exists"):
|
with pytest.raises(Exception, match="index idx_dogs_name already exists"):
|
||||||
dogs.create_index(["name"])
|
dogs.create_index(["name"])
|
||||||
dogs.create_index(["name"], if_not_exists=True)
|
dogs.create_index(["name"], if_not_exists=True)
|
||||||
|
|
@ -891,7 +891,7 @@ def test_insert_thousands_using_generator(fresh_db):
|
||||||
assert [{"name": "i", "type": "INTEGER"}, {"name": "word", "type": "TEXT"}] == [
|
assert [{"name": "i", "type": "INTEGER"}, {"name": "word", "type": "TEXT"}] == [
|
||||||
{"name": col.name, "type": col.type} for col in fresh_db["test"].columns
|
{"name": col.name, "type": col.type} for col in fresh_db["test"].columns
|
||||||
]
|
]
|
||||||
assert 10000 == fresh_db["test"].count
|
assert fresh_db["test"].count == 10000
|
||||||
|
|
||||||
|
|
||||||
def test_insert_thousands_raises_exception_with_extra_columns_after_first_100(fresh_db):
|
def test_insert_thousands_raises_exception_with_extra_columns_after_first_100(fresh_db):
|
||||||
|
|
@ -930,13 +930,13 @@ def test_insert_hash_id(fresh_db):
|
||||||
dogs = fresh_db["dogs"]
|
dogs = fresh_db["dogs"]
|
||||||
id = dogs.insert({"name": "Cleo", "twitter": "cleopaws"}, hash_id="id").last_pk
|
id = dogs.insert({"name": "Cleo", "twitter": "cleopaws"}, hash_id="id").last_pk
|
||||||
assert "f501265970505d9825d8d9f590bfab3519fb20b1" == id
|
assert "f501265970505d9825d8d9f590bfab3519fb20b1" == id
|
||||||
assert 1 == dogs.count
|
assert dogs.count == 1
|
||||||
# Insert replacing a second time should not create a new row
|
# Insert replacing a second time should not create a new row
|
||||||
id2 = dogs.insert(
|
id2 = dogs.insert(
|
||||||
{"name": "Cleo", "twitter": "cleopaws"}, hash_id="id", replace=True
|
{"name": "Cleo", "twitter": "cleopaws"}, hash_id="id", replace=True
|
||||||
).last_pk
|
).last_pk
|
||||||
assert "f501265970505d9825d8d9f590bfab3519fb20b1" == id2
|
assert "f501265970505d9825d8d9f590bfab3519fb20b1" == id2
|
||||||
assert 1 == dogs.count
|
assert dogs.count == 1
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize("use_table_factory", [True, False])
|
@pytest.mark.parametrize("use_table_factory", [True, False])
|
||||||
|
|
@ -974,7 +974,7 @@ def test_works_with_pathlib_path(tmpdir):
|
||||||
path = pathlib.Path(tmpdir / "test.db")
|
path = pathlib.Path(tmpdir / "test.db")
|
||||||
db = Database(path)
|
db = Database(path)
|
||||||
db["demo"].insert_all([{"foo": 1}])
|
db["demo"].insert_all([{"foo": 1}])
|
||||||
assert 1 == db["demo"].count
|
assert db["demo"].count == 1
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.skipif(pd is None, reason="pandas and numpy are not installed")
|
@pytest.mark.skipif(pd is None, reason="pandas and numpy are not installed")
|
||||||
|
|
@ -1091,11 +1091,11 @@ def test_drop_ignore(fresh_db):
|
||||||
|
|
||||||
def test_insert_all_empty_list(fresh_db):
|
def test_insert_all_empty_list(fresh_db):
|
||||||
fresh_db["t"].insert({"foo": 1})
|
fresh_db["t"].insert({"foo": 1})
|
||||||
assert 1 == fresh_db["t"].count
|
assert fresh_db["t"].count == 1
|
||||||
fresh_db["t"].insert_all([])
|
fresh_db["t"].insert_all([])
|
||||||
assert 1 == fresh_db["t"].count
|
assert fresh_db["t"].count == 1
|
||||||
fresh_db["t"].insert_all([], replace=True)
|
fresh_db["t"].insert_all([], replace=True)
|
||||||
assert 1 == fresh_db["t"].count
|
assert fresh_db["t"].count == 1
|
||||||
|
|
||||||
|
|
||||||
def test_insert_all_single_column(fresh_db):
|
def test_insert_all_single_column(fresh_db):
|
||||||
|
|
|
||||||
|
|
@ -18,18 +18,18 @@ def test_delete_where(fresh_db):
|
||||||
table = fresh_db["table"]
|
table = fresh_db["table"]
|
||||||
for i in range(1, 11):
|
for i in range(1, 11):
|
||||||
table.insert({"id": i}, pk="id")
|
table.insert({"id": i}, pk="id")
|
||||||
assert 10 == table.count
|
assert table.count == 10
|
||||||
table.delete_where("id > ?", [5])
|
table.delete_where("id > ?", [5])
|
||||||
assert 5 == table.count
|
assert table.count == 5
|
||||||
|
|
||||||
|
|
||||||
def test_delete_where_all(fresh_db):
|
def test_delete_where_all(fresh_db):
|
||||||
table = fresh_db["table"]
|
table = fresh_db["table"]
|
||||||
for i in range(1, 11):
|
for i in range(1, 11):
|
||||||
table.insert({"id": i}, pk="id")
|
table.insert({"id": i}, pk="id")
|
||||||
assert 10 == table.count
|
assert table.count == 10
|
||||||
table.delete_where()
|
table.delete_where()
|
||||||
assert 0 == table.count
|
assert table.count == 0
|
||||||
|
|
||||||
|
|
||||||
def test_delete_where_analyze(fresh_db):
|
def test_delete_where_analyze(fresh_db):
|
||||||
|
|
|
||||||
|
|
@ -127,7 +127,7 @@ def test_cli_create_spatialite(tmpdir):
|
||||||
cli, ["create-database", str(db_path), "--init-spatialite"]
|
cli, ["create-database", str(db_path), "--init-spatialite"]
|
||||||
)
|
)
|
||||||
|
|
||||||
assert 0 == result.exit_code
|
assert result.exit_code == 0
|
||||||
assert db_path.exists()
|
assert db_path.exists()
|
||||||
assert db_path.read_binary()[:16] == b"SQLite format 3\x00"
|
assert db_path.read_binary()[:16] == b"SQLite format 3\x00"
|
||||||
|
|
||||||
|
|
@ -155,7 +155,7 @@ def test_cli_add_geometry_column(tmpdir):
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
|
|
||||||
assert 0 == result.exit_code
|
assert result.exit_code == 0
|
||||||
|
|
||||||
assert db["geometry_columns"].get(["locations", "geometry"]) == {
|
assert db["geometry_columns"].get(["locations", "geometry"]) == {
|
||||||
"f_table_name": "locations",
|
"f_table_name": "locations",
|
||||||
|
|
@ -189,7 +189,7 @@ def test_cli_add_geometry_column_options(tmpdir):
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
|
|
||||||
assert 0 == result.exit_code
|
assert result.exit_code == 0
|
||||||
|
|
||||||
assert db["geometry_columns"].get(["locations", "geometry"]) == {
|
assert db["geometry_columns"].get(["locations", "geometry"]) == {
|
||||||
"f_table_name": "locations",
|
"f_table_name": "locations",
|
||||||
|
|
@ -240,6 +240,6 @@ def test_cli_create_spatial_index(tmpdir):
|
||||||
cli, ["create-spatial-index", str(db_path), table.name, "geometry"]
|
cli, ["create-spatial-index", str(db_path), table.name, "geometry"]
|
||||||
)
|
)
|
||||||
|
|
||||||
assert 0 == result.exit_code
|
assert result.exit_code == 0
|
||||||
|
|
||||||
assert "idx_locations_geometry" in db.table_names()
|
assert "idx_locations_geometry" in db.table_names()
|
||||||
|
|
|
||||||
|
|
@ -54,18 +54,18 @@ def test_detect_fts_similar_tables(fresh_db, reverse_order):
|
||||||
|
|
||||||
|
|
||||||
def test_tables(existing_db):
|
def test_tables(existing_db):
|
||||||
assert 1 == len(existing_db.tables)
|
assert len(existing_db.tables) == 1
|
||||||
assert "foo" == existing_db.tables[0].name
|
assert existing_db.tables[0].name == "foo"
|
||||||
|
|
||||||
|
|
||||||
def test_views(fresh_db):
|
def test_views(fresh_db):
|
||||||
fresh_db.create_view("foo_view", "select 1")
|
fresh_db.create_view("foo_view", "select 1")
|
||||||
assert 1 == len(fresh_db.views)
|
assert len(fresh_db.views) == 1
|
||||||
view = fresh_db.views[0]
|
view = fresh_db.views[0]
|
||||||
assert isinstance(view, View)
|
assert isinstance(view, View)
|
||||||
assert "foo_view" == view.name
|
assert view.name == "foo_view"
|
||||||
assert "<View foo_view (1)>" == repr(view)
|
assert repr(view) == "<View foo_view (1)>"
|
||||||
assert {"1": str} == view.columns_dict
|
assert view.columns_dict == {"1": str}
|
||||||
|
|
||||||
|
|
||||||
def test_count(existing_db):
|
def test_count(existing_db):
|
||||||
|
|
|
||||||
|
|
@ -109,9 +109,9 @@ def test_m2m_with_table_objects(fresh_db):
|
||||||
)
|
)
|
||||||
expected_tables = {"dogs", "humans", "dogs_humans"}
|
expected_tables = {"dogs", "humans", "dogs_humans"}
|
||||||
assert expected_tables == set(fresh_db.table_names())
|
assert expected_tables == set(fresh_db.table_names())
|
||||||
assert 1 == dogs.count
|
assert dogs.count == 1
|
||||||
assert 2 == humans.count
|
assert humans.count == 2
|
||||||
assert 2 == fresh_db["dogs_humans"].count
|
assert fresh_db["dogs_humans"].count == 2
|
||||||
|
|
||||||
|
|
||||||
def test_m2m_lookup(fresh_db):
|
def test_m2m_lookup(fresh_db):
|
||||||
|
|
|
||||||
|
|
@ -81,9 +81,9 @@ def test_update_with_no_values_sets_last_pk(fresh_db):
|
||||||
table = fresh_db.table("dogs", pk="id")
|
table = fresh_db.table("dogs", pk="id")
|
||||||
table.insert_all([{"id": 1, "name": "Cleo"}, {"id": 2, "name": "Pancakes"}])
|
table.insert_all([{"id": 1, "name": "Cleo"}, {"id": 2, "name": "Pancakes"}])
|
||||||
table.update(1)
|
table.update(1)
|
||||||
assert 1 == table.last_pk
|
assert table.last_pk == 1
|
||||||
table.update(2)
|
table.update(2)
|
||||||
assert 2 == table.last_pk
|
assert table.last_pk == 2
|
||||||
with pytest.raises(NotFoundError):
|
with pytest.raises(NotFoundError):
|
||||||
table.update(3)
|
table.update(3)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -6,25 +6,25 @@ def test_upsert(fresh_db):
|
||||||
table = fresh_db["table"]
|
table = fresh_db["table"]
|
||||||
table.insert({"id": 1, "name": "Cleo"}, pk="id")
|
table.insert({"id": 1, "name": "Cleo"}, pk="id")
|
||||||
table.upsert({"id": 1, "age": 5}, pk="id", alter=True)
|
table.upsert({"id": 1, "age": 5}, pk="id", alter=True)
|
||||||
assert [{"id": 1, "name": "Cleo", "age": 5}] == list(table.rows)
|
assert list(table.rows) == [{"id": 1, "name": "Cleo", "age": 5}]
|
||||||
assert 1 == table.last_pk
|
assert table.last_pk == 1
|
||||||
|
|
||||||
|
|
||||||
def test_upsert_all(fresh_db):
|
def test_upsert_all(fresh_db):
|
||||||
table = fresh_db["table"]
|
table = fresh_db["table"]
|
||||||
table.upsert_all([{"id": 1, "name": "Cleo"}, {"id": 2, "name": "Nixie"}], pk="id")
|
table.upsert_all([{"id": 1, "name": "Cleo"}, {"id": 2, "name": "Nixie"}], pk="id")
|
||||||
table.upsert_all([{"id": 1, "age": 5}, {"id": 2, "age": 5}], pk="id", alter=True)
|
table.upsert_all([{"id": 1, "age": 5}, {"id": 2, "age": 5}], pk="id", alter=True)
|
||||||
assert [
|
assert list(table.rows) == [
|
||||||
{"id": 1, "name": "Cleo", "age": 5},
|
{"id": 1, "name": "Cleo", "age": 5},
|
||||||
{"id": 2, "name": "Nixie", "age": 5},
|
{"id": 2, "name": "Nixie", "age": 5},
|
||||||
] == list(table.rows)
|
]
|
||||||
assert table.last_pk is None
|
assert table.last_pk is None
|
||||||
|
|
||||||
|
|
||||||
def test_upsert_all_single_column(fresh_db):
|
def test_upsert_all_single_column(fresh_db):
|
||||||
table = fresh_db["table"]
|
table = fresh_db["table"]
|
||||||
table.upsert_all([{"name": "Cleo"}], pk="name")
|
table.upsert_all([{"name": "Cleo"}], pk="name")
|
||||||
assert [{"name": "Cleo"}] == list(table.rows)
|
assert list(table.rows) == [{"name": "Cleo"}]
|
||||||
assert table.pks == ["name"]
|
assert table.pks == ["name"]
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue