Institute a central "load this first in testing" package
[dbsrgits/DBIx-Class.git] / xt / extra / sqlite_view_deps.t
CommitLineData
c0329273 1BEGIN { do "./t/lib/ANFANG.pm" or die ( $@ || $! ) }
cb551b07 2use DBIx::Class::Optional::Dependencies -skip_all_without => 'deploy';
6ebf5cbb 3
4use strict;
5use warnings;
6
7use Test::More;
8use Test::Exception;
7c4ade2a 9use Test::Warn;
c0329273 10
6c97a902 11use DBICTest;
6ebf5cbb 12use ViewDeps;
51b31bbe 13use ViewDepsBad;
6ebf5cbb 14
b7b008a2 15#################### SANITY
d40f513f 16
51b31bbe 17my $view = DBIx::Class::ResultSource::View->new;
66fb15b6 18
9efcc79f 19isa_ok( $view, 'DBIx::Class::ResultSource', 'A new view' );
20isa_ok( $view, 'DBIx::Class', 'A new view also' );
6ebf5cbb 21
e55d9d89 22can_ok( $view, $_ ) for qw/new from deploy_depends_on/;
6ebf5cbb 23
b7b008a2 24#################### DEPS
6c97a902 25{
26 my $schema
27 = ViewDeps->connect( DBICTest->_database (quote_char => '"') );
28 ok( $schema, 'Connected to ViewDeps schema OK' );
9efcc79f 29
b7b008a2 30#################### DEPLOY
bf5c3a3f 31
1f9712ad 32 $schema->deploy;
2446dc59 33
b7b008a2 34#################### DOES ORDERING WORK?
35
6c97a902 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 /],
8273e845 49 "SQLT view order triumphantly matches our order."
6c97a902 50 );
bf5c3a3f 51
339f7f5d 52#################### AND WHAT ABOUT USING THE SCHEMA?
53
6c97a902 54 lives_ok( sub { $schema->resultset($_)->next }, "Query on $_ succeeds" )
8a8525be 55 for grep {
56 $schema->resultset($_)
6c97a902 57 ->result_source->isa('DBIx::Class::ResultSource::View')
8a8525be 58 } @{ [ $schema->sources ] };
6c97a902 59}
339f7f5d 60
51b31bbe 61#################### AND WHAT ABOUT A BAD DEPS CHAIN IN A VIEW?
62
6c97a902 63{
64 my $schema2
65 = ViewDepsBad->connect( DBICTest->_database ( quote_char => '"') );
66 ok( $schema2, 'Connected to ViewDepsBad schema OK' );
51b31bbe 67
26c663f1 68 my $lazy_view_validity = !(
69 $schema2->storage->_server_info->{normalized_dbms_version}
70 <
71 3.009
72 );
73
51b31bbe 74#################### DEPLOY2
75
1f9712ad 76 warnings_exist { $schema2->deploy }
26c663f1 77 [ $lazy_view_validity ? () : qr/no such table: main.aba_name_artists/ ],
d2f4cc4a 78 "Deploying the bad schema produces a warning: aba_name_artists was not created.";
51b31bbe 79
80#################### DOES ORDERING WORK 2?
81
6c97a902 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 /],
8273e845 95 "SQLT view order triumphantly matches our order."
6c97a902 96 );
51b31bbe 97
98#################### AND WHAT ABOUT USING THE SCHEMA2?
99
6c97a902 100 lives_ok( sub { $schema2->resultset($_)->next }, "Query on $_ succeeds" )
51b31bbe 101 for grep {
102 $schema2->resultset($_)
6c97a902 103 ->result_source->isa('DBIx::Class::ResultSource::View')
51b31bbe 104 } grep { !/AbaNameArtistsAnd2010CDsWithManyTracks/ }
105 @{ [ $schema2->sources ] };
106
26c663f1 107 $schema2->storage->dbh->do(q( DROP VIEW "aba_name_artists" ))
108 if $lazy_view_validity;
109
6c97a902 110 throws_ok { $schema2->resultset('AbaNameArtistsAnd2010CDsWithManyTracks')->next }
26c663f1 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 )
6c97a902 116 ;
c418c5cc 117}
c418c5cc 118
6ebf5cbb 119done_testing;