Bug 19382: (follow-up) Alternate tests
[koha.git] / t / db_dependent / 01-test_dbic.t
blob63dd55f690f7827bf0bae099d499c876e867a645
1 #!/usr/bin/perl
3 # This file is part of Koha.
5 # Koha is free software; you can redistribute it and/or modify it under the
6 # terms of the GNU General Public License as published by the Free Software
7 # Foundation; either version 3 of the License, or (at your option) any later
8 # version.
10 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
11 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License along
15 # with Koha; if not, see <http://www.gnu.org/licenses>.
17 use Modern::Perl;
19 use Test::More;
20 use Test::MockModule;
22 use Koha::Database;
23 use Koha::Libraries;
25 subtest "Scenario: Show how caching prevents Test::DBIx::Class from working properly and how to circumvent it", sub {
26 my ( $firstSchema, $cachedSchema, $cachedSchema2, $firstLibCount, $libCount );
28 eval {
30 ok(
31 $firstSchema = Koha::Database->schema,
32 'Step: Given a normal DB connection.'
35 $firstLibCount =
36 Koha::Libraries->search->count; # first count normal conn
38 ok( $cachedSchema = Koha::Database::get_schema_cached(),
39 ' And the DB connection is cached' );
41 unlike( getConnectionDBName($cachedSchema),
42 qr/sqlite/i, ' And the cached DB connection type is not sqlite' );
44 use_ok('Test::DBIx::Class');
45 my $db = Test::MockModule->new('Koha::Database');
46 $db->mock( _new_schema => sub { return Schema(); } );
47 ok( 1,
48 'Step: Given Test::DBIx::Class (T:D:C) is loaded and DB accessor is mocked. Connection from cache is still used.'
51 $libCount = Koha::Libraries->search->count;
53 is( $libCount, $firstLibCount,
54 ' Then we got the same count as without T:D:C' );
56 $cachedSchema = Koha::Database::get_schema_cached();
57 is( $cachedSchema, $firstSchema,
58 ' And the cached DB connection is the same as without T:D:C' );
60 is(
61 getConnectionDBName($cachedSchema),
62 getConnectionDBName($firstSchema),
63 ' And the cached DB connection type is unchanged'
66 ok( Koha::Database::flush_schema_cache(),
67 'Step: Given the DB connection cache is flushed' );
69 $libCount = Koha::Libraries->search->count;
71 is( $libCount, 0,
72 ' Then we got 0 libraries because fixtures are not deployed' );
74 $cachedSchema = Koha::Database::get_schema_cached();
75 isnt( $cachedSchema, $firstSchema,
76 ' And the cached DB connection has changed' );
78 like( getConnectionDBName($cachedSchema),
79 qr/sqlite/i, ' And the cached DB connection type is sqlite' );
81 fixtures_ok(
82 [ #Dynamically load fixtures, because we dynamically load T:D:C. Otherwise there be compile errors!
83 Branch => [
84 [ 'branchcode', 'branchname' ],
85 [ 'XXX_test', 'my branchname XXX' ],
88 'Step: Given we deploy T:D:C Fixtures'
91 $libCount = Koha::Libraries->search->count;
93 is( $libCount, 1, ' Then we got the count from fixtures' );
95 $cachedSchema2 = Koha::Database::get_schema_cached();
96 is( $cachedSchema2, $cachedSchema,
97 ' And the cached DB connection is the same from T:D:C' );
99 like( getConnectionDBName($cachedSchema),
100 qr/sqlite/i, ' And the cached DB connection type is sqlite' );
103 ok( 0, $@ ) if $@;
106 done_testing;
108 sub getConnectionDBName {
109 return shift->storage->connect_info->[0]->{dsn};