Fix busted get_column when using +select (and friends)
[dbsrgits/DBIx-Class.git] / t / 76select.t
CommitLineData
218aa968 1use strict;
2use warnings;
3
4use Test::More;
5use Test::Exception;
6use lib qw(t/lib);
7use DBICTest;
8
9my $schema = DBICTest->init_schema();
10
121068ec 11plan tests => 13;
218aa968 12
13my $rs = $schema->resultset('CD')->search({},
14 {
15 '+select' => \ 'COUNT(*)',
16 '+as' => 'count'
17 }
18);
19lives_ok(sub { $rs->first->get_column('count') }, 'additional count rscolumn present');
20dies_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);
28lives_ok(sub { $rs->first->get_column('count') }, 'multiple +select/+as columns, 1st rscolumn present');
29lives_ok(sub { $rs->first->get_column('addedtitle') }, 'multiple +select/+as columns, 2nd rscolumn present');
30
121068ec 31# Tests a regression in ResultSetColumn wrt +select
32my $rs = $schema->resultset('CD')->search(undef,
33 {
34 '+select' => [ \'COUNT(*) AS year_count' ],
35 order_by => 'year_count'
36 }
37);
38my @counts = $rs->get_column('cdid')->all;
39ok(scalar(@counts), 'got rows from ->all using +select');
40
218aa968 41$rs = $schema->resultset('CD')->search({},
42 {
43 '+select' => [ \ 'COUNT(*)', 'title' ],
44 '+as' => [ 'count', 'addedtitle' ]
45 }
46)->search({},
47 {
48 '+select' => 'title',
49 '+as' => 'addedtitle2'
50 }
51);
52lives_ok(sub { $rs->first->get_column('count') }, '+select/+as chained search 1st rscolumn present');
53lives_ok(sub { $rs->first->get_column('addedtitle') }, '+select/+as chained search 1st rscolumn present');
54lives_ok(sub { $rs->first->get_column('addedtitle2') }, '+select/+as chained search 3rd rscolumn present');
0eb27426 55
56
57# test the from search attribute (gets between the FROM and WHERE keywords, allows arbitrary subselects)
58# also shows that outer select attributes are ok (i.e. order_by)
59#
60# from doesn't seem to be useful without using a scalarref - there were no initial tests >:(
61#
0eb27426 62my $cds = $schema->resultset ('CD')->search ({}, { order_by => 'me.cdid'}); # make sure order is consistent
63cmp_ok ($cds->count, '>', 2, 'Initially populated with more than 2 CDs');
64
65my $table = $cds->result_source->name;
66my $subsel = $cds->search ({}, {
67 columns => [qw/cdid title/],
68 from => \ "(SELECT cdid, title FROM $table LIMIT 2) me",
69});
70
71is ($subsel->count, 2, 'Subselect correctly limited the rs to 2 cds');
72is ($subsel->next->title, $cds->next->title, 'First CD title match');
73is ($subsel->next->title, $cds->next->title, 'Second CD title match');
bbdff861 74
75is($schema->resultset('CD')->current_source_alias, "me", '$rs->current_source_alias returns "me"');