Commit | Line | Data |
6ebf5cbb |
1 | #!/usr/bin/perl |
2 | |
3 | use strict; |
4 | use warnings; |
5 | |
6 | use Test::More; |
7 | use Test::Exception; |
7c4ade2a |
8 | use Test::Warn; |
6ebf5cbb |
9 | use lib qw(t/lib); |
6c97a902 |
10 | use DBICTest; |
6ebf5cbb |
11 | use ViewDeps; |
51b31bbe |
12 | use ViewDepsBad; |
6ebf5cbb |
13 | |
14 | BEGIN { |
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 |
21 | use_ok('DBIx::Class::ResultSource::View'); |
22 | |
b7b008a2 |
23 | #################### SANITY |
d40f513f |
24 | |
51b31bbe |
25 | my $view = DBIx::Class::ResultSource::View->new; |
66fb15b6 |
26 | |
9efcc79f |
27 | isa_ok( $view, 'DBIx::Class::ResultSource', 'A new view' ); |
28 | isa_ok( $view, 'DBIx::Class', 'A new view also' ); |
6ebf5cbb |
29 | |
e55d9d89 |
30 | can_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 | |
75d25fea |
76 | my $lazy_view_validity = !( |
77 | $schema2->storage->_server_info->{normalized_dbms_version} |
78 | < |
79 | 3.009 |
80 | ); |
81 | |
51b31bbe |
82 | #################### DEPLOY2 |
83 | |
1f9712ad |
84 | warnings_exist { $schema2->deploy } |
75d25fea |
85 | [ $lazy_view_validity ? () : qr/no such table: main.aba_name_artists/ ], |
d2f4cc4a |
86 | "Deploying the bad schema produces a warning: aba_name_artists was not created."; |
51b31bbe |
87 | |
88 | #################### DOES ORDERING WORK 2? |
89 | |
6c97a902 |
90 | my $sqlt_object2 = $schema2->{sqlt}; |
91 | |
92 | is_deeply( |
93 | [ map { $_->name } $sqlt_object2->get_views ], |
94 | [qw/ |
95 | a_name_artists |
96 | track_number_fives |
97 | year_2010_cds |
98 | ab_name_artists |
99 | year_2010_cds_with_many_tracks |
100 | aba_name_artists_and_2010_cds_with_many_tracks |
101 | aba_name_artists |
102 | /], |
8273e845 |
103 | "SQLT view order triumphantly matches our order." |
6c97a902 |
104 | ); |
51b31bbe |
105 | |
106 | #################### AND WHAT ABOUT USING THE SCHEMA2? |
107 | |
6c97a902 |
108 | lives_ok( sub { $schema2->resultset($_)->next }, "Query on $_ succeeds" ) |
51b31bbe |
109 | for grep { |
110 | $schema2->resultset($_) |
6c97a902 |
111 | ->result_source->isa('DBIx::Class::ResultSource::View') |
51b31bbe |
112 | } grep { !/AbaNameArtistsAnd2010CDsWithManyTracks/ } |
113 | @{ [ $schema2->sources ] }; |
114 | |
75d25fea |
115 | $schema2->storage->dbh->do(q( DROP VIEW "aba_name_artists" )) |
116 | if $lazy_view_validity; |
117 | |
6c97a902 |
118 | throws_ok { $schema2->resultset('AbaNameArtistsAnd2010CDsWithManyTracks')->next } |
75d25fea |
119 | qr/no such table: (?:main\.)?aba_name_artists/, |
120 | sprintf( |
121 | "Query on AbaNameArtistsAnd2010CDsWithManyTracks throws, because the%s view does not exist", |
122 | $lazy_view_validity ? ' underlying' : '' |
123 | ) |
6c97a902 |
124 | ; |
c418c5cc |
125 | } |
c418c5cc |
126 | |
6ebf5cbb |
127 | done_testing; |