< Summary - Backend C Tests - Coverage Report (WSL)

Information
Class: test_machine_standalone_c
Assembly: src.backend.tests.unit
File(s): ./src/backend/tests/unit/test_machine_standalone.c
Line coverage
100%
Covered lines: 197
Uncovered lines: 0
Coverable lines: 197
Total lines: 332
Line coverage: 100%
Branch coverage
68%
Covered branches: 37
Total branches: 54
Branch coverage: 68.5%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Coverage history

Coverage history 0 25 50 75 100 2/18/2026 - 10:50:55 PM Line coverage: 100% (197/197) Branch coverage: 68.5% (37/54) Total lines: 332 2/18/2026 - 10:50:55 PM Line coverage: 100% (197/197) Branch coverage: 68.5% (37/54) Total lines: 332

File(s)

./src/backend/tests/unit/test_machine_standalone.c

#LineLine coverage
 1/*
 2 * STANDALONE MACHINE SERVICE TEST FILE
 3 *
 4 * Compile ALONE:
 5 *   gcc -o test_machine.exe test_machine_standalone.c -std=c99
 6 */
 7
 8#include <stdio.h>
 9#include <stdlib.h>
 10#include <string.h>
 11#include <assert.h>
 12#include <stdbool.h>
 13
 14// ============================================================================
 15// TYPE DEFINITIONS
 16// ============================================================================
 17
 18typedef struct {
 19  void *pg_conn;
 20} DBConnection;
 21
 22typedef struct {
 23  int id;
 24  char name[100];
 25  char model[50];
 26  char location[100];
 27  char status[20];
 28  double health_score;
 29} Machine;
 30
 31typedef struct {
 32  int status;
 33  int ntuples;
 34  char data[100][10][512];
 35} PGresult;
 36
 37// ============================================================================
 38// MOCK STATE
 39// ============================================================================
 40
 41static DBConnection mock_connection = {.pg_conn = (void *)0x1234};
 42static bool mock_connection_available = true;
 43static bool mock_query_success = true;
 44static int mock_row_count = 0;
 45static PGresult mock_result;
 46
 47#define PGRES_COMMAND_OK 1
 48#define PGRES_TUPLES_OK 2
 49
 50// ============================================================================
 51// MOCK FUNCTIONS
 52// ============================================================================
 53
 554DBConnection *db_pool_acquire(void) {
 555  return mock_connection_available ? &mock_connection : NULL;
 56}
 57
 458void db_pool_release(DBConnection *conn) {
 59  (void)conn;
 460}
 61
 462PGresult *PQexec(void *conn, const char *query) {
 63  (void)conn;
 64
 465  if (!mock_query_success) {
 166    mock_result.status = -1;
 167    return &mock_result;
 68  }
 69
 370  if (strstr(query, "SELECT") && strstr(query, "machines")) {
 371    mock_result.status = PGRES_TUPLES_OK;
 372    mock_result.ntuples = mock_row_count;
 73
 10774    for (int i = 0; i < mock_row_count && i < 100; i++) {
 10475      snprintf(mock_result.data[i][0], 512, "%d", i + 1);
 10476      snprintf(mock_result.data[i][1], 512, "Machine-%d", i + 1);
 10477      snprintf(mock_result.data[i][2], 512, "Model-X%d", i + 1);
 10478      snprintf(mock_result.data[i][3], 512, "Location-%d", i + 1);
 10479      snprintf(mock_result.data[i][4], 512, i % 2 == 0 ? "active" : "idle");
 80    }
 81  }
 82
 383  return &mock_result;
 84}
 85
 486int PQresultStatus(PGresult *res) {
 487  return res->status;
 88}
 89
 390int PQntuples(PGresult *res) {
 391  return res->ntuples;
 92}
 93
 7094char *PQgetvalue(PGresult *res, int row, int col) {
 7095  return mock_result.data[row][col];
 96}
 97
 498void PQclear(PGresult *res) {
 99  (void)res;
 4100}
 101
 1102char *PQerrorMessage(void *conn) {
 103  (void)conn;
 1104  return "Mock error";
 105}
 106
 107#define LOG_ERROR(fmt, ...) printf("[ERROR] " fmt "\n", ##__VA_ARGS__)
 108#define LOG_INFO(fmt, ...) printf("[INFO] " fmt "\n", ##__VA_ARGS__)
 109
 110// ============================================================================
 111// MACHINE SERVICE IMPLEMENTATION
 112// ============================================================================
 113
 15114double get_machine_health_score(int machine_id) {
 115  (void)machine_id;
 15116  return 94.7;
 117}
 118
 7119int get_all_machines(Machine *machines, int max_count) {
 7120  if (!machines || max_count <= 0)
 2121    return 0;
 122
 5123  DBConnection *conn_wrapper = db_pool_acquire();
 124
 5125  if (!conn_wrapper) {
 1126    LOG_ERROR("Could not acquire connection to fetch machines.");
 1127    return 0;
 128  }
 129
 4130  PGresult *res = PQexec(conn_wrapper->pg_conn,
 131                         "SELECT id, name, model, location, status FROM machines");
 132
 4133  if (PQresultStatus(res) != PGRES_TUPLES_OK) {
 1134    LOG_ERROR("SELECT machines failed: %s", PQerrorMessage(conn_wrapper->pg_conn));
 1135    PQclear(res);
 1136    db_pool_release(conn_wrapper);
 1137    return 0;
 138  }
 139
 3140  int rows = PQntuples(res);
 3141  int count = (rows < max_count) ? rows : max_count;
 142
 17143  for (int i = 0; i < count; i++) {
 14144    machines[i].id = atoi(PQgetvalue(res, i, 0));
 14145    strncpy(machines[i].name, PQgetvalue(res, i, 1), 99);
 14146    machines[i].name[99] = '\0';
 14147    strncpy(machines[i].model, PQgetvalue(res, i, 2), 49);
 14148    machines[i].model[49] = '\0';
 14149    strncpy(machines[i].location, PQgetvalue(res, i, 3), 99);
 14150    machines[i].location[99] = '\0';
 14151    strncpy(machines[i].status, PQgetvalue(res, i, 4), 19);
 14152    machines[i].status[19] = '\0';
 14153    machines[i].health_score = get_machine_health_score(machines[i].id);
 154  }
 155
 3156  PQclear(res);
 3157  db_pool_release(conn_wrapper);
 3158  LOG_INFO("Successfully fetched %d machines from DB.", count);
 3159  return count;
 160}
 161
 2162int get_machine_by_id(int id, Machine *machine) {
 2163  if (!machine)
 1164    return -1;
 165
 1166  machine->id = id;
 1167  snprintf(machine->name, sizeof(machine->name), "Machine %d", id);
 1168  strncpy(machine->model, "Standard Model", sizeof(machine->model) - 1);
 1169  machine->model[sizeof(machine->model) - 1] = '\0';
 1170  strncpy(machine->location, "Production Line", sizeof(machine->location) - 1);
 1171  machine->location[sizeof(machine->location) - 1] = '\0';
 1172  strncpy(machine->status, "running", sizeof(machine->status) - 1);
 1173  machine->status[sizeof(machine->status) - 1] = '\0';
 1174  machine->health_score = 90.0;
 1175  return 0;
 176}
 177
 178// ============================================================================
 179// TEST HELPER
 180// ============================================================================
 181
 10182void reset_mocks(void) {
 10183  mock_connection_available = true;
 10184  mock_query_success = true;
 10185  mock_row_count = 0;
 10186  memset(&mock_result, 0, sizeof(mock_result));
 10187}
 188
 189// ============================================================================
 190// TESTS
 191// ============================================================================
 192
 1193void test_get_all_machines_null_pointer(void) {
 1194  printf("\n[TEST] test_get_all_machines_null_pointer\n");
 1195  reset_mocks();
 1196  int count = get_all_machines(NULL, 5);
 1197  assert(count == 0);
 1198  printf("✓ PASSED\n");
 1199}
 200
 1201void test_get_all_machines_zero_max(void) {
 1202  printf("\n[TEST] test_get_all_machines_zero_max\n");
 1203  reset_mocks();
 204  Machine machines[5];
 1205  int count = get_all_machines(machines, 0);
 1206  assert(count == 0);
 1207  printf("✓ PASSED\n");
 1208}
 209
 1210void test_get_all_machines_no_connection(void) {
 1211  printf("\n[TEST] test_get_all_machines_no_connection\n");
 1212  reset_mocks();
 1213  mock_connection_available = false;
 214  Machine machines[5];
 1215  int count = get_all_machines(machines, 5);
 1216  assert(count == 0);
 1217  printf("✓ PASSED\n");
 1218}
 219
 1220void test_get_all_machines_query_failure(void) {
 1221  printf("\n[TEST] test_get_all_machines_query_failure\n");
 1222  reset_mocks();
 1223  mock_query_success = false;
 224  Machine machines[5];
 1225  int count = get_all_machines(machines, 5);
 1226  assert(count == 0);
 1227  printf("✓ PASSED\n");
 1228}
 229
 1230void test_get_all_machines_single_machine(void) {
 1231  printf("\n[TEST] test_get_all_machines_single_machine\n");
 1232  reset_mocks();
 1233  mock_row_count = 1;
 234  Machine machines[5];
 1235  int count = get_all_machines(machines, 5);
 1236  assert(count == 1);
 1237  assert(machines[0].id == 1);
 1238  assert(strcmp(machines[0].name, "Machine-1") == 0);
 1239  printf("✓ PASSED\n");
 1240}
 241
 1242void test_get_all_machines_multiple_machines(void) {
 1243  printf("\n[TEST] test_get_all_machines_multiple_machines\n");
 1244  reset_mocks();
 1245  mock_row_count = 3;
 246  Machine machines[5];
 1247  int count = get_all_machines(machines, 5);
 1248  assert(count == 3);
 1249  printf("✓ PASSED\n");
 1250}
 251
 1252void test_get_all_machines_pagination(void) {
 1253  printf("\n[TEST] test_get_all_machines_pagination\n");
 1254  reset_mocks();
 1255  mock_row_count = 100;
 256  Machine machines[10];
 1257  int count = get_all_machines(machines, 10);
 1258  assert(count == 10);
 1259  printf("✓ PASSED\n");
 1260}
 261
 1262void test_get_machine_by_id_null_pointer(void) {
 1263  printf("\n[TEST] test_get_machine_by_id_null_pointer\n");
 1264  reset_mocks();
 1265  int result = get_machine_by_id(10, NULL);
 1266  assert(result == -1);
 1267  printf("✓ PASSED\n");
 1268}
 269
 1270void test_get_machine_by_id_valid(void) {
 1271  printf("\n[TEST] test_get_machine_by_id_valid\n");
 1272  reset_mocks();
 273  Machine machine;
 1274  int result = get_machine_by_id(123, &machine);
 1275  assert(result == 0);
 1276  assert(machine.id == 123);
 1277  assert(strcmp(machine.name, "Machine 123") == 0);
 1278  printf("✓ PASSED\n");
 1279}
 280
 1281void test_get_machine_health_score(void) {
 1282  printf("\n[TEST] test_get_machine_health_score\n");
 1283  reset_mocks();
 1284  double score = get_machine_health_score(1);
 1285  assert(score == 94.7);
 1286  printf("✓ PASSED\n");
 1287}
 288
 289// ============================================================================
 290// MAIN
 291// ============================================================================
 292
 1293int main(void) {
 1294  printf("========================================\n");
 1295  printf("MACHINE SERVICE TEST (STANDALONE)\n");
 1296  printf("========================================\n");
 1297  int total = 0, passed = 0;
 1298  test_get_all_machines_null_pointer();
 1299  total++;
 1300  passed++;
 1301  test_get_all_machines_zero_max();
 1302  total++;
 1303  passed++;
 1304  test_get_all_machines_no_connection();
 1305  total++;
 1306  passed++;
 1307  test_get_all_machines_query_failure();
 1308  total++;
 1309  passed++;
 1310  test_get_all_machines_single_machine();
 1311  total++;
 1312  passed++;
 1313  test_get_all_machines_multiple_machines();
 1314  total++;
 1315  passed++;
 1316  test_get_all_machines_pagination();
 1317  total++;
 1318  passed++;
 1319  test_get_machine_by_id_null_pointer();
 1320  total++;
 1321  passed++;
 1322  test_get_machine_by_id_valid();
 1323  total++;
 1324  passed++;
 1325  test_get_machine_health_score();
 1326  total++;
 1327  passed++;
 1328  printf("\n========================================\n");
 1329  printf("TEST RESULTS: %d/%d PASSED\n", passed, total);
 1330  printf("========================================\n");
 1331  return (passed == total) ? 0 : 1;
 332}

Methods/Properties