< Summary - Backend C Tests - Coverage Report

Information
Class: machine_service_c
Assembly: src.backend.database
File(s): C:\Users\yagiz\Desktop\Project\smart-maintenance-suite\src\backend\database\machine_service.c
Line coverage
50%
Covered lines: 46
Uncovered lines: 45
Coverable lines: 91
Total lines: 168
Line coverage: 50.5%
Branch coverage
46%
Covered branches: 12
Total branches: 26
Branch coverage: 46.1%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Coverage history

Coverage history 0 25 50 75 100

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
get_all_machines71.43%0072.09%
get_machine_by_id33.33%0052%
get_machine_health_score0%0033.33%
add_machine0%000%

File(s)

C:\Users\yagiz\Desktop\Project\smart-maintenance-suite\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
 084bool add_machine(const char *name, const char *model, const char *location, const char *status) {
 085  DBConnection *conn_wrapper = db_pool_acquire();
 86
 087  if (!conn_wrapper) {
 088    printf("[DATABASE ERROR] Could not acquire connection to add machine!\n");
 089    return false;
 90  }
 91
 92  char query[1024];
 093  snprintf(query, sizeof(query),
 94           "INSERT INTO machines (name, model, location, status) VALUES ('%s', '%s', '%s', '%s');",
 95           name, model, location, status);
 096  printf("[DATABASE DEBUG] Executing: %s\n", query);
 097  PGresult *res = PQexec(conn_wrapper->pg_conn, query);
 098  bool success = (PQresultStatus(res) == PGRES_COMMAND_OK);
 99
 0100  if (!success) {
 0101    printf("[DATABASE ERROR] INSERT failed: %s\n", PQerrorMessage(conn_wrapper->pg_conn));
 102  } else {
 0103    printf("[DATABASE SUCCESS] Machine '%s' added to DB.\n", name);
 104  }
 105
 0106  PQclear(res);
 0107  db_pool_release(conn_wrapper);
 0108  return success;
 109}
 110
 111#endif  // TEST_MODE
 112
 113
 114
 115#ifdef TEST_MODE
 116
 117// =====================================
 118// MOCK IMPLEMENTATION (COVERAGE BOOST)
 119// =====================================
 120
 0121int get_all_machines(Machine *machines, int max_count) {
 0122  if (!machines)
 0123    return 0;
 124
 0125  if (max_count <= 0)
 0126    return 0;
 127
 0128  machines[0].id = 1;
 0129  strcpy(machines[0].name, "CNC-01");
 0130  strcpy(machines[0].model, "X100");
 0131  strcpy(machines[0].location, "Line A");
 0132  strcpy(machines[0].status, "active");
 0133  machines[0].health_score = 88.5;
 0134  return 1;
 135}
 136
 0137int get_machine_by_id(int id, Machine *machine) {
 0138  if (!machine)
 0139    return 0;
 140
 0141  if (id <= 0)
 0142    return 0;
 143
 0144  machine->id = id;
 0145  strcpy(machine->name, "Mock Machine");
 0146  strcpy(machine->model, "Mock Model");
 0147  strcpy(machine->location, "Test Lab");
 0148  strcpy(machine->status, "active");
 0149  machine->health_score = 90.0;
 0150  return 1;
 151}
 152
 0153double get_machine_health_score(int id) {
 0154  if (id <= 0)
 0155    return 0.0;
 156
 0157  return 88.5;
 158}
 159
 0160bool add_machine(const char *name, const char *model, const char *location, const char *status) {
 161  (void)name;
 162  (void)model;
 163  (void)location;
 164  (void)status;
 0165  return true;
 166}
 167
 168#endif