added patch from Guillermo to cleanup in/between,
[scpubgit/Q-Branch.git] / t / 04from.t
CommitLineData
83cab70b 1#!/usr/bin/perl -I. -w
2
3use strict;
4use vars qw($TESTING);
5$TESTING = 1;
6use Test;
7
8# use a BEGIN block so we print our plan before SQL::Abstract is loaded
9BEGIN { plan tests => 4 }
10
11use SQL::Abstract;
12
13sub is {
14 my ($got, $expect, $msg) = @_;
15 ok($got eq $expect) or
16 warn "got [${got}]\ninstead of [${expect}]\nfor test ${msg}\n\n";
17}
18
19my $sa = new SQL::Abstract;
20
21my @j = (
22 { child => 'person' },
23 [ { father => 'person' }, { 'father.person_id' => 'child.father_id' }, ],
24 [ { mother => 'person' }, { 'mother.person_id' => 'child.mother_id' } ],
25);
26my $match = 'person child JOIN person father ON ( father.person_id = '
27 . 'child.father_id ) JOIN person mother ON ( mother.person_id '
28 . '= child.mother_id )'
29 ;
30is( $sa->_recurse_from(@j), $match, 'join 1 ok' );
31
32my @j2 = (
33 { mother => 'person' },
34 [ [ { child => 'person' },
35 [ { father => 'person' },
36 { 'father.person_id' => 'child.father_id' }
37 ]
38 ],
39 { 'mother.person_id' => 'child.mother_id' }
40 ],
41);
42$match = 'person mother JOIN (person child JOIN person father ON ('
43 . ' father.person_id = child.father_id )) ON ( mother.person_id = '
44 . 'child.mother_id )'
45 ;
46is( $sa->_recurse_from(@j2), $match, 'join 2 ok' );
47
48my @j3 = (
49 { child => 'person' },
50 [ { father => 'person', -join_type => 'inner' }, { 'father.person_id' => 'child.father_id' }, ],
51 [ { mother => 'person', -join_type => 'inner' }, { 'mother.person_id' => 'child.mother_id' } ],
52);
53$match = 'person child INNER JOIN person father ON ( father.person_id = '
54 . 'child.father_id ) INNER JOIN person mother ON ( mother.person_id '
55 . '= child.mother_id )'
56 ;
57
58is( $sa->_recurse_from(@j3), $match, 'join 3 (inner join) ok');
59
60my @j4 = (
61 { mother => 'person' },
62 [ [ { child => 'person', -join_type => 'left' },
63 [ { father => 'person', -join_type => 'right' },
64 { 'father.person_id' => 'child.father_id' }
65 ]
66 ],
67 { 'mother.person_id' => 'child.mother_id' }
68 ],
69);
70$match = 'person mother LEFT JOIN (person child RIGHT JOIN person father ON ('
71 . ' father.person_id = child.father_id )) ON ( mother.person_id = '
72 . 'child.mother_id )'
73 ;
74is( $sa->_recurse_from(@j4), $match, 'join 4 (nested joins + join types) ok');