51332f8454b045fa5cae0f84f8143d326420492b
[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__->connection($make_dbictest_db::dsn);
36         __PACKAGE__;
37     },
38     'normal' => sub {
39         package DBICTest::Schema::_normal;
40         use base qw/ DBIx::Class::Schema::Loader /;
41         __PACKAGE__->loader_options();
42         __PACKAGE__->naming('current');
43         __PACKAGE__->connect($make_dbictest_db::dsn);
44     },
45     'make_schema_at' => sub {
46         use DBIx::Class::Schema::Loader qw/ make_schema_at /;
47         make_schema_at(
48             'DBICTest::Schema::_make_schema_at',
49             { really_erase_my_files => 1, naming => 'current' },
50             [ $make_dbictest_db::dsn ],
51         );
52         DBICTest::Schema::_make_schema_at->clone;
53     },
54     'embedded_options' => sub {
55         package DBICTest::Schema::_embedded_options;
56         use base qw/ DBIx::Class::Schema::Loader /;
57         __PACKAGE__->naming('current');
58         __PACKAGE__->connect(
59             $make_dbictest_db::dsn,
60             { loader_options => { really_erase_my_files => 1 } }
61         );
62     },
63     'embedded_options_in_attrs' => sub {
64         package DBICTest::Schema::_embedded_options_in_attrs;
65         use base qw/ DBIx::Class::Schema::Loader /;
66         __PACKAGE__->naming('current');
67         __PACKAGE__->connect(
68             $make_dbictest_db::dsn,
69             undef,
70             undef,
71             { AutoCommit => 1, loader_options => { really_erase_my_files => 1 } }
72         );
73     },
74     'embedded_options_make_schema_at' => sub {
75         use DBIx::Class::Schema::Loader qw/ make_schema_at /;
76         make_schema_at(
77             'DBICTest::Schema::_embedded_options_make_schema_at',
78             { },
79             [
80                 $make_dbictest_db::dsn,
81                 { loader_options => {
82                     really_erase_my_files => 1,
83                     naming => 'current'
84                 } },
85             ],
86         );
87         "DBICTest::Schema::_embedded_options_make_schema_at";
88     },
89     'almost_embedded' => sub {
90         package DBICTest::Schema::_almost_embedded;
91         use base qw/ DBIx::Class::Schema::Loader /;
92         __PACKAGE__->loader_options(
93             really_erase_my_files => 1,
94             naming => 'current'
95         );
96         __PACKAGE__->connect(
97             $make_dbictest_db::dsn,
98             undef, undef, { AutoCommit => 1 }
99         );
100     },
101     'make_schema_at_explicit' => sub {
102         use DBIx::Class::Schema::Loader;
103         DBIx::Class::Schema::Loader::make_schema_at(
104             'DBICTest::Schema::_make_schema_at_explicit',
105             { really_erase_my_files => 1, naming => 'current' },
106             [ $make_dbictest_db::dsn ],
107         );
108         DBICTest::Schema::_make_schema_at_explicit->clone;
109     },
110     'no_skip_load_external' => sub {
111         # By default we should pull in t/lib/DBICTest/Schema/_no_skip_load_external/Foo.pm $skip_me since t/lib is in @INC
112         use DBIx::Class::Schema::Loader;
113         DBIx::Class::Schema::Loader::make_schema_at(
114             'DBICTest::Schema::_no_skip_load_external',
115             { really_erase_my_files => 1, naming => 'current' },
116             [ $make_dbictest_db::dsn ],
117         );
118         DBICTest::Schema::_no_skip_load_external->clone;
119     },
120     'skip_load_external' => sub {
121         # When we explicitly skip_load_external t/lib/DBICTest/Schema/_skip_load_external/Foo.pm should be ignored
122         use DBIx::Class::Schema::Loader;
123         DBIx::Class::Schema::Loader::make_schema_at(
124             'DBICTest::Schema::_skip_load_external',
125             { really_erase_my_files => 1, naming => 'current', skip_load_external => 1 },
126             [ $make_dbictest_db::dsn ],
127         );
128         DBICTest::Schema::_skip_load_external->clone;
129     },
130     (DBIx::Class::Schema::Loader::Optional::Dependencies->req_ok_for('use_moose') ?
131         ('use_moose' => sub {
132             package DBICTest::Schema::_use_moose;
133             use base qw/ DBIx::Class::Schema::Loader /;
134             __PACKAGE__->naming('current');
135             __PACKAGE__->connect(
136                 $make_dbictest_db::dsn,
137                 { loader_options => { use_moose =>  1 } }
138             );
139         })
140         : ()
141     ),
142 );
143
144 # 6 tests per k/v pair
145 plan tests => 6 * (@invocations/2) + 2;  # + 2 more manual ones below.
146
147 while(@invocations) {
148     my $style = shift @invocations;
149     my $cref = shift @invocations;
150
151     my $schema = do {
152       local $SIG{__WARN__} = sub {
153         warn $_[0] unless $_[0] =~ /Deleting existing file .+ due to 'really_erase_my_files' setting/
154       };
155       $cref->();
156     };
157
158     test_schema($style, $schema);
159 }
160
161 {
162     no warnings 'once';
163
164     is($DBICTest::Schema::_no_skip_load_external::Foo::skip_me, "bad mojo",
165         "external content loaded");
166     is($DBICTest::Schema::_skip_load_external::Foo::skip_me, undef,
167         "external content not loaded with skip_load_external => 1");
168 }