view deita/deita_test.c @ 97:3bdfffaad162

[MrJuneJune] Updated so it is mobile friendly and fixed few font sizes.
author June Park <parkjune1995@gmail.com>
date Fri, 02 Jan 2026 20:21:58 -0800
parents ae6a88e6e484
children
line wrap: on
line source

#include "deita.h"
#include <stdio.h>
#include <assert.h>
#include <unistd.h>

int main(void)
{
  printf("=== Deita Library Tests ===\n\n");

  const char *test_db_path = "/tmp/deita_test.db";
  unlink(test_db_path);

  // Test 1: Connection creation
  printf("Test 1: Creating SQLite3 connection...\n");
  Deita_Connection *p_connection = Deita_Connection_Create(DEITA_DATABASE_TYPE_SQLITE3, test_db_path);
  assert(p_connection != NULL);
  assert(Deita_Connection_Is_Open(p_connection) == TRUE);
  printf("  PASSED: Connection created successfully\n\n");

  // Test 2: Create table
  printf("Test 2: Creating test table...\n");
  const char *create_table_query =
    "CREATE TABLE IF NOT EXISTS test_users ("
    "  id INTEGER PRIMARY KEY AUTOINCREMENT,"
    "  username TEXT NOT NULL,"
    "  age INTEGER"
    ")";
  int32 result = Deita_Query_Execute_Update(p_connection, create_table_query);
  assert(result == 0);
  printf("  PASSED: Table created\n\n");

  // Test 3: Insert data
  printf("Test 3: Inserting test data...\n");
  const char *insert_query = "INSERT INTO test_users (username, age) VALUES ('alice', 25)";
  result = Deita_Query_Execute_Update(p_connection, insert_query);
  assert(result == 1);
  printf("  PASSED: Inserted 1 row\n\n");

  // Test 4: Insert with prepared statement
  printf("Test 4: Inserting with prepared statement...\n");
  const char *insert_prepared_query = "INSERT INTO test_users (username, age) VALUES (?, ?)";
  const char *params[] = {"bob", "30"};
  result = Deita_Query_Execute_Update_Prepared(p_connection, insert_prepared_query, 2, params);
  assert(result == 1);
  printf("  PASSED: Inserted 1 row with prepared statement\n\n");

  // Test 5: Query data
  printf("Test 5: Querying test data...\n");
  Dowa_Arena *p_arena = Dowa_Arena_Create(ONE_MEGA_BYTE);
  const char *select_query = "SELECT id, username, age FROM test_users ORDER BY id";
  Deita_Result_Set *p_result_set = Deita_Query_Execute(p_connection, select_query, p_arena);
  assert(p_result_set != NULL);

  int32 row_count = 0;
  while (Deita_Result_Set_Next(p_result_set))
  {
    row_count++;
    int64 id = Deita_Result_Set_Get_Integer(p_result_set, 0);
    const char *username = Deita_Result_Set_Get_Text(p_result_set, 1);
    int64 age = Deita_Result_Set_Get_Integer(p_result_set, 2);

    printf("  Row %d: id=%lld, username=%s, age=%lld\n", row_count, id, username, age);

    if (row_count == 1)
    {
      assert(id == 1);
      assert(strcmp(username, "alice") == 0);
      assert(age == 25);
    }
    else if (row_count == 2)
    {
      assert(id == 2);
      assert(strcmp(username, "bob") == 0);
      assert(age == 30);
    }
  }
  assert(row_count == 2);
  Deita_Result_Set_Free(p_result_set);
  printf("  PASSED: Retrieved 2 rows\n\n");

  // Test 6: Query with prepared statement
  printf("Test 6: Querying with prepared statement...\n");
  const char *select_prepared_query = "SELECT username, age FROM test_users WHERE username = ?";
  const char *query_params[] = {"alice"};
  p_result_set = Deita_Query_Execute_Prepared(p_connection, select_prepared_query, 1, query_params, p_arena);
  assert(p_result_set != NULL);

  assert(Deita_Result_Set_Next(p_result_set) == TRUE);
  const char *username = Deita_Result_Set_Get_Text(p_result_set, 0);
  int64 age = Deita_Result_Set_Get_Integer(p_result_set, 1);
  assert(strcmp(username, "alice") == 0);
  assert(age == 25);
  assert(Deita_Result_Set_Next(p_result_set) == FALSE);
  Deita_Result_Set_Free(p_result_set);
  printf("  PASSED: Retrieved correct row with prepared statement\n\n");

  // Test 7: Column metadata
  printf("Test 7: Testing column metadata...\n");
  p_result_set = Deita_Query_Execute(p_connection, "SELECT id, username FROM test_users LIMIT 1", p_arena);
  assert(p_result_set != NULL);

  int32 column_count = Deita_Result_Set_Get_Column_Count(p_result_set);
  assert(column_count == 2);

  const char *col_name_0 = Deita_Result_Set_Get_Column_Name(p_result_set, 0);
  const char *col_name_1 = Deita_Result_Set_Get_Column_Name(p_result_set, 1);
  assert(strcmp(col_name_0, "id") == 0);
  assert(strcmp(col_name_1, "username") == 0);

  assert(Deita_Result_Set_Next(p_result_set) == TRUE);
  Deita_Column_Type type_0 = Deita_Result_Set_Get_Column_Type(p_result_set, 0);
  Deita_Column_Type type_1 = Deita_Result_Set_Get_Column_Type(p_result_set, 1);
  assert(type_0 == DEITA_COLUMN_TYPE_INTEGER);
  assert(type_1 == DEITA_COLUMN_TYPE_TEXT);

  Deita_Result_Set_Free(p_result_set);
  printf("  PASSED: Column metadata correct\n\n");

  // Cleanup
  printf("Cleanup: Closing connection and freeing arena...\n");
  Dowa_Arena_Free(p_arena);
  Deita_Connection_Close(p_connection);
  unlink(test_db_path);

  printf("\n=== All tests passed! ===\n");
  return 0;
}