Bug 18501: Don't use paidfor for DB fields not mapped with a MARC field
[koha.git] / t / db_dependent / 01-test_dbic.t
1 #!/usr/bin/perl
2
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>.
16
17 use Modern::Perl;
18
19 use Test::More;
20 use Test::MockModule;
21
22 use Koha::Database;
23 use Koha::Libraries;
24
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 );
27
28     eval {
29
30         ok(
31             $firstSchema = Koha::Database->schema,
32             'Step: Given a normal DB connection.'
33         );
34
35         $firstLibCount =
36           Koha::Libraries->search->count;    # first count normal conn
37
38         ok( $cachedSchema = Koha::Database::get_schema_cached(),
39             '  And the DB connection is cached' );
40
41         unlike( getConnectionDBName($cachedSchema),
42             qr/sqlite/i, '  And the cached DB connection type is not sqlite' );
43
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.'
49         );
50
51         $libCount = Koha::Libraries->search->count;
52
53         is( $libCount, $firstLibCount,
54             '  Then we got the same count as without T:D:C' );
55
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' );
59
60         is(
61             getConnectionDBName($cachedSchema),
62             getConnectionDBName($firstSchema),
63             '  And the cached DB connection type is unchanged'
64         );
65
66         ok( Koha::Database::flush_schema_cache(),
67             'Step: Given the DB connection cache is flushed' );
68
69         $libCount = Koha::Libraries->search->count;
70
71         is( $libCount, 0,
72             '  Then we got 0 libraries because fixtures are not deployed' );
73
74         $cachedSchema = Koha::Database::get_schema_cached();
75         isnt( $cachedSchema, $firstSchema,
76             '  And the cached DB connection has changed' );
77
78         like( getConnectionDBName($cachedSchema),
79             qr/sqlite/i, '  And the cached DB connection type is sqlite' );
80
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' ],
86                 ]
87             ],
88             'Step: Given we deploy T:D:C Fixtures'
89         );
90
91         $libCount = Koha::Libraries->search->count;
92
93         is( $libCount, 1, '  Then we got the count from fixtures' );
94
95         $cachedSchema2 = Koha::Database::get_schema_cached();
96         is( $cachedSchema2, $cachedSchema,
97             '  And the cached DB connection is the same from T:D:C' );
98
99         like( getConnectionDBName($cachedSchema),
100             qr/sqlite/i, '  And the cached DB connection type is sqlite' );
101
102     };
103     ok( 0, $@ ) if $@;
104   };
105
106 done_testing;
107
108 sub getConnectionDBName {
109     return shift->storage->connect_info->[0]->{dsn};
110 }