3 # This file is part of Koha.
5 # Copyright 2014 - Biblibre SARL
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.
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.
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>.
22 use Test::More tests => 9;
24 use Data::Dumper qw(Dumper);
29 use_ok('t::lib::TestBuilder');
32 my $schema = Koha::Database->new->schema;
33 $schema->storage->txn_begin;
37 subtest 'Start with some trivial tests' => sub {
40 $builder = t::lib::TestBuilder->new;
41 isnt( $builder, undef, 'We got a builder' );
43 is( $builder->build, undef, 'build without arguments returns undef' );
44 is( ref( $builder->schema ), 'Koha::Schema', 'check schema' );
45 is( ref( $builder->can('delete') ), 'CODE', 'found delete method' );
48 warning_like { $builder->build({
50 value => { surname => { invalid_hash => 1 } },
51 }) } qr/^Hash not allowed for surname/,
52 'Build should not accept a hash for this column';
54 # return undef if a record exists
55 my $param = { source => 'Branch', value => { branchcode => 'MPL' } };
56 $builder->build( $param ); # at least it should exist now
57 is( $builder->build( $param ), undef, 'Return undef when exists' );
61 subtest 'Build all sources' => sub {
64 my @sources = $builder->schema->sources;
65 my @source_in_failure;
66 for my $source ( @sources ) {
68 eval { $res = $builder->build( { source => $source } ); };
69 push @source_in_failure, $source if $@ || !defined( $res );
71 is( @source_in_failure, 0,
72 'TestBuilder should be able to create an object for every source' );
73 if ( @source_in_failure ) {
74 diag( "The following sources have not been generated correctly: " .
75 join ', ', @source_in_failure );
80 subtest 'Test length of some generated fields' => sub {
83 # Test the length of a returned character field
84 my $bookseller = $builder->build({ source => 'Aqbookseller' });
85 my $max = $schema->source('Aqbookseller')->column_info('phone')->{size};
86 is( length( $bookseller->{phone} ) > 0, 1,
87 'The length for a generated string (phone) should not be zero' );
88 is( length( $bookseller->{phone} ) <= $max, 1,
89 'Check maximum length for a generated string (phone)' );
93 subtest 'Test FKs in overduerules_transport_type' => sub {
96 my $my_overduerules_transport_type = {
97 message_transport_type => {
98 message_transport_type => 'my msg_t_t',
101 branchcode => 'codeB',
102 categorycode => 'codeC',
106 my $overduerules_transport_type = $builder->build({
107 source => 'OverduerulesTransportType',
108 value => $my_overduerules_transport_type,
111 $overduerules_transport_type->{message_transport_type},
112 $my_overduerules_transport_type->{message_transport_type}->{message_transport_type},
113 'build stores the message_transport_type correctly'
116 $schema->resultset('Overduerule')->find( $overduerules_transport_type->{overduerules_id} )->branchcode,
117 $my_overduerules_transport_type->{overduerules_id}->{branchcode},
118 'build stores the branchcode correctly'
121 $schema->resultset('Overduerule')->find( $overduerules_transport_type->{overduerules_id} )->categorycode,
122 $my_overduerules_transport_type->{overduerules_id}->{categorycode},
123 'build stores the categorycode correctly'
126 $schema->resultset('MessageTransportType')->find( $overduerules_transport_type->{message_transport_type} )->message_transport_type,
127 $overduerules_transport_type->{message_transport_type},
128 'build stores the foreign key message_transport_type correctly'
131 $schema->resultset('Overduerule')->find( $my_overduerules_transport_type->{overduerules_id} )->letter2,
133 'build generates values if they are not given'
138 subtest 'Tests with composite FK in userpermission' => sub {
141 my $my_user_permission = default_userpermission();
142 my $user_permission = $builder->build({
143 source => 'UserPermission',
144 value => $my_user_permission,
147 # Checks on top level of userpermission
149 $user_permission->{borrowernumber},
151 'build generates a borrowernumber correctly'
154 $user_permission->{code},
155 $my_user_permission->{code}->{code},
156 'build stores code correctly'
159 # Checks one level deeper userpermission -> borrower
160 my $patron = $schema->resultset('Borrower')->find({ borrowernumber => $user_permission->{borrowernumber} });
163 $my_user_permission->{borrowernumber}->{surname},
164 'build stores surname correctly'
169 'build generated cardnumber'
172 # Checks two levels deeper userpermission -> borrower -> branch
173 my $branch = $schema->resultset('Branch')->find({ branchcode => $patron->branchcode->branchcode });
176 $my_user_permission->{borrowernumber}->{branchcode}->{branchname},
177 'build stores branchname correctly'
180 $branch->branchaddress1,
182 'build generated branch address'
185 # Checks with composite FK: userpermission -> permission
186 my $perm = $schema->resultset('Permission')->find({ module_bit => $user_permission->{module_bit}, code => $my_user_permission->{code}->{code} });
187 isnt( $perm, undef, 'build generated record for composite FK' );
190 $my_user_permission->{code}->{code},
191 'build stored code correctly'
195 $my_user_permission->{code}->{description},
196 'build stored description correctly'
200 sub default_userpermission {
203 surname => 'my surname',
204 address => 'my adress',
207 branchname => 'my branchname',
211 category_type => 'A',
212 default_privacy => 'default',
221 description => 'my desc',
227 subtest 'Test build with NULL values' => sub {
230 # PK should not be null
231 my $params = { source => 'Branch', value => { branchcode => undef }};
232 is( $builder->build( $params ), undef, 'PK should not be null' );
234 my $info = $schema->source( 'Item' )->column_info( 'barcode' );
235 $params = { source => 'Item', value => { barcode => undef }};
236 my $item = $builder->build( $params );
237 is( $info->{is_nullable} && $item && !defined( $item->{barcode} ), 1,
238 'Barcode can be NULL' );
240 $params = { source => 'Reserve', value => { itemnumber => undef }};
241 my $reserve = $builder->build( $params );
242 $info = $schema->source( 'Reserve' )->column_info( 'itemnumber' );
243 is( $reserve && $info->{is_nullable} && $info->{is_foreign_key} &&
244 !defined( $reserve->{itemnumber} ), 1, 'Nullable FK' );
248 subtest 'Tests for delete method' => sub {
251 # Test delete with single and multiple records
252 my $basket1 = $builder->build({ source => 'Aqbasket' });
253 my $basket2 = $builder->build({ source => 'Aqbasket' });
254 my $basket3 = $builder->build({ source => 'Aqbasket' });
255 my ( $id1, $id2 ) = ( $basket1->{basketno}, $basket2->{basketno} );
256 $builder->delete({ source => 'Aqbasket', records => $basket1 });
257 isnt( exists $basket1->{basketno}, 1, 'Delete cleared PK hash value' );
259 is( $builder->schema->resultset('Aqbasket')->search({ basketno => $id1 })->count, 0, 'Basket1 is no longer found' );
260 is( $builder->schema->resultset('Aqbasket')->search({ basketno => $id2 })->count, 1, 'Basket2 is still found' );
261 is( $builder->delete({ source => 'Aqbasket', records => [ $basket2, $basket3 ] }), 2, "Returned two delete attempts" );
262 is( $builder->schema->resultset('Aqbasket')->search({ basketno => $id2 })->count, 0, 'Basket2 is no longer found' );
265 # Test delete in table without primary key (..)
266 is( $schema->source('TmpHoldsqueue')->primary_columns, 0,
267 'Table without primary key detected' );
268 my $bibno = 123; # just a number
269 my $cnt1 = $schema->resultset('TmpHoldsqueue')->count;
270 # Insert a new record in TmpHoldsqueue with that biblionumber
271 my $val = { biblionumber => $bibno };
272 my $rec = $builder->build({ source => 'TmpHoldsqueue', value => $val });
273 my $cnt2 = $schema->resultset('TmpHoldsqueue')->count;
274 is( defined($rec) && $cnt2 == $cnt1 + 1 , 1, 'Created a record' );
275 is( $builder->delete({ source => 'TmpHoldsqueue', records => $rec }),
276 undef, 'delete returns undef' );
277 is( $rec->{biblionumber}, $bibno, 'Hash value untouched' );
278 is( $schema->resultset('TmpHoldsqueue')->count, $cnt2,
279 "Method did not delete record in table without PK" );
281 # Test delete with NULL values
282 $val = { branchcode => undef };
283 is( $builder->delete({ source => 'Branch', records => $val }), 0,
284 'delete returns zero for an undef search with one key' );
285 $val = { module_bit => 1, #catalogue
287 is( $builder->delete({ source => 'Permission', records => $val }), 0,
288 'delete returns zero for an undef search with a composite PK' );
292 subtest 'Auto-increment values tests' => sub {
295 # Pick a table with AI PK
296 my $source = 'Biblio'; # table
297 my $column = 'biblionumber'; # ai column
299 my $col_info = $schema->source( $source )->column_info( $column );
300 is( $col_info->{is_auto_increment}, 1, "biblio.biblionumber is detected as autoincrement");
303 my $biblio_1 = $builder->build({ source => $source });
305 my $ai_value = $biblio_1->{ biblionumber };
307 my $biblio_2 = $builder->build({ source => $source });
308 # Get the next AI value
309 my $next_ai_value = $biblio_2->{ biblionumber };
310 is( $ai_value + 1, $next_ai_value, "AI values are consecutive");
312 # respect autoincr column
313 warning_like { $builder->build({
315 value => { biblionumber => 123 },
316 }) } qr/^Value not allowed for auto_incr/,
317 'Build should not overwrite an auto_incr column';
320 $schema->storage->txn_rollback;