deployment_statements() is not storage-dependent - only sqlt_type() is
[dbsrgits/DBIx-Class.git] / t / 105view_deps.t
CommitLineData
6ebf5cbb 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
6use Test::More;
7use Test::Exception;
7c4ade2a 8use Test::Warn;
6ebf5cbb 9use lib qw(t/lib);
6c97a902 10use DBICTest;
6ebf5cbb 11use ViewDeps;
51b31bbe 12use ViewDepsBad;
6ebf5cbb 13
14BEGIN {
d2f4cc4a 15 require DBIx::Class;
68de9438 16 plan skip_all => 'Test needs ' .
17 DBIx::Class::Optional::Dependencies->req_missing_for('deploy')
18 unless DBIx::Class::Optional::Dependencies->req_ok_for('deploy');
6ebf5cbb 19}
20
68de9438 21use_ok('DBIx::Class::ResultSource::View');
22
b7b008a2 23#################### SANITY
d40f513f 24
51b31bbe 25my $view = DBIx::Class::ResultSource::View->new;
66fb15b6 26
9efcc79f 27isa_ok( $view, 'DBIx::Class::ResultSource', 'A new view' );
28isa_ok( $view, 'DBIx::Class', 'A new view also' );
6ebf5cbb 29
e55d9d89 30can_ok( $view, $_ ) for qw/new from deploy_depends_on/;
6ebf5cbb 31
b7b008a2 32#################### DEPS
6c97a902 33{
34 my $schema
35 = ViewDeps->connect( DBICTest->_database (quote_char => '"') );
36 ok( $schema, 'Connected to ViewDeps schema OK' );
9efcc79f 37
b7b008a2 38#################### DEPLOY
bf5c3a3f 39
1f9712ad 40 $schema->deploy;
2446dc59 41
b7b008a2 42#################### DOES ORDERING WORK?
43
6c97a902 44 my $sqlt_object = $schema->{sqlt};
45
46 is_deeply(
47 [ map { $_->name } $sqlt_object->get_views ],
48 [qw/
49 a_name_artists
50 track_number_fives
51 year_2010_cds
52 ab_name_artists
53 year_2010_cds_with_many_tracks
54 aba_name_artists
55 aba_name_artists_and_2010_cds_with_many_tracks
56 /],
8273e845 57 "SQLT view order triumphantly matches our order."
6c97a902 58 );
bf5c3a3f 59
339f7f5d 60#################### AND WHAT ABOUT USING THE SCHEMA?
61
6c97a902 62 lives_ok( sub { $schema->resultset($_)->next }, "Query on $_ succeeds" )
8a8525be 63 for grep {
64 $schema->resultset($_)
6c97a902 65 ->result_source->isa('DBIx::Class::ResultSource::View')
8a8525be 66 } @{ [ $schema->sources ] };
6c97a902 67}
339f7f5d 68
51b31bbe 69#################### AND WHAT ABOUT A BAD DEPS CHAIN IN A VIEW?
70
6c97a902 71{
72 my $schema2
73 = ViewDepsBad->connect( DBICTest->_database ( quote_char => '"') );
74 ok( $schema2, 'Connected to ViewDepsBad schema OK' );
51b31bbe 75
76#################### DEPLOY2
77
1f9712ad 78 warnings_exist { $schema2->deploy }
6c97a902 79 [qr/no such table: main.aba_name_artists/],
d2f4cc4a 80 "Deploying the bad schema produces a warning: aba_name_artists was not created.";
51b31bbe 81
82#################### DOES ORDERING WORK 2?
83
6c97a902 84 my $sqlt_object2 = $schema2->{sqlt};
85
86 is_deeply(
87 [ map { $_->name } $sqlt_object2->get_views ],
88 [qw/
89 a_name_artists
90 track_number_fives
91 year_2010_cds
92 ab_name_artists
93 year_2010_cds_with_many_tracks
94 aba_name_artists_and_2010_cds_with_many_tracks
95 aba_name_artists
96 /],
8273e845 97 "SQLT view order triumphantly matches our order."
6c97a902 98 );
51b31bbe 99
100#################### AND WHAT ABOUT USING THE SCHEMA2?
101
6c97a902 102 lives_ok( sub { $schema2->resultset($_)->next }, "Query on $_ succeeds" )
51b31bbe 103 for grep {
104 $schema2->resultset($_)
6c97a902 105 ->result_source->isa('DBIx::Class::ResultSource::View')
51b31bbe 106 } grep { !/AbaNameArtistsAnd2010CDsWithManyTracks/ }
107 @{ [ $schema2->sources ] };
108
6c97a902 109 throws_ok { $schema2->resultset('AbaNameArtistsAnd2010CDsWithManyTracks')->next }
110 qr/no such table: aba_name_artists_and_2010_cds_with_many_tracks/,
111 "Query on AbaNameArtistsAnd2010CDsWithManyTracks throws, because the table does not exist"
112 ;
c418c5cc 113}
c418c5cc 114
6ebf5cbb 115done_testing;