Introduce GOVERNANCE document and empty RESOLUTIONS file.
[dbsrgits/DBIx-Class.git] / t / cdbi / 19-set_sql.t
CommitLineData
c0329273 1BEGIN { do "./t/lib/ANFANG.pm" or die ( $@ || $! ) }
83eef562 2use DBIx::Class::Optional::Dependencies -skip_all_without => 'cdbicompat';
3
510ca912 4use strict;
4a233f30 5use warnings;
83eef562 6
510ca912 7use Test::More;
8
50891152 9use lib 't/cdbi/testlib';
510ca912 10use Film;
11use Actor;
12
13{ # Check __ESSENTIAL__ expansion (RT#13038)
6a3bf251 14 my @cols = Film->columns('Essential');
15 is_deeply \@cols, ['title'], "1 Column in essential";
16 is +Film->transform_sql('__ESSENTIAL__'), 'title', '__ESSENTIAL__ expansion';
8273e845 17
6a3bf251 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';
510ca912 22}
23
24my $f1 = Film->create({ title => 'A', director => 'AA', rating => 'PG' });
25my $f2 = Film->create({ title => 'B', director => 'BA', rating => 'PG' });
26my $f3 = Film->create({ title => 'C', director => 'AA', rating => '15' });
27my $f4 = Film->create({ title => 'D', director => 'BA', rating => '18' });
28my $f5 = Film->create({ title => 'E', director => 'AA', rating => '18' });
29
30Film->set_sql(
6a3bf251 31 pgs => qq{
32 SELECT __ESSENTIAL__
33 FROM __TABLE__
34 WHERE __TABLE__.rating = 'PG'
8273e845 35 ORDER BY title DESC
510ca912 36}
37);
38
39{
6a3bf251 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";
510ca912 45}
46
47{
6a3bf251 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";
510ca912 52};
53
54Film->set_sql(
6a3bf251 55 rating => qq{
56 SELECT __ESSENTIAL__
57 FROM __TABLE__
58 WHERE rating = ?
8273e845 59 ORDER BY title DESC
510ca912 60}
61);
62
63{
6a3bf251 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";
510ca912 68};
69
70{
e60dc79f 71 Film->set_sql(
72 by_id => qq{
73 SELECT __ESSENTIAL__
74 FROM __TABLE__
75 WHERE __IDENTIFIER__
76 }
77 );
8273e845 78
e60dc79f 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{
6a3bf251 87 Actor->has_a(film => "Film");
88 Film->set_sql(
89 namerate => qq{
90 SELECT __ESSENTIAL(f)__
8273e845 91 FROM __TABLE(=f)__, __TABLE(Actor=a)__
92 WHERE __JOIN(a f)__
6a3bf251 93 AND a.name LIKE ?
94 AND f.rating = ?
8273e845 95 ORDER BY title
6a3bf251 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";
510ca912 107}
108
109{ # join in reverse
6a3bf251 110 Actor->has_a(film => "Film");
111 Film->set_sql(
112 ratename => qq{
113 SELECT __ESSENTIAL(f)__
8273e845 114 FROM __TABLE(=f)__, __TABLE(Actor=a)__
115 WHERE __JOIN(f a)__
6a3bf251 116 AND f.rating = ?
117 AND a.name LIKE ?
8273e845 118 ORDER BY title
6a3bf251 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";
510ca912 126}
127
d9bd5195 128done_testing;