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