Bug 33947: Remove GetAllIssues
[koha.git] / t / db_dependent / SocialData.t
1 #!/usr/bin/perl
2
3 # Copyright 2012, 2023 Koha development team
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20 use Modern::Perl;
21 use Test::More tests => 2;
22
23 use t::lib::TestBuilder;
24
25 use Koha::Database;
26 use C4::SocialData qw( get_data get_report );
27
28 my $schema  = Koha::Database->new->schema;
29 my $builder = t::lib::TestBuilder->new;
30
31 $schema->storage->txn_begin;
32
33 # trivial data for trivial tests
34 Koha::Biblioitems->search->update({ isbn => undef });
35 $builder->build({ source => 'Biblioitem', value => { isbn => '0-596-52674-1' } });
36 $builder->build({ source => 'Biblioitem', value => { isbn => '0-596-00289-0' } });
37 $builder->build({ source => 'SocialData', value => { isbn => '0-596-52674-1', score_avg => 6.5 } });
38 $builder->build({ source => 'SocialData', value => { isbn => '0-596-00289-0', score_avg => 7 } });
39
40 subtest 'get_data' => sub {
41     plan tests => 3;
42
43     my $data = C4::SocialData::get_data();
44     is( $data, undef, 'get_data should return undef if no param given');
45
46     $data = C4::SocialData::get_data('0-596-52674-1');
47     is( $data->{isbn}, '0-596-52674-1', 'get_data should return the matching row');
48     is( sprintf("%3.1f", $data->{score_avg}), 6.5, 'check score_avg');
49 };
50
51 subtest 'get_report' => sub {
52     plan tests => 3;
53
54     my $report =  C4::SocialData::get_report();
55     # if isbn not normalized, social data not found, resulting in without key
56     is( $report->{'without'}->[0]->{'original'}, '0-596-52674-1', 'testing get_report gives isbn' );
57     is( $report->{'without'}->[0]->{'isbn'}, '9780596526740', 'testing get_report' );
58
59     # test if we can get with key instead
60     $schema->resultset('SocialData')->search({ isbn => '0-596-52674-1' })->next->update({ isbn => '9780596526740' });
61     $report =  C4::SocialData::get_report();
62     is( $report->{with}->[0]->{isbn}, '9780596526740', 'this isbn has social data' );
63 };
64
65 $schema->storage->txn_rollback;