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