Added { -desc => 'foo' } order by support
[scpubgit/Q-Branch.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 my $sa = new SQL::Abstract;
13
14 my @j = (
15     { child => 'person' },
16     [ { father => 'person' }, { 'father.person_id' => 'child.father_id' }, ],
17     [ { mother => 'person' }, { 'mother.person_id' => 'child.mother_id' } ],
18 );
19 my $match = 'person child JOIN person father ON ( father.person_id = '
20           . 'child.father_id ) JOIN person mother ON ( mother.person_id '
21           . '= child.mother_id )'
22           ;
23 is( $sa->_recurse_from(@j), $match, 'join 1 ok' );
24
25 my @j2 = (
26     { mother => 'person' },
27     [   [   { child => 'person' },
28             [   { father             => 'person' },
29                 { 'father.person_id' => 'child.father_id' }
30             ]
31         ],
32         { 'mother.person_id' => 'child.mother_id' }
33     ],
34 );
35 $match = 'person mother JOIN (person child JOIN person father ON ('
36        . ' father.person_id = child.father_id )) ON ( mother.person_id = '
37        . 'child.mother_id )'
38        ;
39 is( $sa->_recurse_from(@j2), $match, 'join 2 ok' );
40
41 my @j3 = (
42     { child => 'person' },
43     [ { father => 'person', -join_type => 'inner' }, { 'father.person_id' => 'child.father_id' }, ],
44     [ { mother => 'person', -join_type => 'inner'  }, { 'mother.person_id' => 'child.mother_id' } ],
45 );
46 $match = 'person child INNER JOIN person father ON ( father.person_id = '
47           . 'child.father_id ) INNER JOIN person mother ON ( mother.person_id '
48           . '= child.mother_id )'
49           ;
50
51 is( $sa->_recurse_from(@j3), $match, 'join 3 (inner join) ok');
52
53 my @j4 = (
54     { mother => 'person' },
55     [   [   { child => 'person', -join_type => 'left' },
56             [   { father             => 'person', -join_type => 'right' },
57                 { 'father.person_id' => 'child.father_id' }
58             ]
59         ],
60         { 'mother.person_id' => 'child.mother_id' }
61     ],
62 );
63 $match = 'person mother LEFT JOIN (person child RIGHT JOIN person father ON ('
64        . ' father.person_id = child.father_id )) ON ( mother.person_id = '
65        . 'child.mother_id )'
66        ;
67 is( $sa->_recurse_from(@j4), $match, 'join 4 (nested joins + join types) ok');