@@ -547,9 +547,7 @@ test('return interval as string', async () => {
547547 const res = await app . inject ( {
548548 method : 'POST' ,
549549 path : '/query' ,
550- payload : {
551- query : `SELECT '1 day 1 hour 45 minutes'::interval` ,
552- } ,
550+ payload : { query : `SELECT '1 day 1 hour 45 minutes'::interval` } ,
553551 } )
554552 expect ( res . json ( ) ) . toMatchInlineSnapshot ( `
555553 [
@@ -703,9 +701,7 @@ test('error with internalQuery property', async () => {
703701 const res = await app . inject ( {
704702 method : 'POST' ,
705703 path : '/query' ,
706- payload : {
707- query : 'SELECT test_internal_query();' ,
708- } ,
704+ payload : { query : 'SELECT test_internal_query();' } ,
709705 } )
710706
711707 expect ( res . json ( ) ) . toMatchInlineSnapshot ( `
@@ -737,19 +733,107 @@ test('custom application_name', async () => {
737733 const res = await app . inject ( {
738734 method : 'POST' ,
739735 path : '/query' ,
740- headers : {
741- 'x-pg-application-name' : 'test' ,
742- } ,
736+ headers : { 'x-pg-application-name' : 'test' } ,
737+ payload : { query : 'SHOW application_name;' } ,
738+ } )
739+
740+ expect ( res . json ( ) ) . toMatchInlineSnapshot ( `
741+ [
742+ {
743+ "application_name": "test",
744+ },
745+ ]
746+ ` )
747+ } )
748+
749+ test ( 'parameter binding with positional parameters' , async ( ) => {
750+ const res = await app . inject ( {
751+ method : 'POST' ,
752+ path : '/query' ,
743753 payload : {
744- query : 'SHOW application_name;' ,
754+ query : 'SELECT * FROM users WHERE id = $1 AND status = $2' ,
755+ parameters : [ 1 , 'ACTIVE' ] ,
745756 } ,
746757 } )
758+ expect ( res . json ( ) ) . toMatchInlineSnapshot ( `
759+ [
760+ {
761+ "decimal": null,
762+ "id": 1,
763+ "name": "Joe Bloggs",
764+ "status": "ACTIVE",
765+ },
766+ ]
767+ ` )
768+ } )
747769
770+ test ( 'parameter binding with single parameter' , async ( ) => {
771+ const res = await app . inject ( {
772+ method : 'POST' ,
773+ path : '/query' ,
774+ payload : { query : 'SELECT name FROM users WHERE id = $1' , parameters : [ 2 ] } ,
775+ } )
748776 expect ( res . json ( ) ) . toMatchInlineSnapshot ( `
749777 [
750778 {
751- "application_name ": "test ",
779+ "name ": "Jane Doe ",
752780 },
753781 ]
754782 ` )
755783} )
784+
785+ test ( 'parameter binding with no matches' , async ( ) => {
786+ const res = await app . inject ( {
787+ method : 'POST' ,
788+ path : '/query' ,
789+ payload : { query : 'SELECT * FROM users WHERE id = $1' , parameters : [ 999 ] } ,
790+ } )
791+ expect ( res . json ( ) ) . toMatchInlineSnapshot ( `[]` )
792+ } )
793+
794+ test ( 'no parameters field' , async ( ) => {
795+ const res = await app . inject ( {
796+ method : 'POST' ,
797+ path : '/query' ,
798+ payload : { query : 'SELECT COUNT(*) as count FROM users' } ,
799+ } )
800+ expect ( res . json ( ) ) . toMatchInlineSnapshot ( `
801+ [
802+ {
803+ "count": 2,
804+ },
805+ ]
806+ ` )
807+ } )
808+
809+ test ( 'parameter binding with empty parameters array' , async ( ) => {
810+ const res = await app . inject ( {
811+ method : 'POST' ,
812+ path : '/query' ,
813+ payload : { query : 'SELECT COUNT(*) as count FROM users' , parameters : [ ] } ,
814+ } )
815+ expect ( res . json ( ) ) . toMatchInlineSnapshot ( `
816+ [
817+ {
818+ "count": 2,
819+ },
820+ ]
821+ ` )
822+ } )
823+
824+ test ( 'parameter binding error - wrong parameter count' , async ( ) => {
825+ const res = await app . inject ( {
826+ method : 'POST' ,
827+ path : '/query' ,
828+ payload : {
829+ query : 'SELECT * FROM users WHERE id = $1 AND status = $2' ,
830+ parameters : [ 1 ] , // Missing second parameter
831+ } ,
832+ } )
833+ expect ( res . statusCode ) . toBe ( 400 )
834+ const json = res . json ( )
835+ expect ( json . code ) . toBe ( '08P01' )
836+ expect ( json . message ) . toContain (
837+ 'bind message supplies 1 parameters, but prepared statement "" requires 2'
838+ )
839+ } )
0 commit comments