Team Ai
Modelpublic

AryaWu/sqlite

sourceHugging Faceupdated 10mo agoView on Hugging Face
0likes
tests_pager_sqlite3PagerOpenWal.c127 linesDownload Raw Back to tests
1#include "sqliteInt.h"2#include "unity.h"3 4#include <stdio.h>5#include <string.h>6 7/* Helper to fetch Pager* from a live sqlite3* connection */8static Pager* getPagerFromDb(sqlite3 *db){9  Btree *pBt = db->aDb[0].pBt;10  return sqlite3BtreePager(pBt);11}12 13void setUp(void) {14  /* No global setup needed */15}16 17void tearDown(void) {18  /* No global teardown needed */19}20 21/* Test that sqlite3PagerOpenWal returns SQLITE_CANTOPEN if WAL is not supported */22void test_sqlite3PagerOpenWal_returns_CANTOPEN_when_WAL_not_supported(void){23  sqlite3 *db = 0;24  int rc = sqlite3_open("test_pager_openwal_cantopen.db", &db);25  TEST_ASSERT_EQUAL_INT(SQLITE_OK, rc);26  TEST_ASSERT_NOT_NULL(db);27 28  Pager *pPager = getPagerFromDb(db);29  TEST_ASSERT_NOT_NULL(pPager);30 31  /* Precondition: PAGER_OPEN path requires pbOpen==NULL per function asserts. */32  /* Also ensure WAL not already open and not a temp file. */33  TEST_ASSERT_EQUAL_INT(0, pPager->tempFile);34  TEST_ASSERT_NULL(pPager->pWal);35 36  /* Force WAL unsupported: sqlite3PagerWalSupported() returns 0 if noLock is set */37  int savedNoLock = pPager->noLock;38  pPager->noLock = 1;39 40  rc = sqlite3PagerOpenWal(pPager, NULL);41  TEST_ASSERT_EQUAL_INT(SQLITE_CANTOPEN, rc);42 43  /* Restore and cleanup */44  pPager->noLock = savedNoLock;45  sqlite3_close(db);46}47 48/* Test that sqlite3PagerOpenWal opens WAL and updates fields when supported */49void test_sqlite3PagerOpenWal_opens_WAL_and_sets_mode_when_supported(void){50  sqlite3 *db = 0;51  int rc = sqlite3_open("test_pager_openwal_open.db", &db);52  TEST_ASSERT_EQUAL_INT(SQLITE_OK, rc);53  TEST_ASSERT_NOT_NULL(db);54 55  /* Start a deferred transaction and perform a read to enter PAGER_READER. */56  rc = sqlite3_exec(db, "BEGIN;", 0, 0, 0);57  TEST_ASSERT_EQUAL_INT(SQLITE_OK, rc);58  rc = sqlite3_exec(db, "SELECT count(*) FROM sqlite_master;", 0, 0, 0);59  TEST_ASSERT_EQUAL_INT(SQLITE_OK, rc);60 61  Pager *pPager = getPagerFromDb(db);62  TEST_ASSERT_NOT_NULL(pPager);63 64  /* Ensure not already in WAL */65  TEST_ASSERT_NULL(pPager->pWal);66 67  int flag = 0; /* Must be 0 per function asserts */68  rc = sqlite3PagerOpenWal(pPager, &flag);69  TEST_ASSERT_EQUAL_INT(SQLITE_OK, rc);70 71  /* After success, WAL should be open, journalMode set to WAL, and eState set to OPEN */72  TEST_ASSERT_NOT_NULL(pPager->pWal);73  TEST_ASSERT_EQUAL_INT(PAGER_JOURNALMODE_WAL, pPager->journalMode);74  TEST_ASSERT_EQUAL_INT(PAGER_OPEN, pPager->eState);75  TEST_ASSERT_EQUAL_INT(0, flag); /* Not modified in this path */76 77  /* Cleanup */78  rc = sqlite3_exec(db, "ROLLBACK;", 0, 0, 0);79  TEST_ASSERT_EQUAL_INT(SQLITE_OK, rc);80  sqlite3_close(db);81}82 83/* Test that calling sqlite3PagerOpenWal when WAL already open is a no-op and sets *pbOpen=1 */84void test_sqlite3PagerOpenWal_noop_when_WAL_already_open(void){85  sqlite3 *db = 0;86  int rc = sqlite3_open("test_pager_openwal_noop.db", &db);87  TEST_ASSERT_EQUAL_INT(SQLITE_OK, rc);88  TEST_ASSERT_NOT_NULL(db);89 90  /* Begin a deferred transaction and read to enter PAGER_READER */91  rc = sqlite3_exec(db, "BEGIN;", 0, 0, 0);92  TEST_ASSERT_EQUAL_INT(SQLITE_OK, rc);93  rc = sqlite3_exec(db, "SELECT count(*) FROM sqlite_master;", 0, 0, 0);94  TEST_ASSERT_EQUAL_INT(SQLITE_OK, rc);95 96  Pager *pPager = getPagerFromDb(db);97  TEST_ASSERT_NOT_NULL(pPager);98 99  /* First call: open WAL (supported path) */100  int flag = 0;101  rc = sqlite3PagerOpenWal(pPager, &flag);102  TEST_ASSERT_EQUAL_INT(SQLITE_OK, rc);103  TEST_ASSERT_NOT_NULL(pPager->pWal);104 105  /* Ensure we are again in PAGER_READER for the second call (asserts require pbOpen!=NULL in READER) */106  rc = sqlite3_exec(db, "SELECT count(*) FROM sqlite_master;", 0, 0, 0);107  TEST_ASSERT_EQUAL_INT(SQLITE_OK, rc);108 109  /* Second call: since pWal is already open, expect a no-op with *pbOpen=1 */110  flag = 0;111  rc = sqlite3PagerOpenWal(pPager, &flag);112  TEST_ASSERT_EQUAL_INT(SQLITE_OK, rc);113  TEST_ASSERT_EQUAL_INT(1, flag);114 115  /* Cleanup */116  rc = sqlite3_exec(db, "ROLLBACK;", 0, 0, 0);117  TEST_ASSERT_EQUAL_INT(SQLITE_OK, rc);118  sqlite3_close(db);119}120 121int main(void) {122  UNITY_BEGIN();123  RUN_TEST(test_sqlite3PagerOpenWal_returns_CANTOPEN_when_WAL_not_supported);124  RUN_TEST(test_sqlite3PagerOpenWal_opens_WAL_and_sets_mode_when_supported);125  RUN_TEST(test_sqlite3PagerOpenWal_noop_when_WAL_already_open);126  return UNITY_END();127}