3 # This file is part of Koha.
13 subtest "Scenario: Show how caching prevents Test::DBIx::Class from working properly and how to circumvent it", sub {
14 my ($firstSchema, $cachedSchema, $cachedSchema2, $firstLibCount, $libCount);
18 ok($firstSchema = Koha::Database->schema,
19 'Step: Given a normal DB connection.');
21 $firstLibCount = Koha::Libraries->search->count; # first count normal conn
23 ok($cachedSchema = Koha::Database::get_schema_cached(),
24 ' And the DB connection is cached');
26 unlike(getConnectionDBName($cachedSchema), qr/sqlite/i,
27 ' And the cached DB connection type is not sqlite');
29 use_ok('Test::DBIx::Class');
30 my $db = Test::MockModule->new('Koha::Database');
31 $db->mock( _new_schema => sub { return Schema(); } );
33 'Step: Given Test::DBIx::Class (T:D:C) is loaded and DB accessor is mocked. Connection from cache is still used.');
35 $libCount = Koha::Libraries->search->count;
37 is($libCount, $firstLibCount,
38 ' Then we got the same count as without T:D:C');
40 $cachedSchema = Koha::Database::get_schema_cached();
41 is($cachedSchema, $firstSchema,
42 ' And the cached DB connection is the same as without T:D:C');
44 is(getConnectionDBName($cachedSchema), getConnectionDBName($firstSchema),
45 ' And the cached DB connection type is unchanged');
48 ok(Koha::Database::flush_schema_cache(),
49 'Step: Given the DB connection cache is flushed');
51 $libCount = Koha::Libraries->search->count;
54 ' Then we got 0 libraries because fixtures are not deployed');
56 $cachedSchema = Koha::Database::get_schema_cached();
57 isnt($cachedSchema, $firstSchema,
58 ' And the cached DB connection has changed');
60 like(getConnectionDBName($cachedSchema), qr/sqlite/i,
61 ' And the cached DB connection type is sqlite');
64 fixtures_ok( [ #Dynamically load fixtures, because we dynamically load T:D:C. Otherwise there be compile errors!
66 ['branchcode', 'branchname'],
67 ['XXX_test', 'my branchname XXX'],
70 'Step: Given we deploy T:D:C Fixtures');
72 $libCount = Koha::Libraries->search->count;
75 ' Then we got the count from fixtures');
77 $cachedSchema2 = Koha::Database::get_schema_cached();
78 is($cachedSchema2, $cachedSchema,
79 ' And the cached DB connection is the same from T:D:C');
81 like(getConnectionDBName($cachedSchema), qr/sqlite/i,
82 ' And the cached DB connection type is sqlite');
91 sub getConnectionDBName {
92 #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.
93 return shift->storage->connect_info->[0]->{dsn};