Add strict/warnings test, adjust all offenders (wow, that was a lot)
[dbsrgits/DBIx-Class.git] / t / cdbi / columns_as_hashes.t
1 use strict;
2 use warnings;
3 use Test::More;
4 use Test::Warn;
5
6 use lib 't/cdbi/testlib';
7 use Film;
8
9 my $waves = Film->insert({
10     Title     => "Breaking the Waves",
11     Director  => 'Lars von Trier',
12     Rating    => 'R'
13 });
14
15 local $ENV{DBIC_CDBICOMPAT_HASH_WARN} = 0;
16
17 {
18     local $ENV{DBIC_CDBICOMPAT_HASH_WARN} = 1;
19
20     warnings_like {
21         my $rating = $waves->{rating};
22         $waves->Rating("PG");
23         is $rating, "R", 'evaluation of column value is not deferred';
24     } qr{^Column 'rating' of 'Film/$waves' was fetched as a hash at\b};
25
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};
29
30     $waves->Rating("G");
31
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};
35
36
37     warnings_like {
38         $waves->{rating} = "PG";
39     } qr{^Column 'rating' of 'Film/$waves' was stored as a hash at\b};
40
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 }
45
46 warning_is {
47     $waves->{rating}
48 } '', 'DBIC_CDBICOMPAT_HASH_WARN controls warnings';
49
50
51 {
52     $waves->rating("R");
53     $waves->update;
54
55     no warnings 'redefine';
56     local *Film::rating = sub {
57         return "wibble";
58     };
59
60     is $waves->{rating}, "R";
61 }
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     };
72
73     require Actor;
74     Actor->has_a( film => "Film" );
75
76     my $actor = Actor->insert({
77         name    => 'Emily Watson',
78         film    => $waves,
79     });
80
81     ok !eval { $actor->film };
82     is $actor->{film}->id, $waves->id,
83        'hash access still works despite lack of accessor';
84 }
85
86
87 # Emulate that Class::DBI inflates immediately
88 SKIP: {
89     unless (eval { require MyFoo }) {
90       my ($err) = $@ =~ /([^\n]+)/;
91       skip $err, 3
92     }
93
94     my $foo = MyFoo->insert({
95         name    => 'Whatever',
96         tdate   => '1949-02-01',
97     });
98     isa_ok $foo, 'MyFoo';
99
100     isa_ok $foo->{tdate}, 'Date::Simple';
101     is $foo->{tdate}->year, 1949;
102 }
103
104 done_testing;