diff --git a/tests/test_cli.py b/tests/test_cli.py index 10061a2..28204b2 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -1,6 +1,7 @@ from click.testing import CliRunner from yaml_to_sqlite import cli import sqlite_utils +import json TEST_YAML = """ @@ -8,13 +9,21 @@ url: https://github.com/simonw/datasette-cluster-map - name: datasette-vega url: https://github.com/simonw/datasette-vega + nested_with_date: + - title: Hello + date: 2010-01-01 """ EXPECTED = [ { "name": "datasette-cluster-map", "url": "https://github.com/simonw/datasette-cluster-map", + "nested_with_date": None, + }, + { + "name": "datasette-vega", + "url": "https://github.com/simonw/datasette-vega", + "nested_with_date": json.dumps([{"title": "Hello", "date": "2010-01-01"}]), }, - {"name": "datasette-vega", "url": "https://github.com/simonw/datasette-vega"}, ] diff --git a/yaml_to_sqlite/cli.py b/yaml_to_sqlite/cli.py index 202f21f..61c1ee0 100644 --- a/yaml_to_sqlite/cli.py +++ b/yaml_to_sqlite/cli.py @@ -1,6 +1,7 @@ import click import yaml import sqlite_utils +import json @click.command() @@ -14,4 +15,8 @@ def cli(db_path, table, yaml_file, pk): "Covert YAML files to SQLite" db = sqlite_utils.Database(db_path) - db[table].upsert_all(yaml.safe_load(yaml_file), pk=pk) + docs = yaml.safe_load(yaml_file) + # We round-trip the docs to JSON to ensure anything unexpected + # like date objects is converted to valid JSON values + docs = json.loads(json.dumps(docs, default=str)) + db[table].upsert_all(docs, pk=pk)