| | | 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 | | // ÖNEMLİ: libpq-fe.h'i include ETME! |
| | | 10 | | #define PGconn void |
| | | 11 | | #define PGresult void |
| | | 12 | | |
| | | 13 | | #define LIBPQ_FE_H |
| | | 14 | | #define PQconninfoOption void |
| | | 15 | | #define ConnStatusType int |
| | | 16 | | #define ExecStatusType int |
| | | 17 | | #define CONNECTION_OK 0 |
| | | 18 | | #define CONNECTION_BAD 1 |
| | | 19 | | #define PGRES_COMMAND_OK 1 |
| | | 20 | | #define PGRES_TUPLES_OK 2 |
| | | 21 | | #define PGRES_FATAL_ERROR 3 |
| | | 22 | | |
| | | 23 | | #include "inventory_service.h" |
| | | 24 | | #include "db_connection.h" |
| | | 25 | | |
| | | 26 | | // Logger mock'larını manuel tanımla |
| | | 27 | | #undef LOG_ERROR |
| | | 28 | | #undef LOG_INFO |
| | | 29 | | #define LOG_ERROR(msg, ...) |
| | | 30 | | #define LOG_INFO(msg, ...) |
| | | 31 | | |
| | | 32 | | /* ==================================================================== |
| | | 33 | | * MOCK FONKSİYONLAR |
| | | 34 | | * ==================================================================== */ |
| | | 35 | | |
| | | 36 | | static DBConnection mock_conn; |
| | | 37 | | static void *mock_result; |
| | | 38 | | static int mock_acquire_success = 1; |
| | | 39 | | static int mock_result_status = 2; // PGRES_TUPLES_OK |
| | | 40 | | static char mock_error_message[256] = ""; |
| | | 41 | | static char mock_query[1024] = ""; |
| | | 42 | | |
| | | 43 | | static int mock_acquire_called = 0; |
| | | 44 | | static int mock_release_called = 0; |
| | | 45 | | static int mock_pqexec_called = 0; |
| | | 46 | | static int mock_pqclear_called = 0; |
| | | 47 | | static int mock_pqntuples_called = 0; |
| | | 48 | | |
| | | 49 | | typedef struct { |
| | | 50 | | char id[10]; |
| | | 51 | | char part_name[100]; |
| | | 52 | | char sku[50]; |
| | | 53 | | char quantity[10]; |
| | | 54 | | char min_stock[10]; |
| | | 55 | | char unit_cost[20]; |
| | | 56 | | char last_restocked[32]; |
| | | 57 | | } MockRow; |
| | | 58 | | |
| | | 59 | | static MockRow mock_rows[10]; |
| | | 60 | | static int mock_row_count = 0; |
| | | 61 | | |
| | 4 | 62 | | DBConnection *db_pool_acquire(void) { |
| | 4 | 63 | | mock_acquire_called++; |
| | | 64 | | |
| | 4 | 65 | | if (mock_acquire_success) { |
| | 3 | 66 | | mock_conn.pg_conn = (void *)0x1234; |
| | 3 | 67 | | return &mock_conn; |
| | | 68 | | } |
| | | 69 | | |
| | 1 | 70 | | return NULL; |
| | | 71 | | } |
| | | 72 | | |
| | 3 | 73 | | void db_pool_release(DBConnection *conn) { |
| | 3 | 74 | | mock_release_called++; |
| | 3 | 75 | | } |
| | | 76 | | |
| | 3 | 77 | | void *PQexec(void *conn, const char *query) { |
| | 3 | 78 | | mock_pqexec_called++; |
| | 3 | 79 | | strncpy(mock_query, query, sizeof(mock_query) - 1); |
| | 3 | 80 | | return mock_result; |
| | | 81 | | } |
| | | 82 | | |
| | 3 | 83 | | int PQresultStatus(const void *res) { |
| | 3 | 84 | | return mock_result_status; |
| | | 85 | | } |
| | | 86 | | |
| | 0 | 87 | | char *PQerrorMessage(const void *conn) { |
| | 0 | 88 | | return mock_error_message; |
| | | 89 | | } |
| | | 90 | | |
| | 3 | 91 | | void PQclear(void *res) { |
| | 3 | 92 | | mock_pqclear_called++; |
| | 3 | 93 | | } |
| | | 94 | | |
| | 2 | 95 | | int PQntuples(const void *res) { |
| | 2 | 96 | | mock_pqntuples_called++; |
| | 2 | 97 | | return mock_row_count; |
| | | 98 | | } |
| | | 99 | | |
| | 28 | 100 | | char *PQgetvalue(const void *res, int row, int field) { |
| | 28 | 101 | | if (row < mock_row_count) { |
| | 28 | 102 | | switch (field) { |
| | 4 | 103 | | case 0: |
| | 4 | 104 | | return mock_rows[row].id; |
| | | 105 | | |
| | 4 | 106 | | case 1: |
| | 4 | 107 | | return mock_rows[row].part_name; |
| | | 108 | | |
| | 4 | 109 | | case 2: |
| | 4 | 110 | | return mock_rows[row].sku; |
| | | 111 | | |
| | 4 | 112 | | case 3: |
| | 4 | 113 | | return mock_rows[row].quantity; |
| | | 114 | | |
| | 4 | 115 | | case 4: |
| | 4 | 116 | | return mock_rows[row].min_stock; |
| | | 117 | | |
| | 4 | 118 | | case 5: |
| | 4 | 119 | | return mock_rows[row].unit_cost; |
| | | 120 | | |
| | 4 | 121 | | case 6: |
| | 4 | 122 | | return mock_rows[row].last_restocked; |
| | | 123 | | } |
| | | 124 | | } |
| | | 125 | | |
| | 0 | 126 | | return ""; |
| | | 127 | | } |
| | | 128 | | |
| | | 129 | | /* ==================================================================== |
| | | 130 | | * TEST SETUP |
| | | 131 | | * ==================================================================== */ |
| | | 132 | | |
| | 5 | 133 | | static void reset_mocks(void) { |
| | 5 | 134 | | mock_acquire_success = 1; |
| | 5 | 135 | | mock_result_status = 2; // PGRES_TUPLES_OK |
| | 5 | 136 | | mock_error_message[0] = '\0'; |
| | 5 | 137 | | mock_query[0] = '\0'; |
| | 5 | 138 | | mock_acquire_called = 0; |
| | 5 | 139 | | mock_release_called = 0; |
| | 5 | 140 | | mock_pqexec_called = 0; |
| | 5 | 141 | | mock_pqclear_called = 0; |
| | 5 | 142 | | mock_pqntuples_called = 0; |
| | 5 | 143 | | mock_row_count = 0; |
| | 5 | 144 | | memset(mock_rows, 0, sizeof(mock_rows)); |
| | 5 | 145 | | } |
| | | 146 | | |
| | 2 | 147 | | static void setup_test_data(void) { |
| | 2 | 148 | | mock_row_count = 2; |
| | 2 | 149 | | strcpy(mock_rows[0].id, "1"); |
| | 2 | 150 | | strcpy(mock_rows[0].part_name, "Motor Oil"); |
| | 2 | 151 | | strcpy(mock_rows[0].sku, "MO-001"); |
| | 2 | 152 | | strcpy(mock_rows[0].quantity, "50"); |
| | 2 | 153 | | strcpy(mock_rows[0].min_stock, "10"); |
| | 2 | 154 | | strcpy(mock_rows[0].unit_cost, "25.50"); |
| | 2 | 155 | | strcpy(mock_rows[0].last_restocked, "2025-01-01"); |
| | 2 | 156 | | strcpy(mock_rows[1].id, "2"); |
| | 2 | 157 | | strcpy(mock_rows[1].part_name, "Air Filter"); |
| | 2 | 158 | | strcpy(mock_rows[1].sku, "AF-002"); |
| | 2 | 159 | | strcpy(mock_rows[1].quantity, "5"); |
| | 2 | 160 | | strcpy(mock_rows[1].min_stock, "10"); |
| | 2 | 161 | | strcpy(mock_rows[1].unit_cost, "15.75"); |
| | 2 | 162 | | strcpy(mock_rows[1].last_restocked, "2025-01-02"); |
| | 2 | 163 | | } |
| | | 164 | | |
| | | 165 | | /* ==================================================================== |
| | | 166 | | * TEST CASES |
| | | 167 | | * ==================================================================== */ |
| | | 168 | | |
| | 1 | 169 | | static void test_get_all_inventory_success(void **state) { |
| | 1 | 170 | | reset_mocks(); |
| | 1 | 171 | | setup_test_data(); |
| | | 172 | | InventoryItem items[5]; |
| | 1 | 173 | | int count = get_all_inventory(items, 5); |
| | 1 | 174 | | assert_int_equal(count, 2); |
| | 1 | 175 | | assert_int_equal(mock_acquire_called, 1); |
| | 1 | 176 | | assert_int_equal(mock_pqexec_called, 1); |
| | 1 | 177 | | assert_int_equal(items[0].id, 1); |
| | 1 | 178 | | assert_string_equal(items[0].part_name, "Motor Oil"); |
| | 1 | 179 | | assert_int_equal(items[0].quantity, 50); |
| | 1 | 180 | | } |
| | | 181 | | |
| | 1 | 182 | | static void test_get_all_inventory_null_items(void **state) { |
| | 1 | 183 | | reset_mocks(); |
| | 1 | 184 | | int count = get_all_inventory(NULL, 5); |
| | 1 | 185 | | assert_int_equal(count, 0); |
| | 1 | 186 | | assert_int_equal(mock_acquire_called, 0); |
| | 1 | 187 | | } |
| | | 188 | | |
| | 1 | 189 | | static void test_get_all_inventory_acquire_fails(void **state) { |
| | 1 | 190 | | reset_mocks(); |
| | 1 | 191 | | mock_acquire_success = 0; |
| | | 192 | | InventoryItem items[5]; |
| | 1 | 193 | | int count = get_all_inventory(items, 5); |
| | 1 | 194 | | assert_int_equal(count, 0); |
| | 1 | 195 | | } |
| | | 196 | | |
| | 1 | 197 | | static void test_update_inventory_success(void **state) { |
| | 1 | 198 | | reset_mocks(); |
| | 1 | 199 | | mock_result_status = 1; // PGRES_COMMAND_OK |
| | 1 | 200 | | bool result = update_inventory_quantity(1, 10); |
| | 1 | 201 | | assert_true(result); |
| | 1 | 202 | | assert_int_equal(mock_pqexec_called, 1); |
| | 1 | 203 | | } |
| | | 204 | | |
| | 1 | 205 | | static void test_get_low_stock_success(void **state) { |
| | 1 | 206 | | reset_mocks(); |
| | 1 | 207 | | setup_test_data(); |
| | | 208 | | InventoryItem items[5]; |
| | 1 | 209 | | int count = get_low_stock_items(items, 5); |
| | 1 | 210 | | assert_int_equal(count, 2); |
| | 1 | 211 | | } |
| | | 212 | | |
| | | 213 | | /* ==================================================================== |
| | | 214 | | * MAIN |
| | | 215 | | * ==================================================================== */ |
| | | 216 | | |
| | 1 | 217 | | int main(void) { |
| | 1 | 218 | | const struct CMUnitTest tests[] = { |
| | | 219 | | cmocka_unit_test(test_get_all_inventory_success), |
| | | 220 | | cmocka_unit_test(test_get_all_inventory_null_items), |
| | | 221 | | cmocka_unit_test(test_get_all_inventory_acquire_fails), |
| | | 222 | | |
| | | 223 | | cmocka_unit_test(test_update_inventory_success), |
| | | 224 | | |
| | | 225 | | cmocka_unit_test(test_get_low_stock_success), |
| | | 226 | | }; |
| | 1 | 227 | | return cmocka_run_group_tests(tests, NULL, NULL); |
| | | 228 | | } |