Lines 19-25
Link Here
|
19 |
|
19 |
|
20 |
use Modern::Perl; |
20 |
use Modern::Perl; |
21 |
|
21 |
|
22 |
use Test::More tests => 6; |
22 |
use Test::More tests => 7; |
23 |
use Test::Exception; |
23 |
use Test::Exception; |
24 |
|
24 |
|
25 |
use Koha::Database; |
25 |
use Koha::Database; |
Lines 77-82
subtest 'add_guarantor() tests' => sub {
Link Here
|
77 |
$schema->storage->txn_rollback; |
77 |
$schema->storage->txn_rollback; |
78 |
}; |
78 |
}; |
79 |
|
79 |
|
|
|
80 |
subtest 'export' => sub { |
81 |
|
82 |
my @related_sources = _get_related_sources(); |
83 |
plan tests => scalar @related_sources * 2; |
84 |
|
85 |
$schema->storage->txn_begin; |
86 |
|
87 |
my $patron = $builder->build_object( |
88 |
{ |
89 |
class => 'Koha::Patrons', |
90 |
} |
91 |
); |
92 |
|
93 |
my $test_objects = { |
94 |
'Borrower' => $patron->unblessed, |
95 |
}; |
96 |
|
97 |
my $limit_data = 5; # build this many random test objects |
98 |
my $generated_data = 0; |
99 |
|
100 |
foreach my $src ( @related_sources ) { |
101 |
next if $src eq 'Borrower'; # Borrower is a hashref, |
102 |
$test_objects->{$src} = []; # the others are arrayrefs |
103 |
} |
104 |
|
105 |
my $result_source = Koha::Patron->new->_result()->result_source; |
106 |
foreach my $rel ( Koha::Patron->new->_result()->relationships() ) { |
107 |
if ($generated_data >= $limit_data) { |
108 |
last; |
109 |
} |
110 |
|
111 |
my $related_source = $result_source->related_source( $rel ); |
112 |
my $source_name = $related_source->source_name; |
113 |
|
114 |
my $info = $result_source->relationship_info( $rel ); |
115 |
|
116 |
# We are not interested in the "belongs_to" relationship of borrowers. |
117 |
# These are tables like branches, categories and sms_provider. |
118 |
if ( $info->{'attrs'}->{'is_depends_on'} ) { |
119 |
next; |
120 |
} |
121 |
|
122 |
( my $rel_col = (keys %{$info->{'cond'}})[0] ) =~ s/^foreign\.//; |
123 |
|
124 |
# Generate test data into related tables |
125 |
my $built; |
126 |
if ( $related_source->result_class->can('koha_objects_class') ) { |
127 |
$built = $builder->build_object( |
128 |
{ |
129 |
class => $related_source->result_class->koha_objects_class, |
130 |
value => { $rel_col => $patron->borrowernumber } |
131 |
} |
132 |
); |
133 |
$built = $built->unblessed; |
134 |
} else { |
135 |
$built = $builder->build( |
136 |
{ |
137 |
source => $source_name, |
138 |
value => { $rel_col => $patron->borrowernumber } |
139 |
} |
140 |
); |
141 |
} |
142 |
push @{ $test_objects->{$related_source->source_name} }, $built; |
143 |
$generated_data++; |
144 |
} |
145 |
|
146 |
# Generate test data into a random table. Just one because test data |
147 |
# generation takes so long. |
148 |
$builder->build( { |
149 |
source => (keys %$test_objects)[rand keys %$test_objects] |
150 |
} ); |
151 |
|
152 |
my $export = $patron->export; |
153 |
|
154 |
foreach my $rel ( @related_sources ) { |
155 |
my $related_source = $schema->source( $rel ); |
156 |
my $src_name = $related_source->source_name; |
157 |
|
158 |
ok( exists $export->{$src_name}, "$src_name exists in export" ); |
159 |
is_deeply( |
160 |
$export->{$src_name}, |
161 |
$test_objects->{$src_name}, |
162 |
"$src_name export data matches built test data" |
163 |
); |
164 |
} |
165 |
|
166 |
sub _get_related_sources { |
167 |
my $sources = {}; |
168 |
my $res_source = Koha::Patron->new->_result()->result_source; |
169 |
foreach my $rel ( Koha::Patron->new->_result()->relationships() ) { |
170 |
my $related_source = $res_source->related_source($rel); |
171 |
my $info = $res_source->relationship_info( $rel ); |
172 |
next if $info->{'attrs'}->{'is_depends_on'}; |
173 |
next if $sources->{$related_source->source_name}; |
174 |
$sources->{$related_source->source_name} = 1; |
175 |
} |
176 |
$sources->{'Borrower'} = 1; # add Borrower itself |
177 |
my @sorted = sort keys %$sources; |
178 |
return @sorted; |
179 |
} |
180 |
|
181 |
$schema->storage->txn_rollback; |
182 |
}; |
183 |
|
80 |
subtest 'relationships_debt() tests' => sub { |
184 |
subtest 'relationships_debt() tests' => sub { |
81 |
|
185 |
|
82 |
plan tests => 168; |
186 |
plan tests => 168; |
83 |
- |
|
|