mirror of
https://github.com/simonw/sqlite-utils.git
synced 2026-07-23 09:24:31 +02:00
Tests for rows_from_file, refs #440
This commit is contained in:
parent
d379f430f8
commit
ad96bd18c3
1 changed files with 46 additions and 0 deletions
46
tests/test_rows_from_file.py
Normal file
46
tests/test_rows_from_file.py
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
from sqlite_utils.utils import rows_from_file, Format, RowError
|
||||
from io import BytesIO
|
||||
import pytest
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"input,expected_format",
|
||||
(
|
||||
(b"id,name\n1,Cleo", Format.CSV),
|
||||
(b"id\tname\n1\tCleo", Format.TSV),
|
||||
(b'[{"id": "1", "name": "Cleo"}]', Format.JSON),
|
||||
),
|
||||
)
|
||||
def test_rows_from_file_detect_format(input, expected_format):
|
||||
rows, format = rows_from_file(BytesIO(input))
|
||||
assert format == expected_format
|
||||
rows_list = list(rows)
|
||||
assert rows_list == [{"id": "1", "name": "Cleo"}]
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"ignore_extras,restkey,expected",
|
||||
(
|
||||
(True, None, [{"id": "1", "name": "Cleo"}]),
|
||||
(False, "_rest", [{"id": "1", "name": "Cleo", "_rest": ["oops"]}]),
|
||||
# expected of None means expect an error:
|
||||
(False, False, None),
|
||||
),
|
||||
)
|
||||
def test_rows_from_file_extra_fields_strategies(ignore_extras, restkey, expected):
|
||||
try:
|
||||
rows, format = rows_from_file(
|
||||
BytesIO(b"id,name\r\n1,Cleo,oops"),
|
||||
format=Format.CSV,
|
||||
ignore_extras=ignore_extras,
|
||||
restkey=restkey,
|
||||
)
|
||||
list_rows = list(rows)
|
||||
except RowError:
|
||||
if expected is None:
|
||||
# This is fine,
|
||||
return
|
||||
else:
|
||||
# We did not expect an error
|
||||
raise
|
||||
assert list_rows == expected
|
||||
Loading…
Add table
Add a link
Reference in a new issue