Bug 15072: (followup) fix spaces and consistency
[koha.git] / t / db_dependent / TestBuilder.t
blob70aa7cd778eb4c37633a26264d46cb3ad2d60389
1 #!/usr/bin/perl
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>.
20 use Modern::Perl;
21 use Test::More tests => 41;
23 BEGIN {
24 use_ok('t::lib::TestBuilder');
28 my $builder = t::lib::TestBuilder->new();
30 is( $builder->build(), undef, 'build without arguments returns undef' );
32 my @sources = $builder->schema->sources;
33 my @source_in_failure;
34 for my $source (@sources) {
35 eval { $builder->build( { source => $source } ); };
36 push @source_in_failure, $source if $@;
38 is( @source_in_failure, 0, 'TestBuilder should be able to create an object for every sources' );
39 if ( @source_in_failure ) {
40 diag ("The following sources have not been generated correctly: " . join ', ', @source_in_failure)
43 my $my_overduerules_transport_type = {
44 message_transport_type => {
45 message_transport_type => 'my msg_t_t',
47 letternumber => 1,
48 branchcode => {
49 branchcode => 'codeB',
50 categorycode => 'codeC',
52 categorycode => undef,
54 $my_overduerules_transport_type->{categorycode} = $my_overduerules_transport_type->{branchcode};
55 my $overduerules_transport_type = $builder->build({
56 source => 'OverduerulesTransportType',
57 value => $my_overduerules_transport_type,
58 });
59 is(
60 $overduerules_transport_type->{message_transport_type},
61 $my_overduerules_transport_type->{message_transport_type}->{message_transport_type},
62 'build stores the message_transport_type correctly'
64 is(
65 $overduerules_transport_type->{letternumber},
66 $my_overduerules_transport_type->{letternumber},
67 'build stores the letternumber correctly'
69 is(
70 $overduerules_transport_type->{branchcode},
71 $my_overduerules_transport_type->{branchcode}->{branchcode},
72 'build stores the branchcode correctly'
74 is(
75 $overduerules_transport_type->{categorycode},
76 $my_overduerules_transport_type->{categorycode}->{categorycode},
77 'build stores the categorycode correctly'
79 is(
80 $overduerules_transport_type->{_fk}->{message_transport_type}->{message_transport_type},
81 $my_overduerules_transport_type->{message_transport_type}->{message_transport_type},
82 'build stores the foreign key message_transport_type correctly'
84 is(
85 $overduerules_transport_type->{_fk}->{branchcode}->{branchcode},
86 $my_overduerules_transport_type->{branchcode}->{branchcode},
87 'build stores the foreign key branchcode correctly'
89 is(
90 $overduerules_transport_type->{_fk}->{categorycode}->{categorycode},
91 $my_overduerules_transport_type->{categorycode}->{categorycode},
92 'build stores the foreign key categorycode correctly'
94 is_deeply(
95 $overduerules_transport_type->{_fk}->{branchcode},
96 $overduerules_transport_type->{_fk}->{categorycode},
97 'build links the branchcode and the categorycode correctly'
99 isnt(
100 $overduerules_transport_type->{_fk}->{branchcode}->{letter2},
101 undef,
102 'build generates values if they are not given'
105 my $my_user_permission = $t::lib::TestBuilder::default_value->{UserPermission};
106 my $user_permission = $builder->build({
107 source => 'UserPermission',
109 isnt(
110 $user_permission->{borrowernumber},
111 undef,
112 'build generates a borrowernumber correctly'
115 $user_permission->{module_bit},
116 $my_user_permission->{module_bit}->{module_bit}->{bit},
117 'build stores the default value correctly'
120 $user_permission->{code},
121 $my_user_permission->{module_bit}->{code},
122 'build stores the default value correctly'
125 $user_permission->{borrowernumber},
126 $user_permission->{_fk}->{borrowernumber}->{borrowernumber},
127 'build links the foreign key correctly'
130 $user_permission->{_fk}->{borrowernumber}->{surname},
131 $my_user_permission->{borrowernumber}->{surname},
132 'build stores the foreign key value correctly'
135 $user_permission->{_fk}->{borrowernumber}->{address},
136 $my_user_permission->{borrowernumber}->{address},
137 'build stores the foreign key value correctly'
140 $user_permission->{_fk}->{borrowernumber}->{city},
141 $my_user_permission->{borrowernumber}->{city},
142 'build stores the foreign key value correctly'
145 $user_permission->{_fk}->{borrowernumber}->{_fk}->{branchcode}->{branchcode},
146 $my_user_permission->{borrowernumber}->{branchcode}->{branchcode},
147 'build stores the foreign key value correctly'
150 $user_permission->{_fk}->{borrowernumber}->{_fk}->{branchcode}->{branchname},
151 $my_user_permission->{borrowernumber}->{branchcode}->{branchname},
152 'build stores the foreign key value correctly'
155 $user_permission->{_fk}->{borrowernumber}->{_fk}->{categorycode}->{categorycode},
156 $my_user_permission->{borrowernumber}->{categorycode}->{categorycode},
157 'build stores the foreign key value correctly'
160 $user_permission->{_fk}->{borrowernumber}->{_fk}->{categorycode}->{hidelostitems},
161 $my_user_permission->{borrowernumber}->{categorycode}->{hidelostitems},
162 'build stores the foreign key value correctly'
165 $user_permission->{_fk}->{borrowernumber}->{_fk}->{categorycode}->{category_type},
166 $my_user_permission->{borrowernumber}->{categorycode}->{category_type},
167 'build stores the foreign key value correctly'
170 $user_permission->{_fk}->{borrowernumber}->{_fk}->{categorycode}->{defaultprivacy},
171 $my_user_permission->{borrowernumber}->{categorycode}->{defaultprivacy},
172 'build stores the foreign key value correctly'
175 $user_permission->{_fk}->{borrowernumber}->{privacy},
176 $my_user_permission->{borrowernumber}->{privacy},
177 'build stores the foreign key value correctly'
180 $user_permission->{_fk}->{module_bit}->{_fk}->{module_bit}->{bit},
181 $my_user_permission->{module_bit}->{module_bit}->{bit},
182 'build stores the foreign key value correctly'
185 $user_permission->{_fk}->{module_bit}->{code},
186 $my_user_permission->{module_bit}->{code},
187 'build stores the foreign key value correctly'
189 is_deeply(
190 $user_permission->{_fk}->{module_bit},
191 $user_permission->{_fk}->{code},
192 'build links the codes correctly'
194 isnt(
195 $user_permission->{_fk}->{borrowernumber}->{cardnumber},
196 undef,
197 'build generates values if they are not given'
199 isnt(
200 $user_permission->{_fk}->{borrowernumber}->{_fk}->{branchcode}->{branchaddress1},
201 undef,
202 'build generates values if they are not given'
204 isnt(
205 $user_permission->{_fk}->{borrowernumber}->{_fk}->{categorycode}->{description},
206 undef,
207 'build generates values if they are not given'
209 isnt(
210 $user_permission->{_fk}->{module_bit}->{description},
211 undef,
212 'build generates values if they are not given'
214 isnt(
215 $user_permission->{_fk}->{module_bit}->{_fk}->{module_bit}->{flag},
216 undef,
217 'build generates values if they are not given'
221 my $nb_basket = $builder->schema->resultset('Aqbasket')->search();
222 isnt( $nb_basket, 0, 'add stores the generated entries correctly' );
223 $builder->clear( { source => 'Aqbasket' } );
224 $nb_basket = $builder->schema->resultset('Aqbasket')->search();
225 is( $nb_basket, 0, 'clear removes all the entries correctly' );
228 my $rs_aqbookseller = $builder->schema->resultset('Aqbookseller');
229 my $bookseller = $builder->build({
230 source => 'Aqbookseller',
231 only_fk => 1,
233 delete $bookseller->{_fk};
234 my $bookseller_from_db = $rs_aqbookseller->find($bookseller);
235 is( $bookseller_from_db, undef, 'build with only_fk = 1 does not store the entry' );
236 my $bookseller_result = $rs_aqbookseller->create($bookseller);
237 is( $bookseller_result->in_storage, 1, 'build with only_fk = 1 creates the foreign keys correctly' );
239 $bookseller = $builder->build({
240 source => 'Aqbookseller',
242 ok( length( $bookseller->{phone} ) <= 30, 'The length for a generated string should not be longer than the size of the DB field' );
243 delete $bookseller->{_fk};
244 $bookseller_from_db = $rs_aqbookseller->find($bookseller);
245 is( $bookseller_from_db->in_storage, 1, 'build without the parameter only_sk stores the entry correctly' );
247 $bookseller = $builder->build({
248 source => 'Aqbookseller',
249 only_fk => 0,
251 delete $bookseller->{_fk};
252 $bookseller_from_db = $rs_aqbookseller->find($bookseller);
253 is( $bookseller_from_db->in_storage, 1, 'build with only_fk = 0 stores the entry correctly' );