< Summary - Backend C Tests - Coverage Report

Information
Class: test_machine_service_real_c
Assembly: src.backend.tests.unit.database
File(s): C:\Users\yagiz\Desktop\Project\smart-maintenance-suite\src\backend\tests\unit\database\test_machine_service_real.c
Line coverage
94%
Covered lines: 54
Uncovered lines: 3
Coverable lines: 57
Total lines: 124
Line coverage: 94.7%
Branch coverage
91%
Covered branches: 11
Total branches: 12
Branch coverage: 91.6%
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
LOG_INFO100%000%
LOG_ERROR100%000%
db_pool_acquire100%00100%
db_pool_release100%00100%
PQexec100%00100%
PQresultStatus100%00100%
PQclear100%00100%
PQntuples100%00100%
PQgetvalue90%0085.71%
PQerrorMessage100%00100%
test_get_all_machines_paths100%00100%
test_get_machine_by_id_simple100%00100%
test_get_health_score100%00100%
main100%00100%

File(s)

C:\Users\yagiz\Desktop\Project\smart-maintenance-suite\src\backend\tests\unit\database\test_machine_service_real.c

#LineLine coverage
 1#include <stdarg.h>
 2#include <stddef.h>
 3#include <setjmp.h>
 4#include <cmocka.h>
 5#include <string.h>
 6#include <stdio.h>
 7#include <stdlib.h>
 8
 9// PostgreSQL Definitions
 10#define LIBPQ_FE_H
 11#define PGconn void
 12#define PGresult void
 13#define PGRES_TUPLES_OK 2
 14#define PGRES_FATAL_ERROR 3
 15
 16#include "machine_service.h"
 17#include "db_connection.h"
 18
 19// Logger mock
 020void LOG_INFO(const char *format, ...) {}
 21
 022void LOG_ERROR(const char *format, ...) {}
 23
 24// Mock State
 25static DBConnection mock_conn;
 26static int mock_acquire_success = 1;
 27static int mock_result_status = PGRES_TUPLES_OK;
 28static int mock_ntuples = 1;
 29
 30// Mock DB functions
 431DBConnection *db_pool_acquire(void) {
 432  if (mock_acquire_success) {
 333    mock_conn.pg_conn = (void *)0x1234;
 334    return &mock_conn;
 35  }
 36
 137  return NULL;
 38}
 39
 340void db_pool_release(DBConnection *conn) {}
 41
 342void *PQexec(void *conn, const char *query) {
 343  return (void *)0x5678;
 44}
 45
 346int PQresultStatus(const void *res) {
 347  return mock_result_status;
 48}
 49
 350void PQclear(void *res) {}
 51
 252int PQntuples(const void *res) {
 253  return mock_ntuples;
 54}
 55
 3056char *PQgetvalue(const void *res, int row, int field) {
 3057  if (field == 0) return "1"; // ID
 58
 2459  if (field == 1) return "CNC-01"; // Name
 60
 1861  if (field == 2) return "X100"; // Model
 62
 1263  if (field == 3) return "FloorB"; // Location
 64
 665  if (field == 4) return "running"; // Status
 66
 067  return "";
 68}
 69
 170char *PQerrorMessage(const void *conn) {
 171  return "Machine Mock Error";
 72}
 73
 74/* ====================================================================
 75 * TEST CASES
 76 * ==================================================================== */
 77
 178static void test_get_all_machines_paths(void **state) {
 179  mock_acquire_success = 1;
 180  mock_result_status = PGRES_TUPLES_OK;
 181  mock_ntuples = 1;
 82  Machine items[5];
 183  int count = get_all_machines(items, 5);
 184  assert_int_equal(count, 1);
 185  assert_string_equal(items[0].name, "CNC-01");
 86  // Test truncation
 187  mock_ntuples = 10;
 188  count = get_all_machines(items, 5);
 189  assert_int_equal(count, 5);
 90  // Acquire Fail
 191  mock_acquire_success = 0;
 192  count = get_all_machines(items, 5);
 193  assert_int_equal(count, 0);
 94  // Query Fail
 195  mock_acquire_success = 1;
 196  mock_result_status = PGRES_FATAL_ERROR;
 197  count = get_all_machines(items, 5);
 198  assert_int_equal(count, 0);
 99  // Param fail
 1100  assert_int_equal(get_all_machines(NULL, 5), 0);
 1101  assert_int_equal(get_all_machines(items, 0), 0);
 1102}
 103
 1104static void test_get_machine_by_id_simple(void **state) {
 105  Machine m;
 1106  int res = get_machine_by_id(10, &m);
 1107  assert_int_equal(res, 0);
 1108  assert_int_equal(m.id, 10);
 1109  assert_int_equal(get_machine_by_id(1, NULL), -1);
 1110}
 111
 1112static void test_get_health_score(void **state) {
 1113  double score = get_machine_health_score(1);
 1114  assert_true(score > 0.0);
 1115}
 116
 1117int main(void) {
 1118  const struct CMUnitTest tests[] = {
 119    cmocka_unit_test(test_get_all_machines_paths),
 120    cmocka_unit_test(test_get_machine_by_id_simple),
 121    cmocka_unit_test(test_get_health_score),
 122  };
 1123  return cmocka_run_group_tests(tests, NULL, NULL);
 124}