11import { CannerServer } from './cannerServer' ;
22import { CannerDataSource , PGOptions } from '../src' ;
3+ import { MockCannerDataSource } from './mock' ;
34import { ExportOptions , InternalError , streamToArray } from '@vulcan-sql/core' ;
45import { Writable } from 'stream' ;
56import * as sinon from 'ts-sinon' ;
@@ -8,7 +9,9 @@ import { CannerAdapter } from '../src/lib/cannerAdapter';
89
910const pg = new CannerServer ( ) ;
1011let dataSource : CannerDataSource ;
12+ let mockDataSource : MockCannerDataSource ;
1113
14+ const directory = 'tmp_test_canner' ;
1215// restore all sinon mock/stub before each test
1316beforeEach ( ( ) => {
1417 sinon . default . restore ( ) ;
@@ -42,22 +45,22 @@ it('Data source should throw error when activating if any profile is invalid', a
4245
4346// export method should be executed successfully
4447it ( 'Data source should export successfully' , async ( ) => {
45- fs . mkdirSync ( 'tmp' , { recursive : true } ) ;
48+ fs . mkdirSync ( directory , { recursive : true } ) ;
4649 dataSource = new CannerDataSource ( { } , '' , [ pg . getProfile ( 'profile1' ) ] ) ;
4750 await dataSource . activate ( ) ;
4851
4952 // Act, Assert
5053 await expect (
5154 dataSource . export ( {
5255 sql : 'select 1' ,
53- directory : 'tmp' ,
56+ directory,
5457 profileName : 'profile1' ,
5558 } as ExportOptions )
5659 ) . resolves . not . toThrow ( ) ;
57- expect ( fs . readdirSync ( 'tmp' ) . length ) . toBe ( 1 ) ;
60+ expect ( fs . readdirSync ( directory ) . length ) . toBe ( 1 ) ;
5861
5962 // clean up
60- fs . rmSync ( 'tmp' , { recursive : true , force : true } ) ;
63+ fs . rmSync ( directory , { recursive : true , force : true } ) ;
6164} , 100000 ) ;
6265
6366it ( 'Data source should throw error when fail to export data' , async ( ) => {
@@ -73,22 +76,22 @@ it('Data source should throw error when fail to export data', async () => {
7376 ) ;
7477 } ) ;
7578
76- fs . mkdirSync ( 'tmp' , { recursive : true } ) ;
79+ fs . mkdirSync ( directory , { recursive : true } ) ;
7780 dataSource = new CannerDataSource ( { } , '' , [ pg . getProfile ( 'profile1' ) ] ) ;
7881 await dataSource . activate ( ) ;
7982
8083 // Act, Assert
8184 await expect (
8285 dataSource . export ( {
8386 sql : 'select 1' ,
84- directory : 'tmp' ,
87+ directory,
8588 profileName : 'profile1' ,
8689 } as ExportOptions )
8790 ) . rejects . toThrow ( ) ;
88- expect ( fs . readdirSync ( 'tmp' ) . length ) . toBe ( 0 ) ;
91+ expect ( fs . readdirSync ( directory ) . length ) . toBe ( 0 ) ;
8992
9093 // clean up
91- fs . rmSync ( 'tmp' , { recursive : true , force : true } ) ;
94+ fs . rmSync ( directory , { recursive : true , force : true } ) ;
9295} , 100000 ) ;
9396
9497it ( 'Data source should throw error when given directory is not exist' , async ( ) => {
@@ -100,7 +103,7 @@ it('Data source should throw error when given directory is not exist', async ()
100103 await expect (
101104 dataSource . export ( {
102105 sql : 'select 1' ,
103- directory : 'tmp' ,
106+ directory : directory ,
104107 profileName : 'profile1' ,
105108 } as ExportOptions )
106109 ) . rejects . toThrow ( ) ;
@@ -110,13 +113,13 @@ it('Data source should throw error when given profile name is not exist', async
110113 // Arrange
111114 dataSource = new CannerDataSource ( { } , '' , [ pg . getProfile ( 'profile1' ) ] ) ;
112115 await dataSource . activate ( ) ;
113- fs . mkdirSync ( 'tmp' , { recursive : true } ) ;
116+ fs . mkdirSync ( directory , { recursive : true } ) ;
114117
115118 // Act, Assert
116119 await expect (
117120 dataSource . export ( {
118121 sql : 'select 1' ,
119- directory : 'tmp' ,
122+ directory,
120123 profileName : 'profile not exist' ,
121124 } as ExportOptions )
122125 ) . rejects . toThrow ( ) ;
@@ -318,3 +321,55 @@ it('Data source should release connection when readable stream is destroyed', as
318321 expect ( rows . length ) . toBe ( 1 ) ;
319322 // afterEach hook will timeout if any leak occurred.
320323} , 300000 ) ;
324+
325+ it ( 'Should return the same pool when the profile is the same' , async ( ) => {
326+ // Arrange
327+ mockDataSource = new MockCannerDataSource ( { } , '' , [
328+ pg . getProfile ( 'profile1' ) ,
329+ ] ) ;
330+ await mockDataSource . activate ( ) ;
331+ // Act
332+ const pool1 = mockDataSource . getPool ( 'profile1' ) ;
333+ const pool2 = mockDataSource . getPool ( 'profile1' ) ;
334+ // Assert
335+ expect ( pool1 === pool2 ) . toBeTruthy ( ) ;
336+ } , 30000 ) ;
337+
338+ it ( 'Should return the same pool when the profile and authentication is the same' , async ( ) => {
339+ // Arrange
340+ mockDataSource = new MockCannerDataSource ( { } , '' , [
341+ pg . getProfile ( 'profile1' ) ,
342+ ] ) ;
343+ await mockDataSource . activate ( ) ;
344+ // Act
345+ const pool1 = mockDataSource . getPool ( 'profile1' , 'the-same-authentication' ) ;
346+ const pool2 = mockDataSource . getPool ( 'profile1' , 'the-same-authentication' ) ;
347+ // Assert
348+ expect ( pool1 === pool2 ) . toBeTruthy ( ) ;
349+ } , 30000 ) ;
350+
351+ it ( 'Should return different pool if authentication exist in headers even the profile is the same' , async ( ) => {
352+ // Arrange
353+ mockDataSource = new MockCannerDataSource ( { } , '' , [
354+ pg . getProfile ( 'profile1' ) ,
355+ ] ) ;
356+ await mockDataSource . activate ( ) ;
357+ // Act
358+ const pool1 = mockDataSource . getPool ( 'profile1' ) ;
359+ const pool2 = mockDataSource . getPool ( 'profile1' , 'my-authentication' ) ;
360+ // Assert
361+ expect ( pool1 == pool2 ) . toBeFalsy ( ) ;
362+ } , 30000 ) ;
363+
364+ it ( 'Should return different pool with different authentication even the profile is the same' , async ( ) => {
365+ // Arrange
366+ mockDataSource = new MockCannerDataSource ( { } , '' , [
367+ pg . getProfile ( 'profile1' ) ,
368+ ] ) ;
369+ await mockDataSource . activate ( ) ;
370+ // Act
371+ const pool1 = mockDataSource . getPool ( 'profile1' , 'authentication' ) ;
372+ const pool2 = mockDataSource . getPool ( 'profile1' , 'differ-authentication' ) ;
373+ // Assert
374+ expect ( pool1 === pool2 ) . toBeFalsy ( ) ;
375+ } , 30000 ) ;
0 commit comments