Line 0
Link Here
|
0 |
- |
1 |
#!/usr/bin/perl |
|
|
2 |
|
3 |
# Copyright 2025 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::NoWarnings; |
22 |
use Test::More tests => 7; |
23 |
use Test::Exception; |
24 |
|
25 |
use Koha::Database; |
26 |
use Koha::Schema::Util::ExceptionTranslator; |
27 |
|
28 |
use t::lib::TestBuilder; |
29 |
|
30 |
my $builder = t::lib::TestBuilder->new; |
31 |
my $schema = Koha::Database->new->schema; |
32 |
|
33 |
subtest 'foreign_key_constraint_translation' => sub { |
34 |
plan tests => 1; |
35 |
|
36 |
$schema->storage->txn_begin; |
37 |
|
38 |
# Create a mock DBIx::Class::Exception for FK constraint |
39 |
my $exception = bless { |
40 |
msg => "Cannot add or update a child row: a foreign key constraint fails (`koha`.`items`, CONSTRAINT `items_ibfk_1` FOREIGN KEY (`biblionumber`) REFERENCES `biblio` (`biblionumber`))" |
41 |
}, 'DBIx::Class::Exception'; |
42 |
|
43 |
throws_ok { |
44 |
Koha::Schema::Util::ExceptionTranslator->translate_exception($exception); |
45 |
} 'Koha::Exceptions::Object::FKConstraint', 'FK constraint exception is properly translated'; |
46 |
|
47 |
$schema->storage->txn_rollback; |
48 |
}; |
49 |
|
50 |
subtest 'duplicate_key_translation' => sub { |
51 |
plan tests => 1; |
52 |
|
53 |
$schema->storage->txn_begin; |
54 |
|
55 |
# Create a mock DBIx::Class::Exception for duplicate key |
56 |
my $exception = bless { |
57 |
msg => "Duplicate entry 'test\@example.com' for key 'borrowers.email'" |
58 |
}, 'DBIx::Class::Exception'; |
59 |
|
60 |
throws_ok { |
61 |
Koha::Schema::Util::ExceptionTranslator->translate_exception($exception); |
62 |
} 'Koha::Exceptions::Object::DuplicateID', 'Duplicate key exception is properly translated'; |
63 |
|
64 |
$schema->storage->txn_rollback; |
65 |
}; |
66 |
|
67 |
subtest 'bad_value_translation' => sub { |
68 |
plan tests => 1; |
69 |
|
70 |
$schema->storage->txn_begin; |
71 |
|
72 |
# Create a mock DBIx::Class::Exception for bad value |
73 |
my $exception = bless { |
74 |
msg => "Incorrect datetime value: '2025-13-45' for column 'date_due' at row 1" |
75 |
}, 'DBIx::Class::Exception'; |
76 |
|
77 |
throws_ok { |
78 |
Koha::Schema::Util::ExceptionTranslator->translate_exception($exception); |
79 |
} 'Koha::Exceptions::Object::BadValue', 'Bad value exception is properly translated'; |
80 |
|
81 |
$schema->storage->txn_rollback; |
82 |
}; |
83 |
|
84 |
subtest 'enum_truncation_translation' => sub { |
85 |
plan tests => 1; |
86 |
|
87 |
$schema->storage->txn_begin; |
88 |
|
89 |
# Create a mock DBIx::Class::Exception for enum truncation |
90 |
my $exception = bless { |
91 |
msg => "Data truncated for column 'status' at row 1" |
92 |
}, 'DBIx::Class::Exception'; |
93 |
|
94 |
my $columns_info = { |
95 |
status => { data_type => 'enum' } |
96 |
}; |
97 |
|
98 |
throws_ok { |
99 |
Koha::Schema::Util::ExceptionTranslator->translate_exception($exception, $columns_info); |
100 |
} 'Koha::Exceptions::Object::BadValue', 'Enum truncation exception is properly translated'; |
101 |
|
102 |
$schema->storage->txn_rollback; |
103 |
}; |
104 |
|
105 |
subtest 'non_dbix_exception_passthrough' => sub { |
106 |
plan tests => 1; |
107 |
|
108 |
$schema->storage->txn_begin; |
109 |
|
110 |
# Create a regular exception (not DBIx::Class::Exception) |
111 |
my $exception = bless { |
112 |
msg => "Some other error" |
113 |
}, 'Some::Other::Exception'; |
114 |
|
115 |
# Mock the rethrow method |
116 |
$exception->{rethrown} = 0; |
117 |
{ |
118 |
package Some::Other::Exception; |
119 |
sub rethrow { $_[0]->{rethrown} = 1; die $_[0]; } |
120 |
} |
121 |
|
122 |
throws_ok { |
123 |
Koha::Schema::Util::ExceptionTranslator->translate_exception($exception); |
124 |
} qr/Some::Other::Exception/, 'Non-DBIx::Class exceptions are rethrown unchanged'; |
125 |
|
126 |
$schema->storage->txn_rollback; |
127 |
}; |
128 |
|
129 |
subtest 'schema_safe_do_method' => sub { |
130 |
plan tests => 2; |
131 |
|
132 |
$schema->storage->txn_begin; |
133 |
|
134 |
# Test successful operation |
135 |
my $result = $schema->safe_do(sub { |
136 |
return "success"; |
137 |
}); |
138 |
is( $result, "success", 'safe_do returns result on success' ); |
139 |
|
140 |
# Test exception translation |
141 |
throws_ok { |
142 |
$schema->safe_do(sub { |
143 |
# Create a mock DBIx::Class::Exception |
144 |
my $exception = bless { |
145 |
msg => "Duplicate entry 'test' for key 'primary'" |
146 |
}, 'DBIx::Class::Exception'; |
147 |
die $exception; |
148 |
}); |
149 |
} 'Koha::Exceptions::Object::DuplicateID', 'safe_do translates exceptions properly'; |
150 |
|
151 |
$schema->storage->txn_rollback; |
152 |
}; |
153 |
|
154 |
1; |