File: reentrance_count.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 (177 lines) | stat: -rw-r--r-- 5,013 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
use strict;
use warnings;

use Test::More;
BEGIN {
  plan skip_all => 'Disable test entirely until multicreate is rewritten in terms of subqueries';
}

use Test::Exception;
use lib qw(t/lib);
use DBICTest;

my $schema = DBICTest->init_schema();

my $query_stats;
$schema->storage->debugcb (sub { push @{$query_stats->{$_[0]}}, $_[1] });
$schema->storage->debug (1);

lives_ok (sub {
  undef $query_stats;
  $schema->resultset('Artist')->create ({
    name => 'poor artist',
    cds => [
      {
        title => 'cd1',
        year => 2001,
      },
      {
        title => 'cd2',
        year => 2002,
      },
    ],
  });

  is ( @{$query_stats->{INSERT} || []}, 3, 'number of inserts during creation of artist with 2 cds' );
  is ( @{$query_stats->{SELECT} || []}, 0, 'number of selects during creation of artist with 2 cds' )
    || $ENV{DBIC_MULTICREATE_DEBUG} && diag join "\n", @{$query_stats->{SELECT} || []};
});


lives_ok (sub {
  undef $query_stats;
  $schema->resultset('Artist')->create ({
    name => 'poorer artist',
    cds => [
      {
        title => 'cd3',
        year => 2003,
        genre => { name => 'vague genre' },
      },
      {
        title => 'cd4',
        year => 2004,
        genre => { name => 'vague genre' },
      },
    ],
  });

  is ( @{$query_stats->{INSERT} || []}, 4, 'number of inserts during creation of artist with 2 cds, converging on the same genre' );
  is ( @{$query_stats->{SELECT} || []}, 0, 'number of selects during creation of artist with 2 cds, converging on the same genre' )
    || $ENV{DBIC_MULTICREATE_DEBUG} && diag join "\n", @{$query_stats->{SELECT} || []};
});


lives_ok (sub {
  my $genre = $schema->resultset('Genre')->first;
  undef $query_stats;
  $schema->resultset('Artist')->create ({
    name => 'poorest artist',
    cds => [
      {
        title => 'cd5',
        year => 2005,
        genre => $genre,
      },
      {
        title => 'cd6',
        year => 2004,
        genre => $genre,
      },
    ],
  });

  is ( @{$query_stats->{INSERT} || []}, 3, 'number of inserts during creation of artist with 2 cds, converging on the same existing genre' );
  is ( @{$query_stats->{SELECT} || []}, 0, 'number of selects during creation of artist with 2 cds, converging on the same existing genre' )
    || $ENV{DBIC_MULTICREATE_DEBUG} && diag join "\n", @{$query_stats->{SELECT} || []};
});


lives_ok (sub {
  undef $query_stats;
  $schema->resultset('Artist')->create ({
    name => 'poorer than the poorest artist',
    cds => [
      {
        title => 'cd7',
        year => 2007,
        cd_to_producer => [
          {
            producer => {
              name => 'jolly producer',
              producer_to_cd => [
                {
                  cd => {
                    title => 'cd8',
                    year => 2008,
                    artist => {
                      name => 'poorer than the poorest artist',
                    },
                  },
                },
              ],
            },
          },
        ],
      },
    ],
  });

  is ( @{$query_stats->{INSERT} || []}, 6, 'number of inserts during creation of artist->cd->producer->cd->same_artist' );
  is ( @{$query_stats->{SELECT} || []}, 0, 'number of selects during creation of artist->cd->producer->cd->same_artist' )
    || $ENV{DBIC_MULTICREATE_DEBUG} && diag join "\n", @{$query_stats->{SELECT} || []};
});

lives_ok (sub {
  undef $query_stats;
  $schema->resultset ('Artist')->find(1)->create_related (cds => {
    title => 'cd9',
    year => 2009,
    cd_to_producer => [
      {
        producer => {
          name => 'jolly producer',
          producer_to_cd => [
            {
              cd => {
                title => 'cd10',
                year => 2010,
                artist => {
                  name => 'poorer than the poorest artist',
                },
              },
            },
          ],
        },
      },
    ],
  });

  is ( @{$query_stats->{INSERT} || []}, 4, 'number of inserts during creation of existing_artist->cd->existing_producer->cd->existing_artist2' );
  is ( @{$query_stats->{SELECT} || []}, 0, 'number of selects during creation of existing_artist->cd->existing_producer->cd->existing_artist2' )
    || $ENV{DBIC_MULTICREATE_DEBUG} && diag join "\n", @{$query_stats->{SELECT} || []};
});

lives_ok (sub {
  undef $query_stats;

  my $artist = $schema->resultset ('Artist')->first;
  my $producer = $schema->resultset ('Producer')->first;

  $schema->resultset ('CD')->create ({
    title => 'cd11',
    year => 2011,
    artist => $artist,
    cd_to_producer => [
      {
        producer => $producer,
      },
    ],
  });

  is ( @{$query_stats->{INSERT} || []}, 2, 'number of inserts during creation of artist_object->cd->producer_object' );
  is ( @{$query_stats->{SELECT} || []}, 0, 'number of selects during creation of artist_object->cd->producer_object' )
    || $ENV{DBIC_MULTICREATE_DEBUG} && diag join "\n", @{$query_stats->{SELECT} || []};
});

done_testing;