<commit_before><commit_msg>added tests for genomes_to_md5s<commit_after>#!/usr/bin/env perl

use strict;
use warnings;

use Test::More;

use CDMI_APIClient;
use CDMI_EntityAPIClient;

my $url = 'http://192.168.127.12:5000';

my $cdmie = CDMI_EntityAPIClient->new($url);
ok($cdmie, "Got CDMI_EntityAPIClient");

my $cdmi = CDMI_APIClient->new($url);
ok($cdmi, "Got CDMI_APIClient");

my $genomes = $cdmie->all_entities_Genome(0,10000,['id']);
ok(keys %$genomes, "Loaded up some genomes");

my $sample_genomes = [
	{'id' => 'kb|g.3093', 'hash' => '94518bb814a8a79e60637ee6f92ce3bd'},
	{'id' => 'kb|g.5458', 'hash' => '15ade9db34c4a556f56aa39805192a9f'},
	{'id' => 'kb|g.1554', 'hash' => '4a1b448b1dc98b56dec5dcb092d49f4d'},
	{'id' => 'kb|g.2620', 'hash' => 'dc4fb05c22adbc14908f9c857a2ad9f5'},
];

ok($genomes->{$sample_genomes->[0]->{'id'}}, "Found known genome $sample_genomes->[0]->{'id'}");
is($cdmi->genomes_to_md5s([$genomes->{$sample_genomes->[0]->{'id'}}->{'id'}])->{$sample_genomes->[0]->{'id'}},
	$sample_genomes->[0]->{'hash'},
	"Correct md5 for $sample_genomes->[0]->{'id'}"
);

ok($genomes->{$sample_genomes->[1]->{'id'}}, "Found known genome $sample_genomes->[1]->{'id'}");
is($cdmi->genomes_to_md5s([$genomes->{$sample_genomes->[1]->{'id'}}->{'id'}])->{$sample_genomes->[1]->{'id'}},
	$sample_genomes->[1]->{'hash'},
	"Correct md5 for $sample_genomes->[1]->{'id'}"
);

ok($genomes->{$sample_genomes->[2]->{'id'}}, "Found known genome $sample_genomes->[2]->{'id'}");
is($cdmi->genomes_to_md5s([$genomes->{$sample_genomes->[2]->{'id'}}->{'id'}])->{$sample_genomes->[2]->{'id'}},
	$sample_genomes->[2]->{'hash'},
	"Correct md5 for $sample_genomes->[2]->{'id'}"
);

ok($genomes->{$sample_genomes->[3]->{'id'}}, "Found known genome $sample_genomes->[3]->{'id'}");
is($cdmi->genomes_to_md5s([$genomes->{$sample_genomes->[3]->{'id'}}->{'id'}])->{$sample_genomes->[3]->{'id'}},
	$sample_genomes->[3]->{'hash'},
	"Correct md5 for $sample_genomes->[3]->{'id'}"
);

#give it a few at once.
my $results = $cdmi->genomes_to_md5s([$sample_genomes->[0]->{'id'}, $sample_genomes->[1]->{'id'}]);
is($results->{$sample_genomes->[0]->{'id'}}, $sample_genomes->[0]->{'hash'}, "Correct multi-md5 for $sample_genomes->[0]->{'id'}");
is($results->{$sample_genomes->[1]->{'id'}}, $sample_genomes->[1]->{'hash'}, "Correct multi-md5 for $sample_genomes->[1]->{'id'}");

#ok. Now let's try to break it with invalid data.
eval {$cdmi->genomes_to_md5s};
like($@, qr/Invalid argument count \(expecting 1\)/, "Must give genomes_to_md5s an arrayref of genomes (not scalar)");

eval {$cdmi->genomes_to_md5s($sample_genomes->[0]->{'id'}, $sample_genomes->[1]->{'id'})};
like($@, qr/Invalid argument count \(expecting 1\)/, "Must give genomes_to_md5s an arrayref of genomes (not array)");

eval {$cdmi->genomes_to_md5s('genome' => $sample_genomes->[0]->{'id'})};
like($@, qr/Invalid argument count \(expecting 1\)/, "Must give genomes_to_md5s an arrayref of genomes (not hash)");

eval {$cdmi->genomes_to_md5s({'genome' => $sample_genomes->[0]->{'id'}})};
my $res2 = $cdmi->genomes_to_md5s({'genome' => $sample_genomes->[0]->{'id'}});
like($@, qr/Invalid argument count \(expecting 1\)/, "Must give genomes_to_md5s an arrayref of genomes (not hashref)");

is(scalar keys %{$cdmi->genomes_to_md5s([])}, 0, "genomes_to_md5s w/empty arrayref returns empty hashref");

my $invalid_genome_id = 'q38 exploding space modulator';
my $invalid_genome_results = $cdmi->genomes_to_md5s([$invalid_genome_id]);
ok($invalid_genome_results, "Got results for invalid genome id");
ok(! defined $invalid_genome_results->{'invalid_genome_id'}, "No MD5 for invalid genome ID ($invalid_genome_id)");

print "$@\n";

{'fraction_non_alphanumeric': 0.26041950422228277, 'fraction_numerical': 0.04930536638518115, 'mean_word_length': 7.196428571428571, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 122, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 1, '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': '4104493', 'n_tokens_mistral': 1586, 'n_tokens_neox': 1502, 'n_words': 246}