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' ) |
dd417d06 |
9 | : ( tests => 22 ); |
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 | |
54 | my $rs = DBICTest::CD->search( |
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 | |
67 | $rs = DBICTest::CD->search( |
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 | |
75 | $rs = DBICTest::CD->search( |
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 | |
84 | $rs = DBICTest::Artist->search( |
85 | { 'liner_notes.notes' => 'Kill Yourself!' }, |
86 | { join => { 'cds' => 'liner_notes' } }); |
87 | |
88 | cmp_ok( $rs->count, '==', 1, "Single record in resultset"); |
89 | |
90 | is($rs->first->name, 'We Are Goth', 'Correct record returned'); |
91 | |
92 | DBICTest::Schema::CD->add_relationship( |
93 | artist => 'DBICTest::Schema::Artist', |
94 | { 'foreign.artistid' => 'self.artist' }, |
95 | { accessor => 'filter' }, |
96 | ); |
97 | |
98 | DBICTest::Schema::CD->add_relationship( |
99 | liner_notes => 'DBICTest::Schema::LinerNotes', |
100 | { 'foreign.liner_id' => 'self.cdid' }, |
101 | { join_type => 'LEFT', accessor => 'single' }); |
102 | |
103 | $rs = DBICTest::CD->search( |
104 | { 'artist.name' => 'Caterwauler McCrae' }, |
105 | { prefetch => [ qw/artist liner_notes/ ], |
106 | order_by => 'me.cdid' }); |
107 | |
108 | cmp_ok($rs->count, '==', 3, 'Correct number of records returned'); |
109 | |
110 | # start test for prefetch SELECT count |
111 | unlink 't/var/dbic.trace' if -e 't/var/dbic.trace'; |
112 | DBI->trace(1, 't/var/dbic.trace'); |
113 | |
114 | my @cd = $rs->all; |
115 | |
116 | is($cd[0]->title, 'Spoonful of bees', 'First record returned ok'); |
117 | |
7cd300ea |
118 | ok(!defined $cd[0]->liner_notes, 'No prefetch for NULL LEFT join'); |
0567538f |
119 | |
120 | is($cd[1]->{_relationship_data}{liner_notes}->notes, 'Buy Whiskey!', 'Prefetch for present LEFT JOIN'); |
121 | |
dd417d06 |
122 | is(ref $cd[1]->liner_notes, 'DBICTest::LinerNotes', 'Prefetch returns correct class'); |
123 | |
0567538f |
124 | is($cd[2]->{_inflated_column}{artist}->name, 'Caterwauler McCrae', 'Prefetch on parent object ok'); |
125 | |
126 | # count the SELECTs |
127 | DBI->trace(0); |
128 | my $selects = 0; |
129 | my $trace = IO::File->new('t/var/dbic.trace', '<') |
130 | or die "Unable to read trace file"; |
131 | while (<$trace>) { |
132 | $selects++ if /SELECT/; |
133 | } |
134 | $trace->close; |
135 | unlink 't/var/dbic.trace'; |
136 | is($selects, 1, 'prefetch ran only 1 select statement'); |
137 | |
138 | my ($artist) = DBICTest::Artist->search({ 'cds.year' => 2001 }, |
139 | { order_by => 'artistid DESC', join => 'cds' }); |
140 | |
141 | is($artist->name, 'Random Boy Band', "Join search by object ok"); |
142 | |
143 | my @cds = DBICTest::CD->search({ 'liner_notes.notes' => 'Buy Merch!' }, |
144 | { join => 'liner_notes' }); |
145 | |
146 | cmp_ok(scalar @cds, '==', 1, "Single CD retrieved via might_have"); |
147 | |
148 | is($cds[0]->title, "Generic Manufactured Singles", "Correct CD retrieved"); |
149 | |
150 | my @artists = DBICTest::Artist->search({ 'tags.tag' => 'Shiny' }, |
151 | { join => { 'cds' => 'tags' } }); |
152 | |
153 | cmp_ok( @artists, '==', 2, "two-join search ok" ); |
154 | |
155 | } |
156 | |
157 | 1; |