| | | 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_COMMAND_OK 1 |
| | | 14 | | #define PGRES_TUPLES_OK 2 |
| | | 15 | | #define PGRES_FATAL_ERROR 3 |
| | | 16 | | |
| | | 17 | | #include "sensor_service.h" |
| | | 18 | | #include "db_connection.h" |
| | | 19 | | |
| | | 20 | | // Logger mock |
| | 0 | 21 | | void LOG_INFO(const char *format, ...) {} |
| | | 22 | | |
| | 0 | 23 | | void LOG_ERROR(const char *format, ...) {} |
| | | 24 | | |
| | | 25 | | // Mock State |
| | | 26 | | static DBConnection mock_conn; |
| | | 27 | | static int mock_acquire_success = 1; |
| | | 28 | | static int mock_result_status = PGRES_TUPLES_OK; |
| | | 29 | | static int mock_ntuples = 1; |
| | | 30 | | |
| | | 31 | | // Mock DB functions |
| | 6 | 32 | | DBConnection *db_pool_acquire(void) { |
| | 6 | 33 | | if (mock_acquire_success) { |
| | 4 | 34 | | mock_conn.pg_conn = (void *)0x1234; |
| | 4 | 35 | | return &mock_conn; |
| | | 36 | | } |
| | | 37 | | |
| | 2 | 38 | | return NULL; |
| | | 39 | | } |
| | | 40 | | |
| | 4 | 41 | | void db_pool_release(DBConnection *conn) {} |
| | | 42 | | |
| | 4 | 43 | | void *PQexec(void *conn, const char *query) { |
| | 4 | 44 | | return (void *)0x5678; |
| | | 45 | | } |
| | | 46 | | |
| | 4 | 47 | | int PQresultStatus(const void *res) { |
| | 4 | 48 | | return mock_result_status; |
| | | 49 | | } |
| | | 50 | | |
| | 4 | 51 | | void PQclear(void *res) {} |
| | | 52 | | |
| | 1 | 53 | | int PQntuples(const void *res) { |
| | 1 | 54 | | return mock_ntuples; |
| | | 55 | | } |
| | | 56 | | |
| | 5 | 57 | | char *PQgetvalue(const void *res, int row, int field) { |
| | 5 | 58 | | if (field == 0) return "101"; // ID |
| | | 59 | | |
| | 4 | 60 | | if (field == 1) return "Temperature"; // Type |
| | | 61 | | |
| | 3 | 62 | | if (field == 2) return "45.5"; // Value |
| | | 63 | | |
| | 2 | 64 | | if (field == 3) return "C"; // Unit |
| | | 65 | | |
| | 1 | 66 | | if (field == 4) return "2026-02-18 12:00:00"; // Timestamp |
| | | 67 | | |
| | 0 | 68 | | return ""; |
| | | 69 | | } |
| | | 70 | | |
| | 0 | 71 | | char *PQerrorMessage(const void *conn) { |
| | 0 | 72 | | return "Sensor Mock Error"; |
| | | 73 | | } |
| | | 74 | | |
| | | 75 | | // Mock alert engine to avoid linking |
| | 1 | 76 | | void check_and_trigger_alerts(int sensor_id, const char *sensor_type, double value) {} |
| | | 77 | | |
| | | 78 | | /* ==================================================================== |
| | | 79 | | * TEST CASES |
| | | 80 | | * ==================================================================== */ |
| | | 81 | | |
| | 1 | 82 | | static void test_get_sensor_stats_success(void **state) { |
| | 1 | 83 | | mock_acquire_success = 1; |
| | 1 | 84 | | mock_result_status = PGRES_TUPLES_OK; |
| | 1 | 85 | | mock_ntuples = 1; |
| | | 86 | | SensorStatus stats[5]; |
| | 1 | 87 | | int count = get_machine_sensor_stats(1, stats, 5); |
| | 1 | 88 | | assert_int_equal(count, 1); |
| | 1 | 89 | | assert_int_equal(stats[0].sensor_id, 101); |
| | 1 | 90 | | assert_string_equal(stats[0].sensor_type, "Temperature"); |
| | 1 | 91 | | assert_float_equal(stats[0].last_value, 45.5, 0.01); |
| | 1 | 92 | | } |
| | | 93 | | |
| | 1 | 94 | | static void test_get_sensor_stats_fail_paths(void **state) { |
| | | 95 | | SensorStatus stats[5]; |
| | | 96 | | // Acquire fail |
| | 1 | 97 | | mock_acquire_success = 0; |
| | 1 | 98 | | assert_int_equal(get_machine_sensor_stats(1, stats, 5), 0); |
| | | 99 | | // Query fail |
| | 1 | 100 | | mock_acquire_success = 1; |
| | 1 | 101 | | mock_result_status = PGRES_FATAL_ERROR; |
| | 1 | 102 | | assert_int_equal(get_machine_sensor_stats(1, stats, 5), 0); |
| | 1 | 103 | | } |
| | | 104 | | |
| | 1 | 105 | | static void test_add_sensor_reading_success(void **state) { |
| | 1 | 106 | | mock_acquire_success = 1; |
| | 1 | 107 | | mock_result_status = PGRES_COMMAND_OK; |
| | 1 | 108 | | bool res = add_sensor_reading(1, "Temperature", 42.0); |
| | 1 | 109 | | assert_true(res); |
| | 1 | 110 | | } |
| | | 111 | | |
| | 1 | 112 | | static void test_add_sensor_reading_fail(void **state) { |
| | | 113 | | // Acquire fail |
| | 1 | 114 | | mock_acquire_success = 0; |
| | 1 | 115 | | assert_false(add_sensor_reading(1, "Temperature", 42.0)); |
| | | 116 | | // Command fail |
| | 1 | 117 | | mock_acquire_success = 1; |
| | 1 | 118 | | mock_result_status = PGRES_FATAL_ERROR; |
| | 1 | 119 | | assert_false(add_sensor_reading(1, "Temperature", 42.0)); |
| | 1 | 120 | | } |
| | | 121 | | |
| | 1 | 122 | | int main(void) { |
| | 1 | 123 | | const struct CMUnitTest tests[] = { |
| | | 124 | | cmocka_unit_test(test_get_sensor_stats_success), |
| | | 125 | | cmocka_unit_test(test_get_sensor_stats_fail_paths), |
| | | 126 | | cmocka_unit_test(test_add_sensor_reading_success), |
| | | 127 | | cmocka_unit_test(test_add_sensor_reading_fail), |
| | | 128 | | }; |
| | 1 | 129 | | return cmocka_run_group_tests(tests, NULL, NULL); |
| | | 130 | | } |