table.drop(ignore=True) option, refs #237

This commit is contained in:
Simon Willison 2021-02-25 09:05:08 -08:00
commit c236894caa
4 changed files with 37 additions and 5 deletions

View file

@ -6,6 +6,7 @@ from sqlite_utils.db import (
NoObviousTable,
ForeignKey,
Table,
View,
)
from sqlite_utils.utils import sqlite3
import collections
@ -944,6 +945,21 @@ def test_drop_view(fresh_db):
assert [] == fresh_db.view_names()
def test_drop_ignore(fresh_db):
with pytest.raises(sqlite3.OperationalError):
fresh_db["does_not_exist"].drop()
fresh_db["does_not_exist"].drop(ignore=True)
# Testing view is harder, we need to create it in order
# to get a View object, then drop it twice
fresh_db.create_view("foo_view", "select 1")
view = fresh_db["foo_view"]
assert isinstance(view, View)
view.drop()
with pytest.raises(sqlite3.OperationalError):
view.drop()
view.drop(ignore=True)
def test_insert_all_empty_list(fresh_db):
fresh_db["t"].insert({"foo": 1})
assert 1 == fresh_db["t"].count

View file

@ -28,7 +28,9 @@ def test_extract_single_column(fresh_db, table, fk_column):
" [name] TEXT,\n"
" [{}] INTEGER,\n".format(expected_fk)
+ " [end] INTEGER,\n"
+ " FOREIGN KEY([{}]) REFERENCES [{}]([id])\n".format(expected_fk, expected_table)
+ " FOREIGN KEY([{}]) REFERENCES [{}]([id])\n".format(
expected_fk, expected_table
)
+ ")"
)
assert fresh_db[expected_table].schema == (