|
Line 0
Link Here
|
| 0 |
- |
1 |
#!/usr/bin/perl |
|
|
2 |
|
| 3 |
# Copyright 2015 BibLibre |
| 4 |
# |
| 5 |
# This file is part of Koha. |
| 6 |
# |
| 7 |
# Koha is free software; you can redistribute it and/or modify it under the |
| 8 |
# terms of the GNU General Public License as published by the Free Software |
| 9 |
# Foundation; either version 3 of the License, or (at your option) any later |
| 10 |
# version. |
| 11 |
# |
| 12 |
# Koha is distributed in the hope that it will be useful, but WITHOUT ANY |
| 13 |
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR |
| 14 |
# A PARTICULAR PURPOSE. See the GNU General Public License for more details. |
| 15 |
# |
| 16 |
# You should have received a copy of the GNU General Public License along |
| 17 |
# with Koha; if not, see <http://www.gnu.org/licenses>. |
| 18 |
|
| 19 |
use Modern::Perl; |
| 20 |
use C4::Context; |
| 21 |
use Test::More tests => 145; |
| 22 |
use Test::MockModule; |
| 23 |
|
| 24 |
|
| 25 |
BEGIN { |
| 26 |
use_ok('C4::OAI::Sets'); |
| 27 |
use_ok('MARC::Record'); |
| 28 |
use_ok('C4::Biblio'); |
| 29 |
} |
| 30 |
can_ok( |
| 31 |
'C4::OAI::Sets', qw( |
| 32 |
GetOAISets |
| 33 |
GetOAISet |
| 34 |
GetOAISetBySpec |
| 35 |
ModOAISet |
| 36 |
DelOAISet |
| 37 |
AddOAISet |
| 38 |
GetOAISetsMappings |
| 39 |
GetOAISetMappings |
| 40 |
ModOAISetMappings |
| 41 |
GetOAISetsBiblio |
| 42 |
DelOAISetsBiblio |
| 43 |
CalcOAISetsBiblio |
| 44 |
ModOAISetsBiblios |
| 45 |
UpdateOAISetsBiblio |
| 46 |
AddOAISetsBiblios ) |
| 47 |
); |
| 48 |
|
| 49 |
|
| 50 |
my $dbh = C4::Context->dbh; |
| 51 |
$dbh->{AutoCommit} = 0; |
| 52 |
$dbh->{RaiseError} = 1; |
| 53 |
$dbh->do('DELETE FROM oai_sets'); |
| 54 |
$dbh->do('DELETE FROM oai_sets_descriptions'); |
| 55 |
$dbh->do('DELETE FROM oai_sets_mappings'); |
| 56 |
$dbh->do('DELETE FROM oai_sets_biblios'); |
| 57 |
|
| 58 |
|
| 59 |
# ---------- Testing AddOAISet ------------------ |
| 60 |
ok (!defined(AddOAISet), 'AddOAISet without argument is undef'); |
| 61 |
|
| 62 |
my $set_without_spec_and_name_and_desc = {}; |
| 63 |
ok (!defined(AddOAISet($set_without_spec_and_name_and_desc)), 'AddOAISet without "field", "name" and "descriptions" fields is undef'); |
| 64 |
|
| 65 |
my $set_without_spec_and_name = { |
| 66 |
'descriptions' => ['descNoSpecNoName'], |
| 67 |
}; |
| 68 |
ok (!defined(AddOAISet($set_without_spec_and_name)), 'AddOAISet without "field" and "name" fields is undef'); |
| 69 |
|
| 70 |
my $set_without_spec = { |
| 71 |
'name' => 'nameNoSpec', |
| 72 |
'descriptions' => ['descNoSpec'], |
| 73 |
}; |
| 74 |
ok (!defined(AddOAISet($set_without_spec)), 'AddOAISet without "field" field is undef'); |
| 75 |
|
| 76 |
my $set_without_name = { |
| 77 |
'spec' => 'specNoName', |
| 78 |
'descriptions' => ['descNoName'], |
| 79 |
}; |
| 80 |
ok (!defined(AddOAISet($set_without_name)), 'AddOAISet without "name" field is undef'); |
| 81 |
|
| 82 |
#Test to enter in the 'else' case of 'AddOAISet' line 280 |
| 83 |
{ |
| 84 |
my $dbi_st = Test::MockModule->new('DBI::st', no_auto => 1); # ref($sth) == 'DBI::st' |
| 85 |
$dbi_st->mock('execute', sub { return 0; }); |
| 86 |
|
| 87 |
my $setWrong = { |
| 88 |
'spec' => 'specWrong', |
| 89 |
'name' => 'nameWrong', |
| 90 |
}; |
| 91 |
my $setWrong_id = AddOAISet($setWrong); |
| 92 |
|
| 93 |
DelOAISet($setWrong_id); |
| 94 |
} |
| 95 |
|
| 96 |
#Adding a Set without description |
| 97 |
my $set1 = { |
| 98 |
'spec' => 'specSet1', |
| 99 |
'name' => 'nameSet1', |
| 100 |
}; |
| 101 |
my $set1_id = AddOAISet($set1); |
| 102 |
isa_ok(\$set1_id, 'SCALAR', '$set1_id is a SCALAR'); |
| 103 |
|
| 104 |
my $sth = $dbh->prepare("SELECT count(*) FROM oai_sets"); |
| 105 |
$sth->execute; |
| 106 |
my $setsCount = $sth->fetchrow_array; |
| 107 |
is ($setsCount, 1, 'There is 1 set'); |
| 108 |
|
| 109 |
$sth = $dbh->prepare("SELECT spec, name FROM oai_sets"); |
| 110 |
$sth->execute; |
| 111 |
my ($spec, $name) = $sth->fetchrow_array; |
| 112 |
is ($spec, 'specSet1', 'spec field is "specSet1"'); |
| 113 |
is ($name, 'nameSet1', 'name field is "nameSet1"'); |
| 114 |
|
| 115 |
$sth = $dbh->prepare("SELECT description FROM oai_sets_descriptions"); |
| 116 |
$sth->execute; |
| 117 |
my $desc = $sth -> rows; |
| 118 |
is ($desc, 0, 'There is NO set description'); |
| 119 |
|
| 120 |
#Adding a Set with a description |
| 121 |
my $set2 = { |
| 122 |
'spec' => 'specSet2', |
| 123 |
'name' => 'nameSet2', |
| 124 |
'descriptions' => ['descSet2'], |
| 125 |
}; |
| 126 |
my $set2_id = AddOAISet($set2); |
| 127 |
isa_ok(\$set2_id, 'SCALAR', '$set2_id is a SCALAR'); |
| 128 |
|
| 129 |
$sth = $dbh->prepare("SELECT count(*) FROM oai_sets"); |
| 130 |
$sth->execute; |
| 131 |
$setsCount = $sth->fetchrow_array; |
| 132 |
is ($setsCount, 2, 'There is 2 sets'); |
| 133 |
|
| 134 |
$sth = $dbh->prepare("SELECT spec, name FROM oai_sets ORDER BY id DESC"); |
| 135 |
$sth->execute; |
| 136 |
($spec, $name) = $sth->fetchrow_array; |
| 137 |
is ($spec, 'specSet2', 'spec field is "specSet2"'); |
| 138 |
is ($name, 'nameSet2', 'name field is "nameSet2"'); |
| 139 |
|
| 140 |
$sth = $dbh->prepare("SELECT description FROM oai_sets_descriptions"); |
| 141 |
$sth->execute; |
| 142 |
$desc = $sth->fetchrow_array; |
| 143 |
is ($desc, 'descSet2', 'description field is "descSet2"'); |
| 144 |
|
| 145 |
|
| 146 |
# ---------- Testing GetOAISets ----------------- |
| 147 |
my $oai_sets = GetOAISets; |
| 148 |
isa_ok($oai_sets, 'ARRAY', '$oai_sets is an array reference of hash reference describing the sets'); |
| 149 |
|
| 150 |
isa_ok($oai_sets->[0], 'HASH', '$set1 is defined as a hash'); |
| 151 |
is ($oai_sets->[0]->{spec}, 'specSet1', 'spec field is "specSet1"'); |
| 152 |
is ($oai_sets->[0]->{name}, 'nameSet1', 'name field is "nameSet1"'); |
| 153 |
|
| 154 |
isa_ok($oai_sets->[1], 'HASH', '$set2 is defined as a hash'); |
| 155 |
is ($oai_sets->[1]->{spec}, 'specSet2', 'spec field is "specSet2"'); |
| 156 |
is ($oai_sets->[1]->{name}, 'nameSet2', 'name field is "nameSet2"'); |
| 157 |
is_deeply ($oai_sets->[1]->{descriptions}, ['descSet2'], 'description field is "descSet2"'); |
| 158 |
|
| 159 |
ok(!defined($oai_sets->[2]), 'There are only 2 sets'); |
| 160 |
|
| 161 |
|
| 162 |
# ---------- Testing GetOAISet ------------------ |
| 163 |
ok (!defined(GetOAISet), 'GetOAISet without argument is undef'); |
| 164 |
|
| 165 |
my $set = GetOAISet($set1_id); |
| 166 |
isa_ok($set, 'HASH', '$set is a hash reference describing the set with the given set_id'); |
| 167 |
is ($set->{spec}, 'specSet1', 'spec field is "specSet1"'); |
| 168 |
is ($set->{name}, 'nameSet1', 'name field is "nameSet1"'); |
| 169 |
|
| 170 |
$set = GetOAISet($set2_id); |
| 171 |
isa_ok($set, 'HASH', '$set is a hash reference describing the set with the given set_id'); |
| 172 |
is ($set->{spec}, 'specSet2', 'spec field is "specSet2"'); |
| 173 |
is ($set->{name}, 'nameSet2', 'name field is "nameSet2"'); |
| 174 |
is_deeply ($set->{descriptions}, ['descSet2'], 'description field is "descSet2"'); |
| 175 |
|
| 176 |
|
| 177 |
# ---------- Testing GetOAISetBySpec ------------ |
| 178 |
ok (!defined(GetOAISetBySpec), 'GetOAISetBySpec without argument is undef'); |
| 179 |
|
| 180 |
$set = GetOAISetBySpec($set1->{spec}); |
| 181 |
isa_ok($set, 'HASH', '$set is a hash describing the set whose spec is $oai_sets->[0]->{spec}'); |
| 182 |
is ($set->{spec}, 'specSet1', 'spec field is "specSet1"'); |
| 183 |
is ($set->{name}, 'nameSet1', 'name field is "nameSet1"'); |
| 184 |
|
| 185 |
$set = GetOAISetBySpec($set2->{spec}); |
| 186 |
isa_ok($set, 'HASH', '$set is a hash describing the set whose spec is $oai_sets->[1]->{spec}'); |
| 187 |
is ($set->{spec}, 'specSet2', 'spec field is "specSet2"'); |
| 188 |
is ($set->{name}, 'nameSet2', 'name field is "nameSet2"'); |
| 189 |
#GetOAISetBySpec does't return the description field. |
| 190 |
|
| 191 |
|
| 192 |
# ---------- Testing ModOAISet ------------------ |
| 193 |
ok (!defined(ModOAISet), 'ModOAISet without argument is undef'); |
| 194 |
|
| 195 |
my $new_set_without_id = { |
| 196 |
'spec' => 'specNoName', |
| 197 |
'name' => 'nameNoSpec', |
| 198 |
'descriptions' => ['descNoSpecNoName'], |
| 199 |
}; |
| 200 |
ok (!defined(ModOAISet($new_set_without_id)), 'ModOAISet without "id" field is undef'); |
| 201 |
|
| 202 |
|
| 203 |
my $new_set_without_spec_and_name = { |
| 204 |
'id' => $set1_id, |
| 205 |
'descriptions' => ['descNoSpecNoName'], |
| 206 |
}; |
| 207 |
ok (!defined(ModOAISet($new_set_without_spec_and_name)), 'ModOAISet without "field" and "name" fields is undef'); |
| 208 |
|
| 209 |
my $new_set_without_spec = { |
| 210 |
'id' => $set1_id, |
| 211 |
'name' => 'nameNoSpec', |
| 212 |
'descriptions' => ['descNoSpec'], |
| 213 |
}; |
| 214 |
ok (!defined(ModOAISet($new_set_without_spec)), 'ModOAISet without "field" field is undef'); |
| 215 |
|
| 216 |
my $new_set_without_name = { |
| 217 |
'id' => $set1_id, |
| 218 |
'spec' => 'specNoName', |
| 219 |
'descriptions' => ['descNoName'], |
| 220 |
}; |
| 221 |
ok (!defined(ModOAISet($new_set_without_name)), 'ModOAISet without "name" field is undef'); |
| 222 |
|
| 223 |
my $new_set1 = { |
| 224 |
'id' => $set1_id, |
| 225 |
'spec' => 'new_specSet1', |
| 226 |
'name' => 'new_nameSet1', |
| 227 |
'descriptions' => ['new_descSet1'], |
| 228 |
}; |
| 229 |
ModOAISet($new_set1); |
| 230 |
|
| 231 |
my $new_set2 = { |
| 232 |
'id' => $set2_id, |
| 233 |
'spec' => 'new_specSet2', |
| 234 |
'name' => 'new_nameSet2', |
| 235 |
}; |
| 236 |
ModOAISet($new_set2); |
| 237 |
|
| 238 |
$set1 = GetOAISet($set1_id); |
| 239 |
isa_ok($set1, 'HASH', '$set1 is defined as a hash'); |
| 240 |
is ($set1->{spec}, 'new_specSet1', 'spec field is "new_specSet1"'); |
| 241 |
is ($set1->{name}, 'new_nameSet1', 'name field is "new_nameSet1"'); |
| 242 |
is_deeply ($set1->{descriptions}, ['new_descSet1'], 'description field is "new_descSet1"'); |
| 243 |
|
| 244 |
$set2 = GetOAISet($set2_id); |
| 245 |
isa_ok($set2, 'HASH', '$new_set2 is defined as a hash'); |
| 246 |
is ($set2->{spec}, 'new_specSet2', 'spec field is "new_specSet2"'); |
| 247 |
is ($set2->{name}, 'new_nameSet2', 'name field is "new_nameSet2"'); |
| 248 |
|
| 249 |
|
| 250 |
# ---------- Testing ModOAISetMappings ---------- |
| 251 |
ok (!defined(ModOAISetMappings), 'ModOAISetMappings without argument is undef'); |
| 252 |
#Add 1st mapping for set1 |
| 253 |
my $mapping1 = [ |
| 254 |
{ |
| 255 |
marcfield => '206', |
| 256 |
marcsubfield => 'a', |
| 257 |
operator => 'equal', |
| 258 |
marcvalue => 'myMarcValue' |
| 259 |
}, |
| 260 |
]; |
| 261 |
ModOAISetMappings($set1_id, $mapping1); |
| 262 |
|
| 263 |
$sth = $dbh->prepare("SELECT count(*) FROM oai_sets_mappings"); |
| 264 |
$sth->execute; |
| 265 |
my $mappingsCount = $sth->fetchrow_array; |
| 266 |
is ($mappingsCount, 1, 'There is 1 mapping'); |
| 267 |
|
| 268 |
$sth = $dbh->prepare("SELECT marcfield, marcsubfield, operator, marcvalue FROM oai_sets_mappings"); |
| 269 |
$sth->execute; |
| 270 |
my ($marcfield, $marcsubfield, $operator, $marcvalue) = $sth->fetchrow_array; |
| 271 |
is ($marcfield, '206', 'marcfield field is "206"'); |
| 272 |
is ($marcsubfield, 'a', 'marcsubfield field is "a"'); |
| 273 |
is ($operator, 'equal', 'operator field is "equal"'); |
| 274 |
is ($marcvalue, 'myMarcValue', 'marcvalue field is "myMarcValue"'); |
| 275 |
|
| 276 |
#Mod 1st mapping of set1 |
| 277 |
my $mapping1_bis = [ |
| 278 |
{ |
| 279 |
marcfield => '256', |
| 280 |
marcsubfield => 'b', |
| 281 |
operator => 'notequal', |
| 282 |
marcvalue => 'myMarcValueBis' |
| 283 |
}, |
| 284 |
]; |
| 285 |
ModOAISetMappings($set1_id, $mapping1_bis); |
| 286 |
|
| 287 |
$sth = $dbh->prepare("SELECT count(*) FROM oai_sets_mappings"); |
| 288 |
$sth->execute; |
| 289 |
$mappingsCount = $sth->fetchrow_array; |
| 290 |
is ($mappingsCount, 1, 'There is 1 mapping'); |
| 291 |
|
| 292 |
$sth = $dbh->prepare("SELECT marcfield, marcsubfield, operator, marcvalue FROM oai_sets_mappings"); |
| 293 |
$sth->execute; |
| 294 |
($marcfield, $marcsubfield, $operator, $marcvalue) = $sth->fetchrow_array; |
| 295 |
is ($marcfield, '256', 'marcfield field is "256"'); |
| 296 |
is ($marcsubfield, 'b', 'marcsubfield field is "b"'); |
| 297 |
is ($operator, 'notequal', 'operator field is "notequal"'); |
| 298 |
is ($marcvalue, 'myMarcValueBis', 'marcvalue field is "myMarcValueBis"'); |
| 299 |
|
| 300 |
#Add 1st mapping of set2 |
| 301 |
my $mapping2 = [ |
| 302 |
{ |
| 303 |
marcfield => '306', |
| 304 |
marcsubfield => 'c', |
| 305 |
operator => 'equal', |
| 306 |
marcvalue => 'myOtherMarcValue' |
| 307 |
}, |
| 308 |
]; |
| 309 |
ModOAISetMappings($set2_id, $mapping2); |
| 310 |
|
| 311 |
$sth = $dbh->prepare("SELECT count(*) FROM oai_sets_mappings"); |
| 312 |
$sth->execute; |
| 313 |
$mappingsCount = $sth->fetchrow_array; |
| 314 |
is ($mappingsCount, 2, 'There is 2 mappings'); |
| 315 |
|
| 316 |
$sth = $dbh->prepare("SELECT marcfield, marcsubfield, operator, marcvalue FROM oai_sets_mappings ORDER BY set_id DESC LIMIT 1"); |
| 317 |
$sth->execute; |
| 318 |
($marcfield, $marcsubfield, $operator, $marcvalue) = $sth->fetchrow_array; |
| 319 |
is ($marcfield, '306', 'marcfield field is "306"'); |
| 320 |
is ($marcsubfield, 'c', 'marcsubfield field is "c"'); |
| 321 |
is ($operator, 'equal', 'operator field is "equal"'); |
| 322 |
is ($marcvalue, 'myOtherMarcValue', 'marcvalue field is "myOtherMarcValue"'); |
| 323 |
|
| 324 |
|
| 325 |
# ---------- Testing GetOAISetsMappings --------- |
| 326 |
my $mappings = GetOAISetsMappings; |
| 327 |
|
| 328 |
isa_ok($mappings, 'HASH', '$mappings is a hashref of arrayrefs of hashrefs'); |
| 329 |
isa_ok($mappings->{$set1_id}, 'ARRAY', '$mappings->{$set1_id} is a arrayrefs of hashrefs'); |
| 330 |
isa_ok($mappings->{$set1_id}->[0], 'HASH', '$mappings->{$set1_id}->[0] is a hashrefs'); |
| 331 |
is ($mappings->{$set1_id}->[0]->{marcfield}, '256', 'marcfield field is "256"'); |
| 332 |
is ($mappings->{$set1_id}->[0]->{marcsubfield}, 'b', 'marcsubfield field is "b"'); |
| 333 |
is ($mappings->{$set1_id}->[0]->{operator}, 'notequal', 'operator field is "notequal"'); |
| 334 |
is ($mappings->{$set1_id}->[0]->{marcvalue}, 'myMarcValueBis', 'marcvalue field is "myMarcValueBis"'); |
| 335 |
|
| 336 |
isa_ok($mappings->{$set2_id}, 'ARRAY', '$mappings->{$set2_id} is a arrayrefs of hashrefs'); |
| 337 |
isa_ok($mappings->{$set2_id}, 'ARRAY', '$mappings->{$set2_id} is a arrayrefs of hashrefs'); |
| 338 |
isa_ok($mappings->{$set2_id}->[0], 'HASH', '$mappings->{$set2_id}->[0] is a hashrefs'); |
| 339 |
is ($mappings->{$set2_id}->[0]->{marcfield}, '306', 'marcfield field is "306"'); |
| 340 |
is ($mappings->{$set2_id}->[0]->{marcsubfield}, 'c', 'marcsubfield field is "c"'); |
| 341 |
is ($mappings->{$set2_id}->[0]->{operator}, 'equal', 'operator field is "equal"'); |
| 342 |
is ($mappings->{$set2_id}->[0]->{marcvalue}, 'myOtherMarcValue', 'marcvalue field is "myOtherMarcValue"'); |
| 343 |
|
| 344 |
|
| 345 |
# ---------- Testing GetOAISetMappings ---------- |
| 346 |
ok (!defined(GetOAISetMappings), 'GetOAISetMappings without argument is undef'); |
| 347 |
|
| 348 |
my $set_mappings1 = GetOAISetMappings($set1_id); |
| 349 |
isa_ok($set_mappings1->[0], 'HASH', '$set_mappings1->[0] is a hashref'); |
| 350 |
is ($set_mappings1->[0]->{marcfield}, '256', 'marcfield field is "256"'); |
| 351 |
is ($set_mappings1->[0]->{marcsubfield}, 'b', 'marcsubfield field is "b"'); |
| 352 |
is ($set_mappings1->[0]->{operator}, 'notequal', 'operator field is "notequal"'); |
| 353 |
is ($set_mappings1->[0]->{marcvalue}, 'myMarcValueBis', 'marcvalue field is "myMarcValueBis"'); |
| 354 |
|
| 355 |
my $set_mappings2 = GetOAISetMappings($set2_id); |
| 356 |
isa_ok($mappings->{$set2_id}->[0], 'HASH', '$mappings->{$set2_id}->[0] is a hashref'); |
| 357 |
is ($mappings->{$set2_id}->[0]->{marcfield}, '306', 'marcfield field is "306"'); |
| 358 |
is ($mappings->{$set2_id}->[0]->{marcsubfield}, 'c', 'marcsubfield field is "c"'); |
| 359 |
is ($mappings->{$set2_id}->[0]->{operator}, 'equal', 'operator field is "equal"'); |
| 360 |
is ($mappings->{$set2_id}->[0]->{marcvalue}, 'myOtherMarcValue', 'marcvalue field is "myOtherMarcValue"'); |
| 361 |
|
| 362 |
|
| 363 |
# ---------- Testing AddOAISetsBiblios ---------- |
| 364 |
ok (!defined(AddOAISetsBiblios), 'AddOAISetsBiblios without argument is undef'); |
| 365 |
ok (!defined(AddOAISetsBiblios(my $arg=[])), 'AddOAISetsBiblios with a no HASH argument is undef'); |
| 366 |
ok (defined(AddOAISetsBiblios($arg={})), 'AddOAISetsBiblios with a HASH argument is def'); |
| 367 |
|
| 368 |
# Create a biblio instance for testing |
| 369 |
my $biblionumber1 = create_helper_biblio('Moffat, Steven'); |
| 370 |
isa_ok(\$biblionumber1, 'SCALAR', '$biblionumber1 is a SCALAR'); |
| 371 |
my $biblionumber2 = create_helper_biblio('Moffat, Steven'); |
| 372 |
isa_ok(\$biblionumber2, 'SCALAR', '$biblionumber2 is a SCALAR'); |
| 373 |
|
| 374 |
my $oai_sets_biblios = { |
| 375 |
$set1_id => [$biblionumber1, $biblionumber2], # key is the set_id, and value is an array ref of biblionumbers |
| 376 |
$set2_id => [], |
| 377 |
}; |
| 378 |
AddOAISetsBiblios($oai_sets_biblios); |
| 379 |
|
| 380 |
$sth = $dbh->prepare("SELECT count(*) FROM oai_sets_biblios"); |
| 381 |
$sth->execute; |
| 382 |
my $bibliosCount = $sth->fetchrow_array; |
| 383 |
is ($bibliosCount, 2, 'There are 2 biblios in oai_sets_biblios'); |
| 384 |
|
| 385 |
#testing biblio for set1_id |
| 386 |
$sth = $dbh->prepare("SELECT * FROM oai_sets_biblios WHERE set_id = ?"); |
| 387 |
$sth->execute($set1_id); |
| 388 |
my $count = $sth->rows; |
| 389 |
is ($count, '2', '$set_id1 has 2 biblio'); |
| 390 |
|
| 391 |
$sth->execute($set1_id); |
| 392 |
my $line = ${ $sth->fetchall_arrayref( {} ) }[0]; |
| 393 |
is($line->{set_id}, $set1_id, "set_id is good"); |
| 394 |
is($line->{biblionumber}, $biblionumber1, "biblionumber is good"); |
| 395 |
|
| 396 |
$sth->execute($set1_id); |
| 397 |
$line = ${ $sth->fetchall_arrayref( {} ) }[1]; |
| 398 |
is($line->{set_id}, $set1_id, "set_id is good"); |
| 399 |
is($line->{biblionumber}, $biblionumber2, "biblionumber is good"); |
| 400 |
|
| 401 |
#testing biblio for set2_id |
| 402 |
$sth->execute($set2_id); |
| 403 |
$count = $sth->rows; |
| 404 |
is ($count, '0', '$set_id2 has 0 biblio'); |
| 405 |
|
| 406 |
|
| 407 |
# ---------- Testing GetOAISetsBiblio ----------- |
| 408 |
$oai_sets = GetOAISetsBiblio($biblionumber1); |
| 409 |
isa_ok($oai_sets, 'ARRAY', '$oai_sets is an arrayref of hashref where each element of the array is a set'); |
| 410 |
isa_ok($oai_sets->[0], 'HASH', '$oai_sets->[0] is a hashrefs of $set1_id'); |
| 411 |
is($oai_sets->[0]->{id}, $set1_id, 'id is $set1_id'); |
| 412 |
is($oai_sets->[0]->{spec}, $set1->{spec}, 'spec is new_specset1'); |
| 413 |
is($oai_sets->[0]->{name}, $set1->{name}, 'name is new_specname1'); |
| 414 |
|
| 415 |
$oai_sets = GetOAISetsBiblio($biblionumber2); |
| 416 |
isa_ok($oai_sets, 'ARRAY', '$oai_sets is an arrayref of hashref where each element of the array is a set'); |
| 417 |
isa_ok($oai_sets->[0], 'HASH', '$oai_sets->[0] is a hashrefs of $set2_id'); |
| 418 |
is($oai_sets->[0]->{id}, $set1_id, 'id is $set1_id'); |
| 419 |
is($oai_sets->[0]->{spec}, $set1->{spec}, 'spec is new_specset1'); |
| 420 |
is($oai_sets->[0]->{name}, $set1->{name}, 'name is new_specname1'); |
| 421 |
|
| 422 |
|
| 423 |
# ---------- Testing ModOAISetsBiblios ---------- |
| 424 |
ok (!defined(ModOAISetsBiblios), 'ModOAISetsBiblios without argument is undef'); |
| 425 |
ok (!defined(ModOAISetsBiblios($arg=[])), 'ModOAISetsBiblios with a no HASH argument is undef'); |
| 426 |
ok (defined(ModOAISetsBiblios($arg={})), 'ModOAISetsBiblios with a HASH argument is def'); |
| 427 |
|
| 428 |
$oai_sets_biblios = { |
| 429 |
$set1_id => [$biblionumber1], |
| 430 |
$set2_id => [$biblionumber2], |
| 431 |
}; |
| 432 |
ModOAISetsBiblios($oai_sets_biblios); |
| 433 |
|
| 434 |
$sth = $dbh->prepare("SELECT count(*) FROM oai_sets_biblios"); |
| 435 |
$sth->execute; |
| 436 |
$bibliosCount = $sth->fetchrow_array; |
| 437 |
is ($bibliosCount, 2, 'There are 2 biblios in oai_sets_biblios'); |
| 438 |
|
| 439 |
#testing biblio for set1_id |
| 440 |
$sth = $dbh->prepare("SELECT * FROM oai_sets_biblios WHERE set_id = ?"); |
| 441 |
$sth->execute($set1_id); |
| 442 |
$count = $sth->rows; |
| 443 |
is ($count, '1', '$set_id1 has 2 biblio'); |
| 444 |
|
| 445 |
$sth->execute($set1_id); |
| 446 |
$line = ${ $sth->fetchall_arrayref( {} ) }[0]; |
| 447 |
is($line->{set_id}, $set1_id, "set_id is good"); |
| 448 |
is($line->{biblionumber}, $biblionumber1, "biblionumber is good"); |
| 449 |
|
| 450 |
#testing biblio for set2_id |
| 451 |
$sth->execute($set2_id); |
| 452 |
$count = $sth->rows; |
| 453 |
is ($count, '1', '$set_id2 has 1 biblio'); |
| 454 |
|
| 455 |
$sth->execute($set2_id); |
| 456 |
$line = ${ $sth->fetchall_arrayref( {} ) }[0]; |
| 457 |
is($line->{set_id}, $set2_id, "set_id is good"); |
| 458 |
is($line->{biblionumber}, $biblionumber2, "biblionumber is good"); |
| 459 |
|
| 460 |
|
| 461 |
# ---------- Testing DelOAISetsBiblio ----------- |
| 462 |
ok (!defined(DelOAISetsBiblio), 'DelOAISetsBiblio without argument is undef'); |
| 463 |
|
| 464 |
DelOAISetsBiblio($biblionumber1); |
| 465 |
is_deeply(GetOAISetsBiblio($biblionumber1), [], "no biblio1 appear in any OAI sets"); |
| 466 |
|
| 467 |
DelOAISetsBiblio($biblionumber2); |
| 468 |
is_deeply(GetOAISetsBiblio($biblionumber2), [], "no biblio2 appear in any OAI sets"); |
| 469 |
|
| 470 |
|
| 471 |
# ---------- Testing DelOAISet ------------------ |
| 472 |
ok (!defined(DelOAISet), 'DelOAISet without argument is undef'); |
| 473 |
|
| 474 |
DelOAISet($set1_id); |
| 475 |
$sth = $dbh->prepare("SELECT count(*) FROM oai_sets"); |
| 476 |
$sth->execute; |
| 477 |
$setsCount = $sth->fetchrow_array; |
| 478 |
is ($setsCount, 1, 'There is 1 set left'); |
| 479 |
$set1 = GetOAISet($set1_id); |
| 480 |
is_deeply ($set1, {}, '$set1 is empty'); |
| 481 |
|
| 482 |
DelOAISet($set2_id); |
| 483 |
$sth = $dbh->prepare("SELECT count(*) FROM oai_sets"); |
| 484 |
$sth->execute; |
| 485 |
$setsCount = $sth->fetchrow_array; |
| 486 |
is ($setsCount, 0, 'There is no set anymore'); |
| 487 |
$set2 = GetOAISet($set2_id); |
| 488 |
is_deeply ($set2, {}, '$set2 is empty'); |
| 489 |
|
| 490 |
$oai_sets=GetOAISets; |
| 491 |
is_deeply ($oai_sets, [], '$oai_sets is empty'); |
| 492 |
|
| 493 |
|
| 494 |
# ---------- Testing UpdateOAISetsBiblio -------- |
| 495 |
ok (!defined(UpdateOAISetsBiblio), 'UpdateOAISetsBiblio without argument is undef'); |
| 496 |
ok (!defined(UpdateOAISetsBiblio($arg)), 'UpdateOAISetsBiblio with only 1 argument is undef'); |
| 497 |
|
| 498 |
#Create a set |
| 499 |
my $setVH = { |
| 500 |
'spec' => 'Set where Author is Victor Hugo', |
| 501 |
'name' => 'VH' |
| 502 |
}; |
| 503 |
my $setVH_id = AddOAISet($setVH); |
| 504 |
|
| 505 |
#Create mappings : 'author' should be 'Victor Hugo' |
| 506 |
my $marcflavour = C4::Context->preference('marcflavour'); |
| 507 |
my $mappingsVH; |
| 508 |
|
| 509 |
if ($marcflavour eq 'UNIMARC' ){ |
| 510 |
$mappingsVH = [ |
| 511 |
{ |
| 512 |
marcfield => '200', |
| 513 |
marcsubfield => 'f', |
| 514 |
operator => 'equal', |
| 515 |
marcvalue => 'Victor Hugo' |
| 516 |
} |
| 517 |
]; |
| 518 |
} |
| 519 |
else { |
| 520 |
$mappingsVH = [ |
| 521 |
{ |
| 522 |
marcfield => '100', |
| 523 |
marcsubfield => 'a', |
| 524 |
operator => 'equal', |
| 525 |
marcvalue => 'Victor Hugo' |
| 526 |
} |
| 527 |
]; |
| 528 |
} |
| 529 |
ModOAISetMappings($setVH_id, $mappingsVH); |
| 530 |
|
| 531 |
|
| 532 |
#Create a biblio notice corresponding at one of mappings |
| 533 |
my $biblionumberVH = create_helper_biblio('Victor Hugo'); |
| 534 |
|
| 535 |
#Update |
| 536 |
my $record = GetMarcBiblio($biblionumberVH); |
| 537 |
UpdateOAISetsBiblio($biblionumberVH, $record); |
| 538 |
|
| 539 |
#is biblio attached to setVH ? |
| 540 |
my $oai_setsVH = GetOAISetsBiblio($biblionumberVH); |
| 541 |
is($oai_setsVH->[0]->{id}, $setVH_id, 'id is ok'); |
| 542 |
is($oai_setsVH->[0]->{spec}, $setVH->{spec}, 'id is ok'); |
| 543 |
is($oai_setsVH->[0]->{name}, $setVH->{name}, 'id is ok'); |
| 544 |
|
| 545 |
|
| 546 |
# ---------- Testing CalcOAISetsBiblio ---------- |
| 547 |
ok (!defined(CalcOAISetsBiblio), 'CalcOAISetsBiblio without argument is undef'); |
| 548 |
|
| 549 |
my @setsEq = CalcOAISetsBiblio($record); |
| 550 |
is_deeply(@setsEq, $setVH_id, 'The $record only belongs to $setVH'); |
| 551 |
|
| 552 |
#Testing CalcOAISetsBiblio for a mapping which operator is 'notequal' |
| 553 |
#Create a set |
| 554 |
my $setNotVH = { |
| 555 |
'spec' => 'Set where Author is NOT Victor Hugo', |
| 556 |
'name' => 'NOT VH' |
| 557 |
}; |
| 558 |
my $setNotVH_id = AddOAISet($setNotVH); |
| 559 |
|
| 560 |
#Create mappings : 'author' should NOT be 'Victor Hugo' |
| 561 |
$marcflavour = C4::Context->preference('marcflavour'); |
| 562 |
my $mappingsNotVH; |
| 563 |
|
| 564 |
if ($marcflavour eq 'UNIMARC' ){ |
| 565 |
$mappingsNotVH = [ |
| 566 |
{ |
| 567 |
marcfield => '200', |
| 568 |
marcsubfield => 'f', |
| 569 |
operator => 'notequal', |
| 570 |
marcvalue => 'Victor Hugo' |
| 571 |
} |
| 572 |
]; |
| 573 |
} |
| 574 |
else { |
| 575 |
$mappingsNotVH = [ |
| 576 |
{ |
| 577 |
marcfield => '100', |
| 578 |
marcsubfield => 'a', |
| 579 |
operator => 'notequal', |
| 580 |
marcvalue => 'Victor Hugo' |
| 581 |
} |
| 582 |
]; |
| 583 |
} |
| 584 |
ModOAISetMappings($setNotVH_id, $mappingsNotVH); |
| 585 |
|
| 586 |
|
| 587 |
#Create a biblio notice corresponding at one of mappings |
| 588 |
my $biblionumberNotVH = create_helper_biblio('Sponge, Bob'); |
| 589 |
|
| 590 |
#Update |
| 591 |
$record = GetMarcBiblio($biblionumberNotVH); |
| 592 |
UpdateOAISetsBiblio($biblionumberNotVH, $record); |
| 593 |
|
| 594 |
my @setsNotEq = CalcOAISetsBiblio($record); |
| 595 |
is_deeply(@setsNotEq, $setNotVH_id, 'The $record only belongs to $setNotVH'); |
| 596 |
|
| 597 |
|
| 598 |
# |
| 599 |
|
| 600 |
|
| 601 |
|
| 602 |
|
| 603 |
# ---------- Subs -------------------------------- |
| 604 |
|
| 605 |
|
| 606 |
# Helper method to set up a Biblio. |
| 607 |
sub create_helper_biblio { |
| 608 |
my $author = shift; |
| 609 |
|
| 610 |
return unless (defined($author)); |
| 611 |
|
| 612 |
my $marcflavour = C4::Context->preference('marcflavour'); |
| 613 |
my $bib = MARC::Record->new(); |
| 614 |
my $title = 'Silence in the library'; |
| 615 |
|
| 616 |
if ($marcflavour eq 'UNIMARC' ){ |
| 617 |
$bib->append_fields( |
| 618 |
MARC::Field->new('200', ' ', ' ', f => $author), |
| 619 |
MARC::Field->new('200', ' ', ' ', a => $title), |
| 620 |
); |
| 621 |
} |
| 622 |
else{ |
| 623 |
$bib->append_fields( |
| 624 |
MARC::Field->new('100', ' ', ' ', a => $author), |
| 625 |
MARC::Field->new('245', ' ', ' ', a => $title), |
| 626 |
); |
| 627 |
} |
| 628 |
my ($biblionumber)= AddBiblio($bib, ''); |
| 629 |
return $biblionumber; |
| 630 |
} |
| 631 |
|
| 632 |
$dbh->rollback; |