Restructure handling of the test scratch-dir, move all activity
[dbsrgits/DBIx-Class-Schema-Loader.git] / t / 20invocations.t
CommitLineData
fa994d3c 1use strict;
2use Test::More;
0c344f4e 3use Test::Warn;
ef8e9c69 4use DBIx::Class::Schema::Loader::Optional::Dependencies;
fa994d3c 5use lib qw(t/lib);
6use make_dbictest_db;
7
8# Takes a $schema as input, runs 4 basic tests
9sub test_schema {
0c344f4e 10 my ($testname, $schema) = @_;
fa994d3c 11
0c344f4e 12 warnings_are ( sub {
fa994d3c 13 $schema = $schema->clone if !ref $schema;
14 isa_ok($schema, 'DBIx::Class::Schema', $testname);
15
0c344f4e 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);
fa994d3c 21
0c344f4e 22 is($rel_foo->footext, 'Foo record associated with the Bar with barid 3', "$testname correct object");
fa994d3c 23
0c344f4e 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");
fa994d3c 28}
29
30my @invocations = (
fa994d3c 31 'hardcode' => sub {
0c344f4e 32 package DBICTest::Schema::_hardcode;
fa994d3c 33 use base qw/ DBIx::Class::Schema::Loader /;
8b7749d6 34 __PACKAGE__->naming('current');
fa994d3c 35 __PACKAGE__->connection($make_dbictest_db::dsn);
36 __PACKAGE__;
37 },
38 'normal' => sub {
0c344f4e 39 package DBICTest::Schema::_normal;
fa994d3c 40 use base qw/ DBIx::Class::Schema::Loader /;
59cfa251 41 __PACKAGE__->loader_options();
8b7749d6 42 __PACKAGE__->naming('current');
fa994d3c 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(
0c344f4e 48 'DBICTest::Schema::_make_schema_at',
8b7749d6 49 { really_erase_my_files => 1, naming => 'current' },
fa994d3c 50 [ $make_dbictest_db::dsn ],
51 );
0c344f4e 52 DBICTest::Schema::_make_schema_at->clone;
fa994d3c 53 },
54 'embedded_options' => sub {
0c344f4e 55 package DBICTest::Schema::_embedded_options;
fa994d3c 56 use base qw/ DBIx::Class::Schema::Loader /;
8b7749d6 57 __PACKAGE__->naming('current');
fa994d3c 58 __PACKAGE__->connect(
59 $make_dbictest_db::dsn,
28b4691d 60 { loader_options => { really_erase_my_files => 1 } }
fa994d3c 61 );
62 },
63 'embedded_options_in_attrs' => sub {
0c344f4e 64 package DBICTest::Schema::_embedded_options_in_attrs;
fa994d3c 65 use base qw/ DBIx::Class::Schema::Loader /;
8b7749d6 66 __PACKAGE__->naming('current');
fa994d3c 67 __PACKAGE__->connect(
68 $make_dbictest_db::dsn,
69 undef,
70 undef,
28b4691d 71 { AutoCommit => 1, loader_options => { really_erase_my_files => 1 } }
fa994d3c 72 );
73 },
74 'embedded_options_make_schema_at' => sub {
75 use DBIx::Class::Schema::Loader qw/ make_schema_at /;
76 make_schema_at(
0c344f4e 77 'DBICTest::Schema::_embedded_options_make_schema_at',
fa994d3c 78 { },
79 [
80 $make_dbictest_db::dsn,
8b7749d6 81 { loader_options => {
82 really_erase_my_files => 1,
83 naming => 'current'
84 } },
fa994d3c 85 ],
86 );
0c344f4e 87 "DBICTest::Schema::_embedded_options_make_schema_at";
fa994d3c 88 },
89 'almost_embedded' => sub {
0c344f4e 90 package DBICTest::Schema::_almost_embedded;
fa994d3c 91 use base qw/ DBIx::Class::Schema::Loader /;
8b7749d6 92 __PACKAGE__->loader_options(
93 really_erase_my_files => 1,
94 naming => 'current'
95 );
fa994d3c 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(
0c344f4e 104 'DBICTest::Schema::_make_schema_at_explicit',
8b7749d6 105 { really_erase_my_files => 1, naming => 'current' },
fa994d3c 106 [ $make_dbictest_db::dsn ],
107 );
0c344f4e 108 DBICTest::Schema::_make_schema_at_explicit->clone;
0ca61324 109 },
73099af4 110 'no_skip_load_external' => sub {
c213fd3d 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
0ca61324 112 use DBIx::Class::Schema::Loader;
113 DBIx::Class::Schema::Loader::make_schema_at(
0c344f4e 114 'DBICTest::Schema::_no_skip_load_external',
0ca61324 115 { really_erase_my_files => 1, naming => 'current' },
116 [ $make_dbictest_db::dsn ],
117 );
0c344f4e 118 DBICTest::Schema::_no_skip_load_external->clone;
0ca61324 119 },
73099af4 120 'skip_load_external' => sub {
c213fd3d 121 # When we explicitly skip_load_external t/lib/DBICTest/Schema/_skip_load_external/Foo.pm should be ignored
0ca61324 122 use DBIx::Class::Schema::Loader;
123 DBIx::Class::Schema::Loader::make_schema_at(
0c344f4e 124 'DBICTest::Schema::_skip_load_external',
0ca61324 125 { really_erase_my_files => 1, naming => 'current', skip_load_external => 1 },
126 [ $make_dbictest_db::dsn ],
127 );
0c344f4e 128 DBICTest::Schema::_skip_load_external->clone;
0ca61324 129 },
ef8e9c69 130 (DBIx::Class::Schema::Loader::Optional::Dependencies->req_ok_for('use_moose') ?
0c344f4e 131 ('use_moose' => sub {
132 package DBICTest::Schema::_use_moose;
ef8e9c69 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 ),
fa994d3c 142);
143
0c344f4e 144# 6 tests per k/v pair
145plan tests => 6 * (@invocations/2) + 2; # + 2 more manual ones below.
fa994d3c 146
0c344f4e 147while(@invocations) {
fa994d3c 148 my $style = shift @invocations;
0c344f4e 149 my $cref = shift @invocations;
150
151 my $schema = do {
152 local $SIG{__WARN__} = sub {
153 warn $_[0] unless $_[0] =~ /really_erase_my_files/
154 };
155 $cref->();
156 };
157
158 test_schema($style, $schema);
fa994d3c 159}
0ca61324 160
b7219351 161{
162 no warnings 'once';
0ca61324 163
0c344f4e 164 is($DBICTest::Schema::_no_skip_load_external::Foo::skip_me, "bad mojo",
b7219351 165 "external content loaded");
0c344f4e 166 is($DBICTest::Schema::_skip_load_external::Foo::skip_me, undef,
b7219351 167 "external content not loaded with skip_load_external => 1");
168}