9f63d20218155ee4da68e6e666c7912f977c5f01
[koha-ffzg.git] / t / db_dependent / 01-test_dbic.t
1 #!/usr/bin/perl
2
3 # This file is part of Koha.
4 my $verbose = 0;
5
6 use Modern::Perl;
7
8 use Test::More;
9 use Test::MockModule;
10
11 use Koha::Libraries;
12 use C4::Context;
13 Koha::Libraries->search->count;
14
15
16
17 subtest "Scenario: Show how caching prevents Test::DBIx::Class from working properly and how to circumvent it", sub {
18   my ($firstSchema, $schema, $cachedSchema, $cachedSchema2, $firstLibCount, $libCount);
19
20   eval {
21
22   ok($firstSchema = Koha::Database->schema,
23   'Step: Given a normal DB connection.');
24
25   ok($firstLibCount = Koha::Libraries->search->count,
26   '  When the libraries are counted');
27
28   ok($firstLibCount,
29   '  Then we got a count'); #There should be something like 12 branches in the default DB but making an accurate check here to prevent surface for brittleness.
30   print "\$firstLibCount '$firstLibCount'\n" if $verbose;
31
32   ok($cachedSchema = Koha::Database::get_schema_cached(),
33   '  And the DB connection is cached');
34
35   unlike(getConnectionDBName($cachedSchema), qr/sqlite/i,
36   '  And the cached DB connection type is not sqlite');
37   print "getConnectionDBName() -> ".getConnectionDBName($cachedSchema)."\n" if $verbose;
38
39
40   use_ok('Test::DBIx::Class');
41   my $db = Test::MockModule->new('Koha::Database');
42   $db->mock( _new_schema => sub { return Schema(); } );
43   ok(1,
44   'Step: Given Test::DBIx::Class (T:D:C) is loaded and DB accessor is mocked. Connection from cache is still used.');
45
46   ok($libCount = Koha::Libraries->search->count,
47   '  When the libraries are counted');
48
49   is($libCount, $firstLibCount,
50   '  Then we got the same count as without T:D:C');
51
52   $cachedSchema = Koha::Database::get_schema_cached();
53   is($cachedSchema, $firstSchema,
54   '  And the cached DB connection is the same as without T:D:C');
55
56   is(getConnectionDBName($cachedSchema), getConnectionDBName($firstSchema),
57   '  And the cached DB connection type is unchanged');
58
59
60   ok(Koha::Database::flush_schema_cache(),
61   'Step: Given the DB connection cache is flushed');
62
63   ok(! ($libCount = Koha::Libraries->search->count),
64   '  When the libraries are counted');
65
66   is($libCount, 0,
67   '  Then we got 0 libraries because fixtures are not deployed');
68
69   $cachedSchema = Koha::Database::get_schema_cached();
70   isnt($cachedSchema, $firstSchema,
71   '  And the cached DB connection has changed');
72
73   like(getConnectionDBName($cachedSchema), qr/sqlite/i,
74   '  And the cached DB connection type is sqlite');
75
76
77   eval "fixtures_ok [ #Dynamically load fixtures, because we dynamically load T:D:C. Otherwise there be compile errors!
78       Branch => [
79           ['branchcode', 'branchname'],
80           ['XXX_test', 'my branchname XXX'],
81       ]
82   ],
83   'Step: Given we deploy T:D:C Fixtures';";
84
85   ok($libCount = Koha::Libraries->search->count,
86   '  When the libraries are counted');
87
88   is($libCount, 1,
89   '  Then we got the count from fixtures');
90
91   $cachedSchema2 = Koha::Database::get_schema_cached();
92   is($cachedSchema2, $cachedSchema,
93   '  And the cached DB connection is the same from T:D:C');
94
95   like(getConnectionDBName($cachedSchema), qr/sqlite/i,
96   '  And the cached DB connection type is sqlite');
97
98   };
99   ok(0, $@) if $@;
100 };
101
102 done_testing;
103
104
105 sub getConnectionDBName {
106   #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.
107   return shift->storage->connect_info->[0]->{dsn};
108 }