annotate deita/deita_test.c @ 135:ffb764d2fcc5

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