22 * protocol.test.ts - test low level communication protocol
33 */
44
5- /* eslint-disable */
5+ import { SQLiteCloudConnection , SQLiteCloudConfig , SQLiteCloudError , parseConnectionString } from '../src/protocol'
66
7- // https://github.com/jest-community/vscode-jest/blob/master/README.md#autorun
8-
9- // published library
10- // var sqlitecloud = require('sqlitecloud-nodejs-sdk')
11-
12- // code being refactored:
13- import sqlitecloud , { SQLiteCloudError , parseConnectionString } from '../src/protocol'
14-
15- const cert = `-----BEGIN CERTIFICATE-----
7+ const TEST_CERTIFICATE = `-----BEGIN CERTIFICATE-----
168MIID6zCCAtOgAwIBAgIUI0lTm5CfVf3mVP8606CkophcyB4wDQYJKoZIhvcNAQEL
179BQAwgYQxCzAJBgNVBAYTAklUMQswCQYDVQQIDAJNTjEQMA4GA1UEBwwHVmlhZGFu
1810YTEbMBkGA1UECgwSU1FMaXRlIENsb3VkLCBJbmMuMRQwEgYDVQQDDAtTUUxpdGVD
@@ -36,26 +28,33 @@ Sjox3HYOoj2uG2669CLAnw6rkHESbi5imasC9FxWBVxWrnNd0icyiDb1wfBc5W9N
3628otHL5/wB1MaAmCIcQjIxEshj8pSYTecthitmrneimikFf4KFK0YMvGgKrCLmJsg=
3729-----END CERTIFICATE-----`
3830
39- const configDev1 = {
40- clientId : 'dev1.sqlitecloud.io' ,
41- username : 'admin' , //required unless connectionString is provided
42- password : 'admin' , //required unless connectionString is provided
43- host : 'dev1.sqlitecloud.io' , //required unless connectionString is provided
44- port : 9960 , //required unless connectionString is provided
31+ require ( 'dotenv' ) . config ( )
32+
33+ const testConfig : SQLiteCloudConfig = {
34+ clientId : 'test' ,
35+ username : process . env . TEST_USERNAME ,
36+ password : process . env . TEST_PASSWORD ,
37+ host : process . env . TEST_HOST ,
38+ database : process . env . TEST_DATABASE || 'chinook.sqlite' ,
39+ port : ( process . env . TEST_PORT || 9960 ) as number ,
4540 compression : true ,
46- queryTimeout : 10000000 ,
41+ timeout : 100 * 1000 ,
4742 tlsOptions : {
48- ca : cert
43+ ca : TEST_CERTIFICATE
4944 }
5045}
5146
5247describe ( 'protocol' , ( ) => {
53- let client : sqlitecloud
48+ let client : SQLiteCloudConnection
5449
5550 beforeEach ( async ( ) => {
51+ expect ( process . env . TEST_USERNAME ) . toBeDefined ( )
52+ expect ( process . env . TEST_PASSWORD ) . toBeDefined ( )
53+ expect ( process . env . TEST_HOST ) . toBeDefined ( )
54+
5655 if ( ! client ) {
5756 try {
58- const connectingClient = new sqlitecloud ( configDev1 , true )
57+ const connectingClient = new SQLiteCloudConnection ( testConfig )
5958 expect ( connectingClient ) . toBeDefined ( )
6059
6160 await connectingClient . connect ( )
@@ -80,6 +79,48 @@ describe('protocol', () => {
8079 it ( 'should connect' , async ( ) => {
8180 // ...in beforeEach
8281 } )
82+
83+ it ( 'should connect with connection string' , async ( ) => {
84+ const connectionString = `sqlitecloud://${ testConfig . username as string } :${ testConfig . password as string } @${ testConfig . host as string } `
85+ const connection = new SQLiteCloudConnection ( {
86+ connectionString,
87+ tlsOptions : {
88+ ca : TEST_CERTIFICATE
89+ }
90+ } )
91+
92+ expect ( connection ) . toBeDefined ( )
93+ await connection . connect ( )
94+ expect ( connection . connected ) . toBe ( true )
95+ await connection . disconnect ( )
96+ expect ( connection . connected ) . toBe ( false )
97+ } )
98+
99+ it ( 'should throw when connection string lacks credentials' , async ( ) => {
100+ try {
101+ const connectionString = `sqlitecloud://${ testConfig . host as string } `
102+ const connection = new SQLiteCloudConnection ( {
103+ connectionString,
104+ tlsOptions : {
105+ ca : TEST_CERTIFICATE
106+ }
107+ } )
108+
109+ expect ( connection ) . toBeDefined ( )
110+ await connection . connect ( )
111+ // fail the test if the error is not thrown
112+ expect ( true ) . toBe ( false )
113+ } catch ( error : any ) {
114+ expect ( error ) . toBeDefined ( )
115+ expect ( error ) . toBeInstanceOf ( SQLiteCloudError )
116+
117+ const sqliteCloudError = error as SQLiteCloudError
118+ expect ( sqliteCloudError . message ) . toBe ( 'The user, password and host arguments must be specified.' )
119+ expect ( sqliteCloudError . errorCode ) . toBe ( 'ERR_MISSING_ARGS' )
120+ expect ( sqliteCloudError . externalErrorCode ) . toBeUndefined ( )
121+ expect ( sqliteCloudError . offsetCode ) . toBeUndefined ( )
122+ }
123+ } )
83124 } )
84125
85126 describe ( 'send test commands' , ( ) => {
@@ -162,10 +203,11 @@ describe('protocol', () => {
162203 expect ( error ) . toBeDefined ( )
163204 expect ( error ) . toBeInstanceOf ( SQLiteCloudError )
164205
165- expect ( error . message ) . toBe ( 'This is a test error message with an extcode and a devil error code.' )
166- expect ( error . errorCode ) . toBe ( 66666 )
167- expect ( error . externalErrorCode ) . toBe ( 333 )
168- expect ( error . offsetCode ) . toBe ( - 1 )
206+ const sqliteCloudError = error as SQLiteCloudError
207+ expect ( sqliteCloudError . message ) . toBe ( 'This is a test error message with an extcode and a devil error code.' )
208+ expect ( sqliteCloudError . errorCode ) . toBe ( '66666' )
209+ expect ( sqliteCloudError . externalErrorCode ) . toBe ( '333' )
210+ expect ( sqliteCloudError . offsetCode ) . toBe ( - 1 )
169211 }
170212 } )
171213
@@ -200,7 +242,7 @@ describe('protocol', () => {
200242
201243 describe ( 'send select commands' , ( ) => {
202244 it ( 'should select long formatted string' , async ( ) => {
203- let response = await client . sendCommands ( "USE DATABASE :memory:; select printf('%.*c', 1000, 'x') AS DDD" )
245+ const response = await client . sendCommands ( "USE DATABASE :memory:; select printf('%.*c', 1000, 'x') AS DDD" )
204246 expect ( response . numberOfColumns ) . toBe ( 1 )
205247 expect ( response . numberOfRows ) . toBe ( 1 )
206248 expect ( response . version ) . toBe ( 1 )
@@ -211,7 +253,7 @@ describe('protocol', () => {
211253 } )
212254
213255 it ( 'should select database' , async ( ) => {
214- let response = await client . sendCommands ( 'USE DATABASE chinook.sqlite;' )
256+ const response = await client . sendCommands ( 'USE DATABASE chinook.sqlite;' )
215257 expect ( response . numberOfColumns ) . toBeUndefined ( )
216258 expect ( response . numberOfRows ) . toBeUndefined ( )
217259 expect ( response . version ) . toBeUndefined ( )
0 commit comments