Commit | Line | Data |
70350518 |
1 | use strict; |
2 | use warnings; |
3 | |
4 | use Test::More; |
5 | use lib qw(t/lib); |
6 | use DBICTest; |
cb3e35d2 |
7 | use Data::Dumper; |
949172b0 |
8 | use DBIC::SqlMakerTest; |
70350518 |
9 | |
a47e1233 |
10 | my $schema = DBICTest->init_schema(); |
0567538f |
11 | |
4d91ad3f |
12 | my $orig_debug = $schema->storage->debug; |
13 | |
0567538f |
14 | use IO::File; |
15 | |
16 | BEGIN { |
17 | eval "use DBD::SQLite"; |
18 | plan $@ |
19 | ? ( skip_all => 'needs DBD::SQLite for testing' ) |
96e7f9ec |
20 | : ( tests => 16 ); |
0567538f |
21 | } |
22 | |
58ff4acf |
23 | # figure out if we've got a version of sqlite that is older than 3.2.6, in |
24 | # which case COUNT(DISTINCT()) doesn't work |
25 | my $is_broken_sqlite = 0; |
26 | my ($sqlite_major_ver,$sqlite_minor_ver,$sqlite_patch_ver) = |
27 | split /\./, $schema->storage->dbh->get_info(18); |
28 | if( $schema->storage->dbh->get_info(17) eq 'SQLite' && |
29 | ( ($sqlite_major_ver < 3) || |
30 | ($sqlite_major_ver == 3 && $sqlite_minor_ver < 2) || |
31 | ($sqlite_major_ver == 3 && $sqlite_minor_ver == 2 && $sqlite_patch_ver < 6) ) ) { |
32 | $is_broken_sqlite = 1; |
33 | } |
485f6e10 |
34 | |
0567538f |
35 | # test the abstract join => SQL generator |
36 | my $sa = new DBIC::SQL::Abstract; |
37 | |
38 | my @j = ( |
39 | { child => 'person' }, |
40 | [ { father => 'person' }, { 'father.person_id' => 'child.father_id' }, ], |
41 | [ { mother => 'person' }, { 'mother.person_id' => 'child.mother_id' } ], |
42 | ); |
43 | my $match = 'person child JOIN person father ON ( father.person_id = ' |
44 | . 'child.father_id ) JOIN person mother ON ( mother.person_id ' |
45 | . '= child.mother_id )' |
46 | ; |
9b459129 |
47 | is_same_sql_bind( |
48 | $sa->_recurse_from(@j), [], |
49 | $match, [], |
50 | 'join 1 ok' |
51 | ); |
0567538f |
52 | |
53 | my @j2 = ( |
54 | { mother => 'person' }, |
55 | [ [ { child => 'person' }, |
56 | [ { father => 'person' }, |
57 | { 'father.person_id' => 'child.father_id' } |
58 | ] |
59 | ], |
60 | { 'mother.person_id' => 'child.mother_id' } |
61 | ], |
62 | ); |
63 | $match = 'person mother JOIN (person child JOIN person father ON (' |
64 | . ' father.person_id = child.father_id )) ON ( mother.person_id = ' |
65 | . 'child.mother_id )' |
66 | ; |
9b459129 |
67 | is_same_sql_bind( |
68 | $sa->_recurse_from(@j2), [], |
69 | $match, [], |
70 | 'join 2 ok' |
71 | ); |
72 | |
0567538f |
73 | |
74 | my @j3 = ( |
75 | { child => 'person' }, |
76 | [ { father => 'person', -join_type => 'inner' }, { 'father.person_id' => 'child.father_id' }, ], |
77 | [ { mother => 'person', -join_type => 'inner' }, { 'mother.person_id' => 'child.mother_id' } ], |
78 | ); |
79 | $match = 'person child INNER JOIN person father ON ( father.person_id = ' |
80 | . 'child.father_id ) INNER JOIN person mother ON ( mother.person_id ' |
81 | . '= child.mother_id )' |
82 | ; |
83 | |
9b459129 |
84 | is_same_sql_bind( |
85 | $sa->_recurse_from(@j3), [], |
86 | $match, [], |
87 | 'join 3 (inner join) ok' |
88 | ); |
0567538f |
89 | |
ca7b9fdf |
90 | my @j4 = ( |
91 | { mother => 'person' }, |
92 | [ [ { child => 'person', -join_type => 'left' }, |
93 | [ { father => 'person', -join_type => 'right' }, |
94 | { 'father.person_id' => 'child.father_id' } |
95 | ] |
96 | ], |
97 | { 'mother.person_id' => 'child.mother_id' } |
98 | ], |
99 | ); |
100 | $match = 'person mother LEFT JOIN (person child RIGHT JOIN person father ON (' |
101 | . ' father.person_id = child.father_id )) ON ( mother.person_id = ' |
102 | . 'child.mother_id )' |
103 | ; |
9b459129 |
104 | is_same_sql_bind( |
105 | $sa->_recurse_from(@j4), [], |
106 | $match, [], |
107 | 'join 4 (nested joins + join types) ok' |
108 | ); |
ca7b9fdf |
109 | |
635b9634 |
110 | my @j5 = ( |
111 | { child => 'person' }, |
112 | [ { father => 'person' }, { 'father.person_id' => \'!= child.father_id' }, ], |
113 | [ { mother => 'person' }, { 'mother.person_id' => 'child.mother_id' } ], |
114 | ); |
115 | $match = 'person child JOIN person father ON ( father.person_id != ' |
116 | . 'child.father_id ) JOIN person mother ON ( mother.person_id ' |
117 | . '= child.mother_id )' |
118 | ; |
9b459129 |
119 | is_same_sql_bind( |
120 | $sa->_recurse_from(@j5), [], |
121 | $match, [], |
122 | 'join 5 (SCALAR reference for ON statement) ok' |
123 | ); |
635b9634 |
124 | |
125 | my @j6 = ( |
126 | { child => 'person' }, |
127 | [ { father => 'person' }, { 'father.person_id' => { '!=', '42' } }, ], |
128 | [ { mother => 'person' }, { 'mother.person_id' => 'child.mother_id' } ], |
129 | ); |
11aada86 |
130 | $match = qr/^HASH reference arguments are not supported in JOINS - try using "\.\.\." instead/; |
635b9634 |
131 | eval { $sa->_recurse_from(@j6) }; |
132 | like( $@, $match, 'join 6 (HASH reference for ON statement dies) ok' ); |
133 | |
f9db5527 |
134 | my $rs = $schema->resultset("CD")->search( |
0567538f |
135 | { 'year' => 2001, 'artist.name' => 'Caterwauler McCrae' }, |
136 | { from => [ { 'me' => 'cd' }, |
137 | [ |
138 | { artist => 'artist' }, |
139 | { 'me.artist' => 'artist.artistid' } |
140 | ] ] } |
141 | ); |
142 | |
ebaefbc2 |
143 | cmp_ok( $rs + 0, '==', 1, "Single record in resultset"); |
0567538f |
144 | |
145 | is($rs->first->title, 'Forkful of bees', 'Correct record returned'); |
146 | |
f9db5527 |
147 | $rs = $schema->resultset("CD")->search( |
0567538f |
148 | { 'year' => 2001, 'artist.name' => 'Caterwauler McCrae' }, |
149 | { join => 'artist' }); |
150 | |
ebaefbc2 |
151 | cmp_ok( $rs + 0, '==', 1, "Single record in resultset"); |
0567538f |
152 | |
153 | is($rs->first->title, 'Forkful of bees', 'Correct record returned'); |
154 | |
f9db5527 |
155 | $rs = $schema->resultset("CD")->search( |
0567538f |
156 | { 'artist.name' => 'We Are Goth', |
157 | 'liner_notes.notes' => 'Kill Yourself!' }, |
158 | { join => [ qw/artist liner_notes/ ] }); |
159 | |
ebaefbc2 |
160 | cmp_ok( $rs + 0, '==', 1, "Single record in resultset"); |
0567538f |
161 | |
162 | is($rs->first->title, 'Come Be Depressed With Us', 'Correct record returned'); |
163 | |
8fe164b9 |
164 | # when using join attribute, make sure slice()ing all objects has same count as all() |
f9db5527 |
165 | $rs = $schema->resultset("CD")->search( |
8fe164b9 |
166 | { 'artist' => 1 }, |
167 | { join => [qw/artist/], order_by => 'artist.name' } |
168 | ); |
169 | cmp_ok( scalar $rs->all, '==', scalar $rs->slice(0, $rs->count - 1), 'slice() with join has same count as all()' ); |
170 | |
2bd9c7c0 |
171 | ok(!$rs->slice($rs->count+1000, $rs->count+1002)->count, |
172 | 'Slicing beyond end of rs returns a zero count'); |
173 | |
f9db5527 |
174 | $rs = $schema->resultset("Artist")->search( |
0567538f |
175 | { 'liner_notes.notes' => 'Kill Yourself!' }, |
176 | { join => { 'cds' => 'liner_notes' } }); |
177 | |
178 | cmp_ok( $rs->count, '==', 1, "Single record in resultset"); |
179 | |
180 | is($rs->first->name, 'We Are Goth', 'Correct record returned'); |
181 | |