<commit_before>#!/usr/bin/perl -w
use strict;
use warnings;
use lib "test/script-tests/";

use Data::Dumper;
use Test::More 'no_plan';
use JSON;

# script to test
my $script = "mg-biom-merge" ;

# assumption scripts are always in path
my $topDir 			= $ENV{KB_TOP} || "/";
my $service_repo 	= "communities_api" ;
my $script_path 	= $topDir . "" ;
my $num_tests   	= 0;
my $json        	= new JSON;
my $success     	= 1;
my $test_data_path 	= shift @ARGV || join "/" , $topDir , "dev_container/modules" , $service_repo , "test/data" ;
my $test_out_path  	= shift @ARGV || "./" ;
my $create_test_data = 1;



# test if script is in path
ok (`which $script` , "$script is deployed") ;
ok (`$script --help`, "$script executes with --help") ;

# test if script can retrieve data for ids from test list 
# mg-compare-functions --ids <IDs> --source Subsystems --format biom --evalue EVALUE
# changing parameters are:
# - ids: single id from wgs test data list
# - evalue: 5 , 10 , 15




# list of all ids
my @list ;
open(IDs , "$test_data_path/ids.wgs.txt") or die "No test data file $test_data_path/ids.wgs.txt\n" ;

while (my $id = <IDs>){
	chomp $id ;
	push @list , $id ;
}

close(IDs);

# loop throug list and create triples

while ( @list >= 3){
	
	# create triple, slide through id list
	my @triple = (shift @list , $list[0] , $list[1]) ;
	
	# not used yet
	my $source = "Subsystems" ;
	
	# loop through existing profiles/parameters, retrieve profiles and merge them
	foreach my $evalue ("5","10","15" ) {
		ok(get_data(\@triple,$evalue,$source) , "object for id ". join(" " , @triple ) ." and value $evalue") ;
		subtest get_data => sub { get_data(\@triple,$evalue) } ;
	}
	
}

close(IDs);



sub get_data{
	my ($ids, $value) 	= @_;
	my $success 		= undef ;
	
	# create test data
	# system("$script --ids $id --source Subsystems --format biom --evalue $value > $test_data_path/$id.$value.$script") if ($create_test_data); 
	
	#  mg-biom-merge [ --help --retain_dup_ids ] biom1 biom2 [ biom3 biom4 ... ]

	my @tmp ;
	foreach my $i (@$ids){
		push @tmp , "$test_data_path/$i.$value.mg-compare-function" ;
	}
	my $list   = join " " , @tmp ;
	my $prefix = join "-" , @tmp ;
	
	my $txt = `$script $list` ;
	
	
	# check if txt is json
	eval{
		my $o = $json->decode($txt) ;
		# print Dumper $o ;
		
		# modify $o, remove timestamps
		$o->{date} = 'test date' ;
		
		# dump into file
		open(OUT , ">$test_out_path/$prefix.$value.$script") or die "Can't write to $test_out_path/$prefix.$value.$script" ;
		print OUT $json->encode($o) ;
		close(OUT) ;
		
		# create test data
		if ($create_test_data){
			open(OUT , ">$test_data_path/$prefix.$value.$script") or die "Can't write to $test_data_path/$prefix.$value.$script" ;
			print OUT $json->encode($o) ;
			close(OUT) ;
		}
		
	};
		
	ok(!$@ , 'Valid return structure') if ($txt);
	diag($@) if ($@);<commit_msg>debug output<commit_after>#!/usr/bin/perl -w
use strict;
use warnings;
use lib "test/script-tests/";

use Data::Dumper;
use Test::More 'no_plan';
use JSON;

# script to test
my $script = "mg-biom-merge" ;

# assumption scripts are always in path
my $topDir 			= $ENV{KB_TOP} || "/";
my $service_repo 	= "communities_api" ;
my $script_path 	= $topDir . "" ;
my $num_tests   	= 0;
my $json        	= new JSON;
my $success     	= 1;
my $test_data_path 	= shift @ARGV || join "/" , $topDir , "dev_container/modules" , $service_repo , "test/data" ;
my $test_out_path  	= shift @ARGV || "./" ;
my $create_test_data = 1;



# test if script is in path
ok (`which $script` , "$script is deployed") ;
ok (`$script --help`, "$script executes with --help") ;

# test if script can retrieve data for ids from test list 
# mg-compare-functions --ids <IDs> --source Subsystems --format biom --evalue EVALUE
# changing parameters are:
# - ids: single id from wgs test data list
# - evalue: 5 , 10 , 15




# list of all ids
my @list ;
open(IDs , "$test_data_path/ids.wgs.txt") or die "No test data file $test_data_path/ids.wgs.txt\n" ;

while (my $id = <IDs>){
	chomp $id ;
	push @list , $id ;
}

close(IDs);

# loop throug list and create triples

while ( @list >= 3){
	
	# create triple, slide through id list
	my @triple = (shift @list , $list[0] , $list[1]) ;
	
	# not used yet
	my $source = "Subsystems" ;
	
	# loop through existing profiles/parameters, retrieve profiles and merge them
	foreach my $evalue ("5","10","15" ) {
		ok(get_data(\@triple,$evalue,$source) , "object for id ". join(" " , @triple ) ." and value $evalue") ;
		subtest get_data => sub { get_data(\@triple,$evalue) } ;
	}
	
}

close(IDs);



sub get_data{
	my ($ids, $value) 	= @_;
	my $success 		= undef ;
	
	# create test data
	# system("$script --ids $id --source Subsystems --format biom --evalue $value > $test_data_path/$id.$value.$script") if ($create_test_data); 
	
	#  mg-biom-merge [ --help --retain_dup_ids ] biom1 biom2 [ biom3 biom4 ... ]

	my @tmp ;
	foreach my $i (@$ids){
		push @tmp , "$test_data_path/$i.$value.mg-compare-function" ;
	}
	my $list   = join " " , @tmp ;
	my $prefix = join "-" , @tmp ;
	
	my $txt = `$script $list` ;

	print "$script $list";
	print $txt , "\n";
	
	# check if txt is json
	eval{
		my $o = $json->decode($txt) ;
		# print Dumper $o ;
		
		# modify $o, remove timestamps
		$o->{date} = 'test date' ;
		
		# dump into file
		open(OUT , ">$test_out_path/$prefix.$value.$script") or die "Can't write to $test_out_path/$prefix.$value.$script" ;
		print OUT $json->encode($o) ;
		close(OUT) ;
		
		# create test data
		if ($create_test_data){
			open(OUT , ">$test_data_path/$prefix.$value.$script") or die "Can't write to $test_data_path/$prefix.$value.$script" ;
			print OUT $json->encode($o) ;
			close(OUT) ;
		}
		
	};
		
	ok(!$@ , 'Valid return structure') if ($txt);
	diag($@) if ($@);
{'fraction_non_alphanumeric': 0.17402642559109874, 'fraction_numerical': 0.006954102920723227, 'mean_word_length': 3.283693224125093, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5429856', 'n_tokens_mistral': 2300, 'n_tokens_neox': 2115, 'n_words': 619}