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); |
6ebf5cbb |
10 | use ViewDeps; |
51b31bbe |
11 | use ViewDepsBad; |
6ebf5cbb |
12 | |
13 | BEGIN { |
d2f4cc4a |
14 | require DBIx::Class; |
68de9438 |
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'); |
6ebf5cbb |
18 | } |
19 | |
68de9438 |
20 | use_ok('DBIx::Class::ResultSource::View'); |
21 | |
b7b008a2 |
22 | #################### SANITY |
d40f513f |
23 | |
51b31bbe |
24 | my $view = DBIx::Class::ResultSource::View->new; |
66fb15b6 |
25 | |
9efcc79f |
26 | isa_ok( $view, 'DBIx::Class::ResultSource', 'A new view' ); |
27 | isa_ok( $view, 'DBIx::Class', 'A new view also' ); |
6ebf5cbb |
28 | |
e55d9d89 |
29 | can_ok( $view, $_ ) for qw/new from deploy_depends_on/; |
6ebf5cbb |
30 | |
b7b008a2 |
31 | #################### DEPS |
32 | |
8a8525be |
33 | my $schema |
34 | = ViewDeps->connect( 'dbi:SQLite::memory:', { quote_char => '"', } ); |
1e70d9dc |
35 | ok( $schema, 'Connected to ViewDeps schema OK' ); |
9efcc79f |
36 | |
b7b008a2 |
37 | #################### DEPLOY |
bf5c3a3f |
38 | |
2446dc59 |
39 | $schema->deploy( { add_drop_table => 1 } ); |
40 | |
b7b008a2 |
41 | #################### DOES ORDERING WORK? |
42 | |
51b31bbe |
43 | my $sqlt_object = $schema->{sqlt}; |
44 | |
45 | my @keys = keys %{ $sqlt_object->{views} }; |
b7b008a2 |
46 | |
51b31bbe |
47 | my @sqlt_sources = sort { |
48 | $sqlt_object->{views}->{$a}->{order} |
49 | cmp $sqlt_object->{views}->{$b}->{order} |
50 | } @keys; |
b7b008a2 |
51 | |
51b31bbe |
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/; |
2446dc59 |
54 | |
51b31bbe |
55 | is_deeply( \@expected, \@sqlt_sources, |
8a8525be |
56 | "SQLT view order triumphantly matches our order." ); |
bf5c3a3f |
57 | |
339f7f5d |
58 | #################### AND WHAT ABOUT USING THE SCHEMA? |
59 | |
8a8525be |
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 ] }; |
339f7f5d |
65 | |
51b31bbe |
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 | |
d2f4cc4a |
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."; |
51b31bbe |
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 | }, |
c418c5cc |
108 | "Query on AbaNameArtistsAnd2010CDsWithManyTracks dies, because of incorrect deploy_depends_on in AbaNameArtists" |
51b31bbe |
109 | ); |
110 | |
c418c5cc |
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 | |
6ebf5cbb |
117 | done_testing; |