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