Commit | Line | Data |
5ef62e9f |
1 | use strict; |
2 | use Test::More; |
3 | use Test::Warn; |
4 | |
5 | BEGIN { |
6 | eval "use DBIx::Class::CDBICompat;"; |
89bddb49 |
7 | plan skip_all => "Class::Trigger and DBIx::ContextualFetch required: $@" if $@; |
5ef62e9f |
8 | } |
9 | |
50891152 |
10 | use lib 't/cdbi/testlib'; |
5ef62e9f |
11 | use Film; |
12 | |
13 | my $waves = Film->insert({ |
14 | Title => "Breaking the Waves", |
15 | Director => 'Lars von Trier', |
16 | Rating => 'R' |
17 | }); |
18 | |
8ed9eec6 |
19 | local $ENV{DBIC_CDBICOMPAT_HASH_WARN} = 0; |
10221b79 |
20 | |
8ed9eec6 |
21 | { |
22 | local $ENV{DBIC_CDBICOMPAT_HASH_WARN} = 1; |
ebe790db |
23 | |
8ed9eec6 |
24 | warnings_like { |
25 | my $rating = $waves->{rating}; |
26 | $waves->Rating("PG"); |
27 | is $rating, "R", 'evaluation of column value is not deferred'; |
de0ed7f1 |
28 | } qr{^Column 'rating' of 'Film/$waves' was fetched as a hash at\b}; |
5ef62e9f |
29 | |
8ed9eec6 |
30 | warnings_like { |
31 | is $waves->{title}, $waves->Title, "columns can be accessed as hashes"; |
32 | } qr{^Column 'title' of 'Film/$waves' was fetched as a hash at\b}; |
5ef62e9f |
33 | |
8ed9eec6 |
34 | $waves->Rating("G"); |
5ef62e9f |
35 | |
8ed9eec6 |
36 | warnings_like { |
37 | is $waves->{rating}, "G", "updating via the accessor updates the hash"; |
38 | } qr{^Column 'rating' of 'Film/$waves' was fetched as a hash at\b}; |
5ef62e9f |
39 | |
5ef62e9f |
40 | |
8ed9eec6 |
41 | warnings_like { |
42 | $waves->{rating} = "PG"; |
43 | } qr{^Column 'rating' of 'Film/$waves' was stored as a hash at\b}; |
10221b79 |
44 | |
8ed9eec6 |
45 | $waves->update; |
46 | my @films = Film->search( Rating => "PG", Title => "Breaking the Waves" ); |
47 | is @films, 1, "column updated as hash was saved"; |
48 | } |
10221b79 |
49 | |
50 | warning_is { |
10221b79 |
51 | $waves->{rating} |
92a23d90 |
52 | } '', 'DBIC_CDBICOMPAT_HASH_WARN controls warnings'; |
53 | |
54 | |
8ed9eec6 |
55 | { |
92a23d90 |
56 | $waves->rating("R"); |
57 | $waves->update; |
58 | |
59 | no warnings 'redefine'; |
60 | local *Film::rating = sub { |
61 | return "wibble"; |
62 | }; |
63 | |
64 | is $waves->{rating}, "R"; |
65 | } |
d656262b |
66 | |
67 | |
68 | { |
69 | no warnings 'redefine'; |
70 | no warnings 'once'; |
71 | local *Actor::accessor_name_for = sub { |
72 | my($class, $col) = @_; |
73 | return "movie" if lc $col eq "film"; |
74 | return $col; |
75 | }; |
76 | |
77 | require Actor; |
abb4aae3 |
78 | Actor->has_a( film => "Film" ); |
79 | |
d656262b |
80 | my $actor = Actor->insert({ |
81 | name => 'Emily Watson', |
82 | film => $waves, |
83 | }); |
84 | |
85 | ok !eval { $actor->film }; |
86 | is $actor->{film}->id, $waves->id, |
87 | 'hash access still works despite lack of accessor'; |
1c779eb2 |
88 | } |
89 | |
90 | |
91 | # Emulate that Class::DBI inflates immediately |
895b576d |
92 | SKIP: { |
93 | skip "Need MySQL to run this test", 3 unless eval { require MyFoo }; |
1c779eb2 |
94 | |
95 | my $foo = MyFoo->insert({ |
96 | name => 'Whatever', |
97 | tdate => '1949-02-01', |
98 | }); |
99 | isa_ok $foo, 'MyFoo'; |
100 | |
101 | isa_ok $foo->{tdate}, 'Date::Simple'; |
102 | is $foo->{tdate}->year, 1949; |
89bddb49 |
103 | } |
104 | |
105 | done_testing; |