@@ -5,19 +5,20 @@ import * as _ from 'lodash';
55
66import {
77 fetchDatabases ,
8- mergeDatabases
8+ mergeDatabases ,
9+ connectToDatabase
910} from 'lib/static/modules/sqlite' ;
1011
1112describe ( 'lib/static/modules/sqlite' , ( ) => {
1213 const sandbox = sinon . sandbox . create ( ) ;
1314
14- describe ( 'fetchDatabases' , ( ) => {
15- beforeEach ( ( ) => {
16- sandbox . stub ( axios , 'get' ) . resolves ( ) ;
17- } ) ;
15+ beforeEach ( ( ) => {
16+ sandbox . stub ( axios , 'get' ) . resolves ( ) ;
17+ } ) ;
1818
19- afterEach ( ( ) => sandbox . restore ( ) ) ;
19+ afterEach ( ( ) => sandbox . restore ( ) ) ;
2020
21+ describe ( 'fetchDatabases' , ( ) => {
2122 it ( 'should return empty arrays if dbUrls.json not contain useful data' , async ( ) => {
2223 axios . get . resolves ( {
2324 status : 200 ,
@@ -182,56 +183,80 @@ describe('lib/static/modules/sqlite', () => {
182183
183184 afterEach ( ( ) => {
184185 global . window = undefined ;
185- sandbox . restore ( ) ;
186186 } ) ;
187187
188- describe ( 'mergeDatabases' , ( ) => {
189- it ( 'should return null if dataForDbs is empty' , async ( ) => {
190- const mergedDbConnection = await mergeDatabases ( [ ] ) ;
188+ it ( 'should return null if dataForDbs is empty' , async ( ) => {
189+ const mergedDbConnection = await mergeDatabases ( [ ] ) ;
191190
192- assert . equal ( mergedDbConnection , null ) ;
193- } ) ;
191+ assert . equal ( mergedDbConnection , null ) ;
192+ } ) ;
194193
195- it ( 'should not create unnecessary databases if dataForDbs contain data for single db' , async ( ) => {
196- const data = new ArrayBuffer ( 1 ) ;
194+ it ( 'should not create unnecessary databases if dataForDbs contain data for single db' , async ( ) => {
195+ const data = new ArrayBuffer ( 1 ) ;
197196
198- const mergedDbConnection = await mergeDatabases ( [ data ] ) ;
197+ const mergedDbConnection = await mergeDatabases ( [ data ] ) ;
199198
200- assert . instanceOf ( mergedDbConnection , SQL . Database ) ;
201- assert . calledOnceWith ( DatabaseConstructorSpy , new Uint8Array ( 1 ) ) ;
202- assert . notCalled ( SQL . Database . prototype . run ) ;
203- assert . notCalled ( SQL . Database . prototype . close ) ;
204- } ) ;
199+ assert . instanceOf ( mergedDbConnection , SQL . Database ) ;
200+ assert . calledOnceWith ( DatabaseConstructorSpy , new Uint8Array ( 1 ) ) ;
201+ assert . notCalled ( SQL . Database . prototype . run ) ;
202+ assert . notCalled ( SQL . Database . prototype . close ) ;
203+ } ) ;
205204
206- it ( 'should merge several chunk databases into one' , async ( ) => {
207- const chunkSize1 = 1 ;
208- const chunkSize2 = 2 ;
209- const sumOfChunkSizes = chunkSize1 + chunkSize2 ;
210- const data1 = new ArrayBuffer ( chunkSize1 ) ;
211- const data2 = new ArrayBuffer ( chunkSize2 ) ;
212-
213- const mergedDbConnection = await mergeDatabases ( [ data1 , data2 ] ) ;
214-
215- assert . instanceOf ( mergedDbConnection , SQL . Database ) ;
216- assert . calledThrice ( DatabaseConstructorSpy ) ;
217- assert . calledWith ( DatabaseConstructorSpy , new Uint8Array ( chunkSize1 ) ) ;
218- assert . calledWith ( DatabaseConstructorSpy , new Uint8Array ( chunkSize2 ) ) ;
219- assert . calledWith ( DatabaseConstructorSpy , undefined , sumOfChunkSizes ) ;
220- assert . calledTwice ( SQL . Database . prototype . close ) ;
221- } ) ;
205+ it ( 'should merge several chunk databases into one' , async ( ) => {
206+ const chunkSize1 = 1 ;
207+ const chunkSize2 = 2 ;
208+ const sumOfChunkSizes = chunkSize1 + chunkSize2 ;
209+ const data1 = new ArrayBuffer ( chunkSize1 ) ;
210+ const data2 = new ArrayBuffer ( chunkSize2 ) ;
211+
212+ const mergedDbConnection = await mergeDatabases ( [ data1 , data2 ] ) ;
213+
214+ assert . instanceOf ( mergedDbConnection , SQL . Database ) ;
215+ assert . calledThrice ( DatabaseConstructorSpy ) ;
216+ assert . calledWith ( DatabaseConstructorSpy , new Uint8Array ( chunkSize1 ) ) ;
217+ assert . calledWith ( DatabaseConstructorSpy , new Uint8Array ( chunkSize2 ) ) ;
218+ assert . calledWith ( DatabaseConstructorSpy , undefined , sumOfChunkSizes ) ;
219+ assert . calledTwice ( SQL . Database . prototype . close ) ;
220+ } ) ;
222221
223- it ( 'should merge both "suites" tables' , async ( ) => {
224- const data1 = new ArrayBuffer ( 1 ) ;
225- const data2 = new ArrayBuffer ( 1 ) ;
222+ it ( 'should merge both "suites" tables' , async ( ) => {
223+ const data1 = new ArrayBuffer ( 1 ) ;
224+ const data2 = new ArrayBuffer ( 1 ) ;
226225
227- await mergeDatabases ( [ data1 , data2 ] ) ;
226+ await mergeDatabases ( [ data1 , data2 ] ) ;
228227
229- const rawQueries = _
230- . flatten ( SQL . Database . prototype . run . args )
231- . join ( ' ' ) ;
228+ const rawQueries = _
229+ . flatten ( SQL . Database . prototype . run . args )
230+ . join ( ' ' ) ;
232231
233- assert . include ( rawQueries , 'suites' ) ;
234- } ) ;
232+ assert . include ( rawQueries , 'suites' ) ;
233+ } ) ;
234+ } ) ;
235+
236+ describe ( 'connectToDatabase' , ( ) => {
237+ let SQL , Database ;
238+
239+ beforeEach ( ( ) => {
240+ Database = function Database ( ) { } ;
241+ SQL = { Database} ;
242+
243+ global . window = {
244+ initSqlJs : sandbox . stub ( ) . resolves ( SQL )
245+ } ;
246+ } ) ;
247+
248+ afterEach ( ( ) => {
249+ global . window = undefined ;
250+ } ) ;
251+
252+ it ( 'should return connection to database' , async ( ) => {
253+ axios . get
254+ . withArgs ( 'http://127.0.0.1:8080/sqlite.db' , { responseType : 'arraybuffer' } )
255+ . resolves ( { status : 200 , data : 'stub buffer' } ) ;
256+
257+ const db = await connectToDatabase ( 'http://127.0.0.1:8080/sqlite.db' ) ;
258+
259+ assert . instanceOf ( db , Database ) ;
235260 } ) ;
236261 } ) ;
237262} ) ;
0 commit comments