3 # This file is part of Koha.
13 Koha::Libraries->search->count;
17 subtest "Scenario: Show how caching prevents Test::DBIx::Class from working properly and how to circumvent it", sub {
18 my ($firstSchema, $schema, $cachedSchema, $cachedSchema2, $firstLibCount, $libCount);
22 ok($firstSchema = Koha::Database->schema,
23 'Step: Given a normal DB connection.');
25 ok($firstLibCount = Koha::Libraries->search->count,
26 ' When the libraries are counted');
29 ' Then we got a count'); #There should be something like 12 branches in the default DB but making an accurate check here to prevent surface for brittleness.
30 print "\$firstLibCount '$firstLibCount'\n" if $verbose;
32 ok($cachedSchema = Koha::Database::get_schema_cached(),
33 ' And the DB connection is cached');
35 unlike(getConnectionDBName($cachedSchema), qr/sqlite/i,
36 ' And the cached DB connection type is not sqlite');
37 print "getConnectionDBName() -> ".getConnectionDBName($cachedSchema)."\n" if $verbose;
40 use_ok('Test::DBIx::Class');
41 my $db = Test::MockModule->new('Koha::Database');
42 $db->mock( _new_schema => sub { return Schema(); } );
44 'Step: Given Test::DBIx::Class (T:D:C) is loaded and DB accessor is mocked. Connection from cache is still used.');
46 ok($libCount = Koha::Libraries->search->count,
47 ' When the libraries are counted');
49 is($libCount, $firstLibCount,
50 ' Then we got the same count as without T:D:C');
52 $cachedSchema = Koha::Database::get_schema_cached();
53 is($cachedSchema, $firstSchema,
54 ' And the cached DB connection is the same as without T:D:C');
56 is(getConnectionDBName($cachedSchema), getConnectionDBName($firstSchema),
57 ' And the cached DB connection type is unchanged');
60 ok(Koha::Database::flush_schema_cache(),
61 'Step: Given the DB connection cache is flushed');
63 ok(! ($libCount = Koha::Libraries->search->count),
64 ' When the libraries are counted');
67 ' Then we got 0 libraries because fixtures are not deployed');
69 $cachedSchema = Koha::Database::get_schema_cached();
70 isnt($cachedSchema, $firstSchema,
71 ' And the cached DB connection has changed');
73 like(getConnectionDBName($cachedSchema), qr/sqlite/i,
74 ' And the cached DB connection type is sqlite');
77 eval "fixtures_ok [ #Dynamically load fixtures, because we dynamically load T:D:C. Otherwise there be compile errors!
79 ['branchcode', 'branchname'],
80 ['XXX_test', 'my branchname XXX'],
83 'Step: Given we deploy T:D:C Fixtures';";
85 ok($libCount = Koha::Libraries->search->count,
86 ' When the libraries are counted');
89 ' Then we got the count from fixtures');
91 $cachedSchema2 = Koha::Database::get_schema_cached();
92 is($cachedSchema2, $cachedSchema,
93 ' And the cached DB connection is the same from T:D:C');
95 like(getConnectionDBName($cachedSchema), qr/sqlite/i,
96 ' And the cached DB connection type is sqlite');
105 sub getConnectionDBName {
106 #return shift->storage->_dbh_details->{info}->{dbms_version}; #This would return the name from server, but sqlite doesn't report a clear text name and version here.
107 return shift->storage->connect_info->[0]->{dsn};