Bug 9016: (follow-up) fix unit tests
[koha.git] / t / db_dependent / Reports_Guided.t
1 #!/usr/bin/perl
2 #
3 # This Koha test module is a stub!
4 # Add more tests here!!!
5
6 use Modern::Perl;
7
8 use Test::More tests => 14;
9
10 use C4::Context;
11
12 BEGIN {
13     use_ok('C4::Reports::Guided');
14 }
15 can_ok(
16     'C4::Reports::Guided',
17     qw(save_report delete_report execute_query)
18 );
19
20 #Start transaction
21 my $dbh = C4::Context->dbh;
22 $dbh->{RaiseError} = 1;
23 $dbh->{AutoCommit} = 0;
24
25 $dbh->do(q|DELETE FROM saved_sql|);
26
27 #Start tests
28
29 #Test save_report
30 my $count = scalar( @{ get_saved_reports() } );
31 is( $count, 0, "There is no report" );
32
33 my @report_ids;
34 for my $id ( 1 .. 3 ) {
35     push @report_ids, save_report({
36         borrowernumber => $id,
37         savedsql       => "SQL$id",
38         name           => "Name$id",
39         area           => "area$id",
40         group          => "group$id",
41         subgroup       => "subgroup$id",
42         type           => "type$id",
43         notes          => "note$id",
44         cache_expiry   => "null",
45         public         => "null"
46     });
47     $count++;
48 }
49 like( $report_ids[0], '/^\d+$/', "Save_report returns an id for first" );
50 like( $report_ids[1], '/^\d+$/', "Save_report returns an id for second" );
51 like( $report_ids[2], '/^\d+$/', "Save_report returns an id for third" );
52
53 is( scalar( @{ get_saved_reports() } ),
54     $count, "$count reports have been added" );
55
56 #Test delete_report
57 is (delete_report(),undef, "Without id delete_report returns undef");
58
59 is( delete_report( $report_ids[0] ), 1, "report 1 is deleted" );
60 $count--;
61
62 is( scalar( @{ get_saved_reports() } ), $count, "Report1 has been deleted" );
63
64 is( delete_report( $report_ids[1], $report_ids[2] ), 2, "report 2 and 3 are deleted" );
65 $count -= 2;
66
67 is( scalar( @{ get_saved_reports() } ),
68     $count, "Report2 and report3 have been deleted" );
69
70 my $sth = execute_query('SELECT COUNT(*) FROM systempreferences', 0, 10);
71 my $results = $sth->fetchall_arrayref;
72 is(scalar(@$results), 1, 'running a query returned a result');
73
74 my $version = C4::Context->preference('Version');
75 $sth = execute_query(
76     'SELECT value FROM systempreferences WHERE variable = ?',
77     0,
78     10,
79     [ 'Version' ],
80 );
81 $results = $sth->fetchall_arrayref;
82 is_deeply(
83     $results,
84     [ [ $version ] ],
85     'running a query with a parameter returned the expected result'
86 );
87
88 #End transaction
89 $dbh->rollback;
90