File: select_chains_unbalanced.t

package info (click to toggle)
libdbix-class-perl 0.082844-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 5,320 kB
  • sloc: perl: 27,215; sql: 322; sh: 29; makefile: 16
file content (137 lines) | stat: -rw-r--r-- 3,608 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
use strict;
use warnings;

use Test::More;
use Test::Exception;

use lib qw(t/lib);
use DBICTest ':DiffSQL';

my $schema = DBICTest->init_schema();

my $multicol_rs = $schema->resultset('Artist')->search({ artistid => \'1' }, { columns => [qw/name rank/] });

my @chain = (
  {
    select      => 'cdid',
    as          => 'cd_id',
    columns     => [ 'title' ],
  } => 'SELECT
          me.title,
          me.cdid
        FROM cd me'
    => [qw/title cd_id/],

  {
    '+select'   => \ 'DISTINCT(foo, bar)',
    '+as'       => [qw/foo bar/],
  } => 'SELECT
          me.title,
          me.cdid,
          DISTINCT(foo, bar)
        FROM cd me'
    => [qw/title cd_id foo bar/],

  {
    '+select'   => [ 'genreid', $multicol_rs->as_query ],
    '+as'       => [qw/genreid name rank/],
  } => 'SELECT
          me.title,
          me.cdid,
          DISTINCT(foo, bar),
          me.genreid,
          (SELECT me.name, me.rank FROM artist me WHERE ( artistid 1 ))
        FROM cd me'
    => [qw/title cd_id foo bar genreid name rank/],

  {
    '+select'   => { count => 'me.cdid', -as => 'cnt' },  # lack of 'as' infers from '-as'
    '+columns'  => { len => { length => 'me.title' } },
  } => 'SELECT
          me.title,
          LENGTH( me.title ),
          me.cdid,
          DISTINCT(foo, bar),
          me.genreid,
          (SELECT me.name, me.rank FROM artist me WHERE ( artistid 1 )),
          COUNT( me.cdid ) AS cnt
        FROM cd me'
    => [qw/title len cd_id foo bar genreid name rank cnt/],
  {
    '+select'   => \'unaliased randomness',
  } => 'SELECT
          me.title,
          LENGTH( me.title ),
          me.cdid,
          DISTINCT(foo, bar),
          me.genreid,
          (SELECT me.name, me.rank FROM artist me WHERE ( artistid 1 )),
          COUNT( me.cdid ) AS cnt,
          unaliased randomness
        FROM cd me'
    => [qw/title len cd_id foo bar genreid name rank cnt/],
  {
    '+select'   => \'MOAR unaliased randomness',
  } => 'SELECT
          me.title,
          LENGTH( me.title ),
          me.cdid,
          DISTINCT(foo, bar),
          me.genreid,
          (SELECT me.name, me.rank FROM artist me WHERE ( artistid 1 )),
          COUNT( me.cdid ) AS cnt,
          unaliased randomness,
          MOAR unaliased randomness
        FROM cd me'
    => [qw/title len cd_id foo bar genreid name rank cnt/],
);

my $rs = $schema->resultset('CD');

my $testno = 1;
while (@chain) {
  my $attrs = shift @chain;
  my $sql = shift @chain;
  my $as = shift @chain;

  $rs = $rs->search ({}, $attrs);

  is_same_sql_bind (
    $rs->as_query,
    "($sql)",
    [],
    "Test $testno of SELECT assembly ok",
  );

  is_deeply(
    $rs->_resolved_attrs->{as},
    $as,
    "Correct dbic-side aliasing for test $testno",
  );

  $testno++;
}

# make sure proper exceptions are thrown on unbalanced use
{
  my $rs = $schema->resultset('CD')->search({}, { select => \'count(me.cdid)'});

  lives_ok(sub {
    $rs->search({}, { '+select' => 'me.cdid' })->next
  }, 'Two dark selectors are ok');

  throws_ok(sub {
    $rs->search({}, { '+select' => 'me.cdid', '+as' => 'cdid' })->next
  }, qr/resultset contains an unnamed selector/, 'Unnamed followed by named is not');

  throws_ok(sub {
    $rs->search_rs({}, { prefetch => 'tracks' })->next
  }, qr/resultset contains an unnamed selector/, 'Throw on unaliased selector followed by prefetch');

  throws_ok(sub {
    $rs->search_rs({}, { '+select' => 'me.title', '+as' => 'title'  })->next
  }, qr/resultset contains an unnamed selector/, 'Throw on unaliased selector followed by +select/+as');
}


done_testing;