@@ -9,8 +9,7 @@ describe('Test "rest_api" filter', () => {
99 extensions : { rest_api : path . join ( __dirname , '..' , 'src' ) } ,
1010 } ) ;
1111
12- const sql = `{% set result = 1990 %}SELECT {{ result |
13- rest_api(url='', arg='begin_date') }}` ;
12+ const sql = `{% set result = 1990 %}SELECT {{ result | rest_api(url='', arg='begin_date') }}` ;
1413
1514 // Act
1615 await compileAndLoad ( sql ) ;
@@ -24,8 +23,22 @@ describe('Test "rest_api" filter', () => {
2423 ) ;
2524
2625 it (
27- 'The call API extensions created by createFilter function should work with template engine' ,
26+ 'The rest_api function should work with template engine' ,
2827 async ( ) => {
28+ const { compileAndLoad, execute, getExecutedQueries, getCreatedBinding } = await getTestCompiler ( {
29+ extensions : { rest_api : path . join ( __dirname , '..' , 'src' ) } ,
30+ } ) ;
31+
32+ const sql = `{% set result = 1990 %}SELECT {{ result | rest_api(url='http://localhost:3000/api/artists', arg='begin_date') }}` ;
33+
34+ // Act
35+ await compileAndLoad ( sql ) ;
36+ await execute ( { } ) ;
37+
38+ // Assert
39+ const queries = await getExecutedQueries ( ) ;
40+ const bindings = await getCreatedBinding ( ) ;
41+
2942 const expected = JSON . stringify (
3043 [
3144 {
@@ -141,12 +154,20 @@ describe('Test "rest_api" filter', () => {
141154 ]
142155 ) ;
143156
157+ expect ( queries [ 0 ] ) . toBe ( 'SELECT $1' ) ;
158+ expect ( bindings [ 0 ] . get ( '$1' ) ) . toEqual ( expected ) ;
159+ } ,
160+ 50 * 1000
161+ ) ;
162+
163+ it (
164+ 'The rest_api function should work with HTTP Post request' ,
165+ async ( ) => {
144166 const { compileAndLoad, execute, getExecutedQueries, getCreatedBinding } = await getTestCompiler ( {
145167 extensions : { rest_api : path . join ( __dirname , '..' , 'src' ) } ,
146168 } ) ;
147169
148- const sql = `{% set result = 1990 %}SELECT {{ result |
149- rest_api(url='http://localhost:3000/api/artists', arg='begin_date') }}` ;
170+ const sql = `{% set body = '{"title": "BMW Pencil"}' %}SELECT {{ body | rest_api(url='https://dummyjson.com/products/add', method='POST', arg='body') }}` ;
150171
151172 // Act
152173 await compileAndLoad ( sql ) ;
@@ -156,9 +177,13 @@ describe('Test "rest_api" filter', () => {
156177 const queries = await getExecutedQueries ( ) ;
157178 const bindings = await getCreatedBinding ( ) ;
158179
180+ const expected = JSON . stringify ( {
181+ "id" : 101
182+ } ) ;
183+
159184 expect ( queries [ 0 ] ) . toBe ( 'SELECT $1' ) ;
160185 expect ( bindings [ 0 ] . get ( '$1' ) ) . toEqual ( expected ) ;
161186 } ,
162187 50 * 1000
163- ) ;
188+ )
164189} ) ;
0 commit comments