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