| | | 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 |
| | 0 | 20 | | void LOG_INFO(const char *format, ...) {} |
| | 0 | 21 | | void LOG_ERROR(const char *format, ...) {} |
| | | 22 | | |
| | | 23 | | // Mock State |
| | | 24 | | static DBConnection mock_conn; |
| | | 25 | | static int mock_acquire_success = 1; |
| | | 26 | | static int mock_result_status = PGRES_TUPLES_OK; |
| | | 27 | | static int mock_ntuples = 1; |
| | | 28 | | |
| | | 29 | | // Mock DB functions |
| | 4 | 30 | | DBConnection *db_pool_acquire(void) { |
| | 4 | 31 | | if (mock_acquire_success) { |
| | 3 | 32 | | mock_conn.pg_conn = (void *)0x1234; |
| | 3 | 33 | | return &mock_conn; |
| | | 34 | | } |
| | 1 | 35 | | return NULL; |
| | | 36 | | } |
| | | 37 | | |
| | 3 | 38 | | void db_pool_release(DBConnection *conn) {} |
| | | 39 | | |
| | 3 | 40 | | void *PQexec(void *conn, const char *query) { |
| | 3 | 41 | | return (void *)0x5678; |
| | | 42 | | } |
| | | 43 | | |
| | 3 | 44 | | int PQresultStatus(const void *res) { |
| | 3 | 45 | | return mock_result_status; |
| | | 46 | | } |
| | | 47 | | |
| | 3 | 48 | | void PQclear(void *res) {} |
| | | 49 | | |
| | 2 | 50 | | int PQntuples(const void *res) { |
| | 2 | 51 | | return mock_ntuples; |
| | | 52 | | } |
| | | 53 | | |
| | 30 | 54 | | char *PQgetvalue(const void *res, int row, int field) { |
| | 30 | 55 | | if (field == 0) return "1"; // ID |
| | 24 | 56 | | if (field == 1) return "CNC-01"; // Name |
| | 18 | 57 | | if (field == 2) return "X100"; // Model |
| | 12 | 58 | | if (field == 3) return "FloorB"; // Location |
| | 6 | 59 | | if (field == 4) return "running"; // Status |
| | 0 | 60 | | return ""; |
| | | 61 | | } |
| | | 62 | | |
| | 1 | 63 | | char *PQerrorMessage(const void *conn) { |
| | 1 | 64 | | return "Machine Mock Error"; |
| | | 65 | | } |
| | | 66 | | |
| | | 67 | | /* ==================================================================== |
| | | 68 | | * TEST CASES |
| | | 69 | | * ==================================================================== */ |
| | | 70 | | |
| | 1 | 71 | | static void test_get_all_machines_paths(void **state) { |
| | 1 | 72 | | mock_acquire_success = 1; |
| | 1 | 73 | | mock_result_status = PGRES_TUPLES_OK; |
| | 1 | 74 | | mock_ntuples = 1; |
| | | 75 | | |
| | | 76 | | Machine items[5]; |
| | 1 | 77 | | int count = get_all_machines(items, 5); |
| | 1 | 78 | | assert_int_equal(count, 1); |
| | 1 | 79 | | assert_string_equal(items[0].name, "CNC-01"); |
| | | 80 | | |
| | | 81 | | // Test truncation |
| | 1 | 82 | | mock_ntuples = 10; |
| | 1 | 83 | | count = get_all_machines(items, 5); |
| | 1 | 84 | | assert_int_equal(count, 5); |
| | | 85 | | |
| | | 86 | | // Acquire Fail |
| | 1 | 87 | | mock_acquire_success = 0; |
| | 1 | 88 | | count = get_all_machines(items, 5); |
| | 1 | 89 | | assert_int_equal(count, 0); |
| | | 90 | | |
| | | 91 | | // Query Fail |
| | 1 | 92 | | mock_acquire_success = 1; |
| | 1 | 93 | | mock_result_status = PGRES_FATAL_ERROR; |
| | 1 | 94 | | count = get_all_machines(items, 5); |
| | 1 | 95 | | assert_int_equal(count, 0); |
| | | 96 | | |
| | | 97 | | // Param fail |
| | 1 | 98 | | assert_int_equal(get_all_machines(NULL, 5), 0); |
| | 1 | 99 | | assert_int_equal(get_all_machines(items, 0), 0); |
| | 1 | 100 | | } |
| | | 101 | | |
| | 1 | 102 | | static void test_get_machine_by_id_simple(void **state) { |
| | | 103 | | Machine m; |
| | 1 | 104 | | int res = get_machine_by_id(10, &m); |
| | 1 | 105 | | assert_int_equal(res, 0); |
| | 1 | 106 | | assert_int_equal(m.id, 10); |
| | 1 | 107 | | assert_int_equal(get_machine_by_id(1, NULL), -1); |
| | 1 | 108 | | } |
| | | 109 | | |
| | 1 | 110 | | static void test_get_health_score(void **state) { |
| | 1 | 111 | | double score = get_machine_health_score(1); |
| | 1 | 112 | | assert_true(score > 0.0); |
| | 1 | 113 | | } |
| | | 114 | | |
| | 1 | 115 | | int main(void) { |
| | 1 | 116 | | const struct CMUnitTest tests[] = { |
| | | 117 | | cmocka_unit_test(test_get_all_machines_paths), |
| | | 118 | | cmocka_unit_test(test_get_machine_by_id_simple), |
| | | 119 | | cmocka_unit_test(test_get_health_score), |
| | | 120 | | }; |
| | 1 | 121 | | return cmocka_run_group_tests(tests, NULL, NULL); |
| | | 122 | | } |