|
7 | 7 | use Rcalicdan\FiberAsync\Config\ConfigLoader;
|
8 | 8 |
|
9 | 9 | beforeEach(function () {
|
10 |
| - // Create a temporary config directory and files for testing |
11 | 10 | $testDir = sys_get_temp_dir() . '/async-db-test-' . uniqid();
|
12 | 11 | mkdir($testDir);
|
13 | 12 | mkdir($testDir . '/config');
|
14 |
| - mkdir($testDir . '/vendor'); // Required for ConfigLoader to find root |
| 13 | + mkdir($testDir . '/vendor'); |
15 | 14 |
|
16 | 15 | // Create database config file
|
17 | 16 | $databaseConfig = [
|
|
27 | 26 | '<?php return ' . var_export($databaseConfig, true) . ';'
|
28 | 27 | );
|
29 | 28 |
|
30 |
| - // Create .env file |
31 | 29 | file_put_contents($testDir . '/.env', 'DB_CONNECTION=test');
|
32 | 30 |
|
33 | 31 | // Mock the ConfigLoader to use our test directory
|
@@ -1128,119 +1126,6 @@ function () {
|
1128 | 1126 | });
|
1129 | 1127 | });
|
1130 | 1128 |
|
1131 |
| -describe('AsyncQueryBuilder Advanced Expressions and Aggregates', function () { |
1132 |
| - beforeEach(function () { |
1133 |
| - run(function () { |
1134 |
| - await(AsyncDB::rawExecute("DROP TABLE IF EXISTS sales_data")); |
1135 |
| - |
1136 |
| - await(AsyncDB::rawExecute(" |
1137 |
| - CREATE TABLE sales_data ( |
1138 |
| - id INTEGER PRIMARY KEY AUTOINCREMENT, |
1139 |
| - product_name VARCHAR(255), |
1140 |
| - category VARCHAR(100), |
1141 |
| - quantity_sold INTEGER, |
1142 |
| - unit_price DECIMAL(10,2), |
1143 |
| - order_date DATETIME |
1144 |
| - ) |
1145 |
| - ")); |
1146 |
| - |
1147 |
| - $data = [ |
1148 |
| - ['product_name' => 'Laptop', 'category' => 'Electronics', 'quantity_sold' => 5, 'unit_price' => 1200.00, 'order_date' => '2023-01-15'], |
1149 |
| - ['product_name' => 'Mouse', 'category' => 'Electronics', 'quantity_sold' => 20, 'unit_price' => 25.00, 'order_date' => '2023-01-20'], |
1150 |
| - ['product_name' => 'Keyboard', 'category' => 'Electronics', 'quantity_sold' => 15, 'unit_price' => 75.00, 'order_date' => '2023-02-01'], |
1151 |
| - ['product_name' => 'Async PHP', 'category' => 'Books', 'quantity_sold' => 50, 'unit_price' => 40.00, 'order_date' => '2023-01-05'], |
1152 |
| - ['product_name' => 'Pest Cookbook', 'category' => 'Books', 'quantity_sold' => 30, 'unit_price' => 30.00, 'order_date' => '2023-02-10'], |
1153 |
| - ['product_name' => 'Stapler', 'category' => 'Office', 'quantity_sold' => 100, 'unit_price' => 10.00, 'order_date' => '2023-01-25'], |
1154 |
| - ]; |
1155 |
| - |
1156 |
| - await(AsyncDB::table('sales_data')->insertBatch($data)); |
1157 |
| - }); |
1158 |
| - }); |
1159 |
| - |
1160 |
| - it('can use raw expressions in select and order by with a CASE statement', function () { |
1161 |
| - run(function () { |
1162 |
| - $revenueExpression = 'quantity_sold * unit_price'; |
1163 |
| - |
1164 |
| - $tierExpression = "CASE |
1165 |
| - WHEN {$revenueExpression} >= 6000 THEN 'Tier A' |
1166 |
| - WHEN {$revenueExpression} >= 2000 THEN 'Tier B' |
1167 |
| - ELSE 'Tier C' |
1168 |
| - END"; |
1169 |
| - |
1170 |
| - $query = AsyncDB::table('sales_data') |
1171 |
| - ->select([ |
1172 |
| - 'product_name', |
1173 |
| - "$revenueExpression as total_revenue", |
1174 |
| - "$tierExpression as performance_tier" |
1175 |
| - ]) |
1176 |
| - ->orderBy('performance_tier') |
1177 |
| - ->orderBy('total_revenue', 'DESC'); |
1178 |
| - |
1179 |
| - $result = await($query->get()); |
1180 |
| - |
1181 |
| - expect($result)->toHaveCount(6); |
1182 |
| - |
1183 |
| - expect($result[0]['product_name'])->toBe('Laptop'); |
1184 |
| - expect($result[0]['performance_tier'])->toBe('Tier A'); |
1185 |
| - |
1186 |
| - expect($result[1]['product_name'])->toBe('Async PHP'); |
1187 |
| - expect($result[1]['performance_tier'])->toBe('Tier B'); |
1188 |
| - |
1189 |
| - expect($result[5]['product_name'])->toBe('Mouse'); |
1190 |
| - expect($result[5]['performance_tier'])->toBe('Tier C'); |
1191 |
| - }); |
1192 |
| - }); |
1193 |
| - |
1194 |
| - it('can handle complex aggregate queries with multiple HAVING clauses', function () { |
1195 |
| - run(function () { |
1196 |
| - $query = AsyncDB::table('sales_data') |
1197 |
| - ->select([ |
1198 |
| - 'category', |
1199 |
| - 'AVG(unit_price) as avg_price', |
1200 |
| - 'COUNT(id) as item_count' |
1201 |
| - ]) |
1202 |
| - ->groupBy('category') |
1203 |
| - ->having('item_count', '>', 1) |
1204 |
| - ->having('avg_price', '>', 100) |
1205 |
| - ->orderBy('category'); |
1206 |
| - |
1207 |
| - // --- DEBUGGING STATEMENTS ADDED HERE --- |
1208 |
| - echo "\n--- DEBUGGING AGGREGATE/HAVING QUERY ---\n"; |
1209 |
| - echo "Generated SQL: " . $query->toSql() . "\n"; |
1210 |
| - echo "Bindings: "; |
1211 |
| - print_r($query->getBindings()); |
1212 |
| - echo "\n--------------------------------------\n"; |
1213 |
| - // --- END DEBUGGING --- |
1214 |
| - |
1215 |
| - $result = await($query->get()); |
1216 |
| - |
1217 |
| - // Also output the actual result to be certain |
1218 |
| - echo "Actual query result count: " . count($result) . "\n"; |
1219 |
| - print_r($result); |
1220 |
| - echo "\n"; |
1221 |
| - |
1222 |
| - expect($result)->toHaveCount(1); |
1223 |
| - expect($result[0]['category'])->toBe('Electronics'); |
1224 |
| - }); |
1225 |
| - }); |
1226 |
| - |
1227 |
| - it('can handle scalar subqueries in whereRaw', function () { |
1228 |
| - run(function () { |
1229 |
| - $subQuery = AsyncDB::table('sales_data')->select('AVG(unit_price)'); |
1230 |
| - $subSql = $subQuery->toSql(); |
1231 |
| - |
1232 |
| - $query = AsyncDB::table('sales_data') |
1233 |
| - ->whereRaw("unit_price > ({$subSql})") |
1234 |
| - ->orderBy('unit_price', 'DESC'); |
1235 |
| - |
1236 |
| - $result = await($query->get()); |
1237 |
| - |
1238 |
| - expect($result)->toHaveCount(1); |
1239 |
| - expect($result[0]['product_name'])->toBe('Laptop'); |
1240 |
| - }); |
1241 |
| - }); |
1242 |
| -}); |
1243 |
| - |
1244 | 1129 | describe('AsyncQueryBuilder Advanced Subqueries', function () {
|
1245 | 1130 | beforeEach(function () {
|
1246 | 1131 | run(function () {
|
|
0 commit comments