more has_relationship_loaded tests + fix for the failing tests
[dbsrgits/DBIx-Class.git] / t / 103many_to_many_warning.t
1 use strict;
2 use warnings;
3 use Test::More;
4
5 use lib qw(t/lib);
6 use DBICTest;
7
8 my $exp_warn = qr/The many-to-many relationship 'bars' is trying to create/;
9
10 {
11   my @w;
12   local $SIG{__WARN__} = sub { $_[0] =~ $exp_warn ? push @w, $_[0] : warn $_[0] };
13   my $code = gen_code ( suffix => 1 );
14
15   local $ENV{DBIC_OVERWRITE_HELPER_METHODS_OK};
16   eval "$code";
17   ok (! $@, 'Eval code without warnings suppression')
18     || diag $@;
19
20   ok (@w, "Warning triggered without DBIC_OVERWRITE_HELPER_METHODS_OK");
21 }
22
23 {
24   my @w;
25   local $SIG{__WARN__} = sub { $_[0] =~ $exp_warn ? push @w, $_[0] : warn $_[0] };
26
27   my $code = gen_code ( suffix => 2 );
28
29   local $ENV{DBIC_OVERWRITE_HELPER_METHODS_OK} = 1;
30   eval "$code";
31   ok (! $@, 'Eval code with warnings suppression')
32     || diag $@;
33
34   ok (! @w, "No warning triggered with DBIC_OVERWRITE_HELPER_METHODS_OK");
35 }
36
37 sub gen_code {
38
39   my $args = { @_ };
40   my $suffix = $args->{suffix};
41
42   return <<EOF;
43 use strict;
44 use warnings;
45
46 {
47   package #
48     DBICTest::Schema::Foo${suffix};
49   use base 'DBIx::Class::Core';
50
51   __PACKAGE__->table('foo');
52   __PACKAGE__->add_columns(
53     'fooid' => {
54       data_type => 'integer',
55       is_auto_increment => 1,
56     },
57   );
58   __PACKAGE__->set_primary_key('fooid');
59
60
61   __PACKAGE__->has_many('foo_to_bar' => 'DBICTest::Schema::FooToBar${suffix}' => 'bar');
62   __PACKAGE__->many_to_many( foos => foo_to_bar => 'bar' );
63 }
64 {
65   package #
66     DBICTest::Schema::FooToBar${suffix};
67
68   use base 'DBIx::Class::Core';
69   __PACKAGE__->table('foo_to_bar');
70   __PACKAGE__->add_columns(
71     'foo' => {
72       data_type => 'integer',
73     },
74     'bar' => {
75       data_type => 'integer',
76     },
77   );
78   __PACKAGE__->belongs_to('foo' => 'DBICTest::Schema::Foo${suffix}');
79   __PACKAGE__->belongs_to('bar' => 'DBICTest::Schema::Foo${suffix}');
80 }
81 {
82   package #
83     DBICTest::Schema::Bar${suffix};
84
85   use base 'DBIx::Class::Core';
86
87   __PACKAGE__->table('bar');
88   __PACKAGE__->add_columns(
89     'barid' => {
90       data_type => 'integer',
91       is_auto_increment => 1,
92     },
93   );
94
95   __PACKAGE__->set_primary_key('barid');
96   __PACKAGE__->has_many('foo_to_bar' => 'DBICTest::Schema::FooToBar${suffix}' => 'foo');
97
98   __PACKAGE__->many_to_many( bars => foo_to_bar => 'foo' );
99
100   sub add_to_bars {}
101 }
102 EOF
103
104 }
105
106 done_testing;