Factor out author count anouncement, making it seen on plain installs
[dbsrgits/DBIx-Class.git] / t / multi_create / reentrance_count.t
1 use strict;
2 use warnings;
3
4 use Test::More;
5 BEGIN {
6   plan skip_all => 'Disable test entirely until multicreate is rewritten in terms of subqueries';
7 }
8
9 use Test::Exception;
10 use lib qw(t/lib);
11 use DBICTest;
12
13 my $schema = DBICTest->init_schema();
14
15 my $query_stats;
16 $schema->storage->debugcb (sub { push @{$query_stats->{$_[0]}}, $_[1] });
17 $schema->storage->debug (1);
18
19 lives_ok (sub {
20   undef $query_stats;
21   $schema->resultset('Artist')->create ({
22     name => 'poor artist',
23     cds => [
24       {
25         title => 'cd1',
26         year => 2001,
27       },
28       {
29         title => 'cd2',
30         year => 2002,
31       },
32     ],
33   });
34
35   is ( @{$query_stats->{INSERT} || []}, 3, 'number of inserts during creation of artist with 2 cds' );
36   is ( @{$query_stats->{SELECT} || []}, 0, 'number of selects during creation of artist with 2 cds' )
37     || $ENV{DBIC_MULTICREATE_DEBUG} && diag join "\n", @{$query_stats->{SELECT} || []};
38 });
39
40
41 lives_ok (sub {
42   undef $query_stats;
43   $schema->resultset('Artist')->create ({
44     name => 'poorer artist',
45     cds => [
46       {
47         title => 'cd3',
48         year => 2003,
49         genre => { name => 'vague genre' },
50       },
51       {
52         title => 'cd4',
53         year => 2004,
54         genre => { name => 'vague genre' },
55       },
56     ],
57   });
58
59   is ( @{$query_stats->{INSERT} || []}, 4, 'number of inserts during creation of artist with 2 cds, converging on the same genre' );
60   is ( @{$query_stats->{SELECT} || []}, 0, 'number of selects during creation of artist with 2 cds, converging on the same genre' )
61     || $ENV{DBIC_MULTICREATE_DEBUG} && diag join "\n", @{$query_stats->{SELECT} || []};
62 });
63
64
65 lives_ok (sub {
66   my $genre = $schema->resultset('Genre')->first;
67   undef $query_stats;
68   $schema->resultset('Artist')->create ({
69     name => 'poorest artist',
70     cds => [
71       {
72         title => 'cd5',
73         year => 2005,
74         genre => $genre,
75       },
76       {
77         title => 'cd6',
78         year => 2004,
79         genre => $genre,
80       },
81     ],
82   });
83
84   is ( @{$query_stats->{INSERT} || []}, 3, 'number of inserts during creation of artist with 2 cds, converging on the same existing genre' );
85   is ( @{$query_stats->{SELECT} || []}, 0, 'number of selects during creation of artist with 2 cds, converging on the same existing genre' )
86     || $ENV{DBIC_MULTICREATE_DEBUG} && diag join "\n", @{$query_stats->{SELECT} || []};
87 });
88
89
90 lives_ok (sub {
91   undef $query_stats;
92   $schema->resultset('Artist')->create ({
93     name => 'poorer than the poorest artist',
94     cds => [
95       {
96         title => 'cd7',
97         year => 2007,
98         cd_to_producer => [
99           {
100             producer => {
101               name => 'jolly producer',
102               producer_to_cd => [
103                 {
104                   cd => {
105                     title => 'cd8',
106                     year => 2008,
107                     artist => {
108                       name => 'poorer than the poorest artist',
109                     },
110                   },
111                 },
112               ],
113             },
114           },
115         ],
116       },
117     ],
118   });
119
120   is ( @{$query_stats->{INSERT} || []}, 6, 'number of inserts during creation of artist->cd->producer->cd->same_artist' );
121   is ( @{$query_stats->{SELECT} || []}, 0, 'number of selects during creation of artist->cd->producer->cd->same_artist' )
122     || $ENV{DBIC_MULTICREATE_DEBUG} && diag join "\n", @{$query_stats->{SELECT} || []};
123 });
124
125 lives_ok (sub {
126   undef $query_stats;
127   $schema->resultset ('Artist')->find(1)->create_related (cds => {
128     title => 'cd9',
129     year => 2009,
130     cd_to_producer => [
131       {
132         producer => {
133           name => 'jolly producer',
134           producer_to_cd => [
135             {
136               cd => {
137                 title => 'cd10',
138                 year => 2010,
139                 artist => {
140                   name => 'poorer than the poorest artist',
141                 },
142               },
143             },
144           ],
145         },
146       },
147     ],
148   });
149
150   is ( @{$query_stats->{INSERT} || []}, 4, 'number of inserts during creation of existing_artist->cd->existing_producer->cd->existing_artist2' );
151   is ( @{$query_stats->{SELECT} || []}, 0, 'number of selects during creation of existing_artist->cd->existing_producer->cd->existing_artist2' )
152     || $ENV{DBIC_MULTICREATE_DEBUG} && diag join "\n", @{$query_stats->{SELECT} || []};
153 });
154
155 lives_ok (sub {
156   undef $query_stats;
157
158   my $artist = $schema->resultset ('Artist')->first;
159   my $producer = $schema->resultset ('Producer')->first;
160
161   $schema->resultset ('CD')->create ({
162     title => 'cd11',
163     year => 2011,
164     artist => $artist,
165     cd_to_producer => [
166       {
167         producer => $producer,
168       },
169     ],
170   });
171
172   is ( @{$query_stats->{INSERT} || []}, 2, 'number of inserts during creation of artist_object->cd->producer_object' );
173   is ( @{$query_stats->{SELECT} || []}, 0, 'number of selects during creation of artist_object->cd->producer_object' )
174     || $ENV{DBIC_MULTICREATE_DEBUG} && diag join "\n", @{$query_stats->{SELECT} || []};
175 });
176
177 done_testing;