Mercurial
comparison deita/deita_test.c @ 75:ae6a88e6e484
[Deita] Simple DB connection lib.
| author | June Park <parkjune1995@gmail.com> |
|---|---|
| date | Wed, 31 Dec 2025 11:20:08 -0800 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| 74:4b96794c8d59 | 75:ae6a88e6e484 |
|---|---|
| 1 #include "deita.h" | |
| 2 #include <stdio.h> | |
| 3 #include <assert.h> | |
| 4 #include <unistd.h> | |
| 5 | |
| 6 int main(void) | |
| 7 { | |
| 8 printf("=== Deita Library Tests ===\n\n"); | |
| 9 | |
| 10 const char *test_db_path = "/tmp/deita_test.db"; | |
| 11 unlink(test_db_path); | |
| 12 | |
| 13 // Test 1: Connection creation | |
| 14 printf("Test 1: Creating SQLite3 connection...\n"); | |
| 15 Deita_Connection *p_connection = Deita_Connection_Create(DEITA_DATABASE_TYPE_SQLITE3, test_db_path); | |
| 16 assert(p_connection != NULL); | |
| 17 assert(Deita_Connection_Is_Open(p_connection) == TRUE); | |
| 18 printf(" PASSED: Connection created successfully\n\n"); | |
| 19 | |
| 20 // Test 2: Create table | |
| 21 printf("Test 2: Creating test table...\n"); | |
| 22 const char *create_table_query = | |
| 23 "CREATE TABLE IF NOT EXISTS test_users (" | |
| 24 " id INTEGER PRIMARY KEY AUTOINCREMENT," | |
| 25 " username TEXT NOT NULL," | |
| 26 " age INTEGER" | |
| 27 ")"; | |
| 28 int32 result = Deita_Query_Execute_Update(p_connection, create_table_query); | |
| 29 assert(result == 0); | |
| 30 printf(" PASSED: Table created\n\n"); | |
| 31 | |
| 32 // Test 3: Insert data | |
| 33 printf("Test 3: Inserting test data...\n"); | |
| 34 const char *insert_query = "INSERT INTO test_users (username, age) VALUES ('alice', 25)"; | |
| 35 result = Deita_Query_Execute_Update(p_connection, insert_query); | |
| 36 assert(result == 1); | |
| 37 printf(" PASSED: Inserted 1 row\n\n"); | |
| 38 | |
| 39 // Test 4: Insert with prepared statement | |
| 40 printf("Test 4: Inserting with prepared statement...\n"); | |
| 41 const char *insert_prepared_query = "INSERT INTO test_users (username, age) VALUES (?, ?)"; | |
| 42 const char *params[] = {"bob", "30"}; | |
| 43 result = Deita_Query_Execute_Update_Prepared(p_connection, insert_prepared_query, 2, params); | |
| 44 assert(result == 1); | |
| 45 printf(" PASSED: Inserted 1 row with prepared statement\n\n"); | |
| 46 | |
| 47 // Test 5: Query data | |
| 48 printf("Test 5: Querying test data...\n"); | |
| 49 Dowa_Arena *p_arena = Dowa_Arena_Create(ONE_MEGA_BYTE); | |
| 50 const char *select_query = "SELECT id, username, age FROM test_users ORDER BY id"; | |
| 51 Deita_Result_Set *p_result_set = Deita_Query_Execute(p_connection, select_query, p_arena); | |
| 52 assert(p_result_set != NULL); | |
| 53 | |
| 54 int32 row_count = 0; | |
| 55 while (Deita_Result_Set_Next(p_result_set)) | |
| 56 { | |
| 57 row_count++; | |
| 58 int64 id = Deita_Result_Set_Get_Integer(p_result_set, 0); | |
| 59 const char *username = Deita_Result_Set_Get_Text(p_result_set, 1); | |
| 60 int64 age = Deita_Result_Set_Get_Integer(p_result_set, 2); | |
| 61 | |
| 62 printf(" Row %d: id=%lld, username=%s, age=%lld\n", row_count, id, username, age); | |
| 63 | |
| 64 if (row_count == 1) | |
| 65 { | |
| 66 assert(id == 1); | |
| 67 assert(strcmp(username, "alice") == 0); | |
| 68 assert(age == 25); | |
| 69 } | |
| 70 else if (row_count == 2) | |
| 71 { | |
| 72 assert(id == 2); | |
| 73 assert(strcmp(username, "bob") == 0); | |
| 74 assert(age == 30); | |
| 75 } | |
| 76 } | |
| 77 assert(row_count == 2); | |
| 78 Deita_Result_Set_Free(p_result_set); | |
| 79 printf(" PASSED: Retrieved 2 rows\n\n"); | |
| 80 | |
| 81 // Test 6: Query with prepared statement | |
| 82 printf("Test 6: Querying with prepared statement...\n"); | |
| 83 const char *select_prepared_query = "SELECT username, age FROM test_users WHERE username = ?"; | |
| 84 const char *query_params[] = {"alice"}; | |
| 85 p_result_set = Deita_Query_Execute_Prepared(p_connection, select_prepared_query, 1, query_params, p_arena); | |
| 86 assert(p_result_set != NULL); | |
| 87 | |
| 88 assert(Deita_Result_Set_Next(p_result_set) == TRUE); | |
| 89 const char *username = Deita_Result_Set_Get_Text(p_result_set, 0); | |
| 90 int64 age = Deita_Result_Set_Get_Integer(p_result_set, 1); | |
| 91 assert(strcmp(username, "alice") == 0); | |
| 92 assert(age == 25); | |
| 93 assert(Deita_Result_Set_Next(p_result_set) == FALSE); | |
| 94 Deita_Result_Set_Free(p_result_set); | |
| 95 printf(" PASSED: Retrieved correct row with prepared statement\n\n"); | |
| 96 | |
| 97 // Test 7: Column metadata | |
| 98 printf("Test 7: Testing column metadata...\n"); | |
| 99 p_result_set = Deita_Query_Execute(p_connection, "SELECT id, username FROM test_users LIMIT 1", p_arena); | |
| 100 assert(p_result_set != NULL); | |
| 101 | |
| 102 int32 column_count = Deita_Result_Set_Get_Column_Count(p_result_set); | |
| 103 assert(column_count == 2); | |
| 104 | |
| 105 const char *col_name_0 = Deita_Result_Set_Get_Column_Name(p_result_set, 0); | |
| 106 const char *col_name_1 = Deita_Result_Set_Get_Column_Name(p_result_set, 1); | |
| 107 assert(strcmp(col_name_0, "id") == 0); | |
| 108 assert(strcmp(col_name_1, "username") == 0); | |
| 109 | |
| 110 assert(Deita_Result_Set_Next(p_result_set) == TRUE); | |
| 111 Deita_Column_Type type_0 = Deita_Result_Set_Get_Column_Type(p_result_set, 0); | |
| 112 Deita_Column_Type type_1 = Deita_Result_Set_Get_Column_Type(p_result_set, 1); | |
| 113 assert(type_0 == DEITA_COLUMN_TYPE_INTEGER); | |
| 114 assert(type_1 == DEITA_COLUMN_TYPE_TEXT); | |
| 115 | |
| 116 Deita_Result_Set_Free(p_result_set); | |
| 117 printf(" PASSED: Column metadata correct\n\n"); | |
| 118 | |
| 119 // Cleanup | |
| 120 printf("Cleanup: Closing connection and freeing arena...\n"); | |
| 121 Dowa_Arena_Free(p_arena); | |
| 122 Deita_Connection_Close(p_connection); | |
| 123 unlink(test_db_path); | |
| 124 | |
| 125 printf("\n=== All tests passed! ===\n"); | |
| 126 return 0; | |
| 127 } |