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