c8188429c9b65c706029d1515047c1bcc74d8d12
[dbsrgits/DBIx-Class-Schema-Loader.git] / t / 20invocations.t
1 use strict;
2 use Test::More;
3 use lib qw(t/lib);
4 use make_dbictest_db;
5
6 # Takes a $schema as input, runs 4 basic tests
7 sub test_schema {
8     my ($testname, $schema) = @_;
9
10     $schema = $schema->clone if !ref $schema;
11     isa_ok($schema, 'DBIx::Class::Schema', $testname);
12
13     my $foo_rs = $schema->resultset('Bar')->search({ barid => 3})->search_related('fooref');
14     isa_ok($foo_rs, 'DBIx::Class::ResultSet', $testname);
15
16     my $foo_first = $foo_rs->first;
17     like(ref $foo_first, qr/DBICTest::Schema::\d+::Foo/, $testname);
18
19     my $foo_first_text = $foo_first->footext;
20     is($foo_first_text, 'Foo record associated with the Bar with barid 3');
21 }
22
23 my @invocations = (
24     'hardcode' => sub {
25         package DBICTest::Schema::5;
26         use base qw/ DBIx::Class::Schema::Loader /;
27         __PACKAGE__->connection($make_dbictest_db::dsn);
28         __PACKAGE__;
29     },
30     'normal' => sub {
31         package DBICTest::Schema::6;
32         use base qw/ DBIx::Class::Schema::Loader /;
33         __PACKAGE__->loader_options();
34         __PACKAGE__->connect($make_dbictest_db::dsn);
35     },
36     'make_schema_at' => sub {
37         use DBIx::Class::Schema::Loader qw/ make_schema_at /;
38         make_schema_at(
39             'DBICTest::Schema::7',
40             { really_erase_my_files => 1 },
41             [ $make_dbictest_db::dsn ],
42         );
43         DBICTest::Schema::7->clone;
44     },
45     'embedded_options' => sub {
46         package DBICTest::Schema::8;
47         use base qw/ DBIx::Class::Schema::Loader /;
48         __PACKAGE__->connect(
49             $make_dbictest_db::dsn,
50             { loader_options => { really_erase_my_files => 1 } }
51         );
52     },
53     'embedded_options_in_attrs' => sub {
54         package DBICTest::Schema::9;
55         use base qw/ DBIx::Class::Schema::Loader /;
56         __PACKAGE__->connect(
57             $make_dbictest_db::dsn,
58             undef,
59             undef,
60             { AutoCommit => 1, loader_options => { really_erase_my_files => 1 } }
61         );
62     },
63     'embedded_options_make_schema_at' => sub {
64         use DBIx::Class::Schema::Loader qw/ make_schema_at /;
65         make_schema_at(
66             'DBICTest::Schema::10',
67             { },
68             [
69                 $make_dbictest_db::dsn,
70                 { loader_options => { really_erase_my_files => 1 } },
71             ],
72         );
73         "DBICTest::Schema::10";
74     },
75     'almost_embedded' => sub {
76         package DBICTest::Schema::11;
77         use base qw/ DBIx::Class::Schema::Loader /;
78         __PACKAGE__->loader_options( really_erase_my_files => 1 );
79         __PACKAGE__->connect(
80             $make_dbictest_db::dsn,
81             undef, undef, { AutoCommit => 1 }
82         );
83     },
84     'make_schema_at_explicit' => sub {
85         use DBIx::Class::Schema::Loader;
86         DBIx::Class::Schema::Loader::make_schema_at(
87             'DBICTest::Schema::12',
88             { really_erase_my_files => 1 },
89             [ $make_dbictest_db::dsn ],
90         );
91         DBICTest::Schema::12->clone;
92     }
93 );
94
95 # 4 tests per k/v pair
96 plan tests => 2 * @invocations;
97
98 while(@invocations >= 2) {
99     my $style = shift @invocations;
100     my $subref = shift @invocations;
101     test_schema($style, &$subref);
102 }