2 use base qw(Test::Class);
7 eval "use Test::Class";
8 plan skip_all => "Test::Class required for performing database tests" if $@;
9 # Or, maybe I should just die there.
18 use File::Temp qw/ tempdir /;
20 # Since this is an abstract base class, this prevents these tests from
21 # being run directly unless we're testing a subclass. It just makes
23 __PACKAGE__->SKIP_CLASS( 1 );
26 =head2 startup methods
28 these are run once, at the beginning of the whole test suite
32 sub startup_15_truncate_tables : Test( startup => 1 ) {
35 # my @truncate_tables = qw( accountlines
47 # auth_subfield_structure
74 # import_record_matches
84 # language_descriptions
85 # language_rfc4646_to_iso639
86 # language_script_bidi
87 # language_script_mapping
88 # language_subtag_registry
91 # marc_subfield_structure
95 # matchpoint_component_norms
96 # matchpoint_components
109 # repeatable_holidays
126 # subscriptionhistory
127 # subscriptionroutinglist
132 # virtualshelfcontents
138 my @truncate_tables = qw( accountlines
188 subscriptionroutinglist
194 my $failed_to_truncate = 0;
195 foreach my $table ( @truncate_tables ) {
196 my $dbh = C4::Context->dbh();
197 $dbh->do( "truncate $table" )
198 or $failed_to_truncate = 1;
200 is( $failed_to_truncate, 0, 'truncated tables' );
204 =head2 startup_20_add_bookseller
206 we need a bookseller for many of the tests, so let's insert one. Feel
207 free to use this one, or insert your own.
211 sub startup_20_add_bookseller : Test(startup => 1) {
214 my $booksellerinfo = { name => 'bookseller ' . $self->random_string(),
217 my $id = AddBookseller( $booksellerinfo );
218 ok( $id, "created bookseller: $id" );
219 $self->{'booksellerid'} = $id;
224 =head2 startup_22_add_bookfund
226 we need a bookfund for many of the tests. This currently uses one that
227 is in the skeleton database. free to use this one, or insert your
232 sub startup_22_add_bookfund : Test(startup => 2) {
235 my $bookfundid = 'GEN';
236 my $bookfund = GetBookFund( $bookfundid, undef );
237 # diag( Data::Dumper->Dump( [ $bookfund ], qw( bookfund ) ) );
238 is( $bookfund->{'bookfundid'}, $bookfundid, "found bookfund: '$bookfundid'" );
239 is( $bookfund->{'bookfundname'}, 'General Stacks', "found bookfund: '$bookfundid'" );
241 $self->{'bookfundid'} = $bookfundid;
245 =head2 startup_24_add_member
247 Add a patron/member for the tests to use
251 sub startup_24_add_member : Test(startup => 1) {
254 my $memberinfo = { surname => 'surname ' . $self->random_string(),
255 firstname => 'firstname' . $self->random_string(),
256 address => 'address' . $self->random_string(),
257 city => 'city' . $self->random_string(),
258 branchcode => 'CPL', # CPL => Centerville
259 categorycode => 'PT', # PT => PaTron
262 my $id = AddMember( %$memberinfo );
263 ok( $id, "created member: $id" );
264 $self->{'memberid'} = $id;
271 setup methods are run before every test method
275 =head2 teardown methods
277 teardown methods are many time, once at the end of each test method.
281 =head2 shutdown methods
283 shutdown methods are run once, at the end of the test suite
287 =head2 utility methods
289 These are not test methods, but they're handy
295 Nice for generating names and such. It's not actually random, more
303 my $wordsize = 6; # how many letters in your string?
305 # leave out these characters: "oOlL10". They're too confusing.
306 my @alphabet = ( 'a'..'k','m','n','p'..'z', 'A'..'K','M','N','P'..'Z', 2..9 );
309 foreach ( 0..$wordsize ) {
310 $randomstring .= $alphabet[ rand( scalar( @alphabet ) ) ];
312 return $randomstring;
318 $self->add_biblios( count => 10,
322 count: number of biblios to add
323 add_items: should you add items for each one?
329 adds the biblionumbers to the $self->{'biblios'} listref
332 Should I allow you to pass in biblio information, like title?
333 Since this method is in the KohaTest class, all tests in it will be ignored, unless you call this from your own namespace.
334 This runs 10 tests, plus 4 for each "count", plus 3 more for each item added.
342 $param{'count'} = 1 unless defined( $param{'count'} );
343 $param{'add_items'} = 0 unless defined( $param{'add_items'} );
345 foreach my $counter ( 1..$param{'count'} ) {
346 my $marcrecord = MARC::Record->new();
347 isa_ok( $marcrecord, 'MARC::Record' );
348 my $appendedfieldscount = $marcrecord->append_fields( MARC::Field->new( '100', '1', '0',
351 MARC::Field->new( '245', '1', '4',
352 a => sprintf( 'The Adventures of Huckleberry Finn Test %s', $counter ),
353 c => "Mark Twain ; illustrated by E.W. Kemble." )
355 is( $appendedfieldscount, 2, 'added 2 fields' );
357 my $frameworkcode = ''; # XXX I'd like to put something reasonable here.
358 my ( $biblionumber, $biblioitemnumber ) = AddBiblio( $marcrecord, $frameworkcode );
359 ok( $biblionumber, "the biblionumber is $biblionumber" );
360 ok( $biblioitemnumber, "the biblioitemnumber is $biblioitemnumber" );
361 if ( $param{'add_items'} ) {
362 # my @iteminfo = AddItem( {}, $biblionumber );
363 my @iteminfo = AddItemFromMarc( $marcrecord, $biblionumber );
364 is( $iteminfo[0], $biblionumber, "biblionumber is $biblionumber" );
365 is( $iteminfo[1], $biblioitemnumber, "biblioitemnumber is $biblioitemnumber" );
366 ok( $iteminfo[2], "itemnumber is $iteminfo[2]" );
368 push @{$self->{'biblios'}}, $biblionumber;
371 my $query = 'Finn Test';
373 # XXX we're going to repeatedly try to fetch the marc records that
374 # we inserted above. It may take a while before they all show
377 DELAY: foreach my $trial ( 1..$tries ) {
378 diag "waiting for zebra indexing. Trial: $trial of $tries";
379 my ( $error, $results ) = SimpleSearch( $query );
380 if ( $param{'count'} <= scalar( @$results ) ) {
381 ok( $tries, "found all $param{'count'} titles after $trial tries" );
386 if ( $trial == $tries ) {
387 fail( "we never found all $param{'count'} titles even after $tries tries." );
396 Do a fast reindexing of all of the bib and authority
397 records and mark all zebraqueue entries done.
399 Useful for test routines that need to do a
400 lot of indexing without having to wait for
403 In NoZebra model, this only marks zebraqueue
404 done - the records should already be indexed.
411 # mark zebraqueue done regardless of the indexing mode
412 my $dbh = C4::Context->dbh();
413 $dbh->do("UPDATE zebraqueue SET done = 1 WHERE done = 0");
415 return if C4::Context->preference('NoZebra');
417 my $directory = tempdir(CLEANUP => 1);
418 foreach my $record_type qw(biblio authority) {
419 mkdir "$directory/$record_type";
420 my $sth = $dbh->prepare($record_type eq "biblio" ? "SELECT marc FROM biblioitems" : "SELECT marc FROM auth_header");
422 open OUT, ">:utf8", "$directory/$record_type/records";
423 while (my ($blob) = $sth->fetchrow_array) {
427 my $zebra_server = "${record_type}server";
428 my $zebra_config = C4::Context->zebraconfig($zebra_server)->{'config'};
429 my $zebra_db_dir = C4::Context->zebraconfig($zebra_server)->{'directory'};
430 my $zebra_db = $record_type eq 'biblio' ? 'biblios' : 'authorities';
431 system "zebraidx -c $zebra_config -d $zebra_db -g iso2709 init > /dev/null 2>\&1";
432 system "zebraidx -c $zebra_config -d $zebra_db -g iso2709 update $directory/${record_type} > /dev/null 2>\&1";
433 system "zebraidx -c $zebra_config -d $zebra_db -g iso2709 commit > /dev/null 2>\&1";