mirror of
https://github.com/simonw/sqlite-utils.git
synced 2026-09-11 19:14:10 +02:00
.transform(strict=) and sqlite-utils transform --strict/--no-strict (#788)
* .transform(strict=) and sqlite-utils transform --strict/--no-strict Closes #787
This commit is contained in:
parent
dc61f75a0b
commit
b74b727035
8 changed files with 171 additions and 8 deletions
|
|
@ -3,6 +3,7 @@ from sqlite_utils.db import Index, ForeignKey
|
|||
from click.testing import CliRunner
|
||||
from pathlib import Path
|
||||
import subprocess
|
||||
import sqlite3
|
||||
import sys
|
||||
import json
|
||||
import os
|
||||
|
|
@ -1939,6 +1940,64 @@ def test_transform_sql(db_path):
|
|||
assert db["dogs"].schema == original_schema
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"initial_strict,args,expected_strict",
|
||||
(
|
||||
(False, [], False),
|
||||
(True, [], True),
|
||||
(False, ["--strict"], True),
|
||||
(True, ["--no-strict"], False),
|
||||
),
|
||||
)
|
||||
def test_transform_strict_option(db_path, initial_strict, args, expected_strict):
|
||||
db = Database(db_path)
|
||||
if not db.supports_strict:
|
||||
pytest.skip("SQLite version does not support strict tables")
|
||||
db["dogs"].create({"id": int}, strict=initial_strict)
|
||||
|
||||
result = CliRunner().invoke(cli.cli, ["transform", db_path, "dogs"] + args)
|
||||
|
||||
assert result.exit_code == 0, result.output
|
||||
assert db["dogs"].strict is expected_strict
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"initial_strict,flag,sql_is_strict",
|
||||
(
|
||||
(False, "--strict", True),
|
||||
(True, "--no-strict", False),
|
||||
),
|
||||
)
|
||||
def test_transform_strict_option_sql(db_path, initial_strict, flag, sql_is_strict):
|
||||
db = Database(db_path)
|
||||
if not db.supports_strict:
|
||||
pytest.skip("SQLite version does not support strict tables")
|
||||
db["dogs"].create({"id": int}, strict=initial_strict)
|
||||
|
||||
result = CliRunner().invoke(cli.cli, ["transform", db_path, "dogs", flag, "--sql"])
|
||||
|
||||
assert result.exit_code == 0, result.output
|
||||
assert (") STRICT;" in result.output) is sql_is_strict
|
||||
assert db["dogs"].strict is initial_strict
|
||||
|
||||
|
||||
def test_transform_strict_option_with_invalid_data(db_path):
|
||||
db = Database(db_path)
|
||||
if not db.supports_strict:
|
||||
pytest.skip("SQLite version does not support strict tables")
|
||||
dogs = db["dogs"]
|
||||
dogs.create({"id": int})
|
||||
dogs.insert({"id": "not-an-integer"})
|
||||
|
||||
result = CliRunner().invoke(cli.cli, ["transform", db_path, "dogs", "--strict"])
|
||||
|
||||
assert result.exit_code == 1
|
||||
assert isinstance(result.exception, sqlite3.IntegrityError)
|
||||
assert dogs.strict is False
|
||||
assert list(dogs.rows) == [{"id": "not-an-integer"}]
|
||||
assert not any(name.startswith("dogs_new_") for name in db.table_names())
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"extra_args,expected_schema",
|
||||
(
|
||||
|
|
|
|||
|
|
@ -1,3 +1,5 @@
|
|||
import sqlite3
|
||||
|
||||
from sqlite_utils.db import ForeignKey, TransformError
|
||||
from sqlite_utils.utils import OperationalError
|
||||
import pytest
|
||||
|
|
@ -566,13 +568,63 @@ def test_transform_preserves_rowids(fresh_db, table_type):
|
|||
assert previous_rows == next_rows
|
||||
|
||||
|
||||
@pytest.mark.parametrize("strict", (False, True))
|
||||
def test_transform_strict(fresh_db, strict):
|
||||
dogs = fresh_db.table("dogs", strict=strict)
|
||||
@pytest.mark.parametrize(
|
||||
"initial_strict,transform_strict,expected_strict",
|
||||
(
|
||||
(False, None, False),
|
||||
(True, None, True),
|
||||
(False, True, True),
|
||||
(True, False, False),
|
||||
),
|
||||
)
|
||||
def test_transform_strict(fresh_db, initial_strict, transform_strict, expected_strict):
|
||||
if not fresh_db.supports_strict:
|
||||
pytest.skip("SQLite version does not support strict tables")
|
||||
dogs = fresh_db.table("dogs", strict=initial_strict)
|
||||
dogs.insert({"id": 1, "name": "Cleo"})
|
||||
assert dogs.strict == strict or not fresh_db.supports_strict
|
||||
dogs.transform(not_null={"name"})
|
||||
assert dogs.strict == strict or not fresh_db.supports_strict
|
||||
assert dogs.strict is initial_strict
|
||||
dogs.transform(strict=transform_strict)
|
||||
assert dogs.strict is expected_strict
|
||||
|
||||
|
||||
def test_transform_to_strict_with_invalid_data(fresh_db):
|
||||
if not fresh_db.supports_strict:
|
||||
pytest.skip("SQLite version does not support strict tables")
|
||||
dogs = fresh_db["dogs"]
|
||||
dogs.create({"id": int})
|
||||
dogs.insert({"id": "not-an-integer"})
|
||||
|
||||
with pytest.raises(sqlite3.IntegrityError):
|
||||
dogs.transform(strict=True)
|
||||
|
||||
assert dogs.strict is False
|
||||
assert list(dogs.rows) == [{"id": "not-an-integer"}]
|
||||
assert fresh_db.table_names() == ["dogs"]
|
||||
|
||||
|
||||
def test_transform_strict_updates_default(fresh_db):
|
||||
if not fresh_db.supports_strict:
|
||||
pytest.skip("SQLite version does not support strict tables")
|
||||
table = fresh_db.table("items", strict=True)
|
||||
table.create({"id": int})
|
||||
|
||||
table.transform(strict=False)
|
||||
assert table.strict is False
|
||||
|
||||
table.create({"id": int}, replace=True)
|
||||
assert table.strict is False
|
||||
|
||||
|
||||
@pytest.mark.parametrize("method_name", ("transform", "transform_sql"))
|
||||
def test_transform_to_strict_not_supported(fresh_db, method_name):
|
||||
table = fresh_db["items"]
|
||||
table.create({"id": int})
|
||||
fresh_db._supports_strict = False
|
||||
|
||||
with pytest.raises(TransformError, match="SQLite does not support STRICT tables"):
|
||||
getattr(table, method_name)(strict=True)
|
||||
|
||||
assert table.strict is False
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue