Add author test for use strict/warnings
[dbsrgits/DBIx-Class-Schema-Loader.git] / t / 20invocations.t
CommitLineData
fa994d3c 1use strict;
f8c2ca5e 2use warnings;
fa994d3c 3use Test::More;
0c344f4e 4use Test::Warn;
ef8e9c69 5use DBIx::Class::Schema::Loader::Optional::Dependencies;
ff4b0152 6use DBIx::Class::Schema::Loader::Utils qw/sigwarn_silencer/;
fa994d3c 7use lib qw(t/lib);
8use make_dbictest_db;
9
10# Takes a $schema as input, runs 4 basic tests
11sub test_schema {
0c344f4e 12 my ($testname, $schema) = @_;
fa994d3c 13
0c344f4e 14 warnings_are ( sub {
fa994d3c 15 $schema = $schema->clone if !ref $schema;
16 isa_ok($schema, 'DBIx::Class::Schema', $testname);
17
0c344f4e 18 my $rel_foo_rs = $schema->resultset('Bar')->search({ barid => 3})->search_related('fooref');
19 isa_ok($rel_foo_rs, 'DBIx::Class::ResultSet', $testname);
20
21 my $rel_foo = $rel_foo_rs->next;
22 isa_ok($rel_foo, "DBICTest::Schema::_${testname}::Foo", $testname);
fa994d3c 23
0c344f4e 24 is($rel_foo->footext, 'Foo record associated with the Bar with barid 3', "$testname correct object");
fa994d3c 25
0c344f4e 26 my $foo_rs = $schema->resultset('Foo');
27 my $foo_new = $foo_rs->create({footext => "${testname}_foo"});
28 is ($foo_rs->search({footext => "${testname}_foo"})->count, 1, "$testname object created") || die;
29 }, [], "No warnings during $testname invocations");
fa994d3c 30}
31
32my @invocations = (
fa994d3c 33 'hardcode' => sub {
0c344f4e 34 package DBICTest::Schema::_hardcode;
fa994d3c 35 use base qw/ DBIx::Class::Schema::Loader /;
8b7749d6 36 __PACKAGE__->naming('current');
42ea7b88 37 __PACKAGE__->use_namespaces(0);
fa994d3c 38 __PACKAGE__->connection($make_dbictest_db::dsn);
39 __PACKAGE__;
40 },
41 'normal' => sub {
0c344f4e 42 package DBICTest::Schema::_normal;
fa994d3c 43 use base qw/ DBIx::Class::Schema::Loader /;
59cfa251 44 __PACKAGE__->loader_options();
8b7749d6 45 __PACKAGE__->naming('current');
42ea7b88 46 __PACKAGE__->use_namespaces(0);
fa994d3c 47 __PACKAGE__->connect($make_dbictest_db::dsn);
48 },
49 'make_schema_at' => sub {
50 use DBIx::Class::Schema::Loader qw/ make_schema_at /;
51 make_schema_at(
0c344f4e 52 'DBICTest::Schema::_make_schema_at',
42ea7b88 53 {
54 really_erase_my_files => 1,
55 naming => 'current',
56 use_namespaces => 0
57 },
fa994d3c 58 [ $make_dbictest_db::dsn ],
59 );
0c344f4e 60 DBICTest::Schema::_make_schema_at->clone;
fa994d3c 61 },
62 'embedded_options' => sub {
0c344f4e 63 package DBICTest::Schema::_embedded_options;
fa994d3c 64 use base qw/ DBIx::Class::Schema::Loader /;
8b7749d6 65 __PACKAGE__->naming('current');
42ea7b88 66 __PACKAGE__->use_namespaces(0);
fa994d3c 67 __PACKAGE__->connect(
68 $make_dbictest_db::dsn,
28b4691d 69 { loader_options => { really_erase_my_files => 1 } }
fa994d3c 70 );
71 },
72 'embedded_options_in_attrs' => sub {
0c344f4e 73 package DBICTest::Schema::_embedded_options_in_attrs;
fa994d3c 74 use base qw/ DBIx::Class::Schema::Loader /;
8b7749d6 75 __PACKAGE__->naming('current');
42ea7b88 76 __PACKAGE__->use_namespaces(0);
fa994d3c 77 __PACKAGE__->connect(
78 $make_dbictest_db::dsn,
79 undef,
80 undef,
28b4691d 81 { AutoCommit => 1, loader_options => { really_erase_my_files => 1 } }
fa994d3c 82 );
83 },
84 'embedded_options_make_schema_at' => sub {
85 use DBIx::Class::Schema::Loader qw/ make_schema_at /;
86 make_schema_at(
0c344f4e 87 'DBICTest::Schema::_embedded_options_make_schema_at',
fa994d3c 88 { },
89 [
90 $make_dbictest_db::dsn,
8b7749d6 91 { loader_options => {
92 really_erase_my_files => 1,
42ea7b88 93 naming => 'current',
94 use_namespaces => 0,
8b7749d6 95 } },
fa994d3c 96 ],
97 );
0c344f4e 98 "DBICTest::Schema::_embedded_options_make_schema_at";
fa994d3c 99 },
100 'almost_embedded' => sub {
0c344f4e 101 package DBICTest::Schema::_almost_embedded;
fa994d3c 102 use base qw/ DBIx::Class::Schema::Loader /;
8b7749d6 103 __PACKAGE__->loader_options(
104 really_erase_my_files => 1,
42ea7b88 105 naming => 'current',
106 use_namespaces => 0,
8b7749d6 107 );
fa994d3c 108 __PACKAGE__->connect(
109 $make_dbictest_db::dsn,
110 undef, undef, { AutoCommit => 1 }
111 );
112 },
113 'make_schema_at_explicit' => sub {
114 use DBIx::Class::Schema::Loader;
115 DBIx::Class::Schema::Loader::make_schema_at(
0c344f4e 116 'DBICTest::Schema::_make_schema_at_explicit',
42ea7b88 117 {
118 really_erase_my_files => 1,
119 naming => 'current',
120 use_namespaces => 0,
121 },
fa994d3c 122 [ $make_dbictest_db::dsn ],
123 );
0c344f4e 124 DBICTest::Schema::_make_schema_at_explicit->clone;
0ca61324 125 },
73099af4 126 'no_skip_load_external' => sub {
c213fd3d 127 # By default we should pull in t/lib/DBICTest/Schema/_no_skip_load_external/Foo.pm $skip_me since t/lib is in @INC
0ca61324 128 use DBIx::Class::Schema::Loader;
129 DBIx::Class::Schema::Loader::make_schema_at(
0c344f4e 130 'DBICTest::Schema::_no_skip_load_external',
42ea7b88 131 {
132 really_erase_my_files => 1,
133 naming => 'current',
134 use_namespaces => 0,
135 },
0ca61324 136 [ $make_dbictest_db::dsn ],
137 );
0c344f4e 138 DBICTest::Schema::_no_skip_load_external->clone;
0ca61324 139 },
73099af4 140 'skip_load_external' => sub {
c213fd3d 141 # When we explicitly skip_load_external t/lib/DBICTest/Schema/_skip_load_external/Foo.pm should be ignored
0ca61324 142 use DBIx::Class::Schema::Loader;
143 DBIx::Class::Schema::Loader::make_schema_at(
0c344f4e 144 'DBICTest::Schema::_skip_load_external',
42ea7b88 145 {
146 really_erase_my_files => 1,
147 naming => 'current',
148 use_namespaces => 0,
149 skip_load_external => 1,
150 },
0ca61324 151 [ $make_dbictest_db::dsn ],
152 );
0c344f4e 153 DBICTest::Schema::_skip_load_external->clone;
0ca61324 154 },
ef8e9c69 155 (DBIx::Class::Schema::Loader::Optional::Dependencies->req_ok_for('use_moose') ?
0c344f4e 156 ('use_moose' => sub {
157 package DBICTest::Schema::_use_moose;
ef8e9c69 158 use base qw/ DBIx::Class::Schema::Loader /;
159 __PACKAGE__->naming('current');
42ea7b88 160 __PACKAGE__->use_namespaces(0);
ef8e9c69 161 __PACKAGE__->connect(
162 $make_dbictest_db::dsn,
163 { loader_options => { use_moose => 1 } }
164 );
165 })
166 : ()
167 ),
fa994d3c 168);
169
0c344f4e 170# 6 tests per k/v pair
171plan tests => 6 * (@invocations/2) + 2; # + 2 more manual ones below.
fa994d3c 172
0c344f4e 173while(@invocations) {
fa994d3c 174 my $style = shift @invocations;
0c344f4e 175 my $cref = shift @invocations;
176
177 my $schema = do {
ff4b0152 178 local $SIG{__WARN__} = sigwarn_silencer(
179 qr/Deleting existing file .+ due to 'really_erase_my_files' setting/
180 );
0c344f4e 181 $cref->();
182 };
183
184 test_schema($style, $schema);
fa994d3c 185}
0ca61324 186
b7219351 187{
188 no warnings 'once';
0ca61324 189
0c344f4e 190 is($DBICTest::Schema::_no_skip_load_external::Foo::skip_me, "bad mojo",
b7219351 191 "external content loaded");
0c344f4e 192 is($DBICTest::Schema::_skip_load_external::Foo::skip_me, undef,
b7219351 193 "external content not loaded with skip_load_external => 1");
194}