#include #include #include #include #include namespace { using Void::Sqlite::Database; using Void::Sqlite::Migration; using Void::Testing::Test; [[nodiscard]] Database fresh() { auto db = Database::open(":memory:"); Void::Testing::expect(db.has_value()); return std::move(*db); } struct SqliteTests : Void::Testing::Suite { Test open_in_memory() { auto db = Database::open(":memory:"); Void::Testing::expect(db.has_value()); } Test exec_create_and_insert() { auto db = fresh(); Void::Testing::expect( db.exec("CREATE TABLE t(id INTEGER PRIMARY KEY, " "name TEXT, score REAL)") .has_value()); Void::Testing::expect( db.exec("INSERT INTO t(name,score) VALUES('a',1.5)") .has_value()); Void::Testing::expect_eq(db.last_insert_rowid(), static_cast(1)); } Test prepared_bind_and_read() { auto db = fresh(); (void)db.exec("CREATE TABLE t(id INTEGER, name TEXT, s REAL)"); auto ins = db.prepare( "INSERT INTO t(id,name,s) VALUES(?,?,?)"); Void::Testing::expect(ins.has_value()); ins->bind(1, 7).bind(2, std::string_view{"hello"}).bind(3, 2.5); const auto done = ins->step(); Void::Testing::expect(done.has_value() && *done == false); auto sel = db.prepare("SELECT id,name,s FROM t WHERE id=?"); Void::Testing::expect(sel.has_value()); sel->bind(1, 7); const auto row = sel->step(); Void::Testing::expect(row.has_value() && *row == true); Void::Testing::expect_eq(sel->column_int(0), static_cast(7)); Void::Testing::expect_eq(sel->column_text(1), std::string{"hello"}); Void::Testing::expect(sel->column_double(2) == 2.5); const auto end = sel->step(); Void::Testing::expect(end.has_value() && *end == false); } Test null_handling() { auto db = fresh(); (void)db.exec("CREATE TABLE t(v TEXT)"); auto ins = db.prepare("INSERT INTO t(v) VALUES(?)"); ins->bind(1, nullptr); (void)ins->step(); auto sel = db.prepare("SELECT v FROM t"); (void)sel->step(); Void::Testing::expect(sel->is_null(0)); } Test transaction_commit_persists() { auto db = fresh(); (void)db.exec("CREATE TABLE t(n INTEGER)"); { auto tx = db.begin(); Void::Testing::expect(tx.has_value()); (void)db.exec("INSERT INTO t VALUES(1)"); (void)db.exec("INSERT INTO t VALUES(2)"); Void::Testing::expect(tx->commit().has_value()); } auto sel = db.prepare("SELECT COUNT(*) FROM t"); (void)sel->step(); Void::Testing::expect_eq(sel->column_int(0), static_cast(2)); } Test transaction_rollback_on_scope_exit() { auto db = fresh(); (void)db.exec("CREATE TABLE t(n INTEGER)"); { auto tx = db.begin(); (void)db.exec("INSERT INTO t VALUES(99)"); // no commit -> rolled back on destruction } auto sel = db.prepare("SELECT COUNT(*) FROM t"); (void)sel->step(); Void::Testing::expect_eq(sel->column_int(0), static_cast(0)); } Test migrations_apply_in_order_and_are_idempotent() { auto db = fresh(); const std::array migs{{ {1, "CREATE TABLE a(id INTEGER)"}, {2, "ALTER TABLE a ADD COLUMN name TEXT"}, }}; Void::Testing::expect(db.migrate(migs).has_value()); Void::Testing::expect_eq(*db.user_version(), 2); // Second call is a no-op (idempotent) and must not error. Void::Testing::expect(db.migrate(migs).has_value()); Void::Testing::expect_eq(*db.user_version(), 2); // The v2 column exists. Void::Testing::expect( db.exec("INSERT INTO a(id,name) VALUES(1,'x')") .has_value()); } Test prepare_bad_sql_errors() { auto db = fresh(); Void::Testing::expect(!db.prepare("SELECT FROM").has_value()); } }; } // namespace