22 * reserved-commands.test.ts - test sqlitecloud reserved commands
33 */
44
5- import { _ , getConnection , test , CHINOOK_API_KEY , date , parseconnectionstring , CHINOOK_DATABASE_URL , uuid , ip , randomBool , randomDate } from './shared'
6-
7- jest . retryTimes ( 3 )
5+ import {
6+ _ ,
7+ getConnection ,
8+ test ,
9+ CHINOOK_API_KEY ,
10+ date ,
11+ parseconnectionstring ,
12+ CHINOOK_DATABASE_URL ,
13+ uuid ,
14+ ip ,
15+ randomBool ,
16+ randomDate ,
17+ randomName
18+ } from './shared'
19+ import fs from 'fs'
20+ import path from 'path'
21+
22+ //jest.retryTimes(3)
823
924describe . each ( [
1025 [ 'example.com' , 'chinook.sqlite' , 'artists' , 3 , _ , 'example' , true ] ,
@@ -16,6 +31,7 @@ describe.each([
1631 [ _ , _ , _ , 2 , _ , _ , false ]
1732] ) ( 'webhook' , ( url_or_code , database , table , mask , options , secret , ok ) => {
1833 let generated_secret = ''
34+ let id = 0
1935
2036 it ( `should${ ok ? '' : "n't" } add` , done => {
2137 const chinook = getConnection ( )
@@ -33,8 +49,6 @@ describe.each([
3349 )
3450 } )
3551
36- let id = 0
37-
3852 it ( `should${ ok ? '' : "n't" } list` , done => {
3953 const chinook = getConnection ( )
4054 chinook . sendCommands (
@@ -1752,95 +1766,166 @@ describe.each([
17521766
17531767 chinook . sendCommands ( `BLOB CLOSE ${ index } ` , test ( done , chinook , ok ) )
17541768 }
1755- : test ( done , chinook , ok )
1769+ : chinook2 . close ( ) && test ( done , chinook , ok )
17561770 )
17571771 } )
17581772} )
17591773
17601774describe . each ( [
1761- [ 'chinook.sqlite ' , true ] ,
1762- [ _ , false ]
1775+ [ 'northwind.db ' , true ]
1776+ // [_, false]
17631777] ) ( 'upload database' , ( database , ok ) => {
17641778 it ( `should fail to start upload database` , done => {
17651779 const chinook = getConnection ( )
17661780 chinook . sendCommands ( `UPLOAD DATABASE chinook.sqlite` , test ( done , chinook , false ) )
17671781 } )
17681782
1769- /* it(`should${ok ? '' : "n't"} upload database`, done => {
1783+ //fails because we need to handle blobs in the driver
1784+ it . skip ( `should${ ok ? '' : "n't" } upload database` , async ( ) => {
17701785 const chinook = getConnection ( )
1771- chinook.sendCommands(
1772- `UPLOAD DATABASE ${database}`,
1773- ok
1774- ? (e, r) => {
1775- expect(e).toBeNull()
1776- expect(r).toEqual([expect.any(Number), expect.any(Number), expect.any(Number)]) //could fail??
1777- r.forEach((v: number) => expect(v).toBeGreaterThan(0))
1778- for (let i = 0; i < r.length - 1; i++) {
1779- chinook.sendCommands(
1780- `DOWNLOAD STEP`,
1781- i != r.length - 2
1782- ? (e, r) => {
1783- expect(e).toBeNull()
1784- expect(r).toBeInstanceOf(Buffer)
1785- }
1786- : test(done, chinook, ok, expect.any(Buffer))
1787- )
1788- }
1786+
1787+ const upload_database = await chinook . sql ( `UPLOAD DATABASE ${ database } REPLACE` )
1788+ expect ( upload_database ) . toBe ( 'OK' )
1789+ console . log ( 'UPLOAD DATABASE' , upload_database )
1790+
1791+ const fileStream = fs . createReadStream ( path . join ( __dirname , '../assets/' , database as string ) )
1792+
1793+ async function uploader ( connection : any , sql : string | ArrayBuffer ) : Promise < unknown > {
1794+ return await new Promise ( ( resolve , reject ) => {
1795+ // console.debug(`sendCommandsAsync - ${sql}`)
1796+ connection . sendCommands ( [ sql ] , ( error : Error | null , results : any ) => {
1797+ console . log ( 'uploader' , error , results )
1798+ // Explicitly type the 'error' parameter as 'Error'
1799+ if ( error ) {
1800+ reject ( error )
1801+ } else {
1802+ // console.debug(JSON.stringify(results).substring(0, 140) + '...' )
1803+ resolve ( results )
17891804 }
1790- : test(done, chinook, ok)
1791- )
1805+ } )
1806+ } )
1807+ }
1808+
1809+ let chunk
1810+ for await ( chunk of fileStream ) {
1811+ console . log ( chunk . buffer )
1812+ const upload_chunk = await uploader ( chinook , chunk . buffer )
1813+ expect ( upload_chunk ) . toBe ( 'OK' )
1814+ }
1815+
1816+ fileStream . close ( )
1817+
1818+ const close_upload = await uploader ( chinook , new ArrayBuffer ( 0 ) )
1819+ expect ( close_upload ) . toBe ( 'OK' )
1820+
1821+ const list_databases = await chinook . sql ( `LIST DATABASES DETAILED` )
1822+ expect ( list_databases ) . toContainEqual ( {
1823+ size : expect . any ( Number ) ,
1824+ name : database ,
1825+ connections : 0 ,
1826+ encryption : expect . any ( String ) ,
1827+ backup : 0 ,
1828+ nread : 0 ,
1829+ nwrite : 0 ,
1830+ inbytes : 0 ,
1831+ outbytes : 0 ,
1832+ fragmentation : 0.0 ,
1833+ pagesize : 1024 ,
1834+ encoding : 'UTF-8' ,
1835+ status : 1
1836+ } )
1837+
1838+ chinook . close ( )
1839+ } , 60000 )
1840+
1841+ it ( `should reserve and unreserve database` , done => {
1842+ const chinook = getConnection ( )
1843+ const rsrvd_db_test = randomName ( )
1844+ chinook . sendCommands ( `RESERVE DATABASE ${ rsrvd_db_test } UUID ${ rsrvd_db_test } ` , ( e , r ) => {
1845+ expect ( e ) . toBeNull ( )
1846+ expect ( r ) . toBe ( 'OK' )
1847+
1848+ chinook . sendCommands ( `UPLOAD DATABASE ${ rsrvd_db_test } ` , ( e , r ) => {
1849+ expect ( r ) . toBeUndefined ( )
1850+ expect ( e && e . message ) . toMatch ( / i s r e s e r v e d a n d c a n n o t b e u p l o a d e d / i)
1851+
1852+ chinook . sendCommands ( `UPLOAD ABORT` , ( e , r ) => {
1853+ expect ( e ) . toBeNull ( )
1854+ expect ( r ) . toEqual ( 'OK' )
1855+
1856+ chinook . sendCommands ( `UPLOAD DATABASE ${ rsrvd_db_test } ` , ( e , r ) => {
1857+ expect ( r ) . toBeUndefined ( )
1858+ expect ( e && e . message ) . toMatch ( / i s r e s e r v e d a n d c a n n o t b e u p l o a d e d / i)
1859+
1860+ chinook . sendCommands ( `UNRESERVE DATABASE ${ rsrvd_db_test } UUID` , ( e , r ) => {
1861+ expect ( e ) . toBeNull ( )
1862+ expect ( r ) . toEqual ( 'OK' )
1863+
1864+ chinook . sendCommands ( `UPLOAD DATABASE ${ rsrvd_db_test } ` , ( e , r ) => {
1865+ expect ( e ) . toBeNull ( )
1866+ expect ( r ) . toBe ( 'OK' )
1867+
1868+ chinook . sendCommands ( `UPLOAD ABORT` , test ( done , chinook , true ) )
1869+ } )
1870+ } )
1871+ } )
1872+ } )
1873+ } )
1874+ } )
17921875 } )
17931876
1794- it(`should${ok ? '' : "n't"} abort download database`, done => {
1877+ it ( `should try multiple database transfers ` , done => {
17951878 const chinook = getConnection ( )
1796- chinook.sendCommands(
1797- `DOWNLOAD DATABASE ${database}`,
1798- ok
1799- ? (e, r) => {
1879+ const rsrvd_db_test = randomName ( )
1880+ chinook . sendCommands ( `TRANSFER DATABASE ${ rsrvd_db_test } ` , ( e , r ) => {
1881+ expect ( e ) . toBeNull ( )
1882+ expect ( r ) . toBe ( 'OK' )
1883+
1884+ chinook . sendCommands ( `TRANSFER DATABASE ${ rsrvd_db_test } INTERNAL` , ( e , r ) => {
1885+ expect ( r ) . toBeUndefined ( )
1886+ expect ( e && e . message ) . toMatch ( / a n o t h e r u p l o a d o p e r a t i o n i s i n p l a c e / i)
1887+
1888+ chinook . sendCommands ( `UPLOAD ABORT` , ( e , r ) => {
1889+ expect ( e ) . toBeNull ( )
1890+ expect ( r ) . toEqual ( 'OK' )
1891+
1892+ chinook . sendCommands ( `TRANSFER DATABASE ${ rsrvd_db_test } INTERNAL` , ( e , r ) => {
18001893 expect ( e ) . toBeNull ( )
1801- expect(r).toEqual([expect.any(Number), expect.any(Number), expect.any(Number)])
1802- r.forEach((v: number) => expect(v).toBeGreaterThan(0))
1894+ expect ( r ) . toEqual ( 'OK' )
18031895
1804- chinook.sendCommands(`DOWNLOAD STEP `, (e, r) => {
1896+ chinook . sendCommands ( `UPLOAD ABORT ` , ( e , r ) => {
18051897 expect ( e ) . toBeNull ( )
1806- expect(r).toBeInstanceOf(Buffer)
1807- chinook.sendCommands(`DOWNLOAD ABORT`, (e, r) => {
1898+ expect ( r ) . toEqual ( 'OK' )
1899+
1900+ chinook . sendCommands ( `TRANSFER DATABASE ${ rsrvd_db_test } KEY ${ rsrvd_db_test } INTERNAL` , ( e , r ) => {
18081901 expect ( e ) . toBeNull ( )
1809- expect(r).toEqual('OK')
1810- chinook.sendCommands(`DOWNLOAD STEP`, test(done, chinook, ok))
1902+ expect ( r ) . toBe ( 'OK' )
1903+
1904+ chinook . sendCommands ( `UPLOAD ABORT` , test ( done , chinook , true ) )
18111905 } )
18121906 } )
1813- }
1814- : test(done, chinook, ok)
1815- )
1907+ } )
1908+ } )
1909+ } )
1910+ } )
18161911 } )
1912+ } )
18171913
1818- it(`should download database if exists${ok ? '' : " (it doesn't exist)"}`, done => {
1914+ describe ( 'vm commands' , ( ) => {
1915+ it ( `should vm execute` , async ( ) => {
18191916 const chinook = getConnection ( )
1820- chinook.sendCommands(
1821- `DOWNLOAD DATABASE ${database} IF EXISTS`,
1822- ok
1823- ? (e, r) => {
1824- expect(e).toBeNull()
1825- expect(r).toEqual([expect.any(Number), expect.any(Number), expect.any(Number)])
1826- r.forEach((v: number) => expect(v).toBeGreaterThan(0))
1827- for (let i = 0; i < r.length - 1; i++) {
1828- chinook.sendCommands(
1829- `DOWNLOAD STEP`,
1830- i != r.length - 2
1831- ? (e, r) => {
1832- expect(e).toBeNull()
1833- expect(r).toBeInstanceOf(Buffer)
1834- }
1835- : test(done, chinook, ok, expect.any(Buffer))
1836- )
1837- }
1838- }
1839- : (e, r) => {
1840- expect(e).toBeNull()
1841- expect(r).toEqual([0, 0, expect.any(Number)])
1842- chinook.sendCommands(`DOWNLOAD STEP`, test(done, chinook, ok))
1843- }
1844- )
1845- }) */
1917+ expect ( await chinook . sql ( `VM EXECUTE "SELECT * FROM artists"` ) ) . toContainEqual ( {
1918+ ArtistId : 3 ,
1919+ Name : 'Aerosmith'
1920+ } )
1921+ chinook . close ( )
1922+ } )
1923+
1924+ it ( `should vm compile` , async ( ) => {
1925+ const chinook = getConnection ( )
1926+ const result = await chinook . sql ( `VM COMPILE "SELECT * FROM artists"` )
1927+ expect ( result [ 0 ] ) . toBe ( 21 )
1928+ expect ( result [ 7 ] ) . toBe ( 0 )
1929+ chinook . close ( )
1930+ } )
18461931} )
0 commit comments