| | | 1 | | #define _GNU_SOURCE |
| | | 2 | | #include <stdio.h> |
| | | 3 | | #include <string.h> |
| | | 4 | | #include <assert.h> |
| | | 5 | | #include <stdlib.h> |
| | | 6 | | |
| | | 7 | | #include <libpq-fe.h> // GERÇEK libpq header |
| | | 8 | | #include "sensor_service.h" // Senin servis |
| | | 9 | | |
| | | 10 | | /* ========================================================== |
| | | 11 | | GLOBAL MOCK STATE |
| | | 12 | | ========================================================== */ |
| | | 13 | | |
| | | 14 | | static int mock_db_available; |
| | | 15 | | static int mock_query_success; |
| | | 16 | | static int mock_rows; |
| | | 17 | | static int mock_alert_called; |
| | | 18 | | static int last_query_type; /* 1=SELECT, 2=INSERT */ |
| | | 19 | | |
| | | 20 | | /* ========================================================== |
| | | 21 | | RESET |
| | | 22 | | ========================================================== */ |
| | | 23 | | |
| | 8 | 24 | | void reset_mocks() { |
| | 8 | 25 | | mock_db_available = 1; |
| | 8 | 26 | | mock_query_success = 1; |
| | 8 | 27 | | mock_rows = 1; |
| | 8 | 28 | | mock_alert_called = 0; |
| | 8 | 29 | | last_query_type = 0; |
| | 8 | 30 | | } |
| | | 31 | | |
| | | 32 | | /* ========================================================== |
| | | 33 | | FAKE DB CONNECTION |
| | | 34 | | ========================================================== */ |
| | | 35 | | |
| | | 36 | | typedef struct { |
| | | 37 | | PGconn *pg_conn; |
| | | 38 | | } DBConnection; |
| | | 39 | | |
| | | 40 | | static DBConnection fake_conn; |
| | | 41 | | static PGresult *fake_res = (PGresult *)0x1; |
| | | 42 | | /* ========================================================== |
| | | 43 | | MOCK DB POOL |
| | | 44 | | ========================================================== */ |
| | | 45 | | |
| | 8 | 46 | | DBConnection *db_pool_acquire() { |
| | 8 | 47 | | if (!mock_db_available) |
| | 2 | 48 | | return NULL; |
| | | 49 | | |
| | 6 | 50 | | return &fake_conn; |
| | | 51 | | } |
| | | 52 | | |
| | 6 | 53 | | void db_pool_release(DBConnection *conn) { |
| | | 54 | | (void)conn; |
| | 6 | 55 | | } |
| | | 56 | | |
| | | 57 | | /* ========================================================== |
| | | 58 | | MOCK LIBPQ FUNCTIONS |
| | | 59 | | ========================================================== */ |
| | | 60 | | |
| | | 61 | | PGresult *PQexec(PGconn *conn, const char *query) { |
| | | 62 | | (void)conn; |
| | | 63 | | |
| | 6 | 64 | | if (strstr(query, "SELECT")) |
| | 4 | 65 | | last_query_type = 1; |
| | 2 | 66 | | else if (strstr(query, "INSERT")) |
| | 2 | 67 | | last_query_type = 2; |
| | | 68 | | else |
| | 0 | 69 | | last_query_type = 0; |
| | | 70 | | |
| | 6 | 71 | | return fake_res; |
| | | 72 | | } |
| | | 73 | | |
| | | 74 | | ExecStatusType PQresultStatus(const PGresult *res) { |
| | | 75 | | (void)res; |
| | | 76 | | |
| | 6 | 77 | | if (!mock_query_success) |
| | 2 | 78 | | return PGRES_FATAL_ERROR; |
| | | 79 | | |
| | 4 | 80 | | if (last_query_type == 1) |
| | 3 | 81 | | return PGRES_TUPLES_OK; |
| | | 82 | | |
| | 1 | 83 | | if (last_query_type == 2) |
| | 1 | 84 | | return PGRES_COMMAND_OK; |
| | | 85 | | |
| | 0 | 86 | | return PGRES_FATAL_ERROR; |
| | | 87 | | } |
| | | 88 | | |
| | | 89 | | int PQntuples(const PGresult *res) { |
| | | 90 | | (void)res; |
| | 3 | 91 | | return mock_rows; |
| | | 92 | | } |
| | | 93 | | |
| | | 94 | | char *PQgetvalue(const PGresult *res, int row, int col) { |
| | | 95 | | (void)res; |
| | | 96 | | (void)row; |
| | | 97 | | static char buffer[64]; |
| | | 98 | | |
| | 15 | 99 | | switch (col) { |
| | 3 | 100 | | case 0: |
| | 3 | 101 | | strcpy(buffer, "101"); |
| | 3 | 102 | | break; |
| | | 103 | | |
| | 3 | 104 | | case 1: |
| | 3 | 105 | | strcpy(buffer, "Temperature"); |
| | 3 | 106 | | break; |
| | | 107 | | |
| | 3 | 108 | | case 2: |
| | 3 | 109 | | strcpy(buffer, "42.5"); |
| | 3 | 110 | | break; |
| | | 111 | | |
| | 3 | 112 | | case 3: |
| | 3 | 113 | | strcpy(buffer, "C"); |
| | 3 | 114 | | break; |
| | | 115 | | |
| | 3 | 116 | | case 4: |
| | 3 | 117 | | strcpy(buffer, "2025-01-01 12:00:00"); |
| | 3 | 118 | | break; |
| | | 119 | | |
| | 0 | 120 | | default: |
| | 0 | 121 | | strcpy(buffer, ""); |
| | 0 | 122 | | break; |
| | | 123 | | } |
| | | 124 | | |
| | 15 | 125 | | return buffer; |
| | | 126 | | } |
| | | 127 | | |
| | | 128 | | void PQclear(PGresult *res) { |
| | | 129 | | (void)res; |
| | 6 | 130 | | } |
| | | 131 | | |
| | | 132 | | char *PQerrorMessage(const PGconn *conn) { |
| | | 133 | | (void)conn; |
| | 0 | 134 | | return "Mock error"; |
| | | 135 | | } |
| | | 136 | | |
| | | 137 | | /* ========================================================== |
| | | 138 | | MOCK ALERT |
| | | 139 | | ========================================================== */ |
| | | 140 | | |
| | 1 | 141 | | void check_and_trigger_alerts(int sensor_id, |
| | | 142 | | const char *sensor_type, |
| | | 143 | | double value) { |
| | | 144 | | (void)sensor_id; |
| | | 145 | | (void)sensor_type; |
| | | 146 | | (void)value; |
| | 1 | 147 | | mock_alert_called = 1; |
| | 1 | 148 | | } |
| | | 149 | | |
| | | 150 | | /* ========================================================== |
| | | 151 | | TESTS |
| | | 152 | | ========================================================== */ |
| | | 153 | | |
| | 1 | 154 | | void test_connection_failure() { |
| | 1 | 155 | | reset_mocks(); |
| | 1 | 156 | | mock_db_available = 0; |
| | | 157 | | SensorStatus stats[5]; |
| | 1 | 158 | | int result = get_machine_sensor_stats(1, stats, 5); |
| | 1 | 159 | | assert(result == 0); |
| | 1 | 160 | | printf("✔ test_connection_failure\n"); |
| | 1 | 161 | | } |
| | | 162 | | |
| | 1 | 163 | | void test_query_failure() { |
| | 1 | 164 | | reset_mocks(); |
| | 1 | 165 | | mock_query_success = 0; |
| | | 166 | | SensorStatus stats[5]; |
| | 1 | 167 | | int result = get_machine_sensor_stats(1, stats, 5); |
| | 1 | 168 | | assert(result == 0); |
| | 1 | 169 | | printf("✔ test_query_failure\n"); |
| | 1 | 170 | | } |
| | | 171 | | |
| | 1 | 172 | | void test_zero_rows() { |
| | 1 | 173 | | reset_mocks(); |
| | 1 | 174 | | mock_rows = 0; |
| | | 175 | | SensorStatus stats[5]; |
| | 1 | 176 | | int result = get_machine_sensor_stats(1, stats, 5); |
| | 1 | 177 | | assert(result == 0); |
| | 1 | 178 | | printf("✔ test_zero_rows\n"); |
| | 1 | 179 | | } |
| | | 180 | | |
| | 1 | 181 | | void test_successful_fetch() { |
| | 1 | 182 | | reset_mocks(); |
| | 1 | 183 | | mock_rows = 1; |
| | | 184 | | SensorStatus stats[5]; |
| | 1 | 185 | | int result = get_machine_sensor_stats(1, stats, 5); |
| | 1 | 186 | | assert(result == 1); |
| | 1 | 187 | | assert(stats[0].sensor_id == 101); |
| | 1 | 188 | | assert(strcmp(stats[0].sensor_type, "Temperature") == 0); |
| | 1 | 189 | | printf("✔ test_successful_fetch\n"); |
| | 1 | 190 | | } |
| | | 191 | | |
| | 1 | 192 | | void test_truncate_rows() { |
| | 1 | 193 | | reset_mocks(); |
| | 1 | 194 | | mock_rows = 10; |
| | | 195 | | SensorStatus stats[2]; |
| | 1 | 196 | | int result = get_machine_sensor_stats(1, stats, 2); |
| | 1 | 197 | | assert(result == 2); |
| | 1 | 198 | | printf("✔ test_truncate_rows\n"); |
| | 1 | 199 | | } |
| | | 200 | | |
| | 1 | 201 | | void test_insert_connection_fail() { |
| | 1 | 202 | | reset_mocks(); |
| | 1 | 203 | | mock_db_available = 0; |
| | 1 | 204 | | int result = add_sensor_reading(1, "Temperature", 50.0); |
| | 1 | 205 | | assert(result == 0); |
| | 1 | 206 | | printf("✔ test_insert_connection_fail\n"); |
| | 1 | 207 | | } |
| | | 208 | | |
| | 1 | 209 | | void test_insert_query_fail() { |
| | 1 | 210 | | reset_mocks(); |
| | 1 | 211 | | mock_query_success = 0; |
| | 1 | 212 | | int result = add_sensor_reading(1, "Temperature", 50.0); |
| | 1 | 213 | | assert(result == 0); |
| | 1 | 214 | | printf("✔ test_insert_query_fail\n"); |
| | 1 | 215 | | } |
| | | 216 | | |
| | 1 | 217 | | void test_insert_success_alert_called() { |
| | 1 | 218 | | reset_mocks(); |
| | 1 | 219 | | int result = add_sensor_reading(1, "Temperature", 50.0); |
| | 1 | 220 | | assert(result == 1); |
| | 1 | 221 | | assert(mock_alert_called == 1); |
| | 1 | 222 | | printf("✔ test_insert_success_alert_called\n"); |
| | 1 | 223 | | } |
| | | 224 | | |
| | | 225 | | /* ========================================================== |
| | | 226 | | MAIN |
| | | 227 | | ========================================================== */ |
| | | 228 | | |
| | 1 | 229 | | int main() { |
| | 1 | 230 | | test_connection_failure(); |
| | 1 | 231 | | test_query_failure(); |
| | 1 | 232 | | test_zero_rows(); |
| | 1 | 233 | | test_successful_fetch(); |
| | 1 | 234 | | test_truncate_rows(); |
| | 1 | 235 | | test_insert_connection_fail(); |
| | 1 | 236 | | test_insert_query_fail(); |
| | 1 | 237 | | test_insert_success_alert_called(); |
| | 1 | 238 | | printf("\nAll sensor_service tests passed.\n"); |
| | 1 | 239 | | return 0; |
| | | 240 | | } |