has_a works
[dbsrgits/DBIx-Class.git] / t / cdbi-t / 19-set_sql.t
1 use strict;
2 use Test::More;
3
4 BEGIN {
5         eval "use DBD::SQLite";
6         plan $@ ? (skip_all => 'needs DBD::SQLite for testing') : (tests => 17);
7 }
8
9 use lib 't/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
19 my $f1 = Film->create({ title => 'A', director => 'AA', rating => 'PG' });
20 my $f2 = Film->create({ title => 'B', director => 'BA', rating => 'PG' });
21 my $f3 = Film->create({ title => 'C', director => 'AA', rating => '15' });
22 my $f4 = Film->create({ title => 'D', director => 'BA', rating => '18' });
23 my $f5 = Film->create({ title => 'E', director => 'AA', rating => '18' });
24
25 Film->set_sql(
26         pgs => qq{
27         SELECT __ESSENTIAL__
28         FROM   __TABLE__
29         WHERE  __TABLE__.rating = 'PG'
30         ORDER BY title DESC 
31 }
32 );
33
34 {
35         (my $sth = Film->sql_pgs())->execute;
36         my @pgs = Film->sth_to_objects($sth);
37         is @pgs, 2, "Execute our own SQL";
38         is $pgs[0]->id, $f2->id, "get F2";
39         is $pgs[1]->id, $f1->id, "and F1";
40 }
41
42 {
43         my @pgs = Film->search_pgs;
44         is @pgs, 2, "SQL creates search() method";
45         is $pgs[0]->id, $f2->id, "get F2";
46         is $pgs[1]->id, $f1->id, "and F1";
47 };
48
49 Film->set_sql(
50         rating => qq{
51         SELECT __ESSENTIAL__
52         FROM   __TABLE__
53         WHERE  rating = ?
54         ORDER BY title DESC 
55 }
56 );
57
58 {
59         my @pgs = Film->search_rating('18');
60         is @pgs, 2, "Can pass parameters to created search()";
61         is $pgs[0]->id, $f5->id, "F5";
62         is $pgs[1]->id, $f4->id, "and F4";
63 };
64
65 #SKIP: {
66 #  skip "DBIx::Class doesn't have has_a yet", 6;
67 {
68         Actor->has_a(film => "Film");
69         Film->set_sql(
70                 namerate => qq{
71                 SELECT __ESSENTIAL(f)__
72                 FROM   __TABLE(=f)__, __TABLE(Actor=a)__ 
73                 WHERE  __JOIN(a f)__    
74                 AND    a.name LIKE ?
75                 AND    f.rating = ?
76                 ORDER BY title 
77         }
78         );
79
80         my $a1 = Actor->create({ name => "A1", film => $f1 });
81         my $a2 = Actor->create({ name => "A2", film => $f2 });
82         my $a3 = Actor->create({ name => "B1", film => $f1 });
83
84         my @apg = Film->search_namerate("A_", "PG");
85         is @apg, 2, "2 Films with A* that are PG";
86         is $apg[0]->title, "A", "A";
87         is $apg[1]->title, "B", "and B";
88 }
89
90 {    # join in reverse
91         Actor->has_a(film => "Film");
92         Film->set_sql(
93                 ratename => qq{
94                 SELECT __ESSENTIAL(f)__
95                 FROM   __TABLE(=f)__, __TABLE(Actor=a)__ 
96                 WHERE  __JOIN(f a)__    
97                 AND    f.rating = ?
98                 AND    a.name LIKE ?
99                 ORDER BY title 
100         }
101         );
102
103         my @apg = Film->search_ratename(PG => "A_");
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 #} # end SKIP block