Bug 18226: [QA Follow-up] Remove verbose
[koha.git] / t / db_dependent / 01-test_dbic.t
1 #!/usr/bin/perl
2
3 # This file is part of Koha.
4
5 use Modern::Perl;
6
7 use Test::More;
8 use Test::MockModule;
9
10 use Koha::Database;
11 use Koha::Libraries;
12
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);
15
16   eval {
17
18   ok($firstSchema = Koha::Database->schema,
19   'Step: Given a normal DB connection.');
20
21   $firstLibCount = Koha::Libraries->search->count; # first count normal conn
22
23   ok($cachedSchema = Koha::Database::get_schema_cached(),
24   '  And the DB connection is cached');
25
26   unlike(getConnectionDBName($cachedSchema), qr/sqlite/i,
27   '  And the cached DB connection type is not sqlite');
28
29   use_ok('Test::DBIx::Class');
30   my $db = Test::MockModule->new('Koha::Database');
31   $db->mock( _new_schema => sub { return Schema(); } );
32   ok(1,
33   'Step: Given Test::DBIx::Class (T:D:C) is loaded and DB accessor is mocked. Connection from cache is still used.');
34
35   $libCount = Koha::Libraries->search->count;
36
37   is($libCount, $firstLibCount,
38   '  Then we got the same count as without T:D:C');
39
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');
43
44   is(getConnectionDBName($cachedSchema), getConnectionDBName($firstSchema),
45   '  And the cached DB connection type is unchanged');
46
47
48   ok(Koha::Database::flush_schema_cache(),
49   'Step: Given the DB connection cache is flushed');
50
51   $libCount = Koha::Libraries->search->count;
52
53   is($libCount, 0,
54   '  Then we got 0 libraries because fixtures are not deployed');
55
56   $cachedSchema = Koha::Database::get_schema_cached();
57   isnt($cachedSchema, $firstSchema,
58   '  And the cached DB connection has changed');
59
60   like(getConnectionDBName($cachedSchema), qr/sqlite/i,
61   '  And the cached DB connection type is sqlite');
62
63
64   fixtures_ok( [ #Dynamically load fixtures, because we dynamically load T:D:C. Otherwise there be compile errors!
65       Branch => [
66           ['branchcode', 'branchname'],
67           ['XXX_test', 'my branchname XXX'],
68       ]
69   ],
70   'Step: Given we deploy T:D:C Fixtures');
71
72   $libCount = Koha::Libraries->search->count;
73
74   is($libCount, 1,
75   '  Then we got the count from fixtures');
76
77   $cachedSchema2 = Koha::Database::get_schema_cached();
78   is($cachedSchema2, $cachedSchema,
79   '  And the cached DB connection is the same from T:D:C');
80
81   like(getConnectionDBName($cachedSchema), qr/sqlite/i,
82   '  And the cached DB connection type is sqlite');
83
84   };
85   ok(0, $@) if $@;
86 };
87
88 done_testing;
89
90
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};
94 }