Change API of deploy_depends_on: pass it source names. Fix optional deps on 105view_d...
[dbsrgits/DBIx-Class.git] / t / 105view_deps.t
1 #!/usr/bin/perl
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 ViewDeps;
11 use ViewDepsBad;
12
13 BEGIN {
14   require DBIx::Class;
15   plan skip_all =>
16       'Test needs ' . DBIx::Class::Optional::Dependencies->req_missing_for ('deploy')
17     unless DBIx::Class::Optional::Dependencies->req_ok_for ('deploy');
18     use_ok('DBIx::Class::ResultSource::View');
19 }
20
21 #################### SANITY
22
23 my $view = DBIx::Class::ResultSource::View->new;
24
25 isa_ok( $view, 'DBIx::Class::ResultSource', 'A new view' );
26 isa_ok( $view, 'DBIx::Class', 'A new view also' );
27
28 can_ok( $view, $_ ) for qw/new from deploy_depends_on/;
29
30 #################### DEPS
31
32 my $schema
33     = ViewDeps->connect( 'dbi:SQLite::memory:', { quote_char => '"', } );
34 ok( $schema, 'Connected to ViewDeps schema OK' );
35
36 #################### DEPLOY
37
38 $schema->deploy( { add_drop_table => 1 } );
39
40 #################### DOES ORDERING WORK?
41
42 my $sqlt_object = $schema->{sqlt};
43
44 my @keys = keys %{ $sqlt_object->{views} };
45
46 my @sqlt_sources = sort {
47     $sqlt_object->{views}->{$a}->{order}
48         cmp $sqlt_object->{views}->{$b}->{order}
49 } @keys;
50
51 my @expected
52     = qw/a_name_artists track_number_fives year_2010_cds ab_name_artists year_2010_cds_with_many_tracks aba_name_artists aba_name_artists_and_2010_cds_with_many_tracks/;
53
54 is_deeply( \@expected, \@sqlt_sources,
55     "SQLT view order triumphantly matches our order." );
56
57 #################### AND WHAT ABOUT USING THE SCHEMA?
58
59 lives_ok( sub { $schema->resultset($_)->next }, "Query on $_ succeeds" )
60     for grep {
61     $schema->resultset($_)
62         ->result_source->isa('DBIx::Class::ResultSource::View')
63     } @{ [ $schema->sources ] };
64
65 #################### AND WHAT ABOUT A BAD DEPS CHAIN IN A VIEW?
66
67 my $schema2
68     = ViewDepsBad->connect( 'dbi:SQLite::memory:', { quote_char => '"', } );
69 ok( $schema2, 'Connected to ViewDepsBad schema OK' );
70
71 #################### DEPLOY2
72
73 warnings_exist { $schema2->deploy( { add_drop_table => 1 } ); } [qr/no such table: main.aba_name_artists/], "Deploying the bad schema produces a warning: aba_name_artists was not created.";
74
75 #################### DOES ORDERING WORK 2?
76
77 my $sqlt_object2 = $schema2->{sqlt};
78
79 my @keys2 = keys %{ $sqlt_object->{views} };
80
81 my @sqlt_sources2 = sort {
82     $sqlt_object->{views}->{$a}->{order}
83         cmp $sqlt_object->{views}->{$b}->{order}
84 } @keys2;
85
86 my @expected2
87     = qw/a_name_artists track_number_fives year_2010_cds ab_name_artists year_2010_cds_with_many_tracks aba_name_artists aba_name_artists_and_2010_cds_with_many_tracks/;
88
89 is_deeply( \@expected2, \@sqlt_sources2,
90     "SQLT view order triumphantly matches our order." );
91
92 #################### AND WHAT ABOUT USING THE SCHEMA2?
93
94 lives_ok( sub { $schema2->resultset($_)->next }, "Query on $_ succeeds" )
95     for grep {
96     $schema2->resultset($_)
97         ->result_source->isa('DBIx::Class::ResultSource::View')
98     } grep { !/AbaNameArtistsAnd2010CDsWithManyTracks/ }
99     @{ [ $schema2->sources ] };
100
101 dies_ok(
102     sub {
103         $schema2->resultset('AbaNameArtistsAnd2010CDsWithManyTracks')->next;
104     },
105     "Query on AbaNameArtistsAnd2010CDsWithManyTracks dies, because of incorrect deploy_depends_on in AbaNameArtists"
106 );
107
108 throws_ok {
109     $schema2->resultset('AbaNameArtistsAnd2010CDsWithManyTracks')->next;
110 }
111 qr/no such table: aba_name_artists_and_2010_cds_with_many_tracks/,
112     "Query on AbaNameArtistsAnd2010CDsWithManyTracks throws, because the table does not exist";
113
114 done_testing;