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