Add more STRICT table support (#604)

* Add more STRICT table support per https://github.com/simonw/sqlite-utils/issues/344#issuecomment-982014776.
* Make `table.transform()` preserve STRICT mode.
* Fix mypy failures in PR #604
* Link to SQLITE strict page in a few places
This commit is contained in:
Taj Khattra 2023-12-07 21:05:27 -08:00 committed by GitHub
commit 1500c19bd0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 182 additions and 5 deletions

View file

@ -2401,3 +2401,32 @@ def test_load_extension(entrypoint, should_pass, should_fail):
catch_exceptions=False,
)
assert result.exit_code == 1
@pytest.mark.parametrize("strict", (False, True))
def test_create_table_strict(strict):
runner = CliRunner()
with runner.isolated_filesystem():
db = Database("test.db")
result = runner.invoke(
cli.cli,
["create-table", "test.db", "items", "id", "integer"]
+ (["--strict"] if strict else []),
)
assert result.exit_code == 0
assert db["items"].strict == strict or not db.supports_strict
@pytest.mark.parametrize("method", ("insert", "upsert"))
@pytest.mark.parametrize("strict", (False, True))
def test_insert_upsert_strict(tmpdir, method, strict):
db_path = str(tmpdir / "test.db")
result = CliRunner().invoke(
cli.cli,
[method, db_path, "items", "-", "--csv", "--pk", "id"]
+ (["--strict"] if strict else []),
input="id\n1",
)
assert result.exit_code == 0
db = Database(db_path)
assert db["items"].strict == strict or not db.supports_strict

View file

@ -1316,3 +1316,46 @@ def test_rename_table(fresh_db):
# Should error if table does not exist:
with pytest.raises(sqlite3.OperationalError):
fresh_db.rename_table("does_not_exist", "renamed")
@pytest.mark.parametrize("strict", (False, True))
def test_database_strict(strict):
db = Database(memory=True, strict=strict)
table = db.table("t", columns={"id": int})
table.insert({"id": 1})
assert table.strict == strict or not db.supports_strict
@pytest.mark.parametrize("strict", (False, True))
def test_database_strict_override(strict):
db = Database(memory=True, strict=strict)
table = db.table("t", columns={"id": int}, strict=not strict)
table.insert({"id": 1})
assert table.strict != strict or not db.supports_strict
@pytest.mark.parametrize(
"method_name", ("insert", "upsert", "insert_all", "upsert_all")
)
@pytest.mark.parametrize("strict", (False, True))
def test_insert_upsert_strict(fresh_db, method_name, strict):
table = fresh_db["t"]
method = getattr(table, method_name)
record = {"id": 1}
if method_name.endswith("_all"):
record = [record]
method(record, pk="id", strict=strict)
assert table.strict == strict or not fresh_db.supports_strict
@pytest.mark.parametrize("strict", (False, True))
def test_create_table_strict(fresh_db, strict):
table = fresh_db.create_table("t", {"id": int}, strict=strict)
assert table.strict == strict or not fresh_db.supports_strict
@pytest.mark.parametrize("strict", (False, True))
def test_create_strict(fresh_db, strict):
table = fresh_db["t"]
table.create({"id": int}, strict=strict)
assert table.strict == strict or not fresh_db.supports_strict

View file

@ -151,3 +151,9 @@ def test_lookup_with_extra_insert_parameters(fresh_db):
columns=["name", "type"],
)
]
@pytest.mark.parametrize("strict", (False, True))
def test_lookup_new_table_strict(fresh_db, strict):
fresh_db["species"].lookup({"name": "Palm"}, strict=strict)
assert fresh_db["species"].strict == strict or not fresh_db.supports_strict

View file

@ -530,3 +530,12 @@ def test_transform_preserves_rowids(fresh_db, table_type):
tuple(row) for row in fresh_db.execute("select rowid, id, name from places")
)
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)
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