1
1
import { TreeItemCollapsibleState , Uri , l10n } from "vscode" ;
2
2
3
+ import { AxiosResponse } from "axios" ;
3
4
import { expect } from "chai" ;
4
- import nock from "nock " ;
5
+ import * as sinon from "sinon " ;
5
6
6
7
import LibraryDataProvider from "../../../src/components/LibraryNavigator/LibraryDataProvider" ;
7
8
import LibraryModel from "../../../src/components/LibraryNavigator/LibraryModel" ;
8
9
import {
10
+ DefaultRecordLimit ,
9
11
Icons ,
10
12
Messages ,
11
13
} from "../../../src/components/LibraryNavigator/const" ;
@@ -15,23 +17,32 @@ import { DataAccessApi } from "../../../src/connection/rest/api/compute";
15
17
import { getApiConfig } from "../../../src/connection/rest/common" ;
16
18
17
19
class MockRestLibraryAdapter extends RestLibraryAdapter {
18
- constructor ( ) {
20
+ constructor ( api : ReturnType < typeof DataAccessApi > ) {
19
21
super ( ) ;
20
- const apiConfig = getApiConfig ( ) ;
21
- apiConfig . baseOptions . baseURL = "http://test.local" ;
22
- this . dataAccessApi = DataAccessApi ( apiConfig ) ;
22
+ this . dataAccessApi = api ;
23
23
this . sessionId = "1234" ;
24
24
}
25
25
}
26
26
27
27
class MockLibraryModel extends LibraryModel {
28
- constructor ( ) {
29
- super ( new MockRestLibraryAdapter ( ) ) ;
28
+ constructor ( api : ReturnType < typeof DataAccessApi > ) {
29
+ super ( new MockRestLibraryAdapter ( api ) ) ;
30
30
}
31
31
}
32
32
33
- const libraryDataProvider = ( ) =>
34
- new LibraryDataProvider ( new MockLibraryModel ( ) , Uri . from ( { scheme : "file" } ) ) ;
33
+ const dataAccessApi = ( ) => {
34
+ const apiConfig = getApiConfig ( ) ;
35
+ apiConfig . baseOptions . baseURL = "https://test.local" ;
36
+ return DataAccessApi ( apiConfig ) ;
37
+ } ;
38
+
39
+ const libraryDataProvider = (
40
+ api : ReturnType < typeof DataAccessApi > = dataAccessApi ( ) ,
41
+ ) =>
42
+ new LibraryDataProvider (
43
+ new MockLibraryModel ( api ) ,
44
+ Uri . from ( { scheme : "file" } ) ,
45
+ ) ;
35
46
36
47
describe ( "LibraryDataProvider" , async function ( ) {
37
48
it ( "getChildren - returns an empty array when no adapter is specified" , async ( ) => {
@@ -53,19 +64,28 @@ describe("LibraryDataProvider", async function () {
53
64
readOnly : false ,
54
65
} ;
55
66
56
- nock ( "http://test.local" )
57
- . get ( "/sessions/1234/data/lib?start=0&limit=100" )
58
- . reply ( 200 , {
59
- items : [
60
- {
61
- id : "table" ,
62
- name : "table" ,
63
- } ,
64
- ] ,
65
- count : 0 ,
66
- } ) ;
67
+ const api = dataAccessApi ( ) ;
68
+ const getTablesStub = sinon . stub ( api , "getTables" ) ;
69
+ getTablesStub
70
+ . withArgs ( {
71
+ sessionId : "1234" ,
72
+ libref : library . id ,
73
+ limit : DefaultRecordLimit ,
74
+ start : 0 ,
75
+ } )
76
+ . resolves ( {
77
+ data : {
78
+ items : [
79
+ {
80
+ id : "table" ,
81
+ name : "table" ,
82
+ } ,
83
+ ] ,
84
+ count : 0 ,
85
+ } ,
86
+ } as AxiosResponse ) ;
67
87
68
- const provider = libraryDataProvider ( ) ;
88
+ const provider = libraryDataProvider ( api ) ;
69
89
const children = await provider . getChildren ( library ) ;
70
90
71
91
expect ( children [ 0 ] ) . to . deep . equal ( {
@@ -76,28 +96,34 @@ describe("LibraryDataProvider", async function () {
76
96
type : "table" ,
77
97
readOnly : library . readOnly ,
78
98
} ) ;
99
+ getTablesStub . restore ( ) ;
79
100
} ) ;
80
101
81
102
it ( "getChildren - returns libraries without content item" , async ( ) => {
103
+ const api = dataAccessApi ( ) ;
82
104
// One call to get libraries
83
- nock ( "http://test.local" )
84
- . get ( "/sessions/1234/data?start=0&limit=100" )
85
- . reply ( 200 , {
105
+ const getLibrariesStub = sinon . stub ( api , "getLibraries" ) . resolves ( {
106
+ data : {
86
107
items : [
87
108
{
88
109
id : "library" ,
89
110
name : "library" ,
90
111
} ,
91
112
] ,
92
113
count : 0 ,
93
- } ) ;
114
+ } ,
115
+ } as AxiosResponse ) ;
94
116
95
- // One to get
96
- nock ( "http://test.local" ) . get ( "/sessions/1234/data/library" ) . reply ( 200 , {
97
- readOnly : true ,
98
- } ) ;
117
+ // One to get library summary
118
+ const getLibrarySummaryStub = sinon
119
+ . stub ( api , "getLibrarySummary" )
120
+ . resolves ( {
121
+ data : {
122
+ readOnly : true ,
123
+ } ,
124
+ } as AxiosResponse ) ;
99
125
100
- const provider = libraryDataProvider ( ) ;
126
+ const provider = libraryDataProvider ( api ) ;
101
127
const children = await provider . getChildren ( ) ;
102
128
103
129
expect ( children [ 0 ] ) . to . deep . equal ( {
@@ -108,6 +134,9 @@ describe("LibraryDataProvider", async function () {
108
134
type : "library" ,
109
135
readOnly : true ,
110
136
} ) ;
137
+
138
+ getLibrariesStub . restore ( ) ;
139
+ getLibrarySummaryStub . restore ( ) ;
111
140
} ) ;
112
141
113
142
it ( "getTreeItem - returns table tree item" , async ( ) => {
@@ -206,10 +235,13 @@ describe("LibraryDataProvider", async function () {
206
235
library : "lib" ,
207
236
} ;
208
237
209
- nock ( "http://test.local" ) . delete ( "/sessions/1234/data/lib/test" ) . reply ( 200 ) ;
238
+ const api = dataAccessApi ( ) ;
239
+ const deleteTableStub = sinon . stub ( api , "deleteTable" ) ;
210
240
211
- const provider = libraryDataProvider ( ) ;
241
+ const provider = libraryDataProvider ( api ) ;
212
242
await provider . deleteTable ( item ) ;
243
+ expect ( deleteTableStub . calledOnce ) . to . equal ( true ) ;
244
+ deleteTableStub . restore ( ) ;
213
245
} ) ;
214
246
215
247
it ( "deleteTable - fails with error message" , async ( ) => {
@@ -222,9 +254,11 @@ describe("LibraryDataProvider", async function () {
222
254
library : "lib" ,
223
255
} ;
224
256
225
- nock ( "http://test.local" ) . delete ( "/sessions/1234/data/lib/test" ) . reply ( 500 ) ;
257
+ const api = dataAccessApi ( ) ;
258
+ const deleteTableStub = sinon . stub ( api , "deleteTable" ) ;
259
+ deleteTableStub . throwsException ( new Error ( ) ) ;
226
260
227
- const provider = libraryDataProvider ( ) ;
261
+ const provider = libraryDataProvider ( api ) ;
228
262
try {
229
263
await provider . deleteTable ( item ) ;
230
264
} catch ( error ) {
@@ -233,5 +267,7 @@ describe("LibraryDataProvider", async function () {
233
267
. message ,
234
268
) ;
235
269
}
270
+
271
+ deleteTableStub . restore ( ) ;
236
272
} ) ;
237
273
} ) ;
0 commit comments