Bug 18226: [QA Follow-up] Remove further assumptions on branch count
[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 my $verbose = 0;
14
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);
17
18   eval {
19
20   ok($firstSchema = Koha::Database->schema,
21   'Step: Given a normal DB connection.');
22
23   $firstLibCount = Koha::Libraries->search->count; # first count normal conn
24
25   print "\$firstLibCount '$firstLibCount'\n" if $verbose;
26
27   ok($cachedSchema = Koha::Database::get_schema_cached(),
28   '  And the DB connection is cached');
29
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;
33
34
35   use_ok('Test::DBIx::Class');
36   my $db = Test::MockModule->new('Koha::Database');
37   $db->mock( _new_schema => sub { return Schema(); } );
38   ok(1,
39   'Step: Given Test::DBIx::Class (T:D:C) is loaded and DB accessor is mocked. Connection from cache is still used.');
40
41   $libCount = Koha::Libraries->search->count;
42
43   is($libCount, $firstLibCount,
44   '  Then we got the same count as without T:D:C');
45
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');
49
50   is(getConnectionDBName($cachedSchema), getConnectionDBName($firstSchema),
51   '  And the cached DB connection type is unchanged');
52
53
54   ok(Koha::Database::flush_schema_cache(),
55   'Step: Given the DB connection cache is flushed');
56
57   $libCount = Koha::Libraries->search->count;
58
59   is($libCount, 0,
60   '  Then we got 0 libraries because fixtures are not deployed');
61
62   $cachedSchema = Koha::Database::get_schema_cached();
63   isnt($cachedSchema, $firstSchema,
64   '  And the cached DB connection has changed');
65
66   like(getConnectionDBName($cachedSchema), qr/sqlite/i,
67   '  And the cached DB connection type is sqlite');
68
69
70   fixtures_ok( [ #Dynamically load fixtures, because we dynamically load T:D:C. Otherwise there be compile errors!
71       Branch => [
72           ['branchcode', 'branchname'],
73           ['XXX_test', 'my branchname XXX'],
74       ]
75   ],
76   'Step: Given we deploy T:D:C Fixtures');
77
78   $libCount = Koha::Libraries->search->count;
79
80   is($libCount, 1,
81   '  Then we got the count from fixtures');
82
83   $cachedSchema2 = Koha::Database::get_schema_cached();
84   is($cachedSchema2, $cachedSchema,
85   '  And the cached DB connection is the same from T:D:C');
86
87   like(getConnectionDBName($cachedSchema), qr/sqlite/i,
88   '  And the cached DB connection type is sqlite');
89
90   };
91   ok(0, $@) if $@;
92 };
93
94 done_testing;
95
96
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};
100 }