File: select_chains.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 (173 lines) | stat: -rw-r--r-- 4,634 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
use strict;
use warnings;

use Test::More;

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

my $schema = DBICTest->init_schema();

my @chain = (
  {
    columns     => [ 'cdid' ],
    '+columns'  => [ { title_lc => { lower => 'title', -as => 'lctitle' } } ],
    '+select'   => [ 'genreid' ],
    '+as'       => [ 'genreid' ],
  } => 'SELECT me.cdid, LOWER( title ) AS lctitle, me.genreid FROM cd me',

  {
    '+columns'  => [ { max_year => { max => 'me.year', -as => 'last_y' }}, ],
    '+select'   => [ { count => 'me.cdid' }, ],
    '+as'       => [ 'cnt' ],
  } => 'SELECT me.cdid, LOWER( title ) AS lctitle, MAX( me.year ) AS last_y, me.genreid, COUNT( me.cdid ) FROM cd me',

  {
    select      => [ { min => 'me.cdid' }, ],
    as          => [ 'min_id' ],
  } => 'SELECT MIN( me.cdid ) FROM cd me',

  {
    '+columns' => [ { cnt => { count => 'cdid', -as => 'cnt' } } ],
  } => 'SELECT COUNT ( cdid ) AS cnt, MIN( me.cdid ) FROM cd me',

  {
    columns => [ { foo => { coalesce => [qw/a b c/], -as => 'firstfound' } }  ],
  } => 'SELECT COALESCE( a, b, c ) AS firstfound FROM cd me',

  {
    '+columns' => [ 'me.year' ],
    '+select' => [ { max => 'me.year', -as => 'last_y' } ],
    '+as' => [ 'ly' ],
  } => 'SELECT COALESCE( a, b, c ) AS firstfound, me.year, MAX( me.year ) AS last_y FROM cd me',

  {
    '+select'   => [ { count => 'me.cdid', -as => 'cnt' } ],
    '+as'       => [ 'cnt' ],
  } => 'SELECT COALESCE( a, b, c ) AS firstfound, me.year, MAX( me.year ) AS last_y, COUNT( me.cdid ) AS cnt FROM cd me',

  # adding existing stuff should not alter selector
  {
    '+select'   => [ 'me.year' ],
    '+as'       => [ 'year' ],
  } => 'SELECT COALESCE( a, b, c ) AS firstfound, me.year, MAX( me.year ) AS last_y, COUNT( me.cdid ) AS cnt, me.year FROM cd me',

  {
    '+columns'   => [ 'me.year' ],
  } => 'SELECT COALESCE( a, b, c ) AS firstfound, me.year, MAX( me.year ) AS last_y, COUNT( me.cdid ) AS cnt, me.year FROM cd me',

  {
    '+columns'   => 'me.year',
  } => 'SELECT COALESCE( a, b, c ) AS firstfound, me.year, MAX( me.year ) AS last_y, COUNT( me.cdid ) AS cnt, me.year FROM cd me',

  # naked selector at the end should just work
  {
    '+select'   => 'me.moar_stuff',
  } => 'SELECT COALESCE( a, b, c ) AS firstfound, me.year, MAX( me.year ) AS last_y, COUNT( me.cdid ) AS cnt, me.year, me.moar_stuff FROM cd me',

  {
    '+select'   => [ { MOAR => 'f', -as => 'func' } ],
  } => 'SELECT COALESCE( a, b, c ) AS firstfound, me.year, MAX( me.year ) AS last_y, COUNT( me.cdid ) AS cnt, me.year, me.moar_stuff, MOAR(f) AS func FROM cd me',

);

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

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

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

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

  $testno++;
}

# Make sure we don't lose bits even with weird selector specs
# also check that the default selector list is lazy
# and make sure that unaliased +select does not go crazy
$rs = $schema->resultset('CD');
for my $attr (
  { '+columns'  => [ 'me.title' ] },    # this one should be de-duplicated but not the select's

  { '+select'   => \'me.year AS foo' },   # duplication of identical select expected (FIXME ?)
  { '+select'   => \['me.year AS foo'] },

  { '+select'   => [ \'me.artistid AS bar' ] },
  { '+select'   => { count => 'artistid', -as => 'baz' } },
) {
  for (qw/columns select as/) {
    ok (! exists $rs->{attrs}{$_}, "No eager '$_' attr on fresh resultset" );
  }

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

is_same_sql_bind (
  $rs->as_query,
  '( SELECT
      me.cdid,
      me.artist,
      me.title,
      me.year,
      me.genreid,
      me.single_track,
      me.year AS foo,
      me.year AS foo,
      me.artistid AS bar,
      COUNT( artistid ) AS baz
        FROM cd me
  )',
  [],
  'Correct chaining before attr resolution'
);

# Test the order of columns
$rs = $schema->resultset('CD')->search ({}, {
  'select'   => [ 'me.cdid', 'me.title' ],
});

is_same_sql_bind (
  $rs->as_query,
  '( SELECT
      me.cdid,
      me.title
      FROM cd me
  )',
  [],
  'Correct order of selected columns'
);

# Test bare +select with as from root of resultset
$rs = $schema->resultset('CD')->search ({}, {
  '+select'   => [
    \ 'foo',
    { MOAR => 'f', -as => 'func' },
   ],
});

is_same_sql_bind (
  $rs->as_query,
  '( SELECT
      me.cdid,
      me.artist,
      me.title,
      me.year,
      me.genreid,
      me.single_track,
      foo,
      MOAR( f ) AS func
       FROM cd me
  )',
  [],
  'Correct order of selected columns'
);

done_testing;