@@ -14,7 +14,7 @@ limitations under the License.
1414==============================================================================*/
1515namespace tf_categorization_utils {
1616
17- const assert = chai . assert ;
17+ const { assert, expect } = chai ;
1818
1919describe ( 'categorizationUtils' , ( ) => {
2020 const { CategoryType} = tf_categorization_utils ;
@@ -195,6 +195,201 @@ describe('categorizationUtils', () => {
195195 } ) ;
196196 } ) ;
197197
198+ describe ( 'categorizeSelection' , ( ) => {
199+ const { categorizeSelection} = tf_categorization_utils ;
200+
201+ beforeEach ( function ( ) {
202+ const tag1 = {
203+ id : 1 , pluginName : 'scalar' ,
204+ name : 'tag1' , displayName : 'tag1' ,
205+
206+ } ;
207+ const tag2_1 = {
208+ id : 2 , pluginName : 'scalar' ,
209+ name : 'tag2/subtag1' , displayName : 'tag2/subtag1' ,
210+
211+ } ;
212+ const tag2_2 = {
213+ id : 3 , pluginName : 'scalar' ,
214+ name : 'tag2/subtag2' , displayName : 'tag2/subtag2' ,
215+
216+ } ;
217+ const tag3 = {
218+ id : 4 , pluginName : 'scalar' ,
219+ name : 'tag3' , displayName : 'tag3' ,
220+ } ;
221+ const tag4 = {
222+ id : 5 , pluginName : 'custom_scalar' ,
223+ name : 'tag4' , displayName : 'tag4' ,
224+ } ;
225+
226+ this . run1 = { id : 1 , name : 'run1' , startTime : 10 , tags : [ tag1 , tag4 ] } ;
227+ this . run2 = { id : 2 , name : 'run2' , startTime : 5 , tags : [ tag2_1 , tag2_2 ] } ;
228+ this . run3 = { id : 3 , name : 'run3' , startTime : 0 , tags : [ tag2_1 , tag3 ] } ;
229+
230+ this . experiment1 = {
231+ experiment : { id : 1 , name : 'exp1' , startTime : 0 } ,
232+ runs : [ this . run1 , this . run2 ] ,
233+ tagRegex : '' ,
234+ } ;
235+ this . experiment2 = {
236+ experiment : { id : 2 , name : 'exp2' , startTime : 0 } ,
237+ runs : [ this . run2 , this . run3 ] ,
238+ tagRegex : '(subtag1|tag3)' ,
239+ } ;
240+ this . experiment3 = {
241+ experiment : { id : 3 , name : 'exp3' , startTime : 0 } ,
242+ runs : [ this . run1 , this . run2 , this . run3 ] ,
243+ tagRegex : 'junk' ,
244+ } ;
245+ } ) ;
246+
247+ it ( 'merges the results of the query and the prefix groups' , function ( ) {
248+ const result = categorizeSelection (
249+ [ this . experiment1 ] , 'scalar' ) ;
250+
251+ expect ( result ) . to . have . lengthOf ( 3 ) ;
252+ expect ( result [ 0 ] ) . to . have . property ( 'metadata' )
253+ . that . has . property ( 'type' , CategoryType . SEARCH_RESULTS ) ;
254+
255+ expect ( result [ 1 ] ) . to . have . property ( 'metadata' )
256+ . that . has . property ( 'type' , CategoryType . PREFIX_GROUP ) ;
257+ expect ( result [ 2 ] ) . to . have . property ( 'metadata' )
258+ . that . has . property ( 'type' , CategoryType . PREFIX_GROUP ) ;
259+ } ) ;
260+
261+ describe ( 'search group' , ( ) => {
262+ it ( 'filters groups by tag with a tagRegex' , function ( ) {
263+ const [ searchResult ] = categorizeSelection (
264+ [ this . experiment2 ] , 'scalar' ) ;
265+
266+ // should match 'tag2/subtag1' and 'tag3'.
267+ expect ( searchResult ) . to . have . property ( 'items' )
268+ . that . has . lengthOf ( 2 ) ;
269+ expect ( searchResult . items [ 0 ] ) . to . have . property ( 'tag' , 'tag2/subtag1' ) ;
270+ expect ( searchResult . items [ 1 ] ) . to . have . property ( 'tag' , 'tag3' ) ;
271+ } ) ;
272+
273+ it ( 'combines selection without tagRegex with one' , function ( ) {
274+ const [ searchResult ] = categorizeSelection (
275+ [ this . experiment1 , this . experiment2 ] , 'scalar' ) ;
276+
277+ // should match 'tag1', 'tag2/subtag1', 'tag2/subtag2', and 'tag3'.
278+ expect ( searchResult ) . to . have . property ( 'items' )
279+ . that . has . lengthOf ( 4 ) ;
280+ expect ( searchResult . items [ 0 ] ) . to . have . property ( 'tag' , 'tag1' ) ;
281+ expect ( searchResult . items [ 1 ] ) . to . have . property ( 'tag' , 'tag2/subtag1' ) ;
282+ expect ( searchResult . items [ 2 ] ) . to . have . property ( 'tag' , 'tag2/subtag2' ) ;
283+ expect ( searchResult . items [ 3 ] ) . to . have . property ( 'tag' , 'tag3' ) ;
284+
285+ expect ( searchResult . items [ 1 ] ) . to . have . property ( 'series' )
286+ . that . has . lengthOf ( 3 )
287+ . and . that . deep . equal ( [
288+ { experiment : 'exp1' , run : 'run2' } ,
289+ { experiment : 'exp2' , run : 'run2' } ,
290+ { experiment : 'exp2' , run : 'run3' } ,
291+ ] ) ;
292+ } ) ;
293+
294+ it ( 'sorts the tag by name' , function ( ) {
295+ const [ searchResult ] = categorizeSelection (
296+ [ this . experiment2 , this . experiment1 ] , 'scalar' ) ;
297+
298+ // should match 'tag1', 'tag2/subtag1', 'tag2/subtag2', and 'tag3'.
299+ expect ( searchResult ) . to . have . property ( 'items' )
300+ . that . has . lengthOf ( 4 ) ;
301+ expect ( searchResult . items [ 0 ] ) . to . have . property ( 'tag' , 'tag1' ) ;
302+ expect ( searchResult . items [ 1 ] ) . to . have . property ( 'tag' , 'tag2/subtag1' ) ;
303+ expect ( searchResult . items [ 2 ] ) . to . have . property ( 'tag' , 'tag2/subtag2' ) ;
304+ expect ( searchResult . items [ 3 ] ) . to . have . property ( 'tag' , 'tag3' ) ;
305+ } ) ;
306+
307+ it ( 'returns name `multi` when there are multiple selections' , function ( ) {
308+ const [ searchResult2 ] = categorizeSelection (
309+ [ this . experiment2 ] , 'scalar' ) ;
310+ expect ( searchResult2 ) . to . have . property ( 'name' , '(subtag1|tag3)' ) ;
311+
312+ const [ searchResult1 ] = categorizeSelection (
313+ [ this . experiment1 , this . experiment2 ] , 'scalar' ) ;
314+ expect ( searchResult1 ) . to . have . property ( 'name' , 'multi' ) ;
315+ } ) ;
316+
317+ it ( 'returns an empty array when tagRegex does not match any' , function ( ) {
318+ const result = categorizeSelection (
319+ [ this . experiment3 ] , 'custom_scalar' ) ;
320+
321+ expect ( result ) . to . have . lengthOf ( 2 ) ;
322+ expect ( result [ 0 ] ) . to . have . property ( 'items' )
323+ . that . has . lengthOf ( 0 ) ;
324+ } ) ;
325+ } ) ;
326+
327+ describe ( 'prefix group' , ( ) => {
328+ it ( 'creates a group when a tag misses separator' , function ( ) {
329+ const result = categorizeSelection (
330+ [ this . experiment1 ] , 'scalar' ) ;
331+
332+ expect ( result [ 1 ] ) . to . have . property ( 'items' )
333+ . that . has . lengthOf ( 1 ) ;
334+
335+ expect ( result [ 1 ] ) . to . have . property ( 'name' , 'tag1' ) ;
336+ expect ( result [ 1 ] . items [ 0 ] ) . to . have . property ( 'tag' , 'tag1' ) ;
337+ expect ( result [ 1 ] . items [ 0 ] ) . to . have . property ( 'series' )
338+ . that . has . lengthOf ( 1 ) ;
339+ } ) ;
340+
341+ it ( 'creates a grouping when tag has a separator' , function ( ) {
342+ const result = categorizeSelection (
343+ [ this . experiment1 ] , 'scalar' ) ;
344+
345+ expect ( result [ 2 ] ) . to . have . property ( 'items' )
346+ . that . has . lengthOf ( 2 ) ;
347+
348+ expect ( result [ 2 ] ) . to . have . property ( 'name' , 'tag2' ) ;
349+ expect ( result [ 2 ] . items [ 0 ] ) . to . have . property ( 'tag' , 'tag2/subtag1' ) ;
350+ expect ( result [ 2 ] . items [ 1 ] ) . to . have . property ( 'tag' , 'tag2/subtag2' ) ;
351+ expect ( result [ 2 ] . items [ 0 ] ) . to . have . property ( 'series' )
352+ . that . has . lengthOf ( 1 ) ;
353+ } ) ;
354+
355+ it ( 'creates a group with items with experiment and run' , function ( ) {
356+ const result = categorizeSelection (
357+ [ this . experiment1 ] , 'scalar' ) ;
358+
359+ expect ( result [ 1 ] . items [ 0 ] ) . to . have . property ( 'series' )
360+ . that . has . lengthOf ( 1 )
361+ . and . that . deep . equal ( [ { experiment : 'exp1' , run : 'run1' } ] ) ;
362+ } ) ;
363+
364+ it ( 'creates distinct subitems when tags exactly match' , function ( ) {
365+ const result = categorizeSelection (
366+ [ this . experiment2 ] , 'scalar' ) ;
367+
368+ expect ( result [ 1 ] . items [ 0 ] ) . to . have . property ( 'series' )
369+ . that . has . lengthOf ( 2 )
370+ . and . that . deep . equal ( [
371+ { experiment : 'exp2' , run : 'run2' } ,
372+ { experiment : 'exp2' , run : 'run3' } ,
373+ ] ) ;
374+ } ) ;
375+
376+ it ( 'filters out tags of a different pluguin' , function ( ) {
377+ const result = categorizeSelection (
378+ [ this . experiment3 ] , 'custom_scalar' ) ;
379+
380+ expect ( result ) . to . have . lengthOf ( 2 ) ;
381+ expect ( result [ 1 ] ) . to . have . property ( 'name' , 'tag4' ) ;
382+ expect ( result [ 1 ] ) . to . have . property ( 'items' )
383+ . that . has . lengthOf ( 1 ) ;
384+ expect ( result [ 1 ] . items [ 0 ] ) . to . have . property ( 'series' )
385+ . that . has . lengthOf ( 1 )
386+ . and . that . deep . equal ( [
387+ { experiment : 'exp3' , run : 'run1' } ,
388+ ] ) ;
389+ } ) ;
390+ } ) ;
391+ } ) ;
392+
198393} ) ;
199394
200395} // namespace tf_categorization_utils
0 commit comments