Reorganize CDBICompat tests - centralize prereq checks in one place
[dbsrgits/DBIx-Class.git] / t / cdbi / 19-set_sql.t
CommitLineData
510ca912 1use strict;
2use Test::More;
3
50891152 4use lib 't/cdbi/testlib';
510ca912 5use Film;
6use Actor;
7
8{ # Check __ESSENTIAL__ expansion (RT#13038)
6a3bf251 9 my @cols = Film->columns('Essential');
10 is_deeply \@cols, ['title'], "1 Column in essential";
11 is +Film->transform_sql('__ESSENTIAL__'), 'title', '__ESSENTIAL__ expansion';
8273e845 12
6a3bf251 13 # This provides a more interesting test
14 Film->columns(Essential => qw(title rating));
15 is +Film->transform_sql('__ESSENTIAL__'), 'title, rating',
16 'multi-col __ESSENTIAL__ expansion';
510ca912 17}
18
19my $f1 = Film->create({ title => 'A', director => 'AA', rating => 'PG' });
20my $f2 = Film->create({ title => 'B', director => 'BA', rating => 'PG' });
21my $f3 = Film->create({ title => 'C', director => 'AA', rating => '15' });
22my $f4 = Film->create({ title => 'D', director => 'BA', rating => '18' });
23my $f5 = Film->create({ title => 'E', director => 'AA', rating => '18' });
24
25Film->set_sql(
6a3bf251 26 pgs => qq{
27 SELECT __ESSENTIAL__
28 FROM __TABLE__
29 WHERE __TABLE__.rating = 'PG'
8273e845 30 ORDER BY title DESC
510ca912 31}
32);
33
34{
6a3bf251 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";
510ca912 40}
41
42{
6a3bf251 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";
510ca912 47};
48
49Film->set_sql(
6a3bf251 50 rating => qq{
51 SELECT __ESSENTIAL__
52 FROM __TABLE__
53 WHERE rating = ?
8273e845 54 ORDER BY title DESC
510ca912 55}
56);
57
58{
6a3bf251 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";
510ca912 63};
64
65{
e60dc79f 66 Film->set_sql(
67 by_id => qq{
68 SELECT __ESSENTIAL__
69 FROM __TABLE__
70 WHERE __IDENTIFIER__
71 }
72 );
8273e845 73
e60dc79f 74 my $film = Film->retrieve_all->first;
75 my @found = Film->search_by_id($film->id);
76 is @found, 1;
77 is $found[0]->id, $film->id;
78}
79
80
81{
6a3bf251 82 Actor->has_a(film => "Film");
83 Film->set_sql(
84 namerate => qq{
85 SELECT __ESSENTIAL(f)__
8273e845 86 FROM __TABLE(=f)__, __TABLE(Actor=a)__
87 WHERE __JOIN(a f)__
6a3bf251 88 AND a.name LIKE ?
89 AND f.rating = ?
8273e845 90 ORDER BY title
6a3bf251 91 }
92 );
93
94 my $a1 = Actor->create({ name => "A1", film => $f1 });
95 my $a2 = Actor->create({ name => "A2", film => $f2 });
96 my $a3 = Actor->create({ name => "B1", film => $f1 });
97
98 my @apg = Film->search_namerate("A_", "PG");
99 is @apg, 2, "2 Films with A* that are PG";
100 is $apg[0]->title, "A", "A";
101 is $apg[1]->title, "B", "and B";
510ca912 102}
103
104{ # join in reverse
6a3bf251 105 Actor->has_a(film => "Film");
106 Film->set_sql(
107 ratename => qq{
108 SELECT __ESSENTIAL(f)__
8273e845 109 FROM __TABLE(=f)__, __TABLE(Actor=a)__
110 WHERE __JOIN(f a)__
6a3bf251 111 AND f.rating = ?
112 AND a.name LIKE ?
8273e845 113 ORDER BY title
6a3bf251 114 }
115 );
116
117 my @apg = Film->search_ratename(PG => "A_");
118 is @apg, 2, "2 Films with A* that are PG";
119 is $apg[0]->title, "A", "A";
120 is $apg[1]->title, "B", "and B";
510ca912 121}
122
d9bd5195 123done_testing;