@@ -69,7 +69,9 @@ describe("PascalVOC Json Export Provider", () => {
6969 beforeEach ( ( ) => {
7070 const assetServiceMock = AssetService as jest . Mocked < typeof AssetService > ;
7171 assetServiceMock . prototype . getAssetMetadata = jest . fn ( ( asset ) => {
72- const mockTag = MockFactory . createTestTag ( ) ;
72+ const mockTag1 = MockFactory . createTestTag ( "1" ) ;
73+ const mockTag2 = MockFactory . createTestTag ( "2" ) ;
74+ const mockTag = Number ( asset . id . split ( "-" ) [ 1 ] ) > 7 ? mockTag1 : mockTag2 ;
7375 const mockRegion1 = MockFactory . createTestRegion ( "region-1" , [ mockTag . name ] ) ;
7476 const mockRegion2 = MockFactory . createTestRegion ( "region-2" , [ mockTag . name ] ) ;
7577
@@ -352,27 +354,70 @@ describe("PascalVOC Json Export Provider", () => {
352354 } ;
353355
354356 const testProject = { ...baseTestProject } ;
355- const testAssets = MockFactory . createTestAssets ( 10 , 0 ) ;
357+ const testAssets = MockFactory . createTestAssets ( 13 , 0 ) ;
356358 testAssets . forEach ( ( asset ) => asset . state = AssetState . Tagged ) ;
357359 testProject . assets = _ . keyBy ( testAssets , ( asset ) => asset . id ) ;
358- testProject . tags = [ MockFactory . createTestTag ( "1" ) ] ;
360+ testProject . tags = MockFactory . createTestTags ( 3 ) ;
359361
360362 const exportProvider = new PascalVOCExportProvider ( testProject , options ) ;
363+ const getAssetsSpy = jest . spyOn ( exportProvider , "getAssetsForExport" ) ;
364+
361365 await exportProvider . export ( ) ;
362366
363367 const storageProviderMock = LocalFileSystemProxy as any ;
364368 const writeTextFileCalls = storageProviderMock . mock . instances [ 0 ] . writeText . mock . calls as any [ ] ;
365369
366- const valDataIndex = writeTextFileCalls
370+ const valDataIndex1 = writeTextFileCalls
367371 . findIndex ( ( args ) => args [ 0 ] . endsWith ( "/ImageSets/Main/Tag 1_val.txt" ) ) ;
368- const trainDataIndex = writeTextFileCalls
372+ const trainDataIndex1 = writeTextFileCalls
369373 . findIndex ( ( args ) => args [ 0 ] . endsWith ( "/ImageSets/Main/Tag 1_train.txt" ) ) ;
370-
371- const expectedTrainCount = ( testTrainSplit / 100 ) * testAssets . length ;
372- const expectedTestCount = ( ( 100 - testTrainSplit ) / 100 ) * testAssets . length ;
373-
374- expect ( writeTextFileCalls [ valDataIndex ] [ 1 ] . split ( "\n" ) ) . toHaveLength ( expectedTestCount ) ;
375- expect ( writeTextFileCalls [ trainDataIndex ] [ 1 ] . split ( "\n" ) ) . toHaveLength ( expectedTrainCount ) ;
374+ const valDataIndex2 = writeTextFileCalls
375+ . findIndex ( ( args ) => args [ 0 ] . endsWith ( "/ImageSets/Main/Tag 2_val.txt" ) ) ;
376+ const trainDataIndex2 = writeTextFileCalls
377+ . findIndex ( ( args ) => args [ 0 ] . endsWith ( "/ImageSets/Main/Tag 2_train.txt" ) ) ;
378+
379+ const assetsToExport = await getAssetsSpy . mock . results [ 0 ] . value ;
380+ const trainArray = [ ] ;
381+ const testArray = [ ] ;
382+ const tagsAssestList : {
383+ [ index : string ] : {
384+ assetSet : Set < string > ,
385+ testArray : string [ ] ,
386+ trainArray : string [ ] ,
387+ } ,
388+ } = { } ;
389+ testProject . tags . forEach ( ( tag ) =>
390+ tagsAssestList [ tag . name ] = {
391+ assetSet : new Set ( ) , testArray : [ ] ,
392+ trainArray : [ ] ,
393+ } ) ;
394+ assetsToExport . forEach ( ( assetMetadata ) => {
395+ assetMetadata . regions . forEach ( ( region ) => {
396+ region . tags . forEach ( ( tagName ) => {
397+ if ( tagsAssestList [ tagName ] ) {
398+ tagsAssestList [ tagName ] . assetSet . add ( assetMetadata . asset . name ) ;
399+ }
400+ } ) ;
401+ } ) ;
402+ } ) ;
403+
404+ for ( const tagKey of Object . keys ( tagsAssestList ) ) {
405+ const assetSet = tagsAssestList [ tagKey ] . assetSet ;
406+ const testCount = Math . ceil ( ( ( 100 - testTrainSplit ) / 100 ) * assetSet . size ) ;
407+ tagsAssestList [ tagKey ] . testArray = Array . from ( assetSet ) . slice ( 0 , testCount ) ;
408+ tagsAssestList [ tagKey ] . trainArray = Array . from ( assetSet ) . slice ( testCount , assetSet . size ) ;
409+ testArray . push ( ...tagsAssestList [ tagKey ] . testArray ) ;
410+ trainArray . push ( ...tagsAssestList [ tagKey ] . trainArray ) ;
411+ }
412+
413+ expect ( writeTextFileCalls [ valDataIndex1 ] [ 1 ] . split ( / \r ? \n / ) . filter ( ( line ) =>
414+ line . endsWith ( " 1" ) ) ) . toHaveLength ( tagsAssestList [ "Tag 1" ] . testArray . length ) ;
415+ expect ( writeTextFileCalls [ trainDataIndex1 ] [ 1 ] . split ( / \r ? \n / ) . filter ( ( line ) =>
416+ line . endsWith ( " 1" ) ) ) . toHaveLength ( tagsAssestList [ "Tag 1" ] . trainArray . length ) ;
417+ expect ( writeTextFileCalls [ valDataIndex2 ] [ 1 ] . split ( / \r ? \n / ) . filter ( ( line ) =>
418+ line . endsWith ( " 1" ) ) ) . toHaveLength ( tagsAssestList [ "Tag 2" ] . testArray . length ) ;
419+ expect ( writeTextFileCalls [ trainDataIndex2 ] [ 1 ] . split ( / \r ? \n / ) . filter ( ( line ) =>
420+ line . endsWith ( " 1" ) ) ) . toHaveLength ( tagsAssestList [ "Tag 2" ] . trainArray . length ) ;
376421 }
377422
378423 it ( "Correctly generated files based on 50/50 test / train split" , async ( ) => {
0 commit comments