fix tests
[dbsrgits/DBIx-Class-Schema-Loader.git] / t / 25backcompat_v4.t
1 use strict;
2 use warnings;
3 use Test::More;
4 use File::Path qw/rmtree make_path/;
5 use Class::Unload;
6 use File::Temp qw/tempfile tempdir/;
7 use IO::File;
8 use lib qw(t/lib);
9 use make_dbictest_db2;
10
11 my $DUMP_DIR = './t/_common_dump';
12 rmtree $DUMP_DIR;
13 my $SCHEMA_CLASS = 'DBIXCSL_Test::Schema';
14
15 sub run_loader {
16     my %loader_opts = @_;
17
18     eval {
19         foreach my $source_name ($SCHEMA_CLASS->clone->sources) {
20             Class::Unload->unload("${SCHEMA_CLASS}::${source_name}");
21         }
22
23         Class::Unload->unload($SCHEMA_CLASS);
24     };
25     undef $@;
26
27     my @connect_info = $make_dbictest_db2::dsn;
28     my @loader_warnings;
29     local $SIG{__WARN__} = sub { push(@loader_warnings, $_[0]); };
30     eval qq{
31         package $SCHEMA_CLASS;
32         use base qw/DBIx::Class::Schema::Loader/;
33
34         __PACKAGE__->loader_options(\%loader_opts);
35         __PACKAGE__->connection(\@connect_info);
36     };
37
38     ok(!$@, "Loader initialization") or diag $@;
39
40     my $schema = $SCHEMA_CLASS->clone;
41     my (%monikers, %classes);
42     foreach my $source_name ($schema->sources) {
43         my $table_name = $schema->source($source_name)->from;
44         $monikers{$table_name} = $source_name;
45         $classes{$table_name}  = "${SCHEMA_CLASS}::${source_name}";
46     }
47
48     return {
49         schema => $schema,
50         warnings => \@loader_warnings,
51         monikers => \%monikers,
52         classes => \%classes,
53     };
54 }
55
56 sub run_v4_tests {
57     my $res = shift;
58     my $schema = $res->{schema};
59
60     is_deeply [ @{ $res->{monikers} }{qw/foos bar bazs quuxs/} ],
61         [qw/Foos Bar Bazs Quuxs/],
62         'correct monikers in 0.04006 mode';
63
64     isa_ok ((my $bar = eval { $schema->resultset('Bar')->find(1) }),
65         $res->{classes}{bar},
66         'found a bar');
67
68     isa_ok eval { $bar->foo_id }, $res->{classes}{foos},
69         'correct rel name in 0.04006 mode';
70
71     ok my $baz  = eval { $schema->resultset('Bazs')->find(1) };
72
73     isa_ok eval { $baz->quux }, 'DBIx::Class::ResultSet',
74         'correct rel type and name for UNIQUE FK in 0.04006 mode';
75 }
76
77 sub run_v5_tests {
78     my $res = shift;
79     my $schema = $res->{schema};
80
81     is_deeply [ @{ $res->{monikers} }{qw/foos bar bazs quuxs/} ],
82         [qw/Foo Bar Baz Quux/],
83         'correct monikers in current mode';
84
85     ok my $bar = eval { $schema->resultset('Bar')->find(1) };
86
87     isa_ok eval { $bar->foo }, $res->{classes}{foos},
88         'correct rel name in current mode';
89
90     ok my $baz  = eval { $schema->resultset('Baz')->find(1) };
91
92     isa_ok eval { $baz->quux }, $res->{classes}{quuxs},
93         'correct rel type and name for UNIQUE FK in current mode';
94 }
95
96 # test dynamic schema in 0.04006 mode
97 {
98     my $res = run_loader();
99     my $warning = $res->{warnings}[0];
100
101     like $warning, qr/dynamic schema/i,
102         'dynamic schema in backcompat mode detected';
103     like $warning, qr/run in 0\.04006 mode/i,
104         'dynamic schema in 0.04006 mode warning';
105     like $warning, qr/DBIx::Class::Schema::Loader::Manual::UpgradingFromV4/,
106         'warning refers to upgrading doc';
107     
108     run_v4_tests($res);
109 }
110
111 # setting naming accessor on dynamic schema should disable warning (even when
112 # we're setting it to 'v4' .)
113 {
114     my $res = run_loader(naming => 'v4');
115
116     is_deeply $res->{warnings}, [], 'no warnings with naming attribute set';
117
118     run_v4_tests($res);
119 }
120
121 # test upgraded dynamic schema
122 {
123     my $res = run_loader(naming => 'current');
124
125 # to dump a schema for debugging...
126 #    {
127 #        mkdir '/tmp/HLAGH';
128 #        $schema->_loader->{dump_directory} = '/tmp/HLAGH';
129 #        $schema->_loader->_dump_to_dir(values %{ $res->{classes} });
130 #    }
131
132     is_deeply $res->{warnings}, [], 'no warnings with naming attribute set';
133
134     run_v5_tests($res);
135 }
136
137 # test upgraded dynamic schema with external content loaded
138 {
139     my $temp_dir = tempdir;
140     push @INC, $temp_dir;
141
142     my $external_result_dir = join '/', $temp_dir, split /::/, $SCHEMA_CLASS;
143     make_path $external_result_dir;
144
145     IO::File->new(">$external_result_dir/Quuxs.pm")->print(<<"EOF");
146 package ${SCHEMA_CLASS}::Quuxs;
147 sub a_method { 'hlagh' }
148 1;
149 EOF
150
151     my $res = run_loader(naming => 'current');
152     my $schema = $res->{schema};
153
154     is scalar @{ $res->{warnings} }, 1,
155 'correct nummber of warnings for upgraded dynamic schema with external ' .
156 'content for unsingularized Result.';
157
158     my $warning = $res->{warnings}[0];
159     like $warning, qr/Detected external content/i,
160         'detected external content warning';
161
162     is eval { $schema->resultset('Quux')->find(1)->a_method }, 'hlagh',
163 'external custom content for unsingularized Result was loaded by upgraded ' .
164 'dynamic Schema';
165
166     run_v5_tests($res);
167
168     rmtree $temp_dir;
169     pop @INC;
170 }
171
172 # test running against v4 schema without upgrade
173 {
174     # write out the 0.04006 Schema.pm we have in __DATA__
175     (my $schema_dir = "$DUMP_DIR/$SCHEMA_CLASS") =~ s/::[^:]+\z//;
176     make_path $schema_dir;
177     my $schema_pm = "$schema_dir/Schema.pm";
178     open my $fh, '>', $schema_pm or die $!;
179     while (<DATA>) {
180         print $fh $_;
181     }
182     close $fh;
183
184     # now run the loader
185     my $res = run_loader(dump_directory => $DUMP_DIR);
186     my $warning = $res->{warnings}[0];
187
188     like $warning, qr/static schema/i,
189         'static schema in backcompat mode detected';
190     like $warning, qr/0.04006/,
191         'correct version detected';
192     like $warning, qr/DBIx::Class::Schema::Loader::Manual::UpgradingFromV4/,
193         'refers to upgrading doc';
194
195     is scalar @{ $res->{warnings} }, 3,
196         'correct number of warnings for static schema in backcompat mode';
197
198     run_v4_tests($res);
199
200     # add some custom content to a Result that will be replaced
201     my $schema   = $res->{schema};
202     my $quuxs_pm = $schema->_loader
203         ->_get_dump_filename($res->{classes}{quuxs});
204     {
205         local ($^I, @ARGV) = ('', $quuxs_pm);
206         while (<>) {
207             if (/DO NOT MODIFY THIS OR ANYTHING ABOVE/) {
208                 print;
209                 print "sub a_method { 'mtfnpy' }\n";
210             }
211             else {
212                 print;
213             }
214         }
215     }
216
217     # now upgrade the schema
218     $res = run_loader(dump_directory => $DUMP_DIR, naming => 'current');
219     $schema = $res->{schema};
220
221     like $res->{warnings}[0], qr/Dumping manual schema/i,
222         'correct warnings on upgrading static schema (with "naming" set)';
223
224     like $res->{warnings}[1], qr/dump completed/i,
225         'correct warnings on upgrading static schema (with "naming" set)';
226
227     is scalar @{ $res->{warnings} }, 2,
228 'correct number of warnings on upgrading static schema (with "naming" set)'
229         or diag @{ $res->{warnings} };
230
231     run_v5_tests($res);
232
233     (my $result_dir = "$DUMP_DIR/$SCHEMA_CLASS") =~ s{::}{/}g;
234     my $result_count =()= glob "$result_dir/*";
235
236     is $result_count, 4,
237         'un-singularized results were replaced during upgrade';
238
239     # check that custom content was preserved
240     is eval { $schema->resultset('Quux')->find(1)->a_method }, 'mtfnpy',
241         'custom content was carried over from un-singularized Result';
242 }
243
244 done_testing;
245
246 END {
247     rmtree $DUMP_DIR unless $ENV{SCHEMA_LOADER_TESTS_NOCLEANUP};
248 }
249
250 # a Schema.pm made with 0.04006
251
252 __DATA__
253 package DBIXCSL_Test::Schema;
254
255 use strict;
256 use warnings;
257
258 use base 'DBIx::Class::Schema';
259
260 __PACKAGE__->load_classes;
261
262
263 # Created by DBIx::Class::Schema::Loader v0.04006 @ 2009-12-25 01:49:25
264 # DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:ibIJTbfM1ji4pyD/lgSEog
265
266
267 # You can replace this text with custom content, and it will be preserved on regeneration
268 1;
269