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