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