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