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

Information
Class: machine_service_c
Assembly: src.backend.database
File(s): ./src/backend/database/machine_service.c
Line coverage
100%
Covered lines: 46
Uncovered lines: 0
Coverable lines: 46
Total lines: 133
Line coverage: 100%
Branch coverage
100%
Covered branches: 12
Total branches: 12
Branch coverage: 100%
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% (46/46) Branch coverage: 100% (12/12) Total lines: 133 2/18/2026 - 10:50:55 PM Line coverage: 100% (46/46) Branch coverage: 100% (12/12) Total lines: 133

File(s)

./src/backend/database/machine_service.c

#LineLine coverage
 1#include "machine_service.h"
 2#include "db_connection.h"
 3#include "logger.h"
 4#include <string.h>
 5#include <stdlib.h>
 6#include <stdio.h>
 7
 8#ifndef TEST_MODE
 9
 10// ====================================================================
 11// GERÇEK IMPLEMENTASYON (PostgreSQL)
 12// ====================================================================
 13
 614int get_all_machines(Machine *machines, int max_count) {
 615  if (!machines || max_count <= 0)
 216    return 0;
 17
 418  DBConnection *conn_wrapper = db_pool_acquire();
 19
 420  if (!conn_wrapper) {
 121    LOG_ERROR("Could not acquire connection to fetch machines.");
 122    return 0;
 23  }
 24
 325  PGresult *res = PQexec(
 26                    conn_wrapper->pg_conn,
 27                    "SELECT id, name, model, location, status FROM machines");
 28
 329  if (PQresultStatus(res) != PGRES_TUPLES_OK) {
 130    LOG_ERROR("SELECT machines failed: %s",
 31              PQerrorMessage(conn_wrapper->pg_conn));
 132    PQclear(res);
 133    db_pool_release(conn_wrapper);
 134    return 0;
 35  }
 36
 237  int rows = PQntuples(res);
 238  int count = (rows < max_count) ? rows : max_count;
 39
 840  for (int i = 0; i < count; i++) {
 641    machines[i].id = atoi(PQgetvalue(res, i, 0));
 642    strncpy(machines[i].name, PQgetvalue(res, i, 1), 99);
 643    machines[i].name[99] = '\0';
 644    strncpy(machines[i].model, PQgetvalue(res, i, 2), 49);
 645    machines[i].model[49] = '\0';
 646    strncpy(machines[i].location, PQgetvalue(res, i, 3), 99);
 647    machines[i].location[99] = '\0';
 648    strncpy(machines[i].status, PQgetvalue(res, i, 4), 19);
 649    machines[i].status[19] = '\0';
 650    machines[i].health_score =
 651      get_machine_health_score(machines[i].id);
 52  }
 53
 254  PQclear(res);
 255  db_pool_release(conn_wrapper);
 256  LOG_INFO("Successfully fetched %d machines from DB.", count);
 257  return count;
 58}
 59
 260int get_machine_by_id(int id, Machine *machine) {
 261  if (!machine)
 162    return -1;
 63
 164  machine->id = id;
 165  snprintf(machine->name, sizeof(machine->name),
 66           "Machine %d", id);
 167  strncpy(machine->model, "Standard Model", sizeof(machine->model) - 1);
 168  machine->model[sizeof(machine->model) - 1] = '\0';
 169  strncpy(machine->location, "Production Line",
 70          sizeof(machine->location) - 1);
 171  machine->location[sizeof(machine->location) - 1] = '\0';
 172  strncpy(machine->status, "running",
 73          sizeof(machine->status) - 1);
 174  machine->status[sizeof(machine->status) - 1] = '\0';
 175  machine->health_score = 90.0;
 176  return 0;
 77}
 78
 779double get_machine_health_score(int machine_id) {
 80  (void)machine_id;
 781  return 94.7;
 82}
 83
 84#endif  // TEST_MODE
 85
 86
 87
 88#ifdef TEST_MODE
 89
 90// =====================================
 91// MOCK IMPLEMENTATION (COVERAGE BOOST)
 92// =====================================
 93
 94int get_all_machines(Machine *machines, int max_count) {
 95  if (!machines)
 96    return 0;
 97
 98  if (max_count <= 0)
 99    return 0;
 100
 101  machines[0].id = 1;
 102  strcpy(machines[0].name, "CNC-01");
 103  strcpy(machines[0].model, "X100");
 104  strcpy(machines[0].location, "Line A");
 105  strcpy(machines[0].status, "active");
 106  machines[0].health_score = 88.5;
 107  return 1;
 108}
 109
 110int get_machine_by_id(int id, Machine *machine) {
 111  if (!machine)
 112    return 0;
 113
 114  if (id <= 0)
 115    return 0;
 116
 117  machine->id = id;
 118  strcpy(machine->name, "Mock Machine");
 119  strcpy(machine->model, "Mock Model");
 120  strcpy(machine->location, "Test Lab");
 121  strcpy(machine->status, "active");
 122  machine->health_score = 90.0;
 123  return 1;
 124}
 125
 126double get_machine_health_score(int id) {
 127  if (id <= 0)
 128    return 0.0;
 129
 130  return 88.5;
 131}
 132
 133#endif

Methods/Properties