Explicitly disallow redirection of has_many columns to main object
[dbsrgits/DBIx-Class.git] / t / resultset / inflatemap_abuse.t
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 # From http://lists.scsys.co.uk/pipermail/dbix-class/2013-February/011119.html
10 #
11 # > Right, at this point we have an "undefined situation turned into an
12 # > unplanned feature", therefore 0.08242 will downgrade the exception to a
13 # > single-warning-per-process. This seems like a sane middle ground for
14 # > "you gave me an 'as' that worked by accident before - fix it at your
15 # > convenience".
16 #
17 # When the things were reshuffled it became apparent implementing a warning
18 # for the HRI case *only* is going to complicate the code a lot, without
19 # adding much benefit at this point. So just make sure everything works the
20 # way it used to and move on
21
22
23 my $s = DBICTest->init_schema;
24
25 my $rs_2nd_track = $s->resultset('Track')->search(
26   { 'me.position' => 2 },
27   {
28     join => { cd => 'artist' },
29     'columns' => [ 'me.title', { 'artist.cdtitle' => 'cd.title' }, 'artist.name' ],
30     order_by => 'artist.name',
31   }
32 );
33
34 is_deeply (
35   [ map { $_->[-1] } $rs_2nd_track->cursor->all ],
36   [ ('Caterwauler McCrae') x 3, 'Random Boy Band', 'We Are Goth' ],
37   'Artist name cartesian product correct off cursor',
38 );
39
40 is_deeply (
41   $rs_2nd_track->all_hri,
42   [
43     {
44       artist => { cdtitle => "Caterwaulin' Blues", name => "Caterwauler McCrae" },
45       title => "Howlin"
46     },
47     {
48       artist => { cdtitle => "Forkful of bees", name => "Caterwauler McCrae" },
49       title => "Stripy"
50     },
51     {
52       artist => { cdtitle => "Spoonful of bees", name => "Caterwauler McCrae" },
53       title => "Apiary"
54     },
55     {
56       artist => { cdtitle => "Generic Manufactured Singles", name => "Random Boy Band" },
57       title => "Boring Song"
58     },
59     {
60       artist => { cdtitle => "Come Be Depressed With Us", name => "We Are Goth" },
61       title => "Under The Weather"
62     }
63   ],
64   'HRI with invalid inflate map works'
65 );
66
67 throws_ok
68   { $rs_2nd_track->next }
69   qr!\QInflation into non-existent relationship 'artist' of 'Track' requested, check the inflation specification (columns/as) ending in '...artist.name'!,
70   'Correct exception on illegal ::Row inflation attempt'
71 ;
72
73 # make sure has_many column redirection does not do weird stuff when collapse is requested
74 for my $pref_args (
75   { prefetch => 'cds'},
76   { collapse => 1 }
77 ) {
78   for my $col_and_join_args (
79     { '+columns' => { 'cd_title' => 'cds_2.title' }, join => [ 'cds', 'cds' ] },
80     { '+columns' => { 'cd_title' => 'cds.title' }, join => 'cds' },
81     { '+columns' => { 'cd_gr_name' => 'genre.name' }, join => { cds => 'genre' } },
82   ) {
83     for my $call (qw(next all first)) {
84
85       my $weird_rs = $s->resultset('Artist')->search({}, {
86         %$col_and_join_args, %$pref_args,
87       });
88
89       throws_ok
90         { $weird_rs->$call }
91         qr/\QResult collapse not possible - selection from a has_many source redirected to the main object/
92       for (1,2);
93     }
94   }
95 }
96
97 done_testing;