@@ -21,20 +21,31 @@ suite('Import Tracker', () => {
2121 let documentManager : TypeMoq . IMock < IDocumentManager > ;
2222 let openedEventEmitter : EventEmitter < TextDocument > ;
2323 let savedEventEmitter : EventEmitter < TextDocument > ;
24- const pandasHash = hashJs . sha256 ( ) . update ( 'pandas' ) . digest ( 'hex' ) ;
25- const elephasHash = hashJs . sha256 ( ) . update ( 'elephas' ) . digest ( 'hex' ) ;
26- const kerasHash = hashJs . sha256 ( ) . update ( 'keras' ) . digest ( 'hex' ) ;
27- const pysparkHash = hashJs . sha256 ( ) . update ( 'pyspark' ) . digest ( 'hex' ) ;
28- const sparkdlHash = hashJs . sha256 ( ) . update ( 'sparkdl' ) . digest ( 'hex' ) ;
29- const numpyHash = hashJs . sha256 ( ) . update ( 'numpy' ) . digest ( 'hex' ) ;
30- const scipyHash = hashJs . sha256 ( ) . update ( 'scipy' ) . digest ( 'hex' ) ;
31- const sklearnHash = hashJs . sha256 ( ) . update ( 'sklearn' ) . digest ( 'hex' ) ;
32- const randomHash = hashJs . sha256 ( ) . update ( 'random' ) . digest ( 'hex' ) ;
24+ const pandasHash : string = hashJs . sha256 ( ) . update ( 'pandas' ) . digest ( 'hex' ) ;
25+ const elephasHash : string = hashJs . sha256 ( ) . update ( 'elephas' ) . digest ( 'hex' ) ;
26+ const kerasHash : string = hashJs . sha256 ( ) . update ( 'keras' ) . digest ( 'hex' ) ;
27+ const pysparkHash : string = hashJs . sha256 ( ) . update ( 'pyspark' ) . digest ( 'hex' ) ;
28+ const sparkdlHash : string = hashJs . sha256 ( ) . update ( 'sparkdl' ) . digest ( 'hex' ) ;
29+ const numpyHash : string = hashJs . sha256 ( ) . update ( 'numpy' ) . digest ( 'hex' ) ;
30+ const scipyHash : string = hashJs . sha256 ( ) . update ( 'scipy' ) . digest ( 'hex' ) ;
31+ const sklearnHash : string = hashJs . sha256 ( ) . update ( 'sklearn' ) . digest ( 'hex' ) ;
32+ const randomHash : string = hashJs . sha256 ( ) . update ( 'random' ) . digest ( 'hex' ) ;
3333
3434 class Reporter {
3535 public static eventNames : string [ ] = [ ] ;
3636 public static properties : Record < string , string > [ ] = [ ] ;
3737 public static measures : { } [ ] = [ ] ;
38+
39+ public static expectHashes ( ...hashes : string [ ] ) {
40+ expect ( Reporter . eventNames ) . to . contain ( EventName . HASHED_PACKAGE_PERF ) ;
41+ if ( hashes . length > 0 ) {
42+ expect ( Reporter . eventNames ) . to . contain ( EventName . HASHED_PACKAGE_NAME ) ;
43+ }
44+
45+ Reporter . properties . pop ( ) ; // HASHED_PACKAGE_PERF
46+ expect ( Reporter . properties ) . to . deep . equal ( hashes . map ( hash => ( { hashedName : hash } ) ) ) ;
47+ }
48+
3849 public sendTelemetryEvent ( eventName : string , properties ?: { } , measures ?: { } ) {
3950 Reporter . eventNames . push ( eventName ) ;
4051 Reporter . properties . push ( properties ! ) ;
@@ -76,24 +87,21 @@ suite('Import Tracker', () => {
7687 test ( 'Open document' , ( ) => {
7788 emitDocEvent ( 'import pandas\r\n' , openedEventEmitter ) ;
7889
79- expect ( Reporter . eventNames ) . to . deep . equal ( [ EventName . HASHED_PACKAGE_NAME ] ) ;
80- expect ( Reporter . properties ) . to . deep . equal ( [ { hashedName : pandasHash } ] ) ;
90+ Reporter . expectHashes ( pandasHash ) ;
8191 } ) ;
8292
8393 test ( 'Already opened documents' , async ( ) => {
8494 const doc = createDocument ( 'import pandas\r\n' , 'foo.py' , 1 , TypeMoq . Times . atMost ( 100 ) , true ) ;
8595 documentManager . setup ( d => d . textDocuments ) . returns ( ( ) => [ doc . object ] ) ;
8696 await importTracker . activate ( ) ;
8797
88- expect ( Reporter . eventNames ) . to . deep . equal ( [ EventName . HASHED_PACKAGE_NAME ] ) ;
89- expect ( Reporter . properties ) . to . deep . equal ( [ { hashedName : pandasHash } ] ) ;
98+ Reporter . expectHashes ( pandasHash ) ;
9099 } ) ;
91100
92101 test ( 'Save document' , ( ) => {
93102 emitDocEvent ( 'import pandas\r\n' , savedEventEmitter ) ;
94103
95- expect ( Reporter . eventNames ) . to . deep . equal ( [ EventName . HASHED_PACKAGE_NAME ] ) ;
96- expect ( Reporter . properties ) . to . deep . equal ( [ { hashedName : pandasHash } ] ) ;
104+ Reporter . expectHashes ( pandasHash ) ;
97105 } ) ;
98106
99107 test ( 'from <pkg>._ import _, _' , ( ) => {
@@ -120,7 +128,7 @@ suite('Import Tracker', () => {
120128 model.set_weights(weights)` ;
121129
122130 emitDocEvent ( elephas , savedEventEmitter ) ;
123- expect ( Reporter . properties ) . to . deep . equal ( [ { hashedName : elephasHash } , { hashedName : kerasHash } ] ) ;
131+ Reporter . expectHashes ( elephasHash , kerasHash ) ;
124132 } ) ;
125133
126134 test ( 'from <pkg>._ import _' , ( ) => {
@@ -142,7 +150,7 @@ suite('Import Tracker', () => {
142150 print("Training set accuracy = " + str(evaluator.evaluate(predictionAndLabels)))` ;
143151
144152 emitDocEvent ( pyspark , savedEventEmitter ) ;
145- expect ( Reporter . properties ) . to . deep . equal ( [ { hashedName : pysparkHash } , { hashedName : sparkdlHash } ] ) ;
153+ Reporter . expectHashes ( pysparkHash , sparkdlHash ) ;
146154 } ) ;
147155
148156 test ( 'import <pkg> as _' , ( ) => {
@@ -158,7 +166,7 @@ def simplify_ages(df):
158166 df.Age = categories
159167 return df` ;
160168 emitDocEvent ( code , savedEventEmitter ) ;
161- expect ( Reporter . properties ) . to . deep . equal ( [ { hashedName : pandasHash } , { hashedName : numpyHash } , { hashedName : randomHash } ] ) ;
169+ Reporter . expectHashes ( pandasHash , numpyHash , randomHash ) ;
162170 } ) ;
163171
164172 test ( 'from <pkg> import _' , ( ) => {
@@ -172,14 +180,13 @@ x = np.array([r * np.cos(theta) for r in radius])
172180y = np.array([r * np.sin(theta) for r in radius])
173181z = np.array([drumhead_height(1, 1, r, theta, 0.5) for r in radius])` ;
174182 emitDocEvent ( code , savedEventEmitter ) ;
175- expect ( Reporter . properties ) . to . deep . equal ( [ { hashedName : scipyHash } ] ) ;
183+ Reporter . expectHashes ( scipyHash ) ;
176184 } ) ;
177185
178-
179186 test ( 'from <pkg> import _ as _' , ( ) => {
180187 const code = `from pandas import DataFrame as df` ;
181188 emitDocEvent ( code , savedEventEmitter ) ;
182- expect ( Reporter . properties ) . to . deep . equal ( [ { hashedName : pandasHash } ] ) ;
189+ Reporter . expectHashes ( pandasHash ) ;
183190 } ) ;
184191
185192 test ( 'import <pkg1>, <pkg2>' , ( ) => {
@@ -193,27 +200,7 @@ x = np.array([r * np.cos(theta) for r in radius])
193200y = np.array([r * np.sin(theta) for r in radius])
194201z = np.array([drumhead_height(1, 1, r, theta, 0.5) for r in radius])` ;
195202 emitDocEvent ( code , savedEventEmitter ) ;
196- expect ( Reporter . properties ) . to . deep . equal ( [ { hashedName : sklearnHash } , { hashedName : pandasHash } ] ) ;
197- } ) ;
198-
199- /*test('from <pkg> import (_, _)', () => {
200- const code = `from pandas import (DataFrame, Series)`;
201- emitDocEvent(code, savedEventEmitter);
202- expect(Reporter.properties).to.deep.equal([{hashedName: pandasHash}]);
203- });
204-
205- test('from <pkg> import (_,', () => {
206- const code = `
207- from pandas import (DataFrame,
208- Series)`;
209- emitDocEvent(code, savedEventEmitter);
210- expect(Reporter.properties).to.deep.equal([{hashedName: pandasHash}]);
211- });*/
212-
213- test ( 'import pkg # Comment' , ( ) => {
214- const code = `import pandas # Because we wants it.` ;
215- emitDocEvent ( code , savedEventEmitter ) ;
216- expect ( Reporter . properties ) . to . deep . equal ( [ { hashedName : pandasHash } ] ) ;
203+ Reporter . expectHashes ( sklearnHash , pandasHash ) ;
217204 } ) ;
218205
219206 test ( 'Import from within a function' , ( ) => {
@@ -227,36 +214,36 @@ x = np.array([r * np.cos(theta) for r in radius])
227214y = np.array([r * np.sin(theta) for r in radius])
228215z = np.array([drumhead_height(1, 1, r, theta, 0.5) for r in radius])` ;
229216 emitDocEvent ( code , savedEventEmitter ) ;
230- expect ( Reporter . properties ) . to . deep . equal ( [ { hashedName : sklearnHash } ] ) ;
217+ Reporter . expectHashes ( sklearnHash ) ;
231218 } ) ;
232219
233220 test ( 'Do not send the same package twice' , ( ) => {
234221 const code = `
235222import pandas
236223import pandas` ;
237224 emitDocEvent ( code , savedEventEmitter ) ;
238- expect ( Reporter . properties ) . to . deep . equal ( [ { hashedName : pandasHash } ] ) ;
225+ Reporter . expectHashes ( pandasHash ) ;
239226 } ) ;
240227
241228 test ( 'Ignore relative imports' , ( ) => {
242229 const code = 'from .pandas import not_real' ;
243230 emitDocEvent ( code , savedEventEmitter ) ;
244- expect ( Reporter . properties ) . to . deep . equal ( [ ] ) ;
231+ Reporter . expectHashes ( ) ;
245232 } ) ;
246233
247234 test ( 'Ignore docstring for `from` imports' , ( ) => {
248235 const code = `"""
249236from numpy import the random function
250237"""` ;
251238 emitDocEvent ( code , savedEventEmitter ) ;
252- expect ( Reporter . properties ) . to . deep . equal ( [ ] ) ;
239+ Reporter . expectHashes ( ) ;
253240 } ) ;
254241
255242 test ( 'Ignore docstring for `import` imports' , ( ) => {
256243 const code = `"""
257244import numpy for all the things
258245"""` ;
259246 emitDocEvent ( code , savedEventEmitter ) ;
260- expect ( Reporter . properties ) . to . deep . equal ( [ ] ) ;
247+ Reporter . expectHashes ( ) ;
261248 } ) ;
262249} ) ;
0 commit comments