use Optional::Dependencies stole from DBIx::Class for use_moose option
[dbsrgits/DBIx-Class-Schema-Loader.git] / t / 20invocations.t
1 use strict;
2 use Test::More;
3 use DBIx::Class::Schema::Loader::Optional::Dependencies;
4 use lib qw(t/lib);
5 use make_dbictest_db;
6
7 local $SIG{__WARN__} = sub {
8     warn $_[0] unless $_[0] =~ /really_erase_my_files/
9 };
10
11 # Takes a $schema as input, runs 4 basic tests
12 sub test_schema {
13     my ($testname, $schema) = @_;
14
15     $schema = $schema->clone if !ref $schema;
16     isa_ok($schema, 'DBIx::Class::Schema', $testname);
17
18     my $foo_rs = $schema->resultset('Bar')->search({ barid => 3})->search_related('fooref');
19     isa_ok($foo_rs, 'DBIx::Class::ResultSet', $testname);
20
21     my $foo_first = $foo_rs->first;
22     like(ref $foo_first, qr/DBICTest::Schema::\d+::Foo/, $testname);
23
24     my $foo_first_text = $foo_first->footext;
25     is($foo_first_text, 'Foo record associated with the Bar with barid 3');
26 }
27
28 my @invocations = (
29     'hardcode' => sub {
30         package DBICTest::Schema::5;
31         use base qw/ DBIx::Class::Schema::Loader /;
32         __PACKAGE__->naming('current');
33         __PACKAGE__->connection($make_dbictest_db::dsn);
34         __PACKAGE__;
35     },
36     'normal' => sub {
37         package DBICTest::Schema::6;
38         use base qw/ DBIx::Class::Schema::Loader /;
39         __PACKAGE__->loader_options();
40         __PACKAGE__->naming('current');
41         __PACKAGE__->connect($make_dbictest_db::dsn);
42     },
43     'make_schema_at' => sub {
44         use DBIx::Class::Schema::Loader qw/ make_schema_at /;
45         make_schema_at(
46             'DBICTest::Schema::7',
47             { really_erase_my_files => 1, naming => 'current' },
48             [ $make_dbictest_db::dsn ],
49         );
50         DBICTest::Schema::7->clone;
51     },
52     'embedded_options' => sub {
53         package DBICTest::Schema::8;
54         use base qw/ DBIx::Class::Schema::Loader /;
55         __PACKAGE__->naming('current');
56         __PACKAGE__->connect(
57             $make_dbictest_db::dsn,
58             { loader_options => { really_erase_my_files => 1 } }
59         );
60     },
61     'embedded_options_in_attrs' => sub {
62         package DBICTest::Schema::9;
63         use base qw/ DBIx::Class::Schema::Loader /;
64         __PACKAGE__->naming('current');
65         __PACKAGE__->connect(
66             $make_dbictest_db::dsn,
67             undef,
68             undef,
69             { AutoCommit => 1, loader_options => { really_erase_my_files => 1 } }
70         );
71     },
72     'embedded_options_make_schema_at' => sub {
73         use DBIx::Class::Schema::Loader qw/ make_schema_at /;
74         make_schema_at(
75             'DBICTest::Schema::10',
76             { },
77             [
78                 $make_dbictest_db::dsn,
79                 { loader_options => {
80                     really_erase_my_files => 1,
81                     naming => 'current'
82                 } },
83             ],
84         );
85         "DBICTest::Schema::10";
86     },
87     'almost_embedded' => sub {
88         package DBICTest::Schema::11;
89         use base qw/ DBIx::Class::Schema::Loader /;
90         __PACKAGE__->loader_options(
91             really_erase_my_files => 1,
92             naming => 'current'
93         );
94         __PACKAGE__->connect(
95             $make_dbictest_db::dsn,
96             undef, undef, { AutoCommit => 1 }
97         );
98     },
99     'make_schema_at_explicit' => sub {
100         use DBIx::Class::Schema::Loader;
101         DBIx::Class::Schema::Loader::make_schema_at(
102             'DBICTest::Schema::12',
103             { really_erase_my_files => 1, naming => 'current' },
104             [ $make_dbictest_db::dsn ],
105         );
106         DBICTest::Schema::12->clone;
107     },
108     'no_skip_load_external' => sub {
109         # By default we should pull in t/lib/DBICTest/Schema/13/Foo.pm $skip_me since t/lib is in @INC
110         use DBIx::Class::Schema::Loader;
111         DBIx::Class::Schema::Loader::make_schema_at(
112             'DBICTest::Schema::13',
113             { really_erase_my_files => 1, naming => 'current' },
114             [ $make_dbictest_db::dsn ],
115         );
116         DBICTest::Schema::13->clone;
117     },
118     'skip_load_external' => sub {
119         # When we explicitly skip_load_external t/lib/DBICTest/Schema/14/Foo.pm should be ignored
120         use DBIx::Class::Schema::Loader;
121         DBIx::Class::Schema::Loader::make_schema_at(
122             'DBICTest::Schema::14',
123             { really_erase_my_files => 1, naming => 'current', skip_load_external => 1 },
124             [ $make_dbictest_db::dsn ],
125         );
126         DBICTest::Schema::14->clone;
127     },
128     (DBIx::Class::Schema::Loader::Optional::Dependencies->req_ok_for('use_moose') ?
129         ('moose' => sub {
130             package DBICTest::Schema::8;
131             use base qw/ DBIx::Class::Schema::Loader /;
132             __PACKAGE__->naming('current');
133             __PACKAGE__->connect(
134                 $make_dbictest_db::dsn,
135                 { loader_options => { use_moose =>  1 } }
136             );
137         })
138         : ()
139     ),
140 );
141
142 # 4 tests per k/v pair
143 plan tests => 2 * @invocations + 2;  # + 2 more manual ones below.
144
145 while(@invocations >= 2) {
146     my $style = shift @invocations;
147     my $subref = shift @invocations;
148     test_schema($style, &$subref);
149 }
150
151 {
152     no warnings 'once';
153
154     is($DBICTest::Schema::13::Foo::skip_me, "bad mojo",
155         "external content loaded");
156     is($DBICTest::Schema::14::Foo::skip_me, undef,
157         "external content not loaded with skip_load_external => 1");
158 }