sqlite-utils/tests/test_create_view.py
Claude a2e6ea2eca
Raise ValueError instead of AssertionError for invalid arguments
User-facing argument validation in db.py previously used bare
assert statements, which vanish entirely under python -O and raise
AssertionError - an exception type callers should not have to
catch for input validation. Fifteen validation sites now raise
ValueError with the same messages. Internal invariants (an
unreachable branch and a post-update rowcount check) remain
asserts.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01UnLnhsH25Nnv7LHhekUfPd
2026-07-04 19:19:24 +00:00

43 lines
1.4 KiB
Python

import pytest
from sqlite_utils.utils import OperationalError
def test_create_view(fresh_db):
fresh_db.create_view("bar", "select 1 + 1")
rows = fresh_db.execute("select * from bar").fetchall()
assert [(2,)] == rows
def test_create_view_error(fresh_db):
fresh_db.create_view("bar", "select 1 + 1")
with pytest.raises(OperationalError):
fresh_db.create_view("bar", "select 1 + 2")
def test_create_view_only_arrow_one_param(fresh_db):
with pytest.raises(ValueError):
fresh_db.create_view("bar", "select 1 + 2", ignore=True, replace=True)
def test_create_view_ignore(fresh_db):
fresh_db.create_view("bar", "select 1 + 1").create_view(
"bar", "select 1 + 2", ignore=True
)
rows = fresh_db.execute("select * from bar").fetchall()
assert [(2,)] == rows
def test_create_view_replace(fresh_db):
fresh_db.create_view("bar", "select 1 + 1").create_view(
"bar", "select 1 + 2", replace=True
)
rows = fresh_db.execute("select * from bar").fetchall()
assert [(3,)] == rows
def test_create_view_replace_with_same_does_nothing(fresh_db):
fresh_db.create_view("bar", "select 1 + 1")
initial_version = fresh_db.execute("PRAGMA schema_version").fetchone()[0]
fresh_db.create_view("bar", "select 1 + 1", replace=True)
after_version = fresh_db.execute("PRAGMA schema_version").fetchone()[0]
assert after_version == initial_version