Merge remote-tracking branch 'origin/new/bug_4118'
[koha.git] / t / db_dependent / Tags.t
1 #!/usr/bin/perl
2 #
3 # This Koha test module is a stub!  
4 # Add more tests here!!!
5
6 use strict;
7 use warnings;
8
9 use Test::More tests => 30;
10
11 BEGIN {
12         use_ok('C4::Tags');
13 }
14
15 # Simple 'sequential 5' test
16 my $tags = make_tags(1,2,3,4,5);
17 my @strata = (0,1,2,3,4);
18 my ($min, $max) = C4::Tags::stratify_tags(5, $tags);
19 check_tag_strata($tags, \@strata, 'Sequential 5');
20 is($min, 0, 'Sequential 5 min');
21 is($max, 4, 'Sequential 5 max');
22
23 # Reverse test - should have the same results as previous
24 $tags = make_tags(5,4,3,2,1);
25 @strata = (4,3,2,1,0);
26 ($min, $max) = C4::Tags::stratify_tags(5, $tags);
27 check_tag_strata($tags, \@strata, 'Reverse Sequential 5');
28 is($min, 0, 'Sequential 5 min');
29 is($max, 4, 'Sequential 5 max');
30
31 # All the same test - should all have the same results
32 $tags = make_tags(4,4,4,4,4);
33 @strata = (0,0,0,0,0);
34 ($min, $max) = C4::Tags::stratify_tags(5, $tags);
35 check_tag_strata($tags, \@strata, 'All The Same');
36 is($min, 0, 'Sequential 5 min');
37 is($max, 0, 'Sequential 5 max');
38
39 # Some the same, some different
40 $tags = make_tags(1,2,2,3,3,8);
41 @strata = (0,0,0,1,1,4);
42 ($min, $max) = C4::Tags::stratify_tags(5, $tags);
43 check_tag_strata($tags, \@strata, 'All The Same');
44 is($min, 0, 'Sequential 5 min');
45 is($max, 7, 'Sequential 5 max');
46
47 # Runs tests against the results
48 sub check_tag_strata {
49     my ($tags, $expected, $name) = @_;
50
51     foreach my $t (@$tags) {
52         my $w = $t->{weight_total};
53         my $s = $t->{stratum};
54         is($s, shift @$expected, $name . " - $w ($s)");
55     }
56 }
57
58 # Makes some tags with just enough info to test
59 sub make_tags {
60     my @res;
61     while (@_) {
62         push @res, { weight_total => shift @_ };
63     }
64     return \@res;
65 }