Team Ai
Modelpublic

AryaWu/sqlite

sourceHugging Faceupdated 10mo agoView on Hugging Face
0likes
tests_build_sqlite3DeferForeignKey.c125 linesDownload Raw Back to tests
1#include "sqliteInt.h"2#include "unity.h"3#include <string.h>4#include <stdlib.h>5 6/* Unity setup/teardown */7void setUp(void) {8  /* no-op */9}10void tearDown(void) {11  /* no-op */12}13 14/* Helper: initialize a Parse with a new ordinary Table and optional FKey */15#ifndef SQLITE_OMIT_FOREIGN_KEY16static void initParseWithOrdinaryTableAndFKey(Parse *pParse, Table *pTab, FKey *pFKey){17  memset(pParse, 0, sizeof(*pParse));18  memset(pTab, 0, sizeof(*pTab));19  if( pFKey ){20    memset(pFKey, 0, sizeof(*pFKey));21    pTab->u.tab.pFKey = pFKey;22  }else{23    pTab->u.tab.pFKey = 0;24  }25  /* pTab as zero-initialized, with pSelect==NULL and default flags, should be an ordinary table */26  pParse->pNewTable = pTab;27}28#endif29 30/* Test: function is callable and safe when pNewTable==NULL (works in all builds) */31static void test_sqlite3DeferForeignKey_no_new_table_safe(void){32  Parse parse;33  memset(&parse, 0, sizeof(parse));34  parse.pNewTable = NULL; /* explicit */35  /* Should not crash; no observable side-effects to assert other than completion */36  sqlite3DeferForeignKey(&parse, 0);37  sqlite3DeferForeignKey(&parse, 1);38  /* If we reach here, the calls were safe */39  TEST_ASSERT_TRUE(1);40}41 42#ifndef SQLITE_OMIT_FOREIGN_KEY43 44/* Test: sets isDeferred to 0 (INITIALLY IMMEDIATE) on an ordinary table with an FK */45static void test_sqlite3DeferForeignKey_sets_immediate(void){46  Parse parse;47  Table tab;48  FKey fk;49  initParseWithOrdinaryTableAndFKey(&parse, &tab, &fk);50 51  fk.isDeferred = 1; /* start deferred */52  sqlite3DeferForeignKey(&parse, 0);53  TEST_ASSERT_EQUAL_UINT8(0, fk.isDeferred);54}55 56/* Test: sets isDeferred to 1 (INITIALLY DEFERRED) on an ordinary table with an FK */57static void test_sqlite3DeferForeignKey_sets_deferred(void){58  Parse parse;59  Table tab;60  FKey fk;61  initParseWithOrdinaryTableAndFKey(&parse, &tab, &fk);62 63  fk.isDeferred = 0; /* start immediate */64  sqlite3DeferForeignKey(&parse, 1);65  TEST_ASSERT_EQUAL_UINT8(1, fk.isDeferred);66}67 68/* Test: no-op when table has no foreign key (pFKey == NULL) */69static void test_sqlite3DeferForeignKey_no_fk_noop(void){70  Parse parse;71  Table tab;72  memset(&parse, 0, sizeof(parse));73  memset(&tab, 0, sizeof(tab));74  parse.pNewTable = &tab;75  tab.u.tab.pFKey = 0;76 77  /* Should not crash and nothing to modify */78  sqlite3DeferForeignKey(&parse, 1);79  sqlite3DeferForeignKey(&parse, 0);80  TEST_ASSERT_TRUE(1);81}82 83/* Test: no-op for non-ordinary table (simulate with pSelect non-NULL) */84static void test_sqlite3DeferForeignKey_non_ordinary_table_noop(void){85  Parse parse;86  Table tab;87  FKey fk;88  Select dummySelect;89 90  memset(&parse, 0, sizeof(parse));91  memset(&tab, 0, sizeof(tab));92  memset(&fk, 0, sizeof(fk));93  memset(&dummySelect, 0, sizeof(dummySelect));94 95  /* Attach both an FK and a pSelect to simulate a view/non-ordinary table */96  tab.u.tab.pFKey = &fk;97  fk.isDeferred = 0;98  tab.pSelect = &dummySelect;99  parse.pNewTable = &tab;100 101  /* Should return early due to !IsOrdinaryTable(tab) and leave fk.isDeferred unchanged */102  sqlite3DeferForeignKey(&parse, 1);103  TEST_ASSERT_EQUAL_UINT8(0, fk.isDeferred);104 105  sqlite3DeferForeignKey(&parse, 0);106  TEST_ASSERT_EQUAL_UINT8(0, fk.isDeferred);107}108 109#endif /* SQLITE_OMIT_FOREIGN_KEY */110 111/* Main */112int main(void) {113  UNITY_BEGIN();114 115  RUN_TEST(test_sqlite3DeferForeignKey_no_new_table_safe);116 117#ifndef SQLITE_OMIT_FOREIGN_KEY118  RUN_TEST(test_sqlite3DeferForeignKey_sets_immediate);119  RUN_TEST(test_sqlite3DeferForeignKey_sets_deferred);120  RUN_TEST(test_sqlite3DeferForeignKey_no_fk_noop);121  RUN_TEST(test_sqlite3DeferForeignKey_non_ordinary_table_noop);122#endif123 124  return UNITY_END();125}