Institute a central "load this first in testing" package
[dbsrgits/DBIx-Class.git] / xt / extra / sqlite_view_deps.t
1 BEGIN { do "./t/lib/ANFANG.pm" or die ( $@ || $! ) }
2 use DBIx::Class::Optional::Dependencies -skip_all_without => 'deploy';
3
4 use strict;
5 use warnings;
6
7 use Test::More;
8 use Test::Exception;
9 use Test::Warn;
10
11 use DBICTest;
12 use ViewDeps;
13 use ViewDepsBad;
14
15 #################### SANITY
16
17 my $view = DBIx::Class::ResultSource::View->new;
18
19 isa_ok( $view, 'DBIx::Class::ResultSource', 'A new view' );
20 isa_ok( $view, 'DBIx::Class', 'A new view also' );
21
22 can_ok( $view, $_ ) for qw/new from deploy_depends_on/;
23
24 #################### DEPS
25 {
26   my $schema
27     = ViewDeps->connect( DBICTest->_database (quote_char => '"') );
28   ok( $schema, 'Connected to ViewDeps schema OK' );
29
30 #################### DEPLOY
31
32   $schema->deploy;
33
34 #################### DOES ORDERING WORK?
35
36   my $sqlt_object = $schema->{sqlt};
37
38   is_deeply(
39     [ map { $_->name } $sqlt_object->get_views ],
40     [qw/
41       a_name_artists
42       track_number_fives
43       year_2010_cds
44       ab_name_artists
45       year_2010_cds_with_many_tracks
46       aba_name_artists
47       aba_name_artists_and_2010_cds_with_many_tracks
48     /],
49     "SQLT view order triumphantly matches our order."
50   );
51
52 #################### AND WHAT ABOUT USING THE SCHEMA?
53
54   lives_ok( sub { $schema->resultset($_)->next }, "Query on $_ succeeds" )
55     for grep {
56     $schema->resultset($_)
57       ->result_source->isa('DBIx::Class::ResultSource::View')
58     } @{ [ $schema->sources ] };
59 }
60
61 #################### AND WHAT ABOUT A BAD DEPS CHAIN IN A VIEW?
62
63 {
64   my $schema2
65     = ViewDepsBad->connect( DBICTest->_database ( quote_char => '"') );
66   ok( $schema2, 'Connected to ViewDepsBad schema OK' );
67
68   my $lazy_view_validity = !(
69     $schema2->storage->_server_info->{normalized_dbms_version}
70       <
71     3.009
72   );
73
74 #################### DEPLOY2
75
76   warnings_exist { $schema2->deploy }
77     [ $lazy_view_validity ? () : qr/no such table: main.aba_name_artists/ ],
78     "Deploying the bad schema produces a warning: aba_name_artists was not created.";
79
80 #################### DOES ORDERING WORK 2?
81
82   my $sqlt_object2 = $schema2->{sqlt};
83
84   is_deeply(
85     [ map { $_->name } $sqlt_object2->get_views ],
86     [qw/
87       a_name_artists
88       track_number_fives
89       year_2010_cds
90       ab_name_artists
91       year_2010_cds_with_many_tracks
92       aba_name_artists_and_2010_cds_with_many_tracks
93       aba_name_artists
94     /],
95     "SQLT view order triumphantly matches our order."
96   );
97
98 #################### AND WHAT ABOUT USING THE SCHEMA2?
99
100   lives_ok( sub { $schema2->resultset($_)->next }, "Query on $_ succeeds" )
101     for grep {
102     $schema2->resultset($_)
103       ->result_source->isa('DBIx::Class::ResultSource::View')
104     } grep { !/AbaNameArtistsAnd2010CDsWithManyTracks/ }
105     @{ [ $schema2->sources ] };
106
107   $schema2->storage->dbh->do(q( DROP VIEW "aba_name_artists" ))
108     if $lazy_view_validity;
109
110   throws_ok { $schema2->resultset('AbaNameArtistsAnd2010CDsWithManyTracks')->next }
111     qr/no such table: (?:main\.)?aba_name_artists/,
112     sprintf(
113       "Query on AbaNameArtistsAnd2010CDsWithManyTracks throws, because the%s view does not exist",
114       $lazy_view_validity ? ' underlying' : ''
115     )
116   ;
117 }
118
119 done_testing;