Switch the main dev branch back to 'master'
[dbsrgits/DBIx-Class.git] / t / sqlmaker / legacy_joins.t
1 use strict;
2 use warnings;
3
4 use Test::More;
5 use lib qw(t/lib);
6 use DBICTest ':DiffSQL';
7 use DBIx::Class::_Util 'sigwarn_silencer';
8
9 use DBIx::Class::SQLMaker;
10 my $sa = DBIx::Class::SQLMaker->new;
11
12 $SIG{__WARN__} = sigwarn_silencer( qr/\Q{from} structures with conditions not conforming to the SQL::Abstract syntax are deprecated/ );
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_same_sql(
24   $sa->_recurse_from(@j),
25   $match,
26   'join 1 ok'
27 );
28
29 my @j2 = (
30     { mother => 'person' },
31     [   [   { child => 'person' },
32             [   { father             => 'person' },
33                 { 'father.person_id' => 'child.father_id' }
34             ]
35         ],
36         { 'mother.person_id' => 'child.mother_id' }
37     ],
38 );
39 $match = 'person mother JOIN (person child JOIN person father ON ('
40        . ' father.person_id = child.father_id )) ON ( mother.person_id = '
41        . 'child.mother_id )'
42        ;
43 is_same_sql(
44   $sa->_recurse_from(@j2),
45   $match,
46   'join 2 ok'
47 );
48
49 my @j3 = (
50     { child => 'person' },
51     [ { father => 'person', -join_type => 'inner' }, { 'father.person_id' => 'child.father_id' }, ],
52     [ { mother => 'person', -join_type => 'inner'  }, { 'mother.person_id' => 'child.mother_id' } ],
53 );
54 $match = 'person child INNER JOIN person father ON ( father.person_id = '
55           . 'child.father_id ) INNER JOIN person mother ON ( mother.person_id '
56           . '= child.mother_id )'
57           ;
58
59 is_same_sql(
60   $sa->_recurse_from(@j3),
61   $match,
62   'join 3 (inner join) ok'
63 );
64
65 my @j4 = (
66     { mother => 'person' },
67     [   [   { child => 'person', -join_type => 'left' },
68             [   { father             => 'person', -join_type => 'right' },
69                 { 'father.person_id' => 'child.father_id' }
70             ]
71         ],
72         { 'mother.person_id' => 'child.mother_id' }
73     ],
74 );
75 $match = 'person mother LEFT JOIN (person child RIGHT JOIN person father ON ('
76        . ' father.person_id = child.father_id )) ON ( mother.person_id = '
77        . 'child.mother_id )'
78        ;
79 is_same_sql(
80   $sa->_recurse_from(@j4),
81   $match,
82   'join 4 (nested joins + join types) ok'
83 );
84
85 my @j5 = (
86     { child => 'person' },
87     [ { father => 'person' }, { 'father.person_id' => \'!= child.father_id' }, ],
88     [ { mother => 'person' }, { 'mother.person_id' => 'child.mother_id' } ],
89 );
90 $match = 'person child JOIN person father ON ( father.person_id != '
91           . 'child.father_id ) JOIN person mother ON ( mother.person_id '
92           . '= child.mother_id )'
93           ;
94 is_same_sql(
95   $sa->_recurse_from(@j5),
96   $match,
97   'join 5 (SCALAR reference for ON statement) ok'
98 );
99
100 done_testing;