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