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