Commit | Line | Data |
66afce69 |
1 | use strict; |
2 | use warnings; |
3 | use Test::More; |
a0e0a56a |
4 | use File::Path qw/rmtree make_path/; |
66afce69 |
5 | use Class::Unload; |
ffc705f3 |
6 | use File::Temp qw/tempfile tempdir/; |
7 | use IO::File; |
66afce69 |
8 | use lib qw(t/lib); |
9 | use make_dbictest_db2; |
10 | |
11 | my $DUMP_DIR = './t/_common_dump'; |
12 | rmtree $DUMP_DIR; |
a0e0a56a |
13 | my $SCHEMA_CLASS = 'DBIXCSL_Test::Schema'; |
66afce69 |
14 | |
15 | sub run_loader { |
16 | my %loader_opts = @_; |
17 | |
f53dcdf0 |
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 $@; |
66afce69 |
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{ |
a0e0a56a |
31 | package $SCHEMA_CLASS; |
66afce69 |
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 | |
a0e0a56a |
40 | my $schema = $SCHEMA_CLASS->clone; |
66afce69 |
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; |
a0e0a56a |
45 | $classes{$table_name} = "${SCHEMA_CLASS}::${source_name}"; |
66afce69 |
46 | } |
47 | |
48 | return { |
49 | schema => $schema, |
50 | warnings => \@loader_warnings, |
51 | monikers => \%monikers, |
52 | classes => \%classes, |
53 | }; |
54 | } |
55 | |
a0e0a56a |
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 | |
ffc705f3 |
64 | isa_ok ((my $bar = eval { $schema->resultset('Bar')->find(1) }), |
65 | $res->{classes}{bar}, |
66 | 'found a bar'); |
a0e0a56a |
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 | |
66afce69 |
96 | # test dynamic schema in 0.04006 mode |
97 | { |
98 | my $res = run_loader(); |
a0e0a56a |
99 | my $warning = $res->{warnings}[0]; |
66afce69 |
100 | |
a0e0a56a |
101 | like $warning, qr/dynamic schema/i, |
66afce69 |
102 | 'dynamic schema in backcompat mode detected'; |
a0e0a56a |
103 | like $warning, qr/run in 0\.04006 mode/i, |
66afce69 |
104 | 'dynamic schema in 0.04006 mode warning'; |
a0e0a56a |
105 | like $warning, qr/DBIx::Class::Schema::Loader::Manual::UpgradingFromV4/, |
106 | 'warning refers to upgrading doc'; |
107 | |
108 | run_v4_tests($res); |
109 | } |
66afce69 |
110 | |
a0e0a56a |
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'); |
66afce69 |
115 | |
a0e0a56a |
116 | is_deeply $res->{warnings}, [], 'no warnings with naming attribute set'; |
f53dcdf0 |
117 | |
118 | run_v4_tests($res); |
a0e0a56a |
119 | } |
120 | |
121 | # test upgraded dynamic schema |
122 | { |
123 | my $res = run_loader(naming => 'current'); |
66afce69 |
124 | |
a0e0a56a |
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 | # } |
66afce69 |
131 | |
a0e0a56a |
132 | is_deeply $res->{warnings}, [], 'no warnings with naming attribute set'; |
66afce69 |
133 | |
a0e0a56a |
134 | run_v5_tests($res); |
135 | } |
136 | |
ffc705f3 |
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 | |
a0e0a56a |
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 | |
ffc705f3 |
195 | is scalar @{ $res->{warnings} }, 3, |
196 | 'correct number of warnings for static schema in backcompat mode'; |
197 | |
a0e0a56a |
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, |
f53dcdf0 |
228 | 'correct number of warnings on upgrading static schema (with "naming" set)' |
229 | or diag @{ $res->{warnings} }; |
a0e0a56a |
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'; |
66afce69 |
242 | } |
243 | |
244 | done_testing; |
245 | |
ffc705f3 |
246 | END { |
247 | rmtree $DUMP_DIR unless $ENV{SCHEMA_LOADER_TESTS_NOCLEANUP}; |
248 | } |
a0e0a56a |
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 | |