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