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