File: 103many_to_many_warning.t

package info (click to toggle)
libdbix-class-perl 0.082844-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 5,320 kB
  • sloc: perl: 27,215; sql: 322; sh: 29; makefile: 16
file content (106 lines) | stat: -rw-r--r-- 2,256 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
use strict;
use warnings;
use Test::More;

use lib qw(t/lib);
use DBICTest;

my $exp_warn = qr/The many-to-many relationship 'bars' is trying to create/;

{
  my @w;
  local $SIG{__WARN__} = sub { $_[0] =~ $exp_warn ? push @w, $_[0] : warn $_[0] };
  my $code = gen_code ( suffix => 1 );

  local $ENV{DBIC_OVERWRITE_HELPER_METHODS_OK};
  eval "$code";
  ok (! $@, 'Eval code without warnings suppression')
    || diag $@;

  ok (@w, "Warning triggered without DBIC_OVERWRITE_HELPER_METHODS_OK");
}

{
  my @w;
  local $SIG{__WARN__} = sub { $_[0] =~ $exp_warn ? push @w, $_[0] : warn $_[0] };

  my $code = gen_code ( suffix => 2 );

  local $ENV{DBIC_OVERWRITE_HELPER_METHODS_OK} = 1;
  eval "$code";
  ok (! $@, 'Eval code with warnings suppression')
    || diag $@;

  ok (! @w, "No warning triggered with DBIC_OVERWRITE_HELPER_METHODS_OK");
}

sub gen_code {

  my $args = { @_ };
  my $suffix = $args->{suffix};

  return <<EOF;
use strict;
use warnings;

{
  package #
    DBICTest::Schema::Foo${suffix};
  use base 'DBIx::Class::Core';

  __PACKAGE__->table('foo');
  __PACKAGE__->add_columns(
    'fooid' => {
      data_type => 'integer',
      is_auto_increment => 1,
    },
  );
  __PACKAGE__->set_primary_key('fooid');


  __PACKAGE__->has_many('foo_to_bar' => 'DBICTest::Schema::FooToBar${suffix}' => 'bar');
  __PACKAGE__->many_to_many( foos => foo_to_bar => 'bar' );
}
{
  package #
    DBICTest::Schema::FooToBar${suffix};

  use base 'DBIx::Class::Core';
  __PACKAGE__->table('foo_to_bar');
  __PACKAGE__->add_columns(
    'foo' => {
      data_type => 'integer',
    },
    'bar' => {
      data_type => 'integer',
    },
  );
  __PACKAGE__->belongs_to('foo' => 'DBICTest::Schema::Foo${suffix}');
  __PACKAGE__->belongs_to('bar' => 'DBICTest::Schema::Foo${suffix}');
}
{
  package #
    DBICTest::Schema::Bar${suffix};

  use base 'DBIx::Class::Core';

  __PACKAGE__->table('bar');
  __PACKAGE__->add_columns(
    'barid' => {
      data_type => 'integer',
      is_auto_increment => 1,
    },
  );

  __PACKAGE__->set_primary_key('barid');
  __PACKAGE__->has_many('foo_to_bar' => 'DBICTest::Schema::FooToBar${suffix}' => 'foo');

  __PACKAGE__->many_to_many( bars => foo_to_bar => 'foo' );

  sub add_to_bars {}
}
EOF

}

done_testing;