Bug 16155: Adjust TestBuilder.t
[koha.git] / t / db_dependent / TestBuilder.t
1 #!/usr/bin/perl
2
3 # This file is part of Koha.
4 #
5 # Copyright 2014 - Biblibre SARL
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
22 use Test::More tests => 9;
23 use Test::Warn;
24 use Data::Dumper qw(Dumper);
25
26 use Koha::Database;
27
28 BEGIN {
29     use_ok('t::lib::TestBuilder');
30 }
31
32 my $schema = Koha::Database->new->schema;
33 $schema->storage->txn_begin;
34 my $builder;
35
36
37 subtest 'Start with some trivial tests' => sub {
38     plan tests => 6;
39
40     $builder = t::lib::TestBuilder->new;
41     isnt( $builder, undef, 'We got a builder' );
42
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' );
46
47     # invalid argument
48     warning_like { $builder->build({
49             source => 'Borrower',
50             value  => { surname => { invalid_hash => 1 } },
51         }) } qr/^Hash not allowed for surname/,
52         'Build should not accept a hash for this column';
53
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' );
58 };
59
60
61 subtest 'Build all sources' => sub {
62     plan tests => 1;
63
64     my @sources = $builder->schema->sources;
65     my @source_in_failure;
66     for my $source ( @sources ) {
67         my $res;
68         eval { $res = $builder->build( { source => $source } ); };
69         push @source_in_failure, $source if $@ || !defined( $res );
70     }
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 );
76     }
77 };
78
79
80 subtest 'Test length of some generated fields' => sub {
81     plan tests => 2;
82
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)' );
90 };
91
92
93 subtest 'Test FKs in overduerules_transport_type' => sub {
94     plan tests => 5;
95
96     my $my_overduerules_transport_type = {
97         message_transport_type => {
98             message_transport_type => 'my msg_t_t',
99         },
100         overduerules_id => {
101             branchcode   => 'codeB',
102             categorycode => 'codeC',
103         },
104     };
105
106     my $overduerules_transport_type = $builder->build({
107         source => 'OverduerulesTransportType',
108         value  => $my_overduerules_transport_type,
109     });
110     is(
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'
114     );
115     is(
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'
119     );
120     is(
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'
124     );
125     is(
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'
129     );
130     isnt(
131         $schema->resultset('Overduerule')->find( $my_overduerules_transport_type->{overduerules_id} )->letter2,
132         undef,
133         'build generates values if they are not given'
134     );
135 };
136
137
138 subtest 'Tests with composite FK in userpermission' => sub {
139     plan tests => 9;
140
141     my $my_user_permission = default_userpermission();
142     my $user_permission = $builder->build({
143         source => 'UserPermission',
144         value  => $my_user_permission,
145     });
146
147     # Checks on top level of userpermission
148     isnt(
149         $user_permission->{borrowernumber},
150         undef,
151         'build generates a borrowernumber correctly'
152     );
153     is(
154         $user_permission->{code},
155         $my_user_permission->{code}->{code},
156         'build stores code correctly'
157     );
158
159     # Checks one level deeper userpermission -> borrower
160     my $patron = $schema->resultset('Borrower')->find({ borrowernumber => $user_permission->{borrowernumber} });
161     is(
162         $patron->surname,
163         $my_user_permission->{borrowernumber}->{surname},
164         'build stores surname correctly'
165     );
166     isnt(
167         $patron->cardnumber,
168         undef,
169         'build generated cardnumber'
170     );
171
172     # Checks two levels deeper userpermission -> borrower -> branch
173     my $branch = $schema->resultset('Branch')->find({ branchcode => $patron->branchcode->branchcode });
174     is(
175         $branch->branchname,
176         $my_user_permission->{borrowernumber}->{branchcode}->{branchname},
177         'build stores branchname correctly'
178     );
179     isnt(
180         $branch->branchaddress1,
181         undef,
182         'build generated branch address'
183     );
184
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' );
188     is(
189         $perm->code,
190         $my_user_permission->{code}->{code},
191         'build stored code correctly'
192     );
193     is(
194         $perm->description,
195         $my_user_permission->{code}->{description},
196         'build stored description correctly'
197     );
198 };
199
200 sub default_userpermission {
201     return {
202         borrowernumber => {
203             surname => 'my surname',
204             address => 'my adress',
205             city    => 'my city',
206             branchcode => {
207                 branchname => 'my branchname',
208             },
209             categorycode => {
210                 hidelostitems   => 0,
211                 category_type   => 'A',
212                 default_privacy => 'default',
213             },
214             privacy => 1,
215         },
216         module_bit => {
217             flag        => 'my flag',
218         },
219         code => {
220             code        => 'my code',
221             description => 'my desc',
222         },
223     };
224 }
225
226
227 subtest 'Test build with NULL values' => sub {
228     plan tests => 3;
229
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' );
233     # Nullable column
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' );
239     # Nullable FK
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' );
245 };
246
247
248 subtest 'Tests for delete method' => sub {
249     plan tests => 12;
250
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' );
258
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' );
263
264
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" );
280
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
286              code       => undef };
287     is( $builder->delete({ source => 'Permission', records => $val }), 0,
288         'delete returns zero for an undef search with a composite PK' );
289 };
290
291
292 subtest 'Auto-increment values tests' => sub {
293     plan tests => 3;
294
295     # Pick a table with AI PK
296     my $source  = 'Biblio'; # table
297     my $column  = 'biblionumber'; # ai column
298
299     my $col_info = $schema->source( $source )->column_info( $column );
300     is( $col_info->{is_auto_increment}, 1, "biblio.biblionumber is detected as autoincrement");
301
302     # Create a biblio
303     my $biblio_1 = $builder->build({ source => $source });
304     # Get the AI value
305     my $ai_value = $biblio_1->{ biblionumber };
306     # Create a biblio
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");
311
312     # respect autoincr column
313     warning_like { $builder->build({
314             source => $source,
315             value  => { biblionumber => 123 },
316         }) } qr/^Value not allowed for auto_incr/,
317         'Build should not overwrite an auto_incr column';
318 };
319
320 $schema->storage->txn_rollback;
321
322 1;