11import { Uninitialized , gen as genCommon } from '@wix-velo/test-commons'
22import { SchemaOperations } from '@wix-velo/velo-external-db-types'
3+ import { dataSpi } from '@wix-velo/velo-external-db-core'
34const { UpdateImmediately, DeleteImmediately, Truncate, Aggregate, FindWithSort, Projection } = SchemaOperations
45import { testIfSupportedOperationsIncludes } from '@wix-velo/test-commons'
56import * as gen from '../gen'
67import * as schema from '../drivers/schema_api_rest_test_support'
7- import * as data from '../drivers/data_api_rest_test_support'
88import * as matchers from '../drivers/schema_api_rest_matchers'
99import { authAdmin , authOwner , authVisitor } from '@wix-velo/external-db-testkit'
1010import * as authorization from '../drivers/authorization_test_support'
1111import Chance = require( 'chance' )
1212import { initApp , teardownApp , dbTeardown , setupDb , currentDbImplementationName , supportedOperations } from '../resources/e2e_resources'
13- import { Options , QueryRequest , QueryV2 , CountRequest , QueryResponsePart , UpdateRequest , TruncateRequest , RemoveRequest , RemoveResponsePart , InsertRequest , Group } from 'libs/velo-external-db-core/src/spi-model/data_source'
13+
14+
1415import axios from 'axios'
1516import { streamToArray } from '@wix-velo/test-commons'
1617
@@ -33,29 +34,29 @@ const queryRequest = (collectionName, sort, fields, filter?: any) => ({
3334 offset : 0 ,
3435 } ,
3536 cursorPaging : null
36- } as QueryV2 ,
37+ } as dataSpi . QueryV2 ,
3738 includeReferencedItems : [ ] ,
3839 options : {
3940 consistentRead : false ,
4041 appOptions : { } ,
41- } as Options ,
42+ } as dataSpi . Options ,
4243 omitTotalCount : false
43- } as QueryRequest )
44+ } as dataSpi . QueryRequest )
4445
4546
4647
4748const queryCollectionAsArray = ( collectionName , sort , fields , filter ?: any ) => axiosInstance . post ( '/data/query' ,
4849 queryRequest ( collectionName , sort , fields , filter ) ,
49- { responseType : 'stream' , transformRequest : authVisitor . transformRequest } ) . then ( response => streamToArray ( response . data ) )
50+ { responseType : 'stream' , transformRequest : authVisitor . transformRequest } ) . then ( response => streamToArray ( response . data ) )
5051
5152const countRequest = ( collectionName ) => ( {
5253 collectionId : collectionName ,
5354 filter : '' ,
5455 options : {
5556 consistentRead : false ,
5657 appOptions : { } ,
57- } as Options ,
58- } ) as CountRequest
58+ } as dataSpi . Options ,
59+ } ) as dataSpi . CountRequest
5960
6061const updateRequest = ( collectionName , items ) => ( {
6162 // collection name
@@ -67,8 +68,8 @@ const updateRequest = (collectionName, items) => ({
6768 options : {
6869 consistentRead : false ,
6970 appOptions : { } ,
70- } as Options ,
71- } ) as UpdateRequest
71+ } as dataSpi . Options ,
72+ } ) as dataSpi . UpdateRequest
7273
7374const insertRequest = ( collectionName , items , overwriteExisting ) => ( {
7475 collectionId : collectionName ,
@@ -77,13 +78,13 @@ const insertRequest = (collectionName, items, overwriteExisting) => ({
7778 options : {
7879 consistentRead : false ,
7980 appOptions : { } ,
80- } as Options ,
81- } as InsertRequest )
81+ } as dataSpi . Options ,
82+ } as dataSpi . InsertRequest )
8283
83- const givenItems = async ( items , collectionName , auth ) =>
84- axiosInstance . post ( '/data/insert' , insertRequest ( collectionName , items , false ) , { responseType : 'stream' , transformRequest : auth . transformRequest } )
84+ const givenItems = async ( items , collectionName , auth ) =>
85+ axiosInstance . post ( '/data/insert' , insertRequest ( collectionName , items , false ) , { responseType : 'stream' , transformRequest : auth . transformRequest } )
8586
86- const pagingMetadata = ( total , count ) => ( { pagingMetadata : { count : count , offset :0 , total : total , tooManyToCount : false } } as QueryResponsePart )
87+ const pagingMetadata = ( total , count ) => ( { pagingMetadata : { count : count , offset : 0 , total : total , tooManyToCount : false } } as dataSpi . QueryResponsePart )
8788
8889describe ( `Velo External DB Data REST API: ${ currentDbImplementationName ( ) } ` , ( ) => {
8990 beforeAll ( async ( ) => {
@@ -100,7 +101,7 @@ describe(`Velo External DB Data REST API: ${currentDbImplementationName()}`, ()
100101 await givenItems ( [ ctx . item , ctx . anotherItem ] , ctx . collectionName , authAdmin )
101102 await authorization . givenCollectionWithVisitorReadPolicy ( ctx . collectionName )
102103
103- const itemsByOrder = [ ctx . item , ctx . anotherItem ] . sort ( ( a , b ) => ( a [ ctx . column . name ] > b [ ctx . column . name ] ) ? 1 : - 1 ) . map ( item => ( { item} ) )
104+ const itemsByOrder = [ ctx . item , ctx . anotherItem ] . sort ( ( a , b ) => ( a [ ctx . column . name ] > b [ ctx . column . name ] ) ? 1 : - 1 ) . map ( item => ( { item } ) )
104105
105106 await expect ( queryCollectionAsArray ( ctx . collectionName , [ { fieldName : ctx . column . name , order : 'ASC' } ] , undefined ) ) . resolves . toEqual (
106107 ( [ ...itemsByOrder , pagingMetadata ( 2 , 2 ) ] )
@@ -110,14 +111,9 @@ describe(`Velo External DB Data REST API: ${currentDbImplementationName()}`, ()
110111 testIfSupportedOperationsIncludes ( supportedOperations , [ Projection ] ) ( 'find api with projection' , async ( ) => {
111112 await schema . givenCollection ( ctx . collectionName , [ ctx . column ] , authOwner )
112113 await givenItems ( [ ctx . item ] , ctx . collectionName , authAdmin )
113-
114- const response = await axiosInstance . post ( '/data/query' ,
115- queryRequest ( ctx . collectionName , [ ] , [ ctx . column . name ] ) ,
116- { responseType : 'stream' , transformRequest : authVisitor . transformRequest }
117- )
118-
114+
119115 await expect ( queryCollectionAsArray ( ctx . collectionName , [ ] , [ ctx . column . name ] ) ) . resolves . toEqual (
120- expect . arrayContaining ( [ { item : { [ ctx . column . name ] : ctx . item [ ctx . column . name ] } } , pagingMetadata ( 1 , 1 ) ] )
116+ expect . arrayContaining ( [ { item : { [ ctx . column . name ] : ctx . item [ ctx . column . name ] } } , pagingMetadata ( 1 , 1 ) ] )
121117 )
122118 } )
123119
@@ -126,9 +122,9 @@ describe(`Velo External DB Data REST API: ${currentDbImplementationName()}`, ()
126122 test ( 'insert api' , async ( ) => {
127123 await schema . givenCollection ( ctx . collectionName , [ ctx . column ] , authOwner )
128124
129- const response = await axiosInstance . post ( '/data/insert' , insertRequest ( ctx . collectionName , ctx . items , false ) , { responseType : 'stream' , transformRequest : authAdmin . transformRequest } )
125+ const response = await axiosInstance . post ( '/data/insert' , insertRequest ( ctx . collectionName , ctx . items , false ) , { responseType : 'stream' , transformRequest : authAdmin . transformRequest } )
130126
131- const expectedItems = ctx . items . map ( item => ( { item : item } as QueryResponsePart ) )
127+ const expectedItems = ctx . items . map ( item => ( { item : item } as dataSpi . QueryResponsePart ) )
132128
133129 await expect ( streamToArray ( response . data ) ) . resolves . toEqual ( expectedItems )
134130 await expect ( queryCollectionAsArray ( ctx . collectionName , [ ] , undefined ) ) . resolves . toEqual ( expect . arrayContaining (
@@ -143,9 +139,9 @@ describe(`Velo External DB Data REST API: ${currentDbImplementationName()}`, ()
143139 await schema . givenCollection ( ctx . collectionName , [ ctx . column ] , authOwner )
144140 await givenItems ( [ ctx . items [ 0 ] ] , ctx . collectionName , authAdmin )
145141
146- const response = axiosInstance . post ( '/data/insert' , insertRequest ( ctx . collectionName , ctx . items , false ) , { responseType : 'stream' , transformRequest : authAdmin . transformRequest } )
142+ const response = axiosInstance . post ( '/data/insert' , insertRequest ( ctx . collectionName , ctx . items , false ) , { responseType : 'stream' , transformRequest : authAdmin . transformRequest } )
147143
148- const expectedItems = [ QueryResponsePart . item ( ctx . items [ 0 ] ) ]
144+ const expectedItems = [ dataSpi . QueryResponsePart . item ( ctx . items [ 0 ] ) ]
149145
150146 await expect ( response ) . rejects . toThrow ( '400' )
151147
@@ -161,8 +157,8 @@ describe(`Velo External DB Data REST API: ${currentDbImplementationName()}`, ()
161157 await schema . givenCollection ( ctx . collectionName , [ ctx . column ] , authOwner )
162158 await givenItems ( [ ctx . item ] , ctx . collectionName , authAdmin )
163159
164- const response = await axiosInstance . post ( '/data/insert' , insertRequest ( ctx . collectionName , [ ctx . modifiedItem ] , true ) , { responseType : 'stream' , transformRequest : authAdmin . transformRequest } )
165- const expectedItems = [ QueryResponsePart . item ( ctx . modifiedItem ) ]
160+ const response = await axiosInstance . post ( '/data/insert' , insertRequest ( ctx . collectionName , [ ctx . modifiedItem ] , true ) , { responseType : 'stream' , transformRequest : authAdmin . transformRequest } )
161+ const expectedItems = [ dataSpi . QueryResponsePart . item ( ctx . modifiedItem ) ]
166162
167163 await expect ( streamToArray ( response . data ) ) . resolves . toEqual ( expectedItems )
168164 await expect ( queryCollectionAsArray ( ctx . collectionName , [ ] , undefined ) ) . resolves . toEqual ( expect . arrayContaining (
@@ -192,7 +188,7 @@ describe(`Velo External DB Data REST API: ${currentDbImplementationName()}`, ()
192188 sum : ctx . numberColumns [ 1 ] . name
193189 }
194190 ]
195- } as Group ,
191+ } as dataSpi . Group ,
196192 finalFilter : {
197193 $and : [
198194 { myAvg : { $gt : 0 } } ,
@@ -202,12 +198,12 @@ describe(`Velo External DB Data REST API: ${currentDbImplementationName()}`, ()
202198 } , { responseType : 'stream' , ...authAdmin } )
203199
204200 await expect ( streamToArray ( response . data ) ) . resolves . toEqual (
205- expect . arrayContaining ( [ { item : {
201+ expect . arrayContaining ( [ { item : {
206202 _id : ctx . numberItem . _id ,
207203 _owner : ctx . numberItem . _owner ,
208204 myAvg : ctx . numberItem [ ctx . numberColumns [ 0 ] . name ] ,
209205 mySum : ctx . numberItem [ ctx . numberColumns [ 1 ] . name ]
210- } } ,
206+ } } ,
211207 pagingMetadata ( 1 , 1 )
212208 ] ) )
213209 } )
@@ -218,9 +214,9 @@ describe(`Velo External DB Data REST API: ${currentDbImplementationName()}`, ()
218214
219215 const response = await axiosInstance . post ( '/data/remove' , {
220216 collectionId : ctx . collectionName , itemIds : ctx . items . map ( ( i : { _id : any } ) => i . _id )
221- } as RemoveRequest , { responseType : 'stream' , transformRequest : authAdmin . transformRequest } )
217+ } as dataSpi . RemoveRequest , { responseType : 'stream' , transformRequest : authAdmin . transformRequest } )
222218
223- const expectedItems = ctx . items . map ( item => ( { item : item } as RemoveResponsePart ) )
219+ const expectedItems = ctx . items . map ( item => ( { item : item } as dataSpi . RemoveResponsePart ) )
224220
225221 await expect ( streamToArray ( response . data ) ) . resolves . toEqual ( expect . arrayContaining ( expectedItems ) )
226222 await expect ( queryCollectionAsArray ( ctx . collectionName , [ ] , undefined ) ) . resolves . toEqual ( [ pagingMetadata ( 0 , 0 ) ] )
@@ -231,11 +227,11 @@ describe(`Velo External DB Data REST API: ${currentDbImplementationName()}`, ()
231227 await givenItems ( [ ctx . item ] , ctx . collectionName , authAdmin )
232228
233229 const filter = {
234- _id : { $eq : ctx . item . _id }
230+ _id : { $eq : ctx . item . _id }
235231 }
236232
237233 await expect ( queryCollectionAsArray ( ctx . collectionName , undefined , undefined , filter ) ) . resolves . toEqual (
238- ( [ ...[ QueryResponsePart . item ( ctx . item ) ] , pagingMetadata ( 1 , 1 ) ] )
234+ ( [ ...[ dataSpi . QueryResponsePart . item ( ctx . item ) ] , pagingMetadata ( 1 , 1 ) ] )
239235 )
240236 } )
241237
@@ -244,7 +240,7 @@ describe(`Velo External DB Data REST API: ${currentDbImplementationName()}`, ()
244240 await givenItems ( [ ctx . item ] , ctx . collectionName , authAdmin )
245241
246242 const filter = {
247- _id : { $eq : 'wrong' }
243+ _id : { $eq : 'wrong' }
248244 }
249245
250246 await expect ( queryCollectionAsArray ( ctx . collectionName , undefined , undefined , filter ) ) . resolves . toEqual (
@@ -257,27 +253,27 @@ describe(`Velo External DB Data REST API: ${currentDbImplementationName()}`, ()
257253 await givenItems ( [ ctx . item ] , ctx . collectionName , authAdmin )
258254
259255 const filter = {
260- _id : { $eq : ctx . item . _id }
256+ _id : { $eq : ctx . item . _id }
261257 }
262258
263259 await expect ( queryCollectionAsArray ( ctx . collectionName , undefined , [ ctx . column . name ] , filter ) ) . resolves . toEqual (
264- ( [ QueryResponsePart . item ( { [ ctx . column . name ] : ctx . item [ ctx . column . name ] } ) , pagingMetadata ( 1 , 1 ) ] )
260+ ( [ dataSpi . QueryResponsePart . item ( { [ ctx . column . name ] : ctx . item [ ctx . column . name ] } ) , pagingMetadata ( 1 , 1 ) ] )
265261 )
266262 } )
267263
268264 testIfSupportedOperationsIncludes ( supportedOperations , [ UpdateImmediately ] ) ( 'update api' , async ( ) => {
269265 await schema . givenCollection ( ctx . collectionName , [ ctx . column ] , authOwner )
270266 await givenItems ( ctx . items , ctx . collectionName , authAdmin )
271- const response = await axiosInstance . post ( '/data/update' , updateRequest ( ctx . collectionName , ctx . modifiedItems ) , { responseType : 'stream' , transformRequest : authAdmin . transformRequest } )
267+ const response = await axiosInstance . post ( '/data/update' , updateRequest ( ctx . collectionName , ctx . modifiedItems ) , { responseType : 'stream' , transformRequest : authAdmin . transformRequest } )
272268
273- const expectedItems = ctx . modifiedItems . map ( item => ( { item : item } as QueryResponsePart ) )
269+ const expectedItems = ctx . modifiedItems . map ( item => ( { item : item } as dataSpi . QueryResponsePart ) )
274270
275271 await expect ( streamToArray ( response . data ) ) . resolves . toEqual ( expectedItems )
276272
277273 await expect ( queryCollectionAsArray ( ctx . collectionName , [ ] , undefined ) ) . resolves . toEqual ( expect . arrayContaining (
278274 [
279275 ...expectedItems ,
280- pagingMetadata ( ctx . modifiedItems . length , ctx . modifiedItems . length )
276+ pagingMetadata ( ctx . modifiedItems . length , ctx . modifiedItems . length )
281277 ] ) )
282278 } )
283279
@@ -291,7 +287,7 @@ describe(`Velo External DB Data REST API: ${currentDbImplementationName()}`, ()
291287 testIfSupportedOperationsIncludes ( supportedOperations , [ Truncate ] ) ( 'truncate api' , async ( ) => {
292288 await schema . givenCollection ( ctx . collectionName , [ ctx . column ] , authOwner )
293289 await givenItems ( [ ctx . item , ctx . anotherItem ] , ctx . collectionName , authAdmin )
294- await axiosInstance . post ( '/data/truncate' , { collectionId : ctx . collectionName } as TruncateRequest , authAdmin )
290+ await axiosInstance . post ( '/data/truncate' , { collectionId : ctx . collectionName } as dataSpi . TruncateRequest , authAdmin )
295291 await expect ( queryCollectionAsArray ( ctx . collectionName , [ ] , undefined ) ) . resolves . toEqual ( [ pagingMetadata ( 0 , 0 ) ] )
296292 } )
297293
0 commit comments