From fda9d0c60df1f557275c0a19b492d2f945dec1b7 Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Fri, 29 Aug 2014 16:41:29 +0200 Subject: [PATCH] Bug 12609: Add some unit tests for C4::Ratings Signed-off-by: Jonathan Druart --- C4/Ratings.pm | 2 +- t/db_dependent/Ratings.t | 146 ++++++++++++++++++++++++++++------------------- 2 files changed, 88 insertions(+), 60 deletions(-) diff --git a/C4/Ratings.pm b/C4/Ratings.pm index f368bd8..3a618ac 100644 --- a/C4/Ratings.pm +++ b/C4/Ratings.pm @@ -178,10 +178,10 @@ sub GetRating { borrowernumber => $borrowernumber, } ); + return unless $rating; $rating_hash{'rating_value'} = $rating->rating_value(); } else { - $rating_hash{rating_borrowernumber} = undef; $rating_hash{rating_value} = undef; } diff --git a/t/db_dependent/Ratings.t b/t/db_dependent/Ratings.t index 23bde86..f8a2d71 100755 --- a/t/db_dependent/Ratings.t +++ b/t/db_dependent/Ratings.t @@ -1,63 +1,91 @@ #!/usr/bin/perl -use strict; -use warnings; -use Test::More tests => 12; +use Modern::Perl; +use Test::More tests => 14; use C4::Members; -BEGIN { - - use FindBin; - use C4::Ratings; - use_ok('C4::Ratings'); - - DelRating( 1, 901 ); - DelRating( 1, 902 ); - - my $rating5 = GetRating( 1, undef ); - ok( defined $rating5, 'get a rating, without borrowernumber' ); - - my $borrower102 = GetMember( borrowernumber => 102); - my $borrower103 = GetMember( borrowernumber => 103); - SKIP: { - skip 'Missing test borrowers, skipping specific tests', 10 unless ( defined $borrower102 && defined $borrower103 ); - my $rating1 = AddRating( 1, 102, 3 ); - my $rating2 = AddRating( 1, 103, 4 ); - my $rating3 = ModRating( 1, 102, 5 ); - my $rating4 = GetRating( 1, 103 ); - my $rating6 = DelRating( 1, 102 ); - my $rating7 = DelRating( 1, 103 ); - - ok( defined $rating1, 'add a rating' ); - ok( defined $rating2, 'add another rating' ); - ok( defined $rating3, 'update a rating' ); - ok( defined $rating4, 'get a rating, with borrowernumber' ); - ok( defined $rating6, 'delete a rating' ); - ok( defined $rating7, 'delete another rating' ); - - ok( $rating3->{'rating_avg'} == '4', "get a bib's average(float) rating" ); - ok( $rating3->{'rating_avg_int'} == 4.5, "get a bib's average(int) rating" ); - ok( $rating3->{'rating_total'} == 2, "get a bib's total number of ratings" ); - ok( $rating3->{'rating_value'} == 5, "verify user's bib rating" ); - } - -} - -=c - -mason@xen1:~/g/head$ perl t/db_dependent/Ratings.t -1..12 -ok 1 - use C4::Ratings; -ok 2 - add a rating -ok 3 - add another rating -ok 4 - update a rating -ok 5 - get a rating, with borrowernumber -ok 6 - get a rating, without borrowernumber -ok 7 - get a bib's average(float) rating -ok 8 - get a bib's average(int) rating -ok 9 - get a bib's total number of ratings -ok 10 - verify user's bib rating -ok 11 - delete a rating -ok 12 - delete another rating - -=cut +use C4::Context; +use C4::Category; + +use_ok('C4::Ratings'); + +my $dbh = C4::Context->dbh; +$dbh->{RaiseError} = 1; +$dbh->{AutoCommit} = 0; + +my @categories = C4::Category->all; +my $categorycode = $categories[0]->categorycode; +my $branchcode = 'CPL'; + +my %john_doe = ( + cardnumber => '123456', + firstname => 'John', + surname => 'Doe', + categorycode => $categorycode, + branchcode => $branchcode, + dateofbirth => '', + dateexpiry => '9999-12-31', + userid => 'john.doe' +); + +my %jane_doe = ( + cardnumber => '345678', + firstname => 'Jane', + surname => 'Doe', + categorycode => $categorycode, + branchcode => $branchcode, + dateofbirth => '', + dateexpiry => '9999-12-31', + userid => 'jane.doe' +); + +my $borrowernumber1 = AddMember(%john_doe); +my $borrowernumber2 = AddMember(%jane_doe); + +my $rating1 = AddRating( 1, $borrowernumber1, 3 ); +my $rating2 = AddRating( 1, $borrowernumber2, 4 ); +my $rating3 = ModRating( 1, $borrowernumber1, 5 ); +my $rating4 = GetRating( 1, $borrowernumber2 ); +my $rating5 = GetRating(1); + +ok( defined $rating1, 'add a rating' ); +ok( defined $rating2, 'add another rating' ); +ok( defined $rating3, 'update a rating' ); +ok( defined $rating4, 'get a rating, with borrowernumber' ); + +ok( $rating3->{'rating_avg'} == '4', "get a bib's average(float) rating" ); +ok( $rating3->{'rating_avg_int'} == 4.5, "get a bib's average(int) rating" ); +ok( $rating3->{'rating_total'} == 2, "get a bib's total number of ratings" ); +ok( $rating3->{'rating_value'} == 5, "verify user's bib rating" ); + +my $rating_1 = GetRating(1); +my $rating_1_1 = GetRating( 1, $borrowernumber1 ); +is_deeply( + $rating_1, + { + rating_avg_int => 4.5, + rating_total => 2, + rating_avg => 4, + rating_value => undef, + }, + 'GetRating should return total, avg_int and avg if biblionumber is given' +); +is_deeply( + $rating_1_1, + { + rating_avg_int => 4.5, + rating_total => 2, + rating_avg => 4, + rating_value => 5, + }, +'GetRating should return total, avg_int, avg and value if biblionumber is given' +); + +my $rating6 = DelRating( 1, $borrowernumber1 ); +my $rating7 = DelRating( 1, $borrowernumber2 ); + +ok( defined $rating6, 'delete a rating' ); +ok( defined $rating7, 'delete another rating' ); + +is( GetRating( 1, $borrowernumber1 ), + undef, 'GetRating should return undef if no rating exist' ); -- 2.0.0.rc2