errors=r.SET_NULL/r.IGNORE options for parsedate/parsedatetime, closes #416

This commit is contained in:
Simon Willison 2022-03-20 21:01:35 -07:00
commit 878d5f5cea
6 changed files with 108 additions and 20 deletions

View file

@ -48,7 +48,13 @@ def test_convert_help():
@pytest.mark.parametrize(
"recipe",
[n for n in dir(recipes) if not n.startswith("_") and n not in ("json", "parser")],
[
n
for n in dir(recipes)
if not n.startswith("_")
and n not in ("json", "parser")
and callable(getattr(recipes, n))
],
)
def test_recipes_are_documented(documented_recipes, recipe):
assert recipe in documented_recipes

View file

@ -1,4 +1,5 @@
from sqlite_utils import recipes
from sqlite_utils.utils import sqlite3
import json
import pytest
@ -61,6 +62,28 @@ def test_dayfirst_yearfirst(fresh_db, recipe, kwargs, expected):
]
@pytest.mark.parametrize("fn", ("parsedate", "parsedatetime"))
@pytest.mark.parametrize("errors", (None, recipes.SET_NULL, recipes.IGNORE))
def test_dateparse_errors(fresh_db, fn, errors):
fresh_db["example"].insert_all(
[
{"id": 1, "dt": "invalid"},
],
pk="id",
)
if errors is None:
# Should raise an error
with pytest.raises(sqlite3.OperationalError):
fresh_db["example"].convert("dt", lambda value: getattr(recipes, fn)(value))
else:
fresh_db["example"].convert(
"dt", lambda value: getattr(recipes, fn)(value, errors=errors)
)
rows = list(fresh_db["example"].rows)
expected = [{"id": 1, "dt": None if errors is recipes.SET_NULL else "invalid"}]
assert rows == expected
@pytest.mark.parametrize("delimiter", [None, ";", "-"])
def test_jsonsplit(fresh_db, delimiter):
fresh_db["example"].insert_all(