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