Introduce GOVERNANCE document and empty RESOLUTIONS file.
[dbsrgits/DBIx-Class.git] / t / 97result_class.t
1 BEGIN { do "./t/lib/ANFANG.pm" or die ( $@ || $! ) }
2
3 use strict;
4 use warnings;
5
6 use Test::More;
7 use Test::Warn;
8 use Test::Exception;
9
10 use DBICTest;
11
12 my $schema = DBICTest->init_schema();
13
14 {
15   my $cd_rc = $schema->resultset("CD")->result_class;
16
17   throws_ok {
18     $schema->resultset("Artist")
19       ->search_rs({}, {result_class => "IWillExplode"})
20   } qr/Can't locate IWillExplode/, 'nonexistant result_class exception';
21
22 # to make ensure_class_loaded happy, dies on inflate
23   eval 'package IWillExplode; sub dummy {}';
24
25   my $artist_rs = $schema->resultset("Artist")
26     ->search_rs({}, {result_class => "IWillExplode"});
27   is($artist_rs->result_class, 'IWillExplode', 'Correct artist result_class');
28
29   throws_ok {
30     $artist_rs->result_class('mtfnpy')
31   } qr/Can't locate mtfnpy/,
32   'nonexistant result_access exception (from accessor)';
33
34   throws_ok {
35     $artist_rs->first
36   } qr/\QInflator IWillExplode does not provide an inflate_result() method/,
37   'IWillExplode explodes on inflate';
38
39   my $cd_rs = $artist_rs->related_resultset('cds');
40   is($cd_rs->result_class, $cd_rc, 'Correct cd result_class');
41
42   my $cd_rs2 = $schema->resultset("Artist")->search_rs({})->related_resultset('cds');
43   is($cd_rs->result_class, $cd_rc, 'Correct cd2 result_class');
44
45   my $cd_rs3 = $schema->resultset("Artist")->search_rs({},{})->related_resultset('cds');
46   is($cd_rs->result_class, $cd_rc, 'Correct cd3 result_class');
47
48   isa_ok(eval{ $cd_rs->find(1) }, $cd_rc, 'Inflated into correct cd result_class');
49 }
50
51
52 {
53   my $cd_rc = $schema->resultset("CD")->result_class;
54
55   my $artist_rs = $schema->resultset("Artist")
56     ->search_rs({}, {result_class => "IWillExplode"})->search({artistid => 1});
57   is($artist_rs->result_class, 'IWillExplode', 'Correct artist result_class');
58
59   my $cd_rs = $artist_rs->related_resultset('cds');
60   is($cd_rs->result_class, $cd_rc, 'Correct cd result_class');
61
62   isa_ok(eval{ $cd_rs->find(1) }, $cd_rc, 'Inflated into correct cd result_class');
63   isa_ok(eval{ $cd_rs->search({ cdid => 1 })->first }, $cd_rc, 'Inflated into correct cd result_class');
64 }
65
66 {
67   my $rs = $schema->resultset('Artist')->search(
68     { 'cds.title' => 'Spoonful of bees' },
69     { prefetch => 'cds', result_class => 'DBIx::Class::ResultClass::HashRefInflator' },
70   );
71
72   is ($rs->result_class, 'DBIx::Class::ResultClass::HashRefInflator', 'starting with correct resultclass');
73
74   $rs->result_class('DBICTest::Artist');
75   is ($rs->result_class, 'DBICTest::Artist', 'resultclass changed');
76
77   my $art = $rs->next;
78   is (ref $art, 'DBICTest::Artist', 'Correcty blessed output');
79
80   throws_ok
81     { $rs->result_class('IWillExplode') }
82     qr/\QChanging the result_class of a ResultSet instance with an active cursor is not supported/,
83     'Throws on result class change in progress'
84   ;
85
86   my $cds = $art->cds;
87
88   warnings_exist
89     { $cds->result_class('IWillExplode') }
90     qr/\QChanging the result_class of a ResultSet instance with cached results is a noop/,
91     'Warning on noop result_class change'
92   ;
93
94   is ($cds->result_class, 'IWillExplode', 'class changed anyway');
95
96   # even though the original was HRI (at $rs definition time above)
97   # we lost the control over the *prefetched* object result class
98   # when we handed the root object creation to ::Row::inflate_result
99   is( ref $cds->next, 'DBICTest::CD', 'Correctly inflated prefetched result');
100 }
101
102 done_testing;