2022-07-15 14:45:14 -07:00
|
|
|
from sqlite_utils.db import NoTable
|
2022-07-16 05:21:36 +08:00
|
|
|
import datetime
|
2022-07-15 14:23:57 -07:00
|
|
|
import pytest
|
2022-07-16 05:21:36 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_duplicate(fresh_db):
|
|
|
|
|
# Create table using native Sqlite statement:
|
2026-05-17 16:52:48 -07:00
|
|
|
fresh_db.execute("""CREATE TABLE "table1" (
|
2025-11-23 20:43:26 -08:00
|
|
|
"text_col" TEXT,
|
|
|
|
|
"real_col" REAL,
|
|
|
|
|
"int_col" INTEGER,
|
|
|
|
|
"bool_col" INTEGER,
|
2026-05-17 16:52:48 -07:00
|
|
|
"datetime_col" TEXT)""")
|
2022-07-16 05:21:36 +08:00
|
|
|
# Insert one row of mock data:
|
|
|
|
|
dt = datetime.datetime.now()
|
|
|
|
|
data = {
|
|
|
|
|
"text_col": "Cleo",
|
|
|
|
|
"real_col": 3.14,
|
|
|
|
|
"int_col": -255,
|
|
|
|
|
"bool_col": True,
|
|
|
|
|
"datetime_col": str(dt),
|
|
|
|
|
}
|
|
|
|
|
table1 = fresh_db["table1"]
|
|
|
|
|
row_id = table1.insert(data).last_rowid
|
|
|
|
|
# Duplicate table:
|
|
|
|
|
table2 = table1.duplicate("table2")
|
|
|
|
|
# Ensure data integrity:
|
|
|
|
|
assert data == table2.get(row_id)
|
|
|
|
|
# Ensure schema integrity:
|
|
|
|
|
assert [
|
|
|
|
|
{"name": "text_col", "type": "TEXT"},
|
|
|
|
|
{"name": "real_col", "type": "REAL"},
|
|
|
|
|
{"name": "int_col", "type": "INT"},
|
|
|
|
|
{"name": "bool_col", "type": "INT"},
|
|
|
|
|
{"name": "datetime_col", "type": "TEXT"},
|
|
|
|
|
] == [{"name": col.name, "type": col.type} for col in table2.columns]
|
2022-07-15 14:23:57 -07:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_duplicate_fails_if_table_does_not_exist(fresh_db):
|
2022-07-15 14:45:14 -07:00
|
|
|
with pytest.raises(NoTable):
|
2022-07-15 14:23:57 -07:00
|
|
|
fresh_db["not_a_table"].duplicate("duplicated")
|