a2e54856e109ca671ae7cfc153b390197745de53
[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     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 } ); }
74 [qr/no such table: main.aba_name_artists/],
75     "Deploying the bad schema produces a warning: aba_name_artists was not created.";
76
77 #################### DOES ORDERING WORK 2?
78
79 my $sqlt_object2 = $schema2->{sqlt};
80
81 my @keys2 = keys %{ $sqlt_object->{views} };
82
83 my @sqlt_sources2 = sort {
84     $sqlt_object->{views}->{$a}->{order}
85         cmp $sqlt_object->{views}->{$b}->{order}
86 } @keys2;
87
88 my @expected2
89     = 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/;
90
91 is_deeply( \@expected2, \@sqlt_sources2,
92     "SQLT view order triumphantly matches our order." );
93
94 #################### AND WHAT ABOUT USING THE SCHEMA2?
95
96 lives_ok( sub { $schema2->resultset($_)->next }, "Query on $_ succeeds" )
97     for grep {
98     $schema2->resultset($_)
99         ->result_source->isa('DBIx::Class::ResultSource::View')
100     } grep { !/AbaNameArtistsAnd2010CDsWithManyTracks/ }
101     @{ [ $schema2->sources ] };
102
103 dies_ok(
104     sub {
105         $schema2->resultset('AbaNameArtistsAnd2010CDsWithManyTracks')->next;
106     },
107     "Query on AbaNameArtistsAnd2010CDsWithManyTracks dies, because of incorrect deploy_depends_on in AbaNameArtists"
108 );
109
110 throws_ok {
111     $schema2->resultset('AbaNameArtistsAnd2010CDsWithManyTracks')->next;
112 }
113 qr/no such table: aba_name_artists_and_2010_cds_with_many_tracks/,
114     "Query on AbaNameArtistsAnd2010CDsWithManyTracks throws, because the table does not exist";
115
116 done_testing;