mirror of
https://github.com/simonw/sqlite-utils.git
synced 2026-09-11 02:54:23 +02:00
'sqlite-utils insert tablename file.json' command
This commit is contained in:
parent
1c683076d3
commit
9e74289397
4 changed files with 104 additions and 0 deletions
|
|
@ -1,5 +1,6 @@
|
|||
from sqlite_utils import cli, Database
|
||||
from click.testing import CliRunner
|
||||
import json
|
||||
import os
|
||||
import pytest
|
||||
import sqlite3
|
||||
|
|
@ -65,3 +66,47 @@ def test_optimize(db_path):
|
|||
# Sanity check that --no-vacuum doesn't throw errors:
|
||||
result = CliRunner().invoke(cli.cli, ["optimize", "--no-vacuum", db_path])
|
||||
assert 0 == result.exit_code
|
||||
|
||||
|
||||
def test_insert_simple(tmpdir):
|
||||
json_path = str(tmpdir / "dog.json")
|
||||
db_path = str(tmpdir / "dogs.db")
|
||||
open(json_path, "w").write(json.dumps({"name": "Cleo", "age": 4}))
|
||||
result = CliRunner().invoke(cli.cli, ["insert", db_path, "dogs", json_path])
|
||||
assert 0 == result.exit_code
|
||||
assert [{"age": 4, "name": "Cleo"}] == Database(db_path).execute_returning_dicts(
|
||||
"select * from dogs"
|
||||
)
|
||||
db = Database(db_path)
|
||||
assert ["dogs"] == db.table_names()
|
||||
assert [] == db["dogs"].indexes
|
||||
|
||||
|
||||
def test_insert_with_primary_key(db_path, tmpdir):
|
||||
json_path = str(tmpdir / "dog.json")
|
||||
open(json_path, "w").write(json.dumps({"id": 1, "name": "Cleo", "age": 4}))
|
||||
result = CliRunner().invoke(
|
||||
cli.cli, ["insert", db_path, "dogs", json_path, "--pk", "id"]
|
||||
)
|
||||
assert 0 == result.exit_code
|
||||
assert [{"id": 1, "age": 4, "name": "Cleo"}] == Database(
|
||||
db_path
|
||||
).execute_returning_dicts("select * from dogs")
|
||||
db = Database(db_path)
|
||||
assert ["id"] == db["dogs"].pks
|
||||
|
||||
|
||||
def test_insert_multiple_with_primary_key(db_path, tmpdir):
|
||||
json_path = str(tmpdir / "dogs.json")
|
||||
dogs = [{"id": i, "name": "Cleo {}".format(i), "age": i + 3} for i in range(1, 21)]
|
||||
open(json_path, "w").write(json.dumps(dogs))
|
||||
open("/tmp/dogs.json", "w").write(json.dumps(dogs, indent=4))
|
||||
result = CliRunner().invoke(
|
||||
cli.cli, ["insert", db_path, "dogs", json_path, "--pk", "id"]
|
||||
)
|
||||
assert 0 == result.exit_code
|
||||
assert dogs == Database(db_path).execute_returning_dicts(
|
||||
"select * from dogs order by id"
|
||||
)
|
||||
db = Database(db_path)
|
||||
assert ["id"] == db["dogs"].pks
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue