X-Git-Url: http://git.rot13.org/?a=blobdiff_plain;f=t%2Fdb_dependent%2FTags.t;h=5612d3ac1ae99ed18b8c2bd10a512eaf55a79343;hb=01c7c2a12904fae5076914d839c5e829c54deb6f;hp=83b3c9bd3285baa5340ced07bea135e1c98ece2d;hpb=76d52d89e4b5a25454a65198e50f2fe0475566bd;p=koha.git diff --git a/t/db_dependent/Tags.t b/t/db_dependent/Tags.t index 83b3c9bd32..5612d3ac1a 100755 --- a/t/db_dependent/Tags.t +++ b/t/db_dependent/Tags.t @@ -6,9 +6,67 @@ use strict; use warnings; -use Test::More tests => 1; +use Test::More tests => 32; BEGIN { use_ok('C4::Tags'); } +# Check no tags case. +my @tagsarray; +my $tags = \@tagsarray; +my ($min, $max) = C4::Tags::stratify_tags(0, $tags); +is($min, 0, 'Empty array min'); +is($max, 0, 'Empty array max'); + +# Simple 'sequential 5' test +$tags = make_tags(1,2,3,4,5); +my @strata = (0,1,2,3,4); +($min, $max) = C4::Tags::stratify_tags(5, $tags); +check_tag_strata($tags, \@strata, 'Sequential 5'); +is($min, 0, 'Sequential 5 min'); +is($max, 4, 'Sequential 5 max'); + +# Reverse test - should have the same results as previous +$tags = make_tags(5,4,3,2,1); +@strata = (4,3,2,1,0); +($min, $max) = C4::Tags::stratify_tags(5, $tags); +check_tag_strata($tags, \@strata, 'Reverse Sequential 5'); +is($min, 0, 'Sequential 5 min'); +is($max, 4, 'Sequential 5 max'); + +# All the same test - should all have the same results +$tags = make_tags(4,4,4,4,4); +@strata = (0,0,0,0,0); +($min, $max) = C4::Tags::stratify_tags(5, $tags); +check_tag_strata($tags, \@strata, 'All The Same'); +is($min, 0, 'Sequential 5 min'); +is($max, 0, 'Sequential 5 max'); + +# Some the same, some different +$tags = make_tags(1,2,2,3,3,8); +@strata = (0,0,0,1,1,4); +($min, $max) = C4::Tags::stratify_tags(5, $tags); +check_tag_strata($tags, \@strata, 'All The Same'); +is($min, 0, 'Sequential 5 min'); +is($max, 7, 'Sequential 5 max'); + +# Runs tests against the results +sub check_tag_strata { + my ($tags, $expected, $name) = @_; + + foreach my $t (@$tags) { + my $w = $t->{weight_total}; + my $s = $t->{stratum}; + is($s, shift @$expected, $name . " - $w ($s)"); + } +} + +# Makes some tags with just enough info to test +sub make_tags { + my @res; + while (@_) { + push @res, { weight_total => shift @_ }; + } + return \@res; +}