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