Update test for renamed restkey, refs #440, #443

This commit is contained in:
Simon Willison 2022-06-14 14:14:10 -07:00
commit 0cee77b176
3 changed files with 24 additions and 14 deletions

View file

@ -6,6 +6,7 @@ import hashlib
import pathlib import pathlib
import sqlite_utils import sqlite_utils
from sqlite_utils.db import AlterError, BadMultiValues, DescIndex from sqlite_utils.db import AlterError, BadMultiValues, DescIndex
from sqlite_utils.utils import maximize_csv_field_size_limit
from sqlite_utils import recipes from sqlite_utils import recipes
import textwrap import textwrap
import inspect import inspect
@ -46,17 +47,7 @@ If you do not know the encoding, running 'file filename.csv' may tell you.
It's often worth trying: --encoding=latin-1 It's often worth trying: --encoding=latin-1
""".strip() """.strip()
maximize_csv_field_size_limit()
# Increase CSV field size limit to maximum possible
# https://stackoverflow.com/a/15063941
field_size_limit = sys.maxsize
while True:
try:
csv_std.field_size_limit(field_size_limit)
break
except OverflowError:
field_size_limit = int(field_size_limit / 10)
def output_options(fn): def output_options(fn):

View file

@ -7,6 +7,7 @@ import io
import itertools import itertools
import json import json
import os import os
import sys
from . import recipes from . import recipes
from typing import Dict, cast, BinaryIO, Iterable, Optional, Tuple, Type from typing import Dict, cast, BinaryIO, Iterable, Optional, Tuple, Type
@ -29,6 +30,24 @@ SPATIALITE_PATHS = (
"/usr/local/lib/mod_spatialite.dylib", "/usr/local/lib/mod_spatialite.dylib",
) )
# Mainly so we can restore it if needed in the tests:
ORIGINAL_CSV_FIELD_SIZE_LIMIT = csv.field_size_limit()
def maximize_csv_field_size_limit():
"""
Increase the CSV field size limit to the maximum possible.
"""
# https://stackoverflow.com/a/15063941
field_size_limit = sys.maxsize
while True:
try:
csv.field_size_limit(field_size_limit)
break
except OverflowError:
field_size_limit = int(field_size_limit / 10)
def find_spatialite() -> Optional[str]: def find_spatialite() -> Optional[str]:
""" """

View file

@ -19,7 +19,7 @@ def test_rows_from_file_detect_format(input, expected_format):
@pytest.mark.parametrize( @pytest.mark.parametrize(
"ignore_extras,restkey,expected", "ignore_extras,extras_key,expected",
( (
(True, None, [{"id": "1", "name": "Cleo"}]), (True, None, [{"id": "1", "name": "Cleo"}]),
(False, "_rest", [{"id": "1", "name": "Cleo", "_rest": ["oops"]}]), (False, "_rest", [{"id": "1", "name": "Cleo", "_rest": ["oops"]}]),
@ -27,13 +27,13 @@ def test_rows_from_file_detect_format(input, expected_format):
(False, False, None), (False, False, None),
), ),
) )
def test_rows_from_file_extra_fields_strategies(ignore_extras, restkey, expected): def test_rows_from_file_extra_fields_strategies(ignore_extras, extras_key, expected):
try: try:
rows, format = rows_from_file( rows, format = rows_from_file(
BytesIO(b"id,name\r\n1,Cleo,oops"), BytesIO(b"id,name\r\n1,Cleo,oops"),
format=Format.CSV, format=Format.CSV,
ignore_extras=ignore_extras, ignore_extras=ignore_extras,
restkey=restkey, extras_key=extras_key,
) )
list_rows = list(rows) list_rows = list(rows)
except RowError: except RowError: