11import { TreeItemCollapsibleState , Uri , l10n } from "vscode" ;
22
3+ import { AxiosResponse } from "axios" ;
34import { expect } from "chai" ;
4- import nock from "nock " ;
5+ import * as sinon from "sinon " ;
56
67import LibraryDataProvider from "../../../src/components/LibraryNavigator/LibraryDataProvider" ;
78import LibraryModel from "../../../src/components/LibraryNavigator/LibraryModel" ;
89import {
10+ DefaultRecordLimit ,
911 Icons ,
1012 Messages ,
1113} from "../../../src/components/LibraryNavigator/const" ;
@@ -15,23 +17,32 @@ import { DataAccessApi } from "../../../src/connection/rest/api/compute";
1517import { getApiConfig } from "../../../src/connection/rest/common" ;
1618
1719class MockRestLibraryAdapter extends RestLibraryAdapter {
18- constructor ( ) {
20+ constructor ( api : ReturnType < typeof DataAccessApi > ) {
1921 super ( ) ;
20- const apiConfig = getApiConfig ( ) ;
21- apiConfig . baseOptions . baseURL = "http://test.local" ;
22- this . dataAccessApi = DataAccessApi ( apiConfig ) ;
22+ this . dataAccessApi = api ;
2323 this . sessionId = "1234" ;
2424 }
2525}
2626
2727class MockLibraryModel extends LibraryModel {
28- constructor ( ) {
29- super ( new MockRestLibraryAdapter ( ) ) ;
28+ constructor ( api : ReturnType < typeof DataAccessApi > ) {
29+ super ( new MockRestLibraryAdapter ( api ) ) ;
3030 }
3131}
3232
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+ ) ;
3546
3647describe ( "LibraryDataProvider" , async function ( ) {
3748 it ( "getChildren - returns an empty array when no adapter is specified" , async ( ) => {
@@ -53,19 +64,28 @@ describe("LibraryDataProvider", async function () {
5364 readOnly : false ,
5465 } ;
5566
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 ) ;
6787
68- const provider = libraryDataProvider ( ) ;
88+ const provider = libraryDataProvider ( api ) ;
6989 const children = await provider . getChildren ( library ) ;
7090
7191 expect ( children [ 0 ] ) . to . deep . equal ( {
@@ -76,28 +96,34 @@ describe("LibraryDataProvider", async function () {
7696 type : "table" ,
7797 readOnly : library . readOnly ,
7898 } ) ;
99+ getTablesStub . restore ( ) ;
79100 } ) ;
80101
81102 it ( "getChildren - returns libraries without content item" , async ( ) => {
103+ const api = dataAccessApi ( ) ;
82104 // 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 : {
86107 items : [
87108 {
88109 id : "library" ,
89110 name : "library" ,
90111 } ,
91112 ] ,
92113 count : 0 ,
93- } ) ;
114+ } ,
115+ } as AxiosResponse ) ;
94116
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 ) ;
99125
100- const provider = libraryDataProvider ( ) ;
126+ const provider = libraryDataProvider ( api ) ;
101127 const children = await provider . getChildren ( ) ;
102128
103129 expect ( children [ 0 ] ) . to . deep . equal ( {
@@ -108,6 +134,9 @@ describe("LibraryDataProvider", async function () {
108134 type : "library" ,
109135 readOnly : true ,
110136 } ) ;
137+
138+ getLibrariesStub . restore ( ) ;
139+ getLibrarySummaryStub . restore ( ) ;
111140 } ) ;
112141
113142 it ( "getTreeItem - returns table tree item" , async ( ) => {
@@ -206,10 +235,13 @@ describe("LibraryDataProvider", async function () {
206235 library : "lib" ,
207236 } ;
208237
209- nock ( "http://test.local" ) . delete ( "/sessions/1234/data/lib/test" ) . reply ( 200 ) ;
238+ const api = dataAccessApi ( ) ;
239+ const deleteTableStub = sinon . stub ( api , "deleteTable" ) ;
210240
211- const provider = libraryDataProvider ( ) ;
241+ const provider = libraryDataProvider ( api ) ;
212242 await provider . deleteTable ( item ) ;
243+ expect ( deleteTableStub . calledOnce ) . to . equal ( true ) ;
244+ deleteTableStub . restore ( ) ;
213245 } ) ;
214246
215247 it ( "deleteTable - fails with error message" , async ( ) => {
@@ -222,9 +254,11 @@ describe("LibraryDataProvider", async function () {
222254 library : "lib" ,
223255 } ;
224256
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 ( ) ) ;
226260
227- const provider = libraryDataProvider ( ) ;
261+ const provider = libraryDataProvider ( api ) ;
228262 try {
229263 await provider . deleteTable ( item ) ;
230264 } catch ( error ) {
@@ -233,5 +267,7 @@ describe("LibraryDataProvider", async function () {
233267 . message ,
234268 ) ;
235269 }
270+
271+ deleteTableStub . restore ( ) ;
236272 } ) ;
237273} ) ;
0 commit comments