2020-04-12 20:47:36 -07:00
|
|
|
from sqlite_utils import Database
|
|
|
|
|
import sqlite3
|
|
|
|
|
import pathlib
|
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_recreate_ignored_for_in_memory():
|
|
|
|
|
# None of these should raise an exception:
|
|
|
|
|
Database(memory=True, recreate=False)
|
|
|
|
|
Database(memory=True, recreate=True)
|
|
|
|
|
Database(":memory:", recreate=False)
|
|
|
|
|
Database(":memory:", recreate=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_recreate_not_allowed_for_connection():
|
|
|
|
|
conn = sqlite3.connect(":memory:")
|
2025-12-11 16:56:12 -08:00
|
|
|
try:
|
|
|
|
|
with pytest.raises(AssertionError):
|
|
|
|
|
Database(conn, recreate=True)
|
|
|
|
|
finally:
|
|
|
|
|
conn.close()
|
2020-04-12 20:47:36 -07:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
2022-10-25 13:27:18 -07:00
|
|
|
"use_path,create_file_first",
|
|
|
|
|
[(True, True), (True, False), (False, True), (False, False)],
|
2020-04-12 20:47:36 -07:00
|
|
|
)
|
2022-10-25 13:27:18 -07:00
|
|
|
def test_recreate(tmp_path, use_path, create_file_first):
|
2022-10-25 13:06:58 -07:00
|
|
|
filepath = str(tmp_path / "data.db")
|
2020-04-12 20:47:36 -07:00
|
|
|
if use_path:
|
|
|
|
|
filepath = pathlib.Path(filepath)
|
2022-10-25 13:27:18 -07:00
|
|
|
if create_file_first:
|
2022-10-25 13:36:17 -07:00
|
|
|
db = Database(filepath)
|
|
|
|
|
db["t1"].insert({"foo": "bar"})
|
|
|
|
|
assert ["t1"] == db.table_names()
|
2022-10-25 14:00:53 -07:00
|
|
|
db.close()
|
2020-04-12 20:47:36 -07:00
|
|
|
Database(filepath, recreate=True)["t2"].insert({"foo": "bar"})
|
|
|
|
|
assert ["t2"] == Database(filepath).table_names()
|