3 # This file is part of Koha.
15 subtest "Scenario: Show how caching prevents Test::DBIx::Class from working properly and how to circumvent it", sub {
16 my ($firstSchema, $cachedSchema, $cachedSchema2, $firstLibCount, $libCount);
20 ok($firstSchema = Koha::Database->schema,
21 'Step: Given a normal DB connection.');
23 $firstLibCount = Koha::Libraries->search->count; # first count normal conn
25 print "\$firstLibCount '$firstLibCount'\n" if $verbose;
27 ok($cachedSchema = Koha::Database::get_schema_cached(),
28 ' And the DB connection is cached');
30 unlike(getConnectionDBName($cachedSchema), qr/sqlite/i,
31 ' And the cached DB connection type is not sqlite');
32 print "getConnectionDBName() -> ".getConnectionDBName($cachedSchema)."\n" if $verbose;
35 use_ok('Test::DBIx::Class');
36 my $db = Test::MockModule->new('Koha::Database');
37 $db->mock( _new_schema => sub { return Schema(); } );
39 'Step: Given Test::DBIx::Class (T:D:C) is loaded and DB accessor is mocked. Connection from cache is still used.');
41 $libCount = Koha::Libraries->search->count;
43 is($libCount, $firstLibCount,
44 ' Then we got the same count as without T:D:C');
46 $cachedSchema = Koha::Database::get_schema_cached();
47 is($cachedSchema, $firstSchema,
48 ' And the cached DB connection is the same as without T:D:C');
50 is(getConnectionDBName($cachedSchema), getConnectionDBName($firstSchema),
51 ' And the cached DB connection type is unchanged');
54 ok(Koha::Database::flush_schema_cache(),
55 'Step: Given the DB connection cache is flushed');
57 $libCount = Koha::Libraries->search->count;
60 ' Then we got 0 libraries because fixtures are not deployed');
62 $cachedSchema = Koha::Database::get_schema_cached();
63 isnt($cachedSchema, $firstSchema,
64 ' And the cached DB connection has changed');
66 like(getConnectionDBName($cachedSchema), qr/sqlite/i,
67 ' And the cached DB connection type is sqlite');
70 fixtures_ok( [ #Dynamically load fixtures, because we dynamically load T:D:C. Otherwise there be compile errors!
72 ['branchcode', 'branchname'],
73 ['XXX_test', 'my branchname XXX'],
76 'Step: Given we deploy T:D:C Fixtures');
78 $libCount = Koha::Libraries->search->count;
81 ' Then we got the count from fixtures');
83 $cachedSchema2 = Koha::Database::get_schema_cached();
84 is($cachedSchema2, $cachedSchema,
85 ' And the cached DB connection is the same from T:D:C');
87 like(getConnectionDBName($cachedSchema), qr/sqlite/i,
88 ' And the cached DB connection type is sqlite');
97 sub getConnectionDBName {
98 #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.
99 return shift->storage->connect_info->[0]->{dsn};