| | | 1 | | /* |
| | | 2 | | * STANDALONE MAINTENANCE SERVICE TEST FILE |
| | | 3 | | * |
| | | 4 | | * Compile ALONE: |
| | | 5 | | * gcc -o test_maintenance.exe test_maintenance_standalone.c -std=c99 |
| | | 6 | | */ |
| | | 7 | | |
| | | 8 | | #include <stdio.h> |
| | | 9 | | #include <stdlib.h> |
| | | 10 | | #include <string.h> |
| | | 11 | | #include <assert.h> |
| | | 12 | | #include <stdbool.h> |
| | | 13 | | |
| | | 14 | | // ============================================================================ |
| | | 15 | | // TYPE DEFINITIONS |
| | | 16 | | // ============================================================================ |
| | | 17 | | |
| | | 18 | | typedef struct { |
| | | 19 | | void *pg_conn; |
| | | 20 | | } DBConnection; |
| | | 21 | | |
| | | 22 | | typedef struct { |
| | | 23 | | int id; |
| | | 24 | | int machine_id; |
| | | 25 | | char technician_name[100]; |
| | | 26 | | char log_date[32]; |
| | | 27 | | char description[512]; |
| | | 28 | | double cost; |
| | | 29 | | } MaintenanceLog; |
| | | 30 | | |
| | | 31 | | typedef struct { |
| | | 32 | | int status; |
| | | 33 | | int ntuples; |
| | | 34 | | char data[100][10][512]; |
| | | 35 | | } PGresult; |
| | | 36 | | |
| | | 37 | | // ============================================================================ |
| | | 38 | | // MOCK STATE |
| | | 39 | | // ============================================================================ |
| | | 40 | | |
| | | 41 | | static DBConnection mock_connection = {.pg_conn = (void *)0x1234}; |
| | | 42 | | static bool mock_connection_available = true; |
| | | 43 | | static bool mock_query_success = true; |
| | | 44 | | static int mock_row_count = 0; |
| | | 45 | | static PGresult mock_result; |
| | | 46 | | |
| | | 47 | | #define PGRES_COMMAND_OK 1 |
| | | 48 | | #define PGRES_TUPLES_OK 2 |
| | | 49 | | |
| | | 50 | | // ============================================================================ |
| | | 51 | | // MOCK FUNCTIONS |
| | | 52 | | // ============================================================================ |
| | | 53 | | |
| | 8 | 54 | | DBConnection *db_pool_acquire(void) { |
| | 8 | 55 | | return mock_connection_available ? &mock_connection : NULL; |
| | | 56 | | } |
| | | 57 | | |
| | 6 | 58 | | void db_pool_release(DBConnection *conn) { |
| | | 59 | | (void)conn; |
| | 6 | 60 | | } |
| | | 61 | | |
| | 6 | 62 | | PGresult *PQexec(void *conn, const char *query) { |
| | | 63 | | (void)conn; |
| | | 64 | | |
| | 6 | 65 | | if (!mock_query_success) { |
| | 1 | 66 | | mock_result.status = -1; |
| | 1 | 67 | | return &mock_result; |
| | | 68 | | } |
| | | 69 | | |
| | 5 | 70 | | if (strstr(query, "INSERT")) { |
| | 1 | 71 | | mock_result.status = PGRES_COMMAND_OK; |
| | 1 | 72 | | mock_result.ntuples = 0; |
| | 4 | 73 | | } else if (strstr(query, "SELECT")) { |
| | 2 | 74 | | mock_result.status = PGRES_TUPLES_OK; |
| | 2 | 75 | | mock_result.ntuples = mock_row_count; |
| | | 76 | | |
| | 6 | 77 | | for (int i = 0; i < mock_row_count && i < 100; i++) { |
| | 4 | 78 | | snprintf(mock_result.data[i][0], 512, "%d", i + 1); |
| | 4 | 79 | | snprintf(mock_result.data[i][1], 512, "%d", 101 + i); |
| | 4 | 80 | | snprintf(mock_result.data[i][2], 512, "John"); |
| | 4 | 81 | | snprintf(mock_result.data[i][3], 512, "2025-02-15"); |
| | 4 | 82 | | snprintf(mock_result.data[i][4], 512, "Routine maintenance"); |
| | 4 | 83 | | snprintf(mock_result.data[i][5], 512, "250.50"); |
| | | 84 | | } |
| | 2 | 85 | | } else if (strstr(query, "UPDATE") || strstr(query, "DELETE")) { |
| | 2 | 86 | | mock_result.status = PGRES_COMMAND_OK; |
| | 2 | 87 | | mock_result.ntuples = 0; |
| | | 88 | | } |
| | | 89 | | |
| | 5 | 90 | | return &mock_result; |
| | | 91 | | } |
| | | 92 | | |
| | 6 | 93 | | int PQresultStatus(PGresult *res) { |
| | 6 | 94 | | return res->status; |
| | | 95 | | } |
| | | 96 | | |
| | 2 | 97 | | int PQntuples(PGresult *res) { |
| | 2 | 98 | | return res->ntuples; |
| | | 99 | | } |
| | | 100 | | |
| | 24 | 101 | | char *PQgetvalue(PGresult *res, int row, int col) { |
| | 24 | 102 | | return mock_result.data[row][col]; |
| | | 103 | | } |
| | | 104 | | |
| | 6 | 105 | | void PQclear(PGresult *res) { |
| | | 106 | | (void)res; |
| | 6 | 107 | | } |
| | | 108 | | |
| | 1 | 109 | | char *PQerrorMessage(void *conn) { |
| | | 110 | | (void)conn; |
| | 1 | 111 | | return "Mock error"; |
| | | 112 | | } |
| | | 113 | | |
| | | 114 | | #define LOG_ERROR(fmt, ...) printf("[ERROR] " fmt "\n", ##__VA_ARGS__) |
| | | 115 | | #define LOG_INFO(fmt, ...) printf("[INFO] " fmt "\n", ##__VA_ARGS__) |
| | | 116 | | |
| | | 117 | | // ============================================================================ |
| | | 118 | | // MAINTENANCE SERVICE IMPLEMENTATION |
| | | 119 | | // ============================================================================ |
| | | 120 | | |
| | 3 | 121 | | bool add_maintenance_log(int machine_id, const char *technician, const char *description, double cost) { |
| | 3 | 122 | | DBConnection *conn_wrapper = db_pool_acquire(); |
| | | 123 | | |
| | 3 | 124 | | if (!conn_wrapper) { |
| | 1 | 125 | | LOG_ERROR("Could not acquire connection to add maintenance log."); |
| | 1 | 126 | | return false; |
| | | 127 | | } |
| | | 128 | | |
| | | 129 | | char query[1024]; |
| | 2 | 130 | | snprintf(query, sizeof(query), |
| | | 131 | | "INSERT INTO maintenance_logs (machine_id, performer, description, cost) " |
| | | 132 | | "VALUES (%d, '%s', '%s', %.2f);", |
| | | 133 | | machine_id, technician, description, cost); |
| | 2 | 134 | | PGresult *res = PQexec(conn_wrapper->pg_conn, query); |
| | | 135 | | |
| | 2 | 136 | | if (PQresultStatus(res) != PGRES_COMMAND_OK) { |
| | 1 | 137 | | LOG_ERROR("Failed to insert maintenance log: %s", PQerrorMessage(conn_wrapper->pg_conn)); |
| | 1 | 138 | | PQclear(res); |
| | 1 | 139 | | db_pool_release(conn_wrapper); |
| | 1 | 140 | | return false; |
| | | 141 | | } |
| | | 142 | | |
| | 1 | 143 | | PQclear(res); |
| | 1 | 144 | | db_pool_release(conn_wrapper); |
| | 1 | 145 | | LOG_INFO("Maintenance log added for Machine %d.", machine_id); |
| | 1 | 146 | | return true; |
| | | 147 | | } |
| | | 148 | | |
| | 2 | 149 | | int get_maintenance_history(int machine_id, MaintenanceLog *out_logs, int max_logs) { |
| | 2 | 150 | | DBConnection *conn_wrapper = db_pool_acquire(); |
| | | 151 | | |
| | 2 | 152 | | if (!conn_wrapper) { |
| | 1 | 153 | | LOG_ERROR("Could not acquire connection to fetch maintenance history."); |
| | 1 | 154 | | return 0; |
| | | 155 | | } |
| | | 156 | | |
| | | 157 | | char query[256]; |
| | 1 | 158 | | snprintf(query, sizeof(query), |
| | | 159 | | "SELECT id, machine_id, performer, to_char(ts, 'YYYY-MM-DD'), description, cost " |
| | | 160 | | "FROM maintenance_logs WHERE machine_id = %d ORDER BY ts DESC;", |
| | | 161 | | machine_id); |
| | 1 | 162 | | PGresult *res = PQexec(conn_wrapper->pg_conn, query); |
| | | 163 | | |
| | 1 | 164 | | if (PQresultStatus(res) != PGRES_TUPLES_OK) { |
| | 0 | 165 | | LOG_ERROR("Failed to fetch maintenance history: %s", PQerrorMessage(conn_wrapper->pg_conn)); |
| | 0 | 166 | | PQclear(res); |
| | 0 | 167 | | db_pool_release(conn_wrapper); |
| | 0 | 168 | | return 0; |
| | | 169 | | } |
| | | 170 | | |
| | 1 | 171 | | int rows = PQntuples(res); |
| | 1 | 172 | | int count = (rows < max_logs) ? rows : max_logs; |
| | | 173 | | |
| | 2 | 174 | | for (int i = 0; i < count; i++) { |
| | 1 | 175 | | out_logs[i].id = atoi(PQgetvalue(res, i, 0)); |
| | 1 | 176 | | out_logs[i].machine_id = atoi(PQgetvalue(res, i, 1)); |
| | 1 | 177 | | strncpy(out_logs[i].technician_name, PQgetvalue(res, i, 2), 99); |
| | 1 | 178 | | out_logs[i].technician_name[99] = '\0'; |
| | 1 | 179 | | strncpy(out_logs[i].log_date, PQgetvalue(res, i, 3), 31); |
| | 1 | 180 | | out_logs[i].log_date[31] = '\0'; |
| | 1 | 181 | | strncpy(out_logs[i].description, PQgetvalue(res, i, 4), 511); |
| | 1 | 182 | | out_logs[i].description[511] = '\0'; |
| | 1 | 183 | | out_logs[i].cost = atof(PQgetvalue(res, i, 5)); |
| | | 184 | | } |
| | | 185 | | |
| | 1 | 186 | | PQclear(res); |
| | 1 | 187 | | db_pool_release(conn_wrapper); |
| | 1 | 188 | | LOG_INFO("Fetched %d maintenance logs for Machine %d.", count, machine_id); |
| | 1 | 189 | | return count; |
| | | 190 | | } |
| | | 191 | | |
| | 1 | 192 | | int get_all_maintenance_logs(MaintenanceLog *out_logs, int max_logs) { |
| | 1 | 193 | | DBConnection *conn_wrapper = db_pool_acquire(); |
| | | 194 | | |
| | 1 | 195 | | if (!conn_wrapper) return 0; |
| | | 196 | | |
| | 1 | 197 | | const char *query = "SELECT id, machine_id, performer, to_char(ts, 'YYYY-MM-DD'), description, cost " |
| | | 198 | | "FROM maintenance_logs ORDER BY ts DESC;"; |
| | 1 | 199 | | PGresult *res = PQexec(conn_wrapper->pg_conn, query); |
| | | 200 | | |
| | 1 | 201 | | if (PQresultStatus(res) != PGRES_TUPLES_OK) { |
| | 0 | 202 | | PQclear(res); |
| | 0 | 203 | | db_pool_release(conn_wrapper); |
| | 0 | 204 | | return 0; |
| | | 205 | | } |
| | | 206 | | |
| | 1 | 207 | | int rows = PQntuples(res); |
| | 1 | 208 | | int count = (rows < max_logs) ? rows : max_logs; |
| | | 209 | | |
| | 4 | 210 | | for (int i = 0; i < count; i++) { |
| | 3 | 211 | | out_logs[i].id = atoi(PQgetvalue(res, i, 0)); |
| | 3 | 212 | | out_logs[i].machine_id = atoi(PQgetvalue(res, i, 1)); |
| | 3 | 213 | | strncpy(out_logs[i].technician_name, PQgetvalue(res, i, 2), 99); |
| | 3 | 214 | | out_logs[i].technician_name[99] = '\0'; |
| | 3 | 215 | | strncpy(out_logs[i].log_date, PQgetvalue(res, i, 3), 31); |
| | 3 | 216 | | out_logs[i].log_date[31] = '\0'; |
| | 3 | 217 | | strncpy(out_logs[i].description, PQgetvalue(res, i, 4), 511); |
| | 3 | 218 | | out_logs[i].description[511] = '\0'; |
| | 3 | 219 | | out_logs[i].cost = atof(PQgetvalue(res, i, 5)); |
| | | 220 | | } |
| | | 221 | | |
| | 1 | 222 | | PQclear(res); |
| | 1 | 223 | | db_pool_release(conn_wrapper); |
| | 1 | 224 | | return count; |
| | | 225 | | } |
| | | 226 | | |
| | 1 | 227 | | bool update_maintenance_log(int log_id, const char *description, double cost) { |
| | 1 | 228 | | DBConnection *conn_wrapper = db_pool_acquire(); |
| | | 229 | | |
| | 1 | 230 | | if (!conn_wrapper) return false; |
| | | 231 | | |
| | | 232 | | char query[1024]; |
| | 1 | 233 | | snprintf(query, sizeof(query), |
| | | 234 | | "UPDATE maintenance_logs SET description = '%s', cost = %.2f WHERE id = %d;", |
| | | 235 | | description, cost, log_id); |
| | 1 | 236 | | PGresult *res = PQexec(conn_wrapper->pg_conn, query); |
| | 1 | 237 | | bool success = (PQresultStatus(res) == PGRES_COMMAND_OK); |
| | | 238 | | |
| | 1 | 239 | | if (!success) { |
| | 0 | 240 | | LOG_ERROR("Failed to update log: %s", PQerrorMessage(conn_wrapper->pg_conn)); |
| | | 241 | | } else { |
| | 1 | 242 | | LOG_INFO("Maintenance log %d updated successfully.", log_id); |
| | | 243 | | } |
| | | 244 | | |
| | 1 | 245 | | PQclear(res); |
| | 1 | 246 | | db_pool_release(conn_wrapper); |
| | 1 | 247 | | return success; |
| | | 248 | | } |
| | | 249 | | |
| | 1 | 250 | | bool delete_maintenance_log(int log_id) { |
| | 1 | 251 | | DBConnection *conn_wrapper = db_pool_acquire(); |
| | | 252 | | |
| | 1 | 253 | | if (!conn_wrapper) return false; |
| | | 254 | | |
| | | 255 | | char query[128]; |
| | 1 | 256 | | snprintf(query, sizeof(query), "DELETE FROM maintenance_logs WHERE id = %d;", log_id); |
| | 1 | 257 | | PGresult *res = PQexec(conn_wrapper->pg_conn, query); |
| | 1 | 258 | | bool success = (PQresultStatus(res) == PGRES_COMMAND_OK); |
| | | 259 | | |
| | 1 | 260 | | if (!success) { |
| | 0 | 261 | | LOG_ERROR("Failed to delete log: %s", PQerrorMessage(conn_wrapper->pg_conn)); |
| | | 262 | | } else { |
| | 1 | 263 | | LOG_INFO("Maintenance log %d deleted.", log_id); |
| | | 264 | | } |
| | | 265 | | |
| | 1 | 266 | | PQclear(res); |
| | 1 | 267 | | db_pool_release(conn_wrapper); |
| | 1 | 268 | | return success; |
| | | 269 | | } |
| | | 270 | | |
| | | 271 | | // ============================================================================ |
| | | 272 | | // TEST HELPER |
| | | 273 | | // ============================================================================ |
| | | 274 | | |
| | 8 | 275 | | void reset_mocks(void) { |
| | 8 | 276 | | mock_connection_available = true; |
| | 8 | 277 | | mock_query_success = true; |
| | 8 | 278 | | mock_row_count = 0; |
| | 8 | 279 | | memset(&mock_result, 0, sizeof(mock_result)); |
| | 8 | 280 | | } |
| | | 281 | | |
| | | 282 | | // ============================================================================ |
| | | 283 | | // TESTS |
| | | 284 | | // ============================================================================ |
| | | 285 | | |
| | 1 | 286 | | void test_add_maintenance_log_success(void) { |
| | 1 | 287 | | printf("\n[TEST] test_add_maintenance_log_success\n"); |
| | 1 | 288 | | reset_mocks(); |
| | 1 | 289 | | bool result = add_maintenance_log(101, "John Doe", "Oil change", 150.50); |
| | 1 | 290 | | assert(result == true); |
| | 1 | 291 | | printf("✓ PASSED\n"); |
| | 1 | 292 | | } |
| | | 293 | | |
| | 1 | 294 | | void test_add_maintenance_log_no_connection(void) { |
| | 1 | 295 | | printf("\n[TEST] test_add_maintenance_log_no_connection\n"); |
| | 1 | 296 | | reset_mocks(); |
| | 1 | 297 | | mock_connection_available = false; |
| | 1 | 298 | | bool result = add_maintenance_log(101, "John", "Test", 100.0); |
| | 1 | 299 | | assert(result == false); |
| | 1 | 300 | | printf("✓ PASSED\n"); |
| | 1 | 301 | | } |
| | | 302 | | |
| | 1 | 303 | | void test_add_maintenance_log_query_failure(void) { |
| | 1 | 304 | | printf("\n[TEST] test_add_maintenance_log_query_failure\n"); |
| | 1 | 305 | | reset_mocks(); |
| | 1 | 306 | | mock_query_success = false; |
| | 1 | 307 | | bool result = add_maintenance_log(101, "John", "Test", 100.0); |
| | 1 | 308 | | assert(result == false); |
| | 1 | 309 | | printf("✓ PASSED\n"); |
| | 1 | 310 | | } |
| | | 311 | | |
| | 1 | 312 | | void test_get_maintenance_history_success(void) { |
| | 1 | 313 | | printf("\n[TEST] test_get_maintenance_history_success\n"); |
| | 1 | 314 | | reset_mocks(); |
| | 1 | 315 | | mock_row_count = 1; |
| | | 316 | | MaintenanceLog logs[10]; |
| | 1 | 317 | | int count = get_maintenance_history(101, logs, 10); |
| | 1 | 318 | | assert(count == 1); |
| | 1 | 319 | | assert(logs[0].id == 1); |
| | 1 | 320 | | assert(logs[0].machine_id == 101); |
| | 1 | 321 | | printf("✓ PASSED\n"); |
| | 1 | 322 | | } |
| | | 323 | | |
| | 1 | 324 | | void test_get_maintenance_history_no_connection(void) { |
| | 1 | 325 | | printf("\n[TEST] test_get_maintenance_history_no_connection\n"); |
| | 1 | 326 | | reset_mocks(); |
| | 1 | 327 | | mock_connection_available = false; |
| | | 328 | | MaintenanceLog logs[10]; |
| | 1 | 329 | | int count = get_maintenance_history(101, logs, 10); |
| | 1 | 330 | | assert(count == 0); |
| | 1 | 331 | | printf("✓ PASSED\n"); |
| | 1 | 332 | | } |
| | | 333 | | |
| | 1 | 334 | | void test_get_all_maintenance_logs_success(void) { |
| | 1 | 335 | | printf("\n[TEST] test_get_all_maintenance_logs_success\n"); |
| | 1 | 336 | | reset_mocks(); |
| | 1 | 337 | | mock_row_count = 3; |
| | | 338 | | MaintenanceLog logs[10]; |
| | 1 | 339 | | int count = get_all_maintenance_logs(logs, 10); |
| | 1 | 340 | | assert(count == 3); |
| | 1 | 341 | | printf("✓ PASSED\n"); |
| | 1 | 342 | | } |
| | | 343 | | |
| | 1 | 344 | | void test_update_maintenance_log_success(void) { |
| | 1 | 345 | | printf("\n[TEST] test_update_maintenance_log_success\n"); |
| | 1 | 346 | | reset_mocks(); |
| | 1 | 347 | | bool result = update_maintenance_log(1, "Updated", 200.0); |
| | 1 | 348 | | assert(result == true); |
| | 1 | 349 | | printf("✓ PASSED\n"); |
| | 1 | 350 | | } |
| | | 351 | | |
| | 1 | 352 | | void test_delete_maintenance_log_success(void) { |
| | 1 | 353 | | printf("\n[TEST] test_delete_maintenance_log_success\n"); |
| | 1 | 354 | | reset_mocks(); |
| | 1 | 355 | | bool result = delete_maintenance_log(1); |
| | 1 | 356 | | assert(result == true); |
| | 1 | 357 | | printf("✓ PASSED\n"); |
| | 1 | 358 | | } |
| | | 359 | | |
| | | 360 | | // ============================================================================ |
| | | 361 | | // MAIN |
| | | 362 | | // ============================================================================ |
| | | 363 | | |
| | 1 | 364 | | int main(void) { |
| | 1 | 365 | | printf("========================================\n"); |
| | 1 | 366 | | printf("MAINTENANCE SERVICE TEST (STANDALONE)\n"); |
| | 1 | 367 | | printf("========================================\n"); |
| | 1 | 368 | | int total = 0, passed = 0; |
| | 1 | 369 | | test_add_maintenance_log_success(); |
| | 1 | 370 | | total++; |
| | 1 | 371 | | passed++; |
| | 1 | 372 | | test_add_maintenance_log_no_connection(); |
| | 1 | 373 | | total++; |
| | 1 | 374 | | passed++; |
| | 1 | 375 | | test_add_maintenance_log_query_failure(); |
| | 1 | 376 | | total++; |
| | 1 | 377 | | passed++; |
| | 1 | 378 | | test_get_maintenance_history_success(); |
| | 1 | 379 | | total++; |
| | 1 | 380 | | passed++; |
| | 1 | 381 | | test_get_maintenance_history_no_connection(); |
| | 1 | 382 | | total++; |
| | 1 | 383 | | passed++; |
| | 1 | 384 | | test_get_all_maintenance_logs_success(); |
| | 1 | 385 | | total++; |
| | 1 | 386 | | passed++; |
| | 1 | 387 | | test_update_maintenance_log_success(); |
| | 1 | 388 | | total++; |
| | 1 | 389 | | passed++; |
| | 1 | 390 | | test_delete_maintenance_log_success(); |
| | 1 | 391 | | total++; |
| | 1 | 392 | | passed++; |
| | 1 | 393 | | printf("\n========================================\n"); |
| | 1 | 394 | | printf("TEST RESULTS: %d/%d PASSED\n", passed, total); |
| | 1 | 395 | | printf("========================================\n"); |
| | 1 | 396 | | return (passed == total) ? 0 : 1; |
| | | 397 | | } |