@@ -2,6 +2,7 @@ import 'package:collection/collection.dart';
22import  'package:postgrest/postgrest.dart' ;
33import  'package:test/test.dart' ;
44
5+ import  'custom_http_client.dart' ;
56import  'reset_helper.dart' ;
67
78void  main () {
@@ -343,4 +344,138 @@ void main() {
343344    expect (res, isNotNull);
344345    expect (res['type' ], 'FeatureCollection' );
345346  });
347+ 
348+   group ('maxAffected' , () {
349+     test ('maxAffected method can be called on update operations' , () {
350+       expect (
351+         () =>  postgrest
352+             .from ('users' )
353+             .update ({'status' :  'INACTIVE' })
354+             .eq ('id' , 1 )
355+             .maxAffected (1 ),
356+         returnsNormally,
357+       );
358+     });
359+ 
360+     test ('maxAffected method can be called on delete operations' , () {
361+       expect (
362+         () =>  postgrest.from ('channels' ).delete ().eq ('id' , 999 ).maxAffected (5 ),
363+         returnsNormally,
364+       );
365+     });
366+ 
367+     test ('maxAffected method can be called on select operations' , () {
368+       expect (
369+         () =>  postgrest.from ('users' ).select ().maxAffected (1 ),
370+         returnsNormally,
371+       );
372+     });
373+ 
374+     test ('maxAffected method can be called on insert operations' , () {
375+       expect (
376+         () => 
377+             postgrest.from ('users' ).insert ({'username' :  'test' }).maxAffected (1 ),
378+         returnsNormally,
379+       );
380+     });
381+ 
382+     test ('maxAffected method can be chained with select' , () {
383+       expect (
384+         () =>  postgrest
385+             .from ('users' )
386+             .update ({'status' :  'INACTIVE' })
387+             .eq ('id' , 1 )
388+             .maxAffected (1 )
389+             .select (),
390+         returnsNormally,
391+       );
392+     });
393+   });
394+ 
395+   group ('maxAffected integration' , () {
396+     late  CustomHttpClient  customHttpClient;
397+     late  PostgrestClient  postgrestCustomHttpClient;
398+ 
399+     setUp (() {
400+       customHttpClient =  CustomHttpClient ();
401+       postgrestCustomHttpClient =  PostgrestClient (
402+         rootUrl,
403+         httpClient:  customHttpClient,
404+       );
405+     });
406+ 
407+     test ('maxAffected sets correct headers for update' , () async  {
408+       try  {
409+         await  postgrestCustomHttpClient
410+             .from ('users' )
411+             .update ({'status' :  'INACTIVE' })
412+             .eq ('id' , 1 )
413+             .maxAffected (5 );
414+       } catch  (_) {
415+         // Expected to fail with custom client, we just want to check headers 
416+       }
417+ 
418+       expect (customHttpClient.lastRequest, isNotNull);
419+       expect (customHttpClient.lastRequest! .headers['Prefer' ], isNotNull);
420+       expect (customHttpClient.lastRequest! .headers['Prefer' ],
421+           contains ('handling=strict' ));
422+       expect (customHttpClient.lastRequest! .headers['Prefer' ],
423+           contains ('max-affected=5' ));
424+     });
425+ 
426+     test ('maxAffected sets correct headers for delete' , () async  {
427+       try  {
428+         await  postgrestCustomHttpClient
429+             .from ('users' )
430+             .delete ()
431+             .eq ('id' , 1 )
432+             .maxAffected (10 );
433+       } catch  (_) {
434+         // Expected to fail with custom client, we just want to check headers 
435+       }
436+ 
437+       expect (customHttpClient.lastRequest, isNotNull);
438+       expect (customHttpClient.lastRequest! .headers['Prefer' ], isNotNull);
439+       expect (customHttpClient.lastRequest! .headers['Prefer' ],
440+           contains ('handling=strict' ));
441+       expect (customHttpClient.lastRequest! .headers['Prefer' ],
442+           contains ('max-affected=10' ));
443+     });
444+ 
445+     test ('maxAffected preserves existing Prefer headers' , () async  {
446+       try  {
447+         await  postgrestCustomHttpClient
448+             .from ('users' )
449+             .update ({'status' :  'INACTIVE' })
450+             .eq ('id' , 1 )
451+             .select ()
452+             .maxAffected (3 );
453+       } catch  (_) {
454+         // Expected to fail with custom client, we just want to check headers 
455+       }
456+ 
457+       expect (customHttpClient.lastRequest, isNotNull);
458+       final  preferHeader =  customHttpClient.lastRequest! .headers['Prefer' ]! ;
459+       expect (preferHeader, contains ('return=representation' ));
460+       expect (preferHeader, contains ('handling=strict' ));
461+       expect (preferHeader, contains ('max-affected=3' ));
462+     });
463+ 
464+     test (
465+         'maxAffected works with select operations (sets headers but likely ineffective)' ,
466+         () async  {
467+       try  {
468+         await  postgrestCustomHttpClient.from ('users' ).select ().maxAffected (2 );
469+       } catch  (_) {
470+         // Expected to fail with custom client, we just want to check headers 
471+       }
472+ 
473+       expect (customHttpClient.lastRequest, isNotNull);
474+       expect (customHttpClient.lastRequest! .headers['Prefer' ], isNotNull);
475+       expect (customHttpClient.lastRequest! .headers['Prefer' ],
476+           contains ('handling=strict' ));
477+       expect (customHttpClient.lastRequest! .headers['Prefer' ],
478+           contains ('max-affected=2' ));
479+     });
480+   });
346481}
0 commit comments