@@ -2017,6 +2017,43 @@ bool do_test_error_cases (sqlite3 *db) {
20172017 return true;
20182018}
20192019
2020+ bool do_test_null_prikey_insert (sqlite3 * db ) {
2021+ // Create a table with a primary key that allows NULL (no NOT NULL constraint)
2022+ const char * sql = "CREATE TABLE IF NOT EXISTS t_null_pk (id TEXT PRIMARY KEY, value TEXT);"
2023+ "SELECT cloudsync_init('t_null_pk');" ;
2024+ int rc = sqlite3_exec (db , sql , NULL , NULL , NULL );
2025+ if (rc != SQLITE_OK ) return false;
2026+
2027+ // Attempt to insert a row with NULL primary key — should fail
2028+ char * errmsg = NULL ;
2029+ sql = "INSERT INTO t_null_pk (id, value) VALUES (NULL, 'test');" ;
2030+ rc = sqlite3_exec (db , sql , NULL , NULL , & errmsg );
2031+ if (rc == SQLITE_OK ) return false; // should have failed
2032+ if (!errmsg ) return false;
2033+
2034+ // Verify the error message matches the expected format
2035+ const char * expected = "Insert aborted because primary key in table t_null_pk contains NULL values." ;
2036+ bool match = (strcmp (errmsg , expected ) == 0 );
2037+ sqlite3_free (errmsg );
2038+ if (!match ) return false;
2039+
2040+ // Verify that a non-NULL primary key insert succeeds
2041+ sql = "INSERT INTO t_null_pk (id, value) VALUES ('valid_id', 'test');" ;
2042+ rc = sqlite3_exec (db , sql , NULL , NULL , NULL );
2043+ if (rc != SQLITE_OK ) return false;
2044+
2045+ // Verify the metatable has exactly 1 row (only the valid insert)
2046+ sqlite3_stmt * stmt = NULL ;
2047+ rc = sqlite3_prepare_v2 (db , "SELECT COUNT(*) FROM t_null_pk_cloudsync;" , -1 , & stmt , NULL );
2048+ if (rc != SQLITE_OK ) return false;
2049+ if (sqlite3_step (stmt ) != SQLITE_ROW ) { sqlite3_finalize (stmt ); return false; }
2050+ int count = sqlite3_column_int (stmt , 0 );
2051+ sqlite3_finalize (stmt );
2052+ if (count != 1 ) return false;
2053+
2054+ return true;
2055+ }
2056+
20202057bool do_test_internal_functions (void ) {
20212058 sqlite3 * db = NULL ;
20222059 sqlite3_stmt * vm = NULL ;
@@ -7842,6 +7879,7 @@ int main (int argc, const char * argv[]) {
78427879 result += test_report ("DBUtils Test:" , do_test_dbutils ());
78437880 result += test_report ("Minor Test:" , do_test_others (db ));
78447881 result += test_report ("Test Error Cases:" , do_test_error_cases (db ));
7882+ result += test_report ("Null PK Insert Test:" , do_test_null_prikey_insert (db ));
78457883 result += test_report ("Test Single PK:" , do_test_single_pk (print_result ));
78467884
78477885 int test_mask = TEST_INSERT | TEST_UPDATE | TEST_DELETE ;
0 commit comments