Merge the relationship resolution rework
[dbsrgits/DBIx-Class.git] / t / cdbi / 19-set_sql.t
1 BEGIN { do "./t/lib/ANFANG.pm" or die ( $@ || $! ) }
2 use DBIx::Class::Optional::Dependencies -skip_all_without => 'cdbicompat';
3
4 use strict;
5 use warnings;
6
7 use Test::More;
8
9 use lib 't/cdbi/testlib';
10 use Film;
11 use Actor;
12
13 { # Check __ESSENTIAL__ expansion (RT#13038)
14   my @cols = Film->columns('Essential');
15   is_deeply \@cols, ['title'], "1 Column in essential";
16   is +Film->transform_sql('__ESSENTIAL__'), 'title', '__ESSENTIAL__ expansion';
17
18   # This provides a more interesting test
19   Film->columns(Essential => qw(title rating));
20   is +Film->transform_sql('__ESSENTIAL__'), 'title, rating',
21       'multi-col __ESSENTIAL__ expansion';
22 }
23
24 my $f1 = Film->create({ title => 'A', director => 'AA', rating => 'PG' });
25 my $f2 = Film->create({ title => 'B', director => 'BA', rating => 'PG' });
26 my $f3 = Film->create({ title => 'C', director => 'AA', rating => '15' });
27 my $f4 = Film->create({ title => 'D', director => 'BA', rating => '18' });
28 my $f5 = Film->create({ title => 'E', director => 'AA', rating => '18' });
29
30 Film->set_sql(
31   pgs => qq{
32   SELECT __ESSENTIAL__
33   FROM   __TABLE__
34   WHERE  __TABLE__.rating = 'PG'
35   ORDER BY title DESC
36 }
37 );
38
39 {
40   (my $sth = Film->sql_pgs())->execute;
41   my @pgs = Film->sth_to_objects($sth);
42   is @pgs, 2, "Execute our own SQL";
43   is $pgs[0]->id, $f2->id, "get F2";
44   is $pgs[1]->id, $f1->id, "and F1";
45 }
46
47 {
48   my @pgs = Film->search_pgs;
49   is @pgs, 2, "SQL creates search() method";
50   is $pgs[0]->id, $f2->id, "get F2";
51   is $pgs[1]->id, $f1->id, "and F1";
52 };
53
54 Film->set_sql(
55   rating => qq{
56   SELECT __ESSENTIAL__
57   FROM   __TABLE__
58   WHERE  rating = ?
59   ORDER BY title DESC
60 }
61 );
62
63 {
64   my @pgs = Film->search_rating('18');
65   is @pgs, 2, "Can pass parameters to created search()";
66   is $pgs[0]->id, $f5->id, "F5";
67   is $pgs[1]->id, $f4->id, "and F4";
68 };
69
70 {
71     Film->set_sql(
72         by_id => qq{
73             SELECT  __ESSENTIAL__
74             FROM    __TABLE__
75             WHERE   __IDENTIFIER__
76         }
77     );
78
79     my $film = Film->retrieve_all->first;
80     my @found = Film->search_by_id($film->id);
81     is @found, 1;
82     is $found[0]->id, $film->id;
83 }
84
85
86 {
87   Actor->has_a(film => "Film");
88   Film->set_sql(
89     namerate => qq{
90     SELECT __ESSENTIAL(f)__
91     FROM   __TABLE(=f)__, __TABLE(Actor=a)__
92     WHERE  __JOIN(a f)__
93     AND    a.name LIKE ?
94     AND    f.rating = ?
95     ORDER BY title
96   }
97   );
98
99   my $a1 = Actor->create({ name => "A1", film => $f1 });
100   my $a2 = Actor->create({ name => "A2", film => $f2 });
101   my $a3 = Actor->create({ name => "B1", film => $f1 });
102
103   my @apg = Film->search_namerate("A_", "PG");
104   is @apg, 2, "2 Films with A* that are PG";
105   is $apg[0]->title, "A", "A";
106   is $apg[1]->title, "B", "and B";
107 }
108
109 {    # join in reverse
110   Actor->has_a(film => "Film");
111   Film->set_sql(
112     ratename => qq{
113     SELECT __ESSENTIAL(f)__
114     FROM   __TABLE(=f)__, __TABLE(Actor=a)__
115     WHERE  __JOIN(f a)__
116     AND    f.rating = ?
117     AND    a.name LIKE ?
118     ORDER BY title
119   }
120   );
121
122   my @apg = Film->search_ratename(PG => "A_");
123   is @apg, 2, "2 Films with A* that are PG";
124   is $apg[0]->title, "A", "A";
125   is $apg[1]->title, "B", "and B";
126 }
127
128 done_testing;