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; |
8 | |
9 | my $schema = DBICTest->init_schema(); |
10 | |
0eb27426 |
11 | plan tests => 11; |
218aa968 |
12 | |
13 | my $rs = $schema->resultset('CD')->search({}, |
14 | { |
15 | '+select' => \ 'COUNT(*)', |
16 | '+as' => 'count' |
17 | } |
18 | ); |
19 | lives_ok(sub { $rs->first->get_column('count') }, 'additional count rscolumn present'); |
20 | dies_ok(sub { $rs->first->get_column('nonexistent_column') }, 'nonexistant column requests still throw exceptions'); |
21 | |
22 | $rs = $schema->resultset('CD')->search({}, |
23 | { |
24 | '+select' => [ \ 'COUNT(*)', 'title' ], |
25 | '+as' => [ 'count', 'addedtitle' ] |
26 | } |
27 | ); |
28 | lives_ok(sub { $rs->first->get_column('count') }, 'multiple +select/+as columns, 1st rscolumn present'); |
29 | lives_ok(sub { $rs->first->get_column('addedtitle') }, 'multiple +select/+as columns, 2nd rscolumn present'); |
30 | |
31 | $rs = $schema->resultset('CD')->search({}, |
32 | { |
33 | '+select' => [ \ 'COUNT(*)', 'title' ], |
34 | '+as' => [ 'count', 'addedtitle' ] |
35 | } |
36 | )->search({}, |
37 | { |
38 | '+select' => 'title', |
39 | '+as' => 'addedtitle2' |
40 | } |
41 | ); |
42 | lives_ok(sub { $rs->first->get_column('count') }, '+select/+as chained search 1st rscolumn present'); |
43 | lives_ok(sub { $rs->first->get_column('addedtitle') }, '+select/+as chained search 1st rscolumn present'); |
44 | lives_ok(sub { $rs->first->get_column('addedtitle2') }, '+select/+as chained search 3rd rscolumn present'); |
0eb27426 |
45 | |
46 | |
47 | # test the from search attribute (gets between the FROM and WHERE keywords, allows arbitrary subselects) |
48 | # also shows that outer select attributes are ok (i.e. order_by) |
49 | # |
50 | # from doesn't seem to be useful without using a scalarref - there were no initial tests >:( |
51 | # |
0eb27426 |
52 | my $cds = $schema->resultset ('CD')->search ({}, { order_by => 'me.cdid'}); # make sure order is consistent |
53 | cmp_ok ($cds->count, '>', 2, 'Initially populated with more than 2 CDs'); |
54 | |
55 | my $table = $cds->result_source->name; |
56 | my $subsel = $cds->search ({}, { |
57 | columns => [qw/cdid title/], |
58 | from => \ "(SELECT cdid, title FROM $table LIMIT 2) me", |
59 | }); |
60 | |
61 | is ($subsel->count, 2, 'Subselect correctly limited the rs to 2 cds'); |
62 | is ($subsel->next->title, $cds->next->title, 'First CD title match'); |
63 | is ($subsel->next->title, $cds->next->title, 'Second CD title match'); |