abstract from code imported from DBIC::SQL::Abstract plus tests
[dbsrgits/SQL-Abstract.git] / t / 04from.t
1 #!/usr/bin/perl -I. -w
2
3 use strict;
4 use vars qw($TESTING);
5 $TESTING = 1;
6 use Test;
7
8 # use a BEGIN block so we print our plan before SQL::Abstract is loaded
9 BEGIN { plan tests => 4 }
10
11 use SQL::Abstract;
12
13 sub 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
19 my $sa = new SQL::Abstract;
20
21 my @j = (
22     { child => 'person' },
23     [ { father => 'person' }, { 'father.person_id' => 'child.father_id' }, ],
24     [ { mother => 'person' }, { 'mother.person_id' => 'child.mother_id' } ],
25 );
26 my $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           ;
30 is( $sa->_recurse_from(@j), $match, 'join 1 ok' );
31
32 my @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        ;
46 is( $sa->_recurse_from(@j2), $match, 'join 2 ok' );
47
48 my @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
58 is( $sa->_recurse_from(@j3), $match, 'join 3 (inner join) ok');
59
60 my @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        ;
74 is( $sa->_recurse_from(@j4), $match, 'join 4 (nested joins + join types) ok');