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