Add strict/warnings test, adjust all offenders (wow, that was a lot)
[dbsrgits/DBIx-Class.git] / t / cdbi / 19-set_sql.t
CommitLineData
510ca912 1use strict;
4a233f30 2use warnings;
510ca912 3use Test::More;
4
50891152 5use lib 't/cdbi/testlib';
510ca912 6use Film;
7use Actor;
8
9{ # Check __ESSENTIAL__ expansion (RT#13038)
6a3bf251 10 my @cols = Film->columns('Essential');
11 is_deeply \@cols, ['title'], "1 Column in essential";
12 is +Film->transform_sql('__ESSENTIAL__'), 'title', '__ESSENTIAL__ expansion';
8273e845 13
6a3bf251 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';
510ca912 18}
19
20my $f1 = Film->create({ title => 'A', director => 'AA', rating => 'PG' });
21my $f2 = Film->create({ title => 'B', director => 'BA', rating => 'PG' });
22my $f3 = Film->create({ title => 'C', director => 'AA', rating => '15' });
23my $f4 = Film->create({ title => 'D', director => 'BA', rating => '18' });
24my $f5 = Film->create({ title => 'E', director => 'AA', rating => '18' });
25
26Film->set_sql(
6a3bf251 27 pgs => qq{
28 SELECT __ESSENTIAL__
29 FROM __TABLE__
30 WHERE __TABLE__.rating = 'PG'
8273e845 31 ORDER BY title DESC
510ca912 32}
33);
34
35{
6a3bf251 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";
510ca912 41}
42
43{
6a3bf251 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";
510ca912 48};
49
50Film->set_sql(
6a3bf251 51 rating => qq{
52 SELECT __ESSENTIAL__
53 FROM __TABLE__
54 WHERE rating = ?
8273e845 55 ORDER BY title DESC
510ca912 56}
57);
58
59{
6a3bf251 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";
510ca912 64};
65
66{
e60dc79f 67 Film->set_sql(
68 by_id => qq{
69 SELECT __ESSENTIAL__
70 FROM __TABLE__
71 WHERE __IDENTIFIER__
72 }
73 );
8273e845 74
e60dc79f 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{
6a3bf251 83 Actor->has_a(film => "Film");
84 Film->set_sql(
85 namerate => qq{
86 SELECT __ESSENTIAL(f)__
8273e845 87 FROM __TABLE(=f)__, __TABLE(Actor=a)__
88 WHERE __JOIN(a f)__
6a3bf251 89 AND a.name LIKE ?
90 AND f.rating = ?
8273e845 91 ORDER BY title
6a3bf251 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";
510ca912 103}
104
105{ # join in reverse
6a3bf251 106 Actor->has_a(film => "Film");
107 Film->set_sql(
108 ratename => qq{
109 SELECT __ESSENTIAL(f)__
8273e845 110 FROM __TABLE(=f)__, __TABLE(Actor=a)__
111 WHERE __JOIN(f a)__
6a3bf251 112 AND f.rating = ?
113 AND a.name LIKE ?
8273e845 114 ORDER BY title
6a3bf251 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";
510ca912 122}
123
d9bd5195 124done_testing;