mirror of
https://github.com/simonw/dclient.git
synced 2026-11-24 10:59:42 +01:00
First insert test, using a mock - refs #8
This commit is contained in:
parent
0d4e5cfc73
commit
b204ff56f6
2 changed files with 49 additions and 4 deletions
|
|
@ -7,6 +7,7 @@ from sqlite_utils.utils import rows_from_file, Format, TypeTracker, progressbar
|
||||||
import sys
|
import sys
|
||||||
from .utils import token_for_url
|
from .utils import token_for_url
|
||||||
|
|
||||||
|
|
||||||
def get_config_dir():
|
def get_config_dir():
|
||||||
return pathlib.Path(click.get_app_dir("io.datasette.dclient"))
|
return pathlib.Path(click.get_app_dir("io.datasette.dclient"))
|
||||||
|
|
||||||
|
|
@ -182,10 +183,14 @@ def insert(
|
||||||
bytes_so_far = 0
|
bytes_so_far = 0
|
||||||
for batch in _batches(rows, batch_size):
|
for batch in _batches(rows, batch_size):
|
||||||
if file_size is not None:
|
if file_size is not None:
|
||||||
bytes_consumed_so_far = fp.tell()
|
try:
|
||||||
new_bytes = bytes_consumed_so_far - bytes_so_far
|
bytes_consumed_so_far = fp.tell()
|
||||||
bar.update(new_bytes)
|
new_bytes = bytes_consumed_so_far - bytes_so_far
|
||||||
bytes_so_far += new_bytes
|
bar.update(new_bytes)
|
||||||
|
bytes_so_far += new_bytes
|
||||||
|
except ValueError:
|
||||||
|
# File has likely been closed, so fp.tell() fails
|
||||||
|
pass
|
||||||
types = None
|
types = None
|
||||||
if first and not no_detect_types:
|
if first and not no_detect_types:
|
||||||
# Detect types on first batch
|
# Detect types on first batch
|
||||||
|
|
|
||||||
40
tests/test_insert.py
Normal file
40
tests/test_insert.py
Normal file
|
|
@ -0,0 +1,40 @@
|
||||||
|
from click.testing import CliRunner
|
||||||
|
from dclient.cli import cli
|
||||||
|
import json
|
||||||
|
import pathlib
|
||||||
|
|
||||||
|
|
||||||
|
def test_insert_mocked(httpx_mock, tmpdir):
|
||||||
|
httpx_mock.add_response(
|
||||||
|
json={
|
||||||
|
"ok": True,
|
||||||
|
"database": "data",
|
||||||
|
"table": "table1",
|
||||||
|
"table_url": "http://localhost:8012/data/table1",
|
||||||
|
"table_api_url": "http://localhost:8012/data/table1.json",
|
||||||
|
"schema": "CREATE TABLE [table1] (...)",
|
||||||
|
"row_count": 100,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
(tmpdir / "data.csv")
|
||||||
|
path = pathlib.Path(tmpdir) / "data.csv"
|
||||||
|
path.write_text("a,b,c\n1,2,3\n")
|
||||||
|
runner = CliRunner()
|
||||||
|
result = runner.invoke(
|
||||||
|
cli,
|
||||||
|
[
|
||||||
|
"insert",
|
||||||
|
"https://localhost:8012/data",
|
||||||
|
"table1",
|
||||||
|
str(path),
|
||||||
|
"--csv",
|
||||||
|
"--token",
|
||||||
|
"x",
|
||||||
|
],
|
||||||
|
catch_exceptions=False,
|
||||||
|
)
|
||||||
|
assert result.exit_code == 0
|
||||||
|
assert result.output == "Inserting rows\n"
|
||||||
|
request = httpx_mock.get_request()
|
||||||
|
assert request.headers["authorization"] == "Bearer x"
|
||||||
|
assert json.loads(request.read()) == {"rows": [{"a": 1, "b": 2, "c": 3}]}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue