1
1
const { expect } = require ( 'chai' ) ;
2
+ const fs = require ( 'fs' ) ;
3
+ const path = require ( 'path' ) ;
4
+ const os = require ( 'os' ) ;
5
+ const uuid = require ( 'uuid' ) ;
2
6
const config = require ( './utils/config' ) ;
3
7
const { DBSQLClient } = require ( '../..' ) ;
4
- const fs = require ( 'fs' ) ;
8
+ const StagingError = require ( '../../dist/errors/StagingError' ) . default ;
9
+
10
+ describe ( 'Staging Test' , ( ) => {
11
+ const catalog = config . database [ 0 ] ;
12
+ const schema = config . database [ 1 ] ;
13
+ const volume = config . volume ;
14
+
15
+ const localPath = fs . mkdtempSync ( path . join ( os . tmpdir ( ) , 'databricks-sql-tests-' ) ) ;
16
+
17
+ before ( ( ) => {
18
+ expect ( catalog ) . to . not . be . undefined ;
19
+ expect ( schema ) . to . not . be . undefined ;
20
+ expect ( volume ) . to . not . be . undefined ;
21
+ } ) ;
22
+
23
+ after ( ( ) => {
24
+ fs . rmSync ( localPath , {
25
+ recursive : true ,
26
+ force : true ,
27
+ } ) ;
28
+ } ) ;
5
29
6
- // TODO: Temporarily disable those tests until we figure out issues with E2E test env
7
- describe . skip ( 'Staging Test' , ( ) => {
8
30
it ( 'put staging data and receive it' , async ( ) => {
9
31
const client = new DBSQLClient ( ) ;
10
32
await client . connect ( {
11
33
host : config . host ,
12
34
path : config . path ,
13
35
token : config . token ,
14
36
} ) ;
15
- let tempPath = 'tests/e2e/staging/data' ;
16
- fs . writeFileSync ( tempPath , 'Hello World!' ) ;
17
37
18
38
const session = await client . openSession ( {
19
- initialCatalog : config . database [ 0 ] ,
20
- initialSchema : config . database [ 1 ] ,
39
+ initialCatalog : catalog ,
40
+ initialSchema : schema ,
41
+ } ) ;
42
+
43
+ const expectedData = 'Hello World!' ;
44
+ const stagingFileName = `/Volumes/${ catalog } /${ schema } /${ volume } /${ uuid . v4 ( ) } .csv` ;
45
+ const localFile = path . join ( localPath , `${ uuid . v4 ( ) } .csv` ) ;
46
+
47
+ fs . writeFileSync ( localFile , expectedData ) ;
48
+ await session . executeStatement ( `PUT '${ localFile } ' INTO '${ stagingFileName } ' OVERWRITE` , {
49
+ stagingAllowedLocalPath : [ localPath ] ,
50
+ } ) ;
51
+ fs . rmSync ( localFile ) ;
52
+
53
+ await session . executeStatement ( `GET '${ stagingFileName } ' TO '${ localFile } '` , {
54
+ stagingAllowedLocalPath : [ localPath ] ,
21
55
} ) ;
22
- await session . executeStatement (
23
- `PUT '${ tempPath } ' INTO '/Volumes/${ config . database [ 0 ] } /${ config . database [ 1 ] } /e2etests/file1.csv' OVERWRITE` ,
24
- { stagingAllowedLocalPath : [ 'tests/e2e/staging' ] } ,
25
- ) ;
26
- await session . executeStatement (
27
- `GET '/Volumes/${ config . database [ 0 ] } /${ config . database [ 1 ] } /e2etests/file1.csv' TO 'tests/e2e/staging/file'` ,
28
- { stagingAllowedLocalPath : [ 'tests/e2e/staging' ] } ,
29
- ) ;
30
- let result = fs . readFileSync ( 'tests/e2e/staging/file' ) ;
31
- expect ( result . toString ( ) === 'Hello World!' ) . to . be . true ;
56
+ const result = fs . readFileSync ( localFile ) ;
57
+ fs . rmSync ( localFile ) ;
58
+ expect ( result . toString ( ) === expectedData ) . to . be . true ;
32
59
} ) ;
33
60
34
61
it ( 'put staging data and remove it' , async ( ) => {
@@ -38,20 +65,39 @@ describe.skip('Staging Test', () => {
38
65
path : config . path ,
39
66
token : config . token ,
40
67
} ) ;
41
- let tempPath = 'tests/e2e/staging/data' ;
42
- fs . writeFileSync ( tempPath , ( data = 'Hello World!' ) ) ;
43
68
44
- let session = await client . openSession ( {
45
- initialCatalog : config . database [ 0 ] ,
46
- initialSchema : config . database [ 1 ] ,
69
+ const session = await client . openSession ( {
70
+ initialCatalog : catalog ,
71
+ initialSchema : schema ,
47
72
} ) ;
48
- await session . executeStatement (
49
- `PUT '${ tempPath } ' INTO '/Volumes/${ config . database [ 0 ] } /${ config . database [ 1 ] } /e2etests/file1.csv' OVERWRITE` ,
50
- { stagingAllowedLocalPath : [ 'tests/e2e/staging' ] } ,
51
- ) ;
52
- await session . executeStatement ( `REMOVE '/Volumes/${ config . database [ 0 ] } /${ config . database [ 1 ] } /e2etests/file1.csv'` , {
53
- stagingAllowedLocalPath : [ 'tests/e2e/staging' ] ,
73
+
74
+ const expectedData = 'Hello World!' ;
75
+ const stagingFileName = `/Volumes/${ catalog } /${ schema } /${ volume } /${ uuid . v4 ( ) } .csv` ;
76
+ const localFile = path . join ( localPath , `${ uuid . v4 ( ) } .csv` ) ;
77
+
78
+ fs . writeFileSync ( localFile , expectedData ) ;
79
+ await session . executeStatement ( `PUT '${ localFile } ' INTO '${ stagingFileName } ' OVERWRITE` , {
80
+ stagingAllowedLocalPath : [ localPath ] ,
54
81
} ) ;
82
+ fs . rmSync ( localFile ) ;
83
+
84
+ await session . executeStatement ( `REMOVE '${ stagingFileName } '` , { stagingAllowedLocalPath : [ localPath ] } ) ;
85
+
86
+ try {
87
+ await session . executeStatement ( `GET '${ stagingFileName } ' TO '${ localFile } '` , {
88
+ stagingAllowedLocalPath : [ localPath ] ,
89
+ } ) ;
90
+ expect . fail ( 'It should throw HTTP 404 error' ) ;
91
+ } catch ( error ) {
92
+ if ( error instanceof StagingError ) {
93
+ // File should not exist after deleting
94
+ expect ( error . message ) . to . contain ( '404' ) ;
95
+ } else {
96
+ throw error ;
97
+ }
98
+ } finally {
99
+ fs . rmSync ( localFile , { force : true } ) ;
100
+ }
55
101
} ) ;
56
102
57
103
it ( 'delete non-existent data' , async ( ) => {
@@ -61,22 +107,24 @@ describe.skip('Staging Test', () => {
61
107
path : config . path ,
62
108
token : config . token ,
63
109
} ) ;
64
- let tempPath = 'tests/e2e/staging/data' ;
65
- fs . writeFileSync ( tempPath , ( data = 'Hello World!' ) ) ;
66
110
67
- let session = await client . openSession ( {
68
- initialCatalog : config . database [ 0 ] ,
69
- initialSchema : config . database [ 1 ] ,
111
+ const session = await client . openSession ( {
112
+ initialCatalog : catalog ,
113
+ initialSchema : schema ,
70
114
} ) ;
71
- await session . executeStatement (
72
- `PUT '${ tempPath } ' INTO '/Volumes/${ config . database [ 0 ] } /${ config . database [ 1 ] } /e2etests/file1.csv' OVERWRITE` ,
73
- { stagingAllowedLocalPath : [ 'tests/e2e/staging' ] } ,
74
- ) ;
75
- await session . executeStatement (
76
- `GET '/Volumes/${ config . database [ 0 ] } /${ config . database [ 1 ] } /e2etests/file1.csv' TO 'tests/e2e/staging/file'` ,
77
- { stagingAllowedLocalPath : [ 'tests/e2e/staging' ] } ,
78
- ) ;
79
- let result = fs . readFileSync ( 'tests/e2e/staging/file' ) ;
80
- expect ( result . toString ( ) === 'Hello World!' ) . to . be . true ;
115
+
116
+ const stagingFileName = `/Volumes/${ catalog } /${ schema } /${ volume } /${ uuid . v4 ( ) } .csv` ;
117
+
118
+ try {
119
+ await session . executeStatement ( `REMOVE '${ stagingFileName } '` , { stagingAllowedLocalPath : [ localPath ] } ) ;
120
+ // In some cases, `REMOVE` may silently succeed for non-existing files (see comment in relevant
121
+ // part of `DBSQLSession` code). But if it fails - it has to be an HTTP 404 error
122
+ } catch ( error ) {
123
+ if ( error instanceof StagingError ) {
124
+ expect ( error . message ) . to . contain ( '404' ) ;
125
+ } else {
126
+ throw error ;
127
+ }
128
+ }
81
129
} ) ;
82
130
} ) ;
0 commit comments