Commit | Line | Data |
0567538f |
1 | sub run_tests { |
2 | |
3 | use IO::File; |
4 | |
5 | BEGIN { |
6 | eval "use DBD::SQLite"; |
7 | plan $@ |
8 | ? ( skip_all => 'needs DBD::SQLite for testing' ) |
8fe164b9 |
9 | : ( tests => 23 ); |
0567538f |
10 | } |
11 | |
12 | # test the abstract join => SQL generator |
13 | my $sa = new DBIC::SQL::Abstract; |
14 | |
15 | my @j = ( |
16 | { child => 'person' }, |
17 | [ { father => 'person' }, { 'father.person_id' => 'child.father_id' }, ], |
18 | [ { mother => 'person' }, { 'mother.person_id' => 'child.mother_id' } ], |
19 | ); |
20 | my $match = 'person child JOIN person father ON ( father.person_id = ' |
21 | . 'child.father_id ) JOIN person mother ON ( mother.person_id ' |
22 | . '= child.mother_id )' |
23 | ; |
24 | is( $sa->_recurse_from(@j), $match, 'join 1 ok' ); |
25 | |
26 | my @j2 = ( |
27 | { mother => 'person' }, |
28 | [ [ { child => 'person' }, |
29 | [ { father => 'person' }, |
30 | { 'father.person_id' => 'child.father_id' } |
31 | ] |
32 | ], |
33 | { 'mother.person_id' => 'child.mother_id' } |
34 | ], |
35 | ); |
36 | $match = 'person mother JOIN (person child JOIN person father ON (' |
37 | . ' father.person_id = child.father_id )) ON ( mother.person_id = ' |
38 | . 'child.mother_id )' |
39 | ; |
40 | is( $sa->_recurse_from(@j2), $match, 'join 2 ok' ); |
41 | |
42 | my @j3 = ( |
43 | { child => 'person' }, |
44 | [ { father => 'person', -join_type => 'inner' }, { 'father.person_id' => 'child.father_id' }, ], |
45 | [ { mother => 'person', -join_type => 'inner' }, { 'mother.person_id' => 'child.mother_id' } ], |
46 | ); |
47 | $match = 'person child INNER JOIN person father ON ( father.person_id = ' |
48 | . 'child.father_id ) INNER JOIN person mother ON ( mother.person_id ' |
49 | . '= child.mother_id )' |
50 | ; |
51 | |
52 | is( $sa->_recurse_from(@j3), $match, 'join 3 (inner join) ok'); |
53 | |
3712e4f4 |
54 | my $rs = DBICTest->class("CD")->search( |
0567538f |
55 | { 'year' => 2001, 'artist.name' => 'Caterwauler McCrae' }, |
56 | { from => [ { 'me' => 'cd' }, |
57 | [ |
58 | { artist => 'artist' }, |
59 | { 'me.artist' => 'artist.artistid' } |
60 | ] ] } |
61 | ); |
62 | |
63 | cmp_ok( $rs->count, '==', 1, "Single record in resultset"); |
64 | |
65 | is($rs->first->title, 'Forkful of bees', 'Correct record returned'); |
66 | |
3712e4f4 |
67 | $rs = DBICTest->class("CD")->search( |
0567538f |
68 | { 'year' => 2001, 'artist.name' => 'Caterwauler McCrae' }, |
69 | { join => 'artist' }); |
70 | |
71 | cmp_ok( $rs->count, '==', 1, "Single record in resultset"); |
72 | |
73 | is($rs->first->title, 'Forkful of bees', 'Correct record returned'); |
74 | |
3712e4f4 |
75 | $rs = DBICTest->class("CD")->search( |
0567538f |
76 | { 'artist.name' => 'We Are Goth', |
77 | 'liner_notes.notes' => 'Kill Yourself!' }, |
78 | { join => [ qw/artist liner_notes/ ] }); |
79 | |
80 | cmp_ok( $rs->count, '==', 1, "Single record in resultset"); |
81 | |
82 | is($rs->first->title, 'Come Be Depressed With Us', 'Correct record returned'); |
83 | |
8fe164b9 |
84 | # when using join attribute, make sure slice()ing all objects has same count as all() |
85 | $rs = DBICTest->class("CD")->search( |
86 | { 'artist' => 1 }, |
87 | { join => [qw/artist/], order_by => 'artist.name' } |
88 | ); |
89 | cmp_ok( scalar $rs->all, '==', scalar $rs->slice(0, $rs->count - 1), 'slice() with join has same count as all()' ); |
90 | |
3712e4f4 |
91 | $rs = DBICTest->class("Artist")->search( |
0567538f |
92 | { 'liner_notes.notes' => 'Kill Yourself!' }, |
93 | { join => { 'cds' => 'liner_notes' } }); |
94 | |
95 | cmp_ok( $rs->count, '==', 1, "Single record in resultset"); |
96 | |
97 | is($rs->first->name, 'We Are Goth', 'Correct record returned'); |
98 | |
3712e4f4 |
99 | $rs = DBICTest->class("CD")->search( |
0567538f |
100 | { 'artist.name' => 'Caterwauler McCrae' }, |
101 | { prefetch => [ qw/artist liner_notes/ ], |
102 | order_by => 'me.cdid' }); |
103 | |
104 | cmp_ok($rs->count, '==', 3, 'Correct number of records returned'); |
105 | |
106 | # start test for prefetch SELECT count |
107 | unlink 't/var/dbic.trace' if -e 't/var/dbic.trace'; |
108 | DBI->trace(1, 't/var/dbic.trace'); |
109 | |
110 | my @cd = $rs->all; |
111 | |
112 | is($cd[0]->title, 'Spoonful of bees', 'First record returned ok'); |
113 | |
7cd300ea |
114 | ok(!defined $cd[0]->liner_notes, 'No prefetch for NULL LEFT join'); |
0567538f |
115 | |
116 | is($cd[1]->{_relationship_data}{liner_notes}->notes, 'Buy Whiskey!', 'Prefetch for present LEFT JOIN'); |
117 | |
dd417d06 |
118 | is(ref $cd[1]->liner_notes, 'DBICTest::LinerNotes', 'Prefetch returns correct class'); |
119 | |
0567538f |
120 | is($cd[2]->{_inflated_column}{artist}->name, 'Caterwauler McCrae', 'Prefetch on parent object ok'); |
121 | |
122 | # count the SELECTs |
123 | DBI->trace(0); |
124 | my $selects = 0; |
125 | my $trace = IO::File->new('t/var/dbic.trace', '<') |
126 | or die "Unable to read trace file"; |
127 | while (<$trace>) { |
128 | $selects++ if /SELECT/; |
129 | } |
130 | $trace->close; |
131 | unlink 't/var/dbic.trace'; |
132 | is($selects, 1, 'prefetch ran only 1 select statement'); |
133 | |
3712e4f4 |
134 | my ($artist) = DBICTest->class("Artist")->search({ 'cds.year' => 2001 }, |
0567538f |
135 | { order_by => 'artistid DESC', join => 'cds' }); |
136 | |
137 | is($artist->name, 'Random Boy Band', "Join search by object ok"); |
138 | |
3712e4f4 |
139 | my @cds = DBICTest->class("CD")->search({ 'liner_notes.notes' => 'Buy Merch!' }, |
0567538f |
140 | { join => 'liner_notes' }); |
141 | |
142 | cmp_ok(scalar @cds, '==', 1, "Single CD retrieved via might_have"); |
143 | |
144 | is($cds[0]->title, "Generic Manufactured Singles", "Correct CD retrieved"); |
145 | |
3712e4f4 |
146 | my @artists = DBICTest->class("Artist")->search({ 'tags.tag' => 'Shiny' }, |
0567538f |
147 | { join => { 'cds' => 'tags' } }); |
148 | |
149 | cmp_ok( @artists, '==', 2, "two-join search ok" ); |
150 | |
151 | } |
152 | |
153 | 1; |