b2eb18ad34951e3fd98b6f71f1188099ce671250
[dbsrgits/DBIx-Class-Schema-Loader.git] / t / 45relationships.t
1 use strict;
2 use Test::More tests => 12;
3 use Test::Exception;
4 use lib qw(t/lib);
5 use make_dbictest_db;
6
7 use DBIx::Class::Schema::Loader;
8
9 my $schema_counter = 0;
10
11 # test skip_relationships
12 my $regular = schema_with();
13 is( ref($regular->source('Bar')->relationship_info('fooref')), 'HASH',
14     'regularly-made schema has fooref rel',
15   );
16 my $skip_rel = schema_with( skip_relationships => 1 );
17 is_deeply( $skip_rel->source('Bar')->relationship_info('fooref'), undef,
18     'skip_relationships blocks generation of fooref rel',
19   );
20
21 # test hashref as rel_name_map
22 my $hash_relationship = schema_with(
23     rel_name_map => {
24         fooref => "got_fooref",
25         bars   => "ignored",
26         Foo    => {
27             bars => "got_bars",
28             fooref => "ignored",
29         },
30     }
31 );
32 is( ref($hash_relationship->source('Foo')->relationship_info('got_bars')),
33     'HASH',
34     'single level hash in rel_name_map picked up correctly'
35   );
36 is( ref($hash_relationship->source('Bar')->relationship_info('got_fooref')),
37     'HASH',
38     'double level hash in rel_name_map picked up correctly'
39   );
40
41 # test coderef as rel_name_map
42 my $code_relationship = schema_with(
43     rel_name_map => sub {
44         my ($args) = @_;
45
46         if ($args->{local_moniker} eq 'Foo') {
47             is_deeply(
48                 $args,
49                 {
50                     name           => 'bars',
51                     type           => 'has_many',
52                     local_class    =>
53                         "DBICTest::Schema::${schema_counter}::Result::Foo",
54                     local_moniker  => 'Foo',
55                     local_columns  => ['fooid'],
56                     remote_class   =>
57                         "DBICTest::Schema::${schema_counter}::Result::Bar",
58                     remote_moniker => 'Bar',
59                     remote_columns => ['fooref'],
60                 },
61                 'correct args for Foo passed'
62               );
63             return 'bars_caught';
64         }
65         elsif ($args->{local_moniker} eq 'Bar') {
66             is_deeply(
67                 $args,
68                 {
69                     name           => 'fooref',
70                     type           => 'belongs_to',
71                     local_class    =>
72                         "DBICTest::Schema::${schema_counter}::Result::Bar",
73                     local_moniker  => 'Bar',
74                     local_columns  => ['fooref'],
75                     remote_class   =>
76                         "DBICTest::Schema::${schema_counter}::Result::Foo",
77                     remote_moniker => 'Foo',
78                     remote_columns => ['fooid'],
79                 },
80                 'correct args for Foo passed'
81               );
82         
83             return 'fooref_caught';
84         }
85     }
86   );
87 is( ref($code_relationship->source('Foo')->relationship_info('bars_caught')),
88     'HASH',
89     'rel_name_map overrode local_info correctly'
90   );
91 is( ref($code_relationship->source('Bar')->relationship_info('fooref_caught')),
92     'HASH',
93     'rel_name_map overrode remote_info correctly'
94   );
95
96
97
98 # test relationship_attrs
99 throws_ok {
100     schema_with( relationship_attrs => 'laughably invalid!!!' );
101 } qr/relationship_attrs/, 'throws error for invalid relationship_attrs';
102
103
104 {
105     my $nodelete = schema_with( relationship_attrs =>
106                                 {
107                                  all        => { cascade_delete => 0 },
108                                  belongs_to => { cascade_delete => 1 },
109                                 },
110                               );
111
112     my $bars_info   = $nodelete->source('Foo')->relationship_info('bars');
113     #use Data::Dumper;
114     #die Dumper([ $nodelete->source('Foo')->relationships() ]);
115     my $fooref_info = $nodelete->source('Bar')->relationship_info('fooref');
116     is( ref($fooref_info), 'HASH',
117         'fooref rel is present',
118       );
119     is( $bars_info->{attrs}->{cascade_delete}, 0,
120         'relationship_attrs settings seem to be getting through to the generated rels',
121       );
122     is( $fooref_info->{attrs}->{cascade_delete}, 1,
123         'belongs_to in relationship_attrs overrides all def',
124       );
125 }
126
127 #### generates a new schema with the given opts every time it's called
128 sub schema_with {
129     $schema_counter++;
130     DBIx::Class::Schema::Loader::make_schema_at(
131             'DBICTest::Schema::'.$schema_counter,
132             { naming => 'current', @_ },
133             [ $make_dbictest_db::dsn ],
134     );
135     "DBICTest::Schema::$schema_counter"->clone;
136 }