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