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