Line 0
Link Here
|
0 |
- |
1 |
# Copyright 2012 Catalyst IT Ltd. |
|
|
2 |
# |
3 |
# This file is part of Koha. |
4 |
# |
5 |
# Koha is free software; you can redistribute it and/or modify it under the |
6 |
# terms of the GNU General Public License as published by the Free Software |
7 |
# Foundation; either version 2 of the License, or (at your option) any later |
8 |
# version. |
9 |
# |
10 |
# Koha is distributed in the hope that it will be useful, but WITHOUT ANY |
11 |
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR |
12 |
# A PARTICULAR PURPOSE. See the GNU General Public License for more details. |
13 |
# |
14 |
# You should have received a copy of the GNU General Public License along |
15 |
# with Koha; if not, write to the Free Software Foundation, Inc., |
16 |
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
17 |
|
18 |
use strict; |
19 |
use warnings; |
20 |
|
21 |
use Test::More tests => 19; # last test to print |
22 |
|
23 |
use_ok('C4::Reports::Guided'); |
24 |
|
25 |
# This is the query I found that triggered bug 8594. |
26 |
my $sql = "SELECT aqorders.ordernumber, biblio.title, biblio.biblionumber, items.homebranch, |
27 |
aqorders.entrydate, aqorders.datereceived, |
28 |
(SELECT DATE(datetime) FROM statistics |
29 |
WHERE itemnumber=items.itemnumber AND |
30 |
(type='return' OR type='issue') LIMIT 1) |
31 |
AS shelvedate, |
32 |
DATEDIFF(COALESCE( |
33 |
(SELECT DATE(datetime) FROM statistics |
34 |
WHERE itemnumber=items.itemnumber AND |
35 |
(type='return' OR type='issue') LIMIT 1), |
36 |
aqorders.datereceived), aqorders.entrydate) AS totaldays |
37 |
FROM aqorders |
38 |
LEFT JOIN biblio USING (biblionumber) |
39 |
LEFT JOIN items ON (items.biblionumber = biblio.biblionumber |
40 |
AND dateaccessioned=aqorders.datereceived) |
41 |
WHERE (entrydate >= '2011-01-01' AND (datereceived < '2011-02-01' OR datereceived IS NULL)) |
42 |
AND items.homebranch LIKE 'INFO' |
43 |
ORDER BY title"; |
44 |
|
45 |
my ($res_sql, $res_lim1, $res_lim2) = C4::Reports::Guided::strip_limit($sql); |
46 |
is($res_sql, $sql, "Not breaking subqueries"); |
47 |
is($res_lim1, 0, "Returns correct default offset"); |
48 |
is($res_lim2, undef, "Returns correct default LIMIT"); |
49 |
|
50 |
# Now the same thing, but we want it to remove the LIMIT from the end |
51 |
|
52 |
my $test_sql = $res_sql . " LIMIT 242"; |
53 |
($res_sql, $res_lim1, $res_lim2) = C4::Reports::Guided::strip_limit($test_sql); |
54 |
# The replacement drops a ' ' where the limit was |
55 |
is(trim($res_sql), $sql, "Correctly removes only final LIMIT"); |
56 |
is($res_lim1, 0, "Returns correct default offset"); |
57 |
is($res_lim2, 242, "Returns correct extracted LIMIT"); |
58 |
|
59 |
$test_sql = $res_sql . " LIMIT 13,242"; |
60 |
($res_sql, $res_lim1, $res_lim2) = C4::Reports::Guided::strip_limit($test_sql); |
61 |
# The replacement drops a ' ' where the limit was |
62 |
is(trim($res_sql), $sql, "Correctly removes only final LIMIT (with offset)"); |
63 |
is($res_lim1, 13, "Returns correct extracted offset"); |
64 |
is($res_lim2, 242, "Returns correct extracted LIMIT"); |
65 |
|
66 |
# After here is the simpler case, where there isn't a WHERE clause to worry |
67 |
# about. |
68 |
|
69 |
# First case with nothing to change |
70 |
$sql = "SELECT * FROM items"; |
71 |
($res_sql, $res_lim1, $res_lim2) = C4::Reports::Guided::strip_limit($sql); |
72 |
is($res_sql, $sql, "Not breaking simple queries"); |
73 |
is($res_lim1, 0, "Returns correct default offset"); |
74 |
is($res_lim2, undef, "Returns correct default LIMIT"); |
75 |
|
76 |
$test_sql = $sql . " LIMIT 242"; |
77 |
($res_sql, $res_lim1, $res_lim2) = C4::Reports::Guided::strip_limit($test_sql); |
78 |
is(trim($res_sql), $sql, "Correctly removes LIMIT in simple case"); |
79 |
is($res_lim1, 0, "Returns correct default offset"); |
80 |
is($res_lim2, 242, "Returns correct extracted LIMIT"); |
81 |
|
82 |
$test_sql = $sql . " LIMIT 13,242"; |
83 |
($res_sql, $res_lim1, $res_lim2) = C4::Reports::Guided::strip_limit($test_sql); |
84 |
is(trim($res_sql), $sql, "Correctly removes LIMIT in simple case (with offset)"); |
85 |
is($res_lim1, 13, "Returns correct extracted offset"); |
86 |
is($res_lim2, 242, "Returns correct extracted LIMIT"); |
87 |
|
88 |
sub trim { |
89 |
my ($s) = @_; |
90 |
$s =~ s/^\s*(.*?)\s*$/$1/s; |
91 |
return $s; |
92 |
} |