|
Lines 19-25
Link Here
|
| 19 |
|
19 |
|
| 20 |
use Modern::Perl; |
20 |
use Modern::Perl; |
| 21 |
|
21 |
|
| 22 |
use Test::More tests => 13; |
22 |
use Test::More tests => 14; |
| 23 |
|
23 |
|
| 24 |
use C4::Biblio; |
24 |
use C4::Biblio; |
| 25 |
use C4::Context; |
25 |
use C4::Context; |
|
Lines 32-37
use Koha::Library;
Link Here
|
| 32 |
use Koha::Libraries; |
32 |
use Koha::Libraries; |
| 33 |
use Koha::Database; |
33 |
use Koha::Database; |
| 34 |
use Koha::CirculationRules; |
34 |
use Koha::CirculationRules; |
|
|
35 |
use Koha::Cache::Memory::Lite; |
| 35 |
|
36 |
|
| 36 |
use t::lib::Mocks; |
37 |
use t::lib::Mocks; |
| 37 |
use t::lib::TestBuilder; |
38 |
use t::lib::TestBuilder; |
|
Lines 413-415
subtest 'outgoing_transfers' => sub {
Link Here
|
| 413 |
|
414 |
|
| 414 |
$schema->storage->txn_rollback; |
415 |
$schema->storage->txn_rollback; |
| 415 |
}; |
416 |
}; |
| 416 |
- |
417 |
|
|
|
418 |
subtest 'find cache tests' => sub { |
| 419 |
plan tests => 15; |
| 420 |
|
| 421 |
$schema->storage->txn_begin; |
| 422 |
|
| 423 |
my $library1 = $builder->build_object( |
| 424 |
{ |
| 425 |
class => 'Koha::Libraries', |
| 426 |
value => { |
| 427 |
branchname => 'My library' |
| 428 |
} |
| 429 |
} |
| 430 |
); |
| 431 |
my $library2 = $builder->build_object( |
| 432 |
{ |
| 433 |
class => 'Koha::Libraries', |
| 434 |
value => { |
| 435 |
branchname => 'My other library' |
| 436 |
} |
| 437 |
} |
| 438 |
); |
| 439 |
|
| 440 |
# Since build_object calls find internally |
| 441 |
# we first have to flush the cache since these |
| 442 |
# objects are now already cached |
| 443 |
Koha::Cache::Memory::Lite->get_instance()->flush(); |
| 444 |
|
| 445 |
my $ids_bucket = Koha::Libraries->_find_cache_bucket('ids'); |
| 446 |
ok(!%{$ids_bucket}, 'Find cache has been cleared after flushing memory cache'); |
| 447 |
|
| 448 |
my $cached_library = Koha::Libraries->find($library1->branchcode); |
| 449 |
Koha::Libraries->find($library2->branchcode); |
| 450 |
|
| 451 |
my $cached_library_ids_bucket = Koha::Libraries->_find_cache_get( |
| 452 |
Koha::Libraries->_find_cache_cache_key($library1->branchcode), |
| 453 |
'ids' |
| 454 |
); |
| 455 |
is (ref($cached_library_ids_bucket), 'Koha::Library', 'Library should be cached after previously have been fetched'); |
| 456 |
is ($cached_library_ids_bucket->branchcode, $library1->branchcode, 'The cashed library should be the expected library'); |
| 457 |
|
| 458 |
# Set property of library by-passing the cache expiration logic |
| 459 |
# so we can veriy library is not only present in cache by also |
| 460 |
# properly retrieved in find |
| 461 |
$cached_library->_result->set_column('branchname', 'My library test'); |
| 462 |
$cached_library = Koha::Libraries->find($library1->branchcode); |
| 463 |
is ($cached_library->branchname, 'My library test', 'The cashed library returned by find should be the cached library'); |
| 464 |
|
| 465 |
$library1->branchname('New branchname'); |
| 466 |
$cached_library_ids_bucket = Koha::Libraries->_find_cache_get( |
| 467 |
Koha::Libraries->_find_cache_cache_key($library1->branchcode), |
| 468 |
'ids' |
| 469 |
); |
| 470 |
is ($cached_library_ids_bucket, undef, 'Cache has been expired after changing library property'); |
| 471 |
|
| 472 |
$cached_library = Koha::Libraries->_find_cache_get( |
| 473 |
Koha::Libraries->_find_cache_cache_key($library2->branchcode), |
| 474 |
'ids' |
| 475 |
); |
| 476 |
is (ref($cached_library), 'Koha::Library', 'Cache should still contain other cached libraries'); |
| 477 |
|
| 478 |
my $stored_library = Koha::Libraries->find($library1->branchcode); |
| 479 |
is ($stored_library->branchname, 'My library', 'Library is fetched from database after cache has been expired'); |
| 480 |
|
| 481 |
$cached_library = Koha::Libraries->_find_cache_get( |
| 482 |
Koha::Libraries->_find_cache_cache_key($library1->branchcode), |
| 483 |
'ids' |
| 484 |
); |
| 485 |
is (ref($cached_library), 'Koha::Library', 'Library should be cached after previously have been fetched'); |
| 486 |
|
| 487 |
$library1->store(); |
| 488 |
$cached_library = Koha::Libraries->_find_cache_get( |
| 489 |
Koha::Libraries->_find_cache_cache_key($library1->branchcode), |
| 490 |
'ids' |
| 491 |
); |
| 492 |
is ($cached_library, undef, 'Cache has been expired after saving library to database'); |
| 493 |
|
| 494 |
Koha::Libraries->find({ branchcode => $library1->branchcode }); |
| 495 |
Koha::Libraries->find({ branchcode => $library2->branchcode }); |
| 496 |
|
| 497 |
$cached_library = Koha::Libraries->_find_cache_get( |
| 498 |
Koha::Libraries->_find_cache_cache_key({ branchcode => $library1->branchcode }), |
| 499 |
'args' |
| 500 |
); |
| 501 |
is ( |
| 502 |
ref($cached_library), |
| 503 |
'Koha::Library', |
| 504 |
'Library should be cached after previously have been fetched, and use the args cache bucket if find was called with column conditions' |
| 505 |
); |
| 506 |
|
| 507 |
$cached_library->_result->set_column('branchname', 'My library test'); |
| 508 |
$cached_library = Koha::Libraries->find({ branchcode => $library1->branchcode }); |
| 509 |
is ( |
| 510 |
$cached_library->branchname, |
| 511 |
'My library test', |
| 512 |
'The cashed library returned by find should be the cached library, using the args cache bucket' |
| 513 |
); |
| 514 |
|
| 515 |
$cached_library->branchname('Some other branchname'); |
| 516 |
$cached_library = Koha::Libraries->_find_cache_get( |
| 517 |
Koha::Libraries->_find_cache_cache_key({ branchcode => $library1->branchcode }), |
| 518 |
'args' |
| 519 |
); |
| 520 |
is ( |
| 521 |
$cached_library, |
| 522 |
undef, |
| 523 |
'Cache has been expired after changing branchname, using the args cache bucket' |
| 524 |
); |
| 525 |
|
| 526 |
$cached_library = Koha::Libraries->_find_cache_get( |
| 527 |
Koha::Libraries->_find_cache_cache_key({ branchcode => $library2->branchcode }), |
| 528 |
'args' |
| 529 |
); |
| 530 |
is ( |
| 531 |
$cached_library, |
| 532 |
undef, |
| 533 |
'The whole args cache bucket has been emptied after triggering cache expiration' |
| 534 |
); |
| 535 |
|
| 536 |
Koha::Libraries->find($library1->branchcode, { key => 'primary' }); |
| 537 |
$cached_library = Koha::Libraries->_find_cache_get( |
| 538 |
Koha::Libraries->_find_cache_cache_key($library1->branchcode, { key => 'primary' }), |
| 539 |
'args' |
| 540 |
); |
| 541 |
is ( |
| 542 |
ref($cached_library), |
| 543 |
'Koha::Library', |
| 544 |
'The args cache bucket should be used if find is called with the attrs argument' |
| 545 |
); |
| 546 |
|
| 547 |
$library1->delete(); |
| 548 |
$cached_library = Koha::Libraries->_find_cache_get( |
| 549 |
Koha::Libraries->_find_cache_cache_key($library1->branchcode, { key => 'primary' }), |
| 550 |
'args' |
| 551 |
); |
| 552 |
is ($cached_library, undef, 'Cache has been expired after deleting library'); |
| 553 |
|
| 554 |
$schema->storage->txn_rollback; |
| 555 |
} |