Commit | Line | Data |
c0329273 |
1 | BEGIN { do "./t/lib/ANFANG.pm" or die ( $@ || $! ) } |
2 | |
6c0ddbf7 |
3 | use strict; |
8273e845 |
4 | use warnings; |
6c0ddbf7 |
5 | |
6 | use Test::More; |
704a0f8e |
7 | use Test::Warn; |
c5bc9ba6 |
8 | use Test::Exception; |
c0329273 |
9 | |
6c0ddbf7 |
10 | use DBICTest; |
11 | |
12 | my $schema = DBICTest->init_schema(); |
13 | |
6c0ddbf7 |
14 | { |
15 | my $cd_rc = $schema->resultset("CD")->result_class; |
8273e845 |
16 | |
c5bc9ba6 |
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 | |
6c0ddbf7 |
25 | my $artist_rs = $schema->resultset("Artist") |
26 | ->search_rs({}, {result_class => "IWillExplode"}); |
27 | is($artist_rs->result_class, 'IWillExplode', 'Correct artist result_class'); |
c5bc9ba6 |
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 |
908aa1bb |
36 | } qr/\QInflator IWillExplode does not provide an inflate_result() method/, |
c5bc9ba6 |
37 | 'IWillExplode explodes on inflate'; |
38 | |
6c0ddbf7 |
39 | my $cd_rs = $artist_rs->related_resultset('cds'); |
40 | is($cd_rs->result_class, $cd_rc, 'Correct cd result_class'); |
b72d5dd3 |
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'); |
8273e845 |
47 | |
b72d5dd3 |
48 | isa_ok(eval{ $cd_rs->find(1) }, $cd_rc, 'Inflated into correct cd result_class'); |
6c0ddbf7 |
49 | } |
50 | |
51 | |
52 | { |
53 | my $cd_rc = $schema->resultset("CD")->result_class; |
8273e845 |
54 | |
6c0ddbf7 |
55 | my $artist_rs = $schema->resultset("Artist") |
b72d5dd3 |
56 | ->search_rs({}, {result_class => "IWillExplode"})->search({artistid => 1}); |
6c0ddbf7 |
57 | is($artist_rs->result_class, 'IWillExplode', 'Correct artist result_class'); |
8273e845 |
58 | |
6c0ddbf7 |
59 | my $cd_rs = $artist_rs->related_resultset('cds'); |
60 | is($cd_rs->result_class, $cd_rc, 'Correct cd result_class'); |
8273e845 |
61 | |
62 | isa_ok(eval{ $cd_rs->find(1) }, $cd_rc, 'Inflated into correct cd result_class'); |
b72d5dd3 |
63 | isa_ok(eval{ $cd_rs->search({ cdid => 1 })->first }, $cd_rc, 'Inflated into correct cd result_class'); |
6c0ddbf7 |
64 | } |
704a0f8e |
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; |