@@ -203,4 +203,329 @@ mod tests {
203
203
]
204
204
) ;
205
205
}
206
+
207
+ // Testing select with no rows in table
208
+ #[ test]
209
+ fn execute_select_empty_table ( ) {
210
+ let mut table = Table :: new ( ) ;
211
+ let mut statement = Statement :: default ( ) ;
212
+ prepare_statement ( "select" , & mut statement) ;
213
+ let result = execute_statement ( statement, & mut table) ;
214
+
215
+ if let ExecuteResult :: Success ( Some ( rows) ) = result {
216
+ assert_eq ! ( rows. len( ) , 0 ) ;
217
+ } else {
218
+ panic ! ( "Expected Success with empty vector" ) ;
219
+ }
220
+ }
221
+
222
+ // Testing select with specific ID that doesn't exist
223
+ #[ test]
224
+ fn execute_select_nonexistent_id ( ) {
225
+ let mut table = Table :: new ( ) ;
226
+ do_sql_cmd ( & mut table
, "insert 13 rosh [email protected] " ) ;
227
+
228
+ let mut statement = Statement :: default ( ) ;
229
+ prepare_statement ( "select 42" , & mut statement) ;
230
+ let result = execute_statement ( statement, & mut table) ;
231
+
232
+ if let ExecuteResult :: Success ( Some ( rows) ) = result {
233
+ assert_eq ! ( rows. len( ) , 0 ) ;
234
+ } else {
235
+ panic ! ( "Expected Success with empty vector" ) ;
236
+ }
237
+ }
238
+
239
+ // Testing select with specific ID that exists
240
+ #[ test]
241
+ fn execute_select_existing_id ( ) {
242
+ let mut table = Table :: new ( ) ;
243
+ do_sql_cmd ( & mut table
, "insert 13 rosh [email protected] " ) ;
244
+ do_sql_cmd ( & mut table
, "insert 42 stefan [email protected] " ) ;
245
+
246
+ let mut statement = Statement :: default ( ) ;
247
+ prepare_statement ( "select 42" , & mut statement) ;
248
+ let result = execute_statement ( statement, & mut table) ;
249
+
250
+ if let ExecuteResult :: Success ( Some ( rows) ) = result {
251
+ assert_eq ! ( rows. len( ) , 1 ) ;
252
+ assert_eq ! ( rows[ 0 ] . id, 42 ) ;
253
+ assert_eq ! ( rows[ 0 ] . username, "stefan" ) ;
254
+ assert_eq ! ( rows
[ 0 ] . email
, "[email protected] " ) ;
255
+ } else {
256
+ panic ! ( "Expected Success with one row" ) ;
257
+ }
258
+ }
259
+
260
+ // Testing select all with multiple rows
261
+ #[ test]
262
+ fn execute_select_all_multiple_rows ( ) {
263
+ let mut table = Table :: new ( ) ;
264
+ do_sql_cmd ( & mut table
, "insert 13 rosh [email protected] " ) ;
265
+ do_sql_cmd ( & mut table
, "insert 42 stefan [email protected] " ) ;
266
+ do_sql_cmd (
267
+ & mut table,
268
+ "insert 1699 sniper_penut [email protected] " ,
269
+ ) ;
270
+
271
+ let mut statement = Statement :: default ( ) ;
272
+ prepare_statement ( "select" , & mut statement) ;
273
+ let result = execute_statement ( statement, & mut table) ;
274
+
275
+ if let ExecuteResult :: Success ( Some ( rows) ) = result {
276
+ assert_eq ! ( rows. len( ) , 3 ) ;
277
+ } else {
278
+ panic ! ( "Expected Success with three rows" ) ;
279
+ }
280
+ }
281
+
282
+ // Testing insert with duplicate ID
283
+ #[ test]
284
+ fn execute_insert_duplicate_id ( ) {
285
+ let mut table = Table :: new ( ) ;
286
+ do_sql_cmd ( & mut table
, "insert 13 rosh [email protected] " ) ;
287
+ do_sql_cmd ( & mut table
, "insert 13 stefan [email protected] " ) ;
288
+
289
+ // The second insert should be added as a new row
290
+ assert_eq ! (
291
+ table. data,
292
+ vec![
293
+ Row {
294
+ id: 13 ,
295
+ username: "rosh" . to_string( ) ,
296
+ email
: "[email protected] " . to_string
( )
297
+ } ,
298
+ Row {
299
+ id: 13 ,
300
+ username: "stefan" . to_string( ) ,
301
+ email
: "[email protected] " . to_string
( )
302
+ }
303
+ ]
304
+ ) ;
305
+ }
306
+
307
+ // Testing insert with zero ID
308
+ #[ test]
309
+ fn execute_insert_zero_id ( ) {
310
+ let mut table = Table :: new ( ) ;
311
+ do_sql_cmd ( & mut table
, "insert 0 rosh [email protected] " ) ;
312
+
313
+ assert_eq ! (
314
+ table. data,
315
+ vec![ Row {
316
+ id: 0 ,
317
+ username: "rosh" . to_string( ) ,
318
+ email
: "[email protected] " . to_string
( )
319
+ } ]
320
+ ) ;
321
+ }
322
+
323
+ // Testing insert with maximum u32 ID
324
+ #[ test]
325
+ fn execute_insert_max_id ( ) {
326
+ let mut table = Table :: new ( ) ;
327
+ do_sql_cmd ( & mut table
, "insert 4294967295 rosh [email protected] " ) ;
328
+
329
+ assert_eq ! (
330
+ table. data,
331
+ vec![ Row {
332
+ id: 4294967295 ,
333
+ username: "rosh" . to_string( ) ,
334
+ email
: "[email protected] " . to_string
( )
335
+ } ]
336
+ ) ;
337
+ }
338
+
339
+ // Testing select with zero ID
340
+ #[ test]
341
+ fn execute_select_zero_id ( ) {
342
+ let mut table = Table :: new ( ) ;
343
+ do_sql_cmd ( & mut table
, "insert 0 rosh [email protected] " ) ;
344
+
345
+ let mut statement = Statement :: default ( ) ;
346
+ prepare_statement ( "select 0" , & mut statement) ;
347
+ let result = execute_statement ( statement, & mut table) ;
348
+
349
+ if let ExecuteResult :: Success ( Some ( rows) ) = result {
350
+ assert_eq ! ( rows. len( ) , 1 ) ;
351
+ assert_eq ! ( rows[ 0 ] . id, 0 ) ;
352
+ } else {
353
+ panic ! ( "Expected Success with one row" ) ;
354
+ }
355
+ }
356
+
357
+ // Testing select with maximum u32 ID
358
+ #[ test]
359
+ fn execute_select_max_id ( ) {
360
+ let mut table = Table :: new ( ) ;
361
+ do_sql_cmd ( & mut table
, "insert 4294967295 rosh [email protected] " ) ;
362
+
363
+ let mut statement = Statement :: default ( ) ;
364
+ prepare_statement ( "select 4294967295" , & mut statement) ;
365
+ let result = execute_statement ( statement, & mut table) ;
366
+
367
+ if let ExecuteResult :: Success ( Some ( rows) ) = result {
368
+ assert_eq ! ( rows. len( ) , 1 ) ;
369
+ assert_eq ! ( rows[ 0 ] . id, 4294967295 ) ;
370
+ } else {
371
+ panic ! ( "Expected Success with one row" ) ;
372
+ }
373
+ }
374
+
375
+ // Testing insert with special characters in username
376
+ #[ test]
377
+ fn execute_insert_special_chars_username ( ) {
378
+ let mut table = Table :: new ( ) ;
379
+ do_sql_cmd ( & mut table
, "insert 13 rosh!@#$ [email protected] " ) ;
380
+
381
+ assert_eq ! (
382
+ table. data,
383
+ vec![ Row {
384
+ id: 13 ,
385
+ username: "rosh!@#$" . to_string( ) ,
386
+ email
: "[email protected] " . to_string
( )
387
+ } ]
388
+ ) ;
389
+ }
390
+
391
+ // Testing insert with special characters in email
392
+ #[ test]
393
+ fn execute_insert_special_chars_email ( ) {
394
+ let mut table = Table :: new ( ) ;
395
+ do_sql_cmd ( & mut table
, "insert 13 rosh [email protected] " ) ;
396
+
397
+ assert_eq ! (
398
+ table. data,
399
+ vec![ Row {
400
+ id: 13 ,
401
+ username: "rosh" . to_string( ) ,
402
+ email
: "[email protected] " . to_string
( )
403
+ } ]
404
+ ) ;
405
+ }
406
+
407
+ // Testing insert with very long username
408
+ #[ test]
409
+ fn execute_insert_long_username ( ) {
410
+ let mut table = Table :: new ( ) ;
411
+ let long_username = "a" . repeat ( 1000 ) ;
412
+ do_sql_cmd (
413
+ & mut table,
414
+ & format ! ( "insert 13 {} [email protected] " , long_username
) ,
415
+ ) ;
416
+
417
+ assert_eq ! (
418
+ table. data,
419
+ vec![ Row {
420
+ id: 13 ,
421
+ username: long_username,
422
+ email
: "[email protected] " . to_string
( )
423
+ } ]
424
+ ) ;
425
+ }
426
+
427
+ // Testing insert with very long email
428
+ #[ test]
429
+ fn execute_insert_long_email ( ) {
430
+ let mut table = Table :: new ( ) ;
431
+ let long_email = format ! ( "{}@gmail.com" , "a" . repeat( 1000 ) ) ;
432
+ do_sql_cmd ( & mut table, & format ! ( "insert 13 rosh {}" , long_email) ) ;
433
+
434
+ assert_eq ! (
435
+ table. data,
436
+ vec![ Row {
437
+ id: 13 ,
438
+ username: "rosh" . to_string( ) ,
439
+ email: long_email,
440
+ } ]
441
+ ) ;
442
+ }
443
+
444
+ // Testing multiple selects on the same table
445
+ #[ test]
446
+ fn execute_multiple_selects ( ) {
447
+ let mut table = Table :: new ( ) ;
448
+ do_sql_cmd ( & mut table
, "insert 13 rosh [email protected] " ) ;
449
+ do_sql_cmd ( & mut table
, "insert 42 stefan [email protected] " ) ;
450
+ do_sql_cmd (
451
+ & mut table,
452
+ "insert 1699 sniper_penut [email protected] " ,
453
+ ) ;
454
+
455
+ // Select all
456
+ let mut statement = Statement :: default ( ) ;
457
+ prepare_statement ( "select" , & mut statement) ;
458
+ let result = execute_statement ( statement, & mut table) ;
459
+
460
+ if let ExecuteResult :: Success ( Some ( rows) ) = result {
461
+ assert_eq ! ( rows. len( ) , 3 ) ;
462
+ } else {
463
+ panic ! ( "Expected Success with three rows" ) ;
464
+ }
465
+
466
+ // Select specific ID
467
+ let mut statement = Statement :: default ( ) ;
468
+ prepare_statement ( "select 42" , & mut statement) ;
469
+ let result = execute_statement ( statement, & mut table) ;
470
+
471
+ if let ExecuteResult :: Success ( Some ( rows) ) = result {
472
+ assert_eq ! ( rows. len( ) , 1 ) ;
473
+ assert_eq ! ( rows[ 0 ] . id, 42 ) ;
474
+ } else {
475
+ panic ! ( "Expected Success with one row" ) ;
476
+ }
477
+ }
478
+
479
+ // Testing insert and select with whitespace
480
+ #[ test]
481
+ fn execute_insert_select_whitespace ( ) {
482
+ let mut table = Table :: new ( ) ;
483
+ do_sql_cmd ( & mut table
, " insert 13 rosh [email protected] " ) ;
484
+
485
+ let mut statement = Statement :: default ( ) ;
486
+ prepare_statement ( " select 13 " , & mut statement) ;
487
+ let result = execute_statement ( statement, & mut table) ;
488
+
489
+ if let ExecuteResult :: Success ( Some ( rows) ) = result {
490
+ assert_eq ! ( rows. len( ) , 0 ) ;
491
+ } else {
492
+ panic ! ( "Expected Success with empty vector" ) ;
493
+ }
494
+ }
495
+
496
+ // Testing insert and select with tab characters
497
+ #[ test]
498
+ fn execute_insert_select_tab ( ) {
499
+ let mut table = Table :: new ( ) ;
500
+ do_sql_cmd ( & mut table
, "insert\t 13\t rosh\t [email protected] " ) ;
501
+
502
+ let mut statement = Statement :: default ( ) ;
503
+ prepare_statement ( "select\t 13" , & mut statement) ;
504
+ let result = execute_statement ( statement, & mut table) ;
505
+
506
+ if let ExecuteResult :: Success ( Some ( rows) ) = result {
507
+ assert_eq ! ( rows. len( ) , 1 ) ;
508
+ assert_eq ! ( rows[ 0 ] . id, 13 ) ;
509
+ } else {
510
+ panic ! ( "Expected Success with one row" ) ;
511
+ }
512
+ }
513
+
514
+ // Testing insert and select with newline characters
515
+ #[ test]
516
+ fn execute_insert_select_newline ( ) {
517
+ let mut table = Table :: new ( ) ;
518
+ do_sql_cmd ( & mut table
, "insert\n 13\n rosh\n [email protected] " ) ;
519
+
520
+ let mut statement = Statement :: default ( ) ;
521
+ prepare_statement ( "select\n 13" , & mut statement) ;
522
+ let result = execute_statement ( statement, & mut table) ;
523
+
524
+ if let ExecuteResult :: Success ( Some ( rows) ) = result {
525
+ assert_eq ! ( rows. len( ) , 1 ) ;
526
+ assert_eq ! ( rows[ 0 ] . id, 13 ) ;
527
+ } else {
528
+ panic ! ( "Expected Success with one row" ) ;
529
+ }
530
+ }
206
531
}
0 commit comments