Team Ai
Modelpublic

AryaWu/sqlite

sourceHugging Faceupdated 10mo agoView on Hugging Face
0likes
tests_json_jsonEachConnect.c120 linesDownload Raw Back to tests
1#include "sqliteInt.h"2#include "unity.h"3 4#include <string.h>5#include <stdlib.h>6#include <stdint.h>7 8/* Prototype for the provided test wrapper */9extern int test_jsonEachConnect(sqlite3 *db, void *pAux, int argc,10                                const char *const*argv,11                                sqlite3_vtab **ppVtab, char **pzErr);12 13/* Probe structure to inspect the beginning of the allocated vtab object.14   This mirrors the leading fields set by jsonEachConnect. */15typedef struct JsonEachConnection_probe {16  sqlite3_vtab base;       /* Must be first (same as real struct) */17  sqlite3 *db;             /* Database connection */18  unsigned char eMode;     /* 1=json_each/tree, 2=jsonb_each/tree */19  unsigned char bRecursive;/* 1=tree, 0=each */20} JsonEachConnection_probe;21 22void setUp(void) {23  /* No global setup needed */24}25 26void tearDown(void) {27  /* No global teardown needed */28}29 30/* Helper to run connect and check expected flags */31static void helper_check_mode(const char *modname, unsigned char expMode,32                              unsigned char expRecursive){33  sqlite3 *db = 0;34  int rc = sqlite3_open(":memory:", &db);35  TEST_ASSERT_EQUAL_INT(SQLITE_OK, rc);36  TEST_ASSERT_NOT_NULL(db);37 38  const char *argv0_only[1];39  argv0_only[0] = modname;40  sqlite3_vtab *pVtab = NULL;41  char *zErr = NULL;42 43  rc = test_jsonEachConnect(db, NULL, 1, argv0_only, &pVtab, &zErr);44  TEST_ASSERT_EQUAL_INT_MESSAGE(SQLITE_OK, rc, "jsonEachConnect should return SQLITE_OK");45  TEST_ASSERT_NOT_NULL_MESSAGE(pVtab, "jsonEachConnect should allocate and return a vtab object");46  TEST_ASSERT_NULL(zErr);47 48  /* Copy out the leading bytes to inspect fields without aliasing issues */49  JsonEachConnection_probe probe;50  memset(&probe, 0, sizeof(probe));51  memcpy(&probe, pVtab, sizeof(probe));52 53  /* Basic invariants */54  TEST_ASSERT_EQUAL_PTR(db, probe.db);55  TEST_ASSERT_EQUAL_UINT8(expMode, probe.eMode);56  TEST_ASSERT_EQUAL_UINT8(expRecursive, probe.bRecursive);57 58  /* sqlite3DbMallocZero zero-initializes, so base should be clear */59  TEST_ASSERT_EQUAL_INT(0, probe.base.nRef);60  TEST_ASSERT_NULL(probe.base.zErrMsg);61  /* pModule is owned by SQLite core, but we didn't set it here; expect zero */62  TEST_ASSERT_NULL(probe.base.pModule);63 64  /* Clean up: free the allocated vtab and close the database */65  sqlite3DbFree(db, pVtab);66  sqlite3_close(db);67}68 69void test_jsonEachConnect_json_each_sets_mode1_nonrecursive(void){70  helper_check_mode("json_each", 1, 0);71}72 73void test_jsonEachConnect_json_tree_sets_mode1_recursive(void){74  helper_check_mode("json_tree", 1, 1);75}76 77void test_jsonEachConnect_jsonb_each_sets_mode2_nonrecursive(void){78  helper_check_mode("jsonb_each", 2, 0);79}80 81void test_jsonEachConnect_jsonb_tree_sets_mode2_recursive(void){82  helper_check_mode("jsonb_tree", 2, 1);83}84 85void test_jsonEachConnect_allocates_and_initializes_base(void){86  sqlite3 *db = 0;87  int rc = sqlite3_open(":memory:", &db);88  TEST_ASSERT_EQUAL_INT(SQLITE_OK, rc);89  TEST_ASSERT_NOT_NULL(db);90 91  const char *argv0_only[1] = { "json_each" };92  sqlite3_vtab *pVtab = NULL;93  char *zErr = NULL;94 95  rc = test_jsonEachConnect(db, NULL, 1, argv0_only, &pVtab, &zErr);96  TEST_ASSERT_EQUAL_INT(SQLITE_OK, rc);97  TEST_ASSERT_NOT_NULL(pVtab);98 99  JsonEachConnection_probe probe;100  memset(&probe, 0, sizeof(probe));101  memcpy(&probe, pVtab, sizeof(probe));102 103  /* Base is zero-initialized */104  TEST_ASSERT_EQUAL_INT(0, probe.base.nRef);105  TEST_ASSERT_NULL(probe.base.zErrMsg);106  TEST_ASSERT_NULL(probe.base.pModule);107 108  sqlite3DbFree(db, pVtab);109  sqlite3_close(db);110}111 112int main(void){113  UNITY_BEGIN();114  RUN_TEST(test_jsonEachConnect_json_each_sets_mode1_nonrecursive);115  RUN_TEST(test_jsonEachConnect_json_tree_sets_mode1_recursive);116  RUN_TEST(test_jsonEachConnect_jsonb_each_sets_mode2_nonrecursive);117  RUN_TEST(test_jsonEachConnect_jsonb_tree_sets_mode2_recursive);118  RUN_TEST(test_jsonEachConnect_allocates_and_initializes_base);119  return UNITY_END();120}