Commit | Line | Data |
218aa968 |
1 | use strict; |
2 | use warnings; |
3 | |
4 | use Test::More; |
5 | use Test::Exception; |
6 | use lib qw(t/lib); |
7 | use DBICTest; |
faf79003 |
8 | use DBIC::SqlMakerTest; |
218aa968 |
9 | |
10 | my $schema = DBICTest->init_schema(); |
11 | |
c6137661 |
12 | plan tests => 23; |
218aa968 |
13 | |
14 | my $rs = $schema->resultset('CD')->search({}, |
15 | { |
16 | '+select' => \ 'COUNT(*)', |
17 | '+as' => 'count' |
18 | } |
19 | ); |
20 | lives_ok(sub { $rs->first->get_column('count') }, 'additional count rscolumn present'); |
21 | dies_ok(sub { $rs->first->get_column('nonexistent_column') }, 'nonexistant column requests still throw exceptions'); |
22 | |
23 | $rs = $schema->resultset('CD')->search({}, |
24 | { |
25 | '+select' => [ \ 'COUNT(*)', 'title' ], |
26 | '+as' => [ 'count', 'addedtitle' ] |
27 | } |
28 | ); |
29 | lives_ok(sub { $rs->first->get_column('count') }, 'multiple +select/+as columns, 1st rscolumn present'); |
30 | lives_ok(sub { $rs->first->get_column('addedtitle') }, 'multiple +select/+as columns, 2nd rscolumn present'); |
31 | |
121068ec |
32 | # Tests a regression in ResultSetColumn wrt +select |
9dbe8766 |
33 | $rs = $schema->resultset('CD')->search(undef, |
121068ec |
34 | { |
35 | '+select' => [ \'COUNT(*) AS year_count' ], |
36 | order_by => 'year_count' |
37 | } |
38 | ); |
39 | my @counts = $rs->get_column('cdid')->all; |
40 | ok(scalar(@counts), 'got rows from ->all using +select'); |
41 | |
218aa968 |
42 | $rs = $schema->resultset('CD')->search({}, |
43 | { |
44 | '+select' => [ \ 'COUNT(*)', 'title' ], |
45 | '+as' => [ 'count', 'addedtitle' ] |
46 | } |
47 | )->search({}, |
48 | { |
49 | '+select' => 'title', |
50 | '+as' => 'addedtitle2' |
51 | } |
52 | ); |
53 | lives_ok(sub { $rs->first->get_column('count') }, '+select/+as chained search 1st rscolumn present'); |
54 | lives_ok(sub { $rs->first->get_column('addedtitle') }, '+select/+as chained search 1st rscolumn present'); |
55 | lives_ok(sub { $rs->first->get_column('addedtitle2') }, '+select/+as chained search 3rd rscolumn present'); |
0eb27426 |
56 | |
57 | |
58 | # test the from search attribute (gets between the FROM and WHERE keywords, allows arbitrary subselects) |
59 | # also shows that outer select attributes are ok (i.e. order_by) |
60 | # |
61 | # from doesn't seem to be useful without using a scalarref - there were no initial tests >:( |
62 | # |
0eb27426 |
63 | my $cds = $schema->resultset ('CD')->search ({}, { order_by => 'me.cdid'}); # make sure order is consistent |
64 | cmp_ok ($cds->count, '>', 2, 'Initially populated with more than 2 CDs'); |
65 | |
66 | my $table = $cds->result_source->name; |
67 | my $subsel = $cds->search ({}, { |
68 | columns => [qw/cdid title/], |
69 | from => \ "(SELECT cdid, title FROM $table LIMIT 2) me", |
70 | }); |
71 | |
72 | is ($subsel->count, 2, 'Subselect correctly limited the rs to 2 cds'); |
73 | is ($subsel->next->title, $cds->next->title, 'First CD title match'); |
74 | is ($subsel->next->title, $cds->next->title, 'Second CD title match'); |
bbdff861 |
75 | |
76 | is($schema->resultset('CD')->current_source_alias, "me", '$rs->current_source_alias returns "me"'); |
61d26fae |
77 | |
faf79003 |
78 | |
79 | |
80 | $rs = $schema->resultset('CD')->search({}, |
61d26fae |
81 | { |
82 | 'join' => 'artist', |
faf79003 |
83 | 'columns' => ['cdid', 'title', 'artist.name'], |
61d26fae |
84 | } |
85 | ); |
faf79003 |
86 | |
87 | my @query = @${$rs->as_query}; |
c6137661 |
88 | TODO: { local $TODO = 'as_query() inconsistent'; is (scalar @query, 2, 'as_query() returned empty bindval arrayref') || push @query, [] } |
faf79003 |
89 | |
90 | is_same_sql_bind ( |
91 | @query, |
faf79003 |
92 | '(SELECT me.cdid, me.title, artist.name FROM cd me JOIN artist artist ON artist.artistid = me.artist)', |
93 | [], |
94 | 'Use of columns attribute results in proper sql' |
95 | ); |
96 | |
97 | lives_ok(sub { |
98 | $rs->first->get_column('cdid') |
99 | }, 'columns 1st rscolumn present'); |
100 | |
101 | lives_ok(sub { |
102 | $rs->first->get_column('title') |
103 | }, 'columns 2nd rscolumn present'); |
104 | |
4c253752 |
105 | TODO: { |
106 | local $TODO = "Need to remove '.' from accessors"; |
107 | # I think this is too much dwis #ribasushi |
108 | lives_ok(sub { |
109 | $rs->first->get_column("artist.name") |
110 | }, 'columns 3rd rscolumn present'); |
111 | } |
112 | |
113 | |
faf79003 |
114 | |
115 | $rs = $schema->resultset('CD')->search({}, |
116 | { |
117 | 'join' => 'artist', |
118 | '+columns' => ['cdid', 'title', 'artist.name'], |
119 | } |
120 | ); |
121 | |
122 | @query = @${$rs->as_query}; |
c6137661 |
123 | TODO: { local $TODO = 'as_query() inconsistent'; is (scalar @query, 2, 'as_query() returned empty bindval arrayref') || push @query, [] } |
faf79003 |
124 | |
125 | is_same_sql_bind ( |
126 | @query, |
faf79003 |
127 | '(SELECT me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track, me.cdid, me.title, artist.name FROM cd me JOIN artist artist ON artist.artistid = me.artist)', |
128 | [], |
129 | 'Use of columns attribute results in proper sql' |
130 | ); |
131 | |
132 | lives_ok(sub { |
133 | $rs->first->get_column('cdid') |
134 | }, 'columns 1st rscolumn present'); |
135 | |
136 | lives_ok(sub { |
137 | $rs->first->get_column('title') |
138 | }, 'columns 2nd rscolumn present'); |
4c253752 |
139 | |
140 | TODO: { |
141 | local $TODO = "Need to remove '.' from accessors"; |
142 | # I think this is too much dwis #ribasushi |
143 | lives_ok(sub { |
144 | $rs->first->get_column("artist.name") |
145 | }, 'columns 3rd rscolumn present'); |
146 | } |