Add strict/warnings test, adjust all offenders (wow, that was a lot)
[dbsrgits/DBIx-Class.git] / t / cdbi / columns_as_hashes.t
CommitLineData
5ef62e9f 1use strict;
4a233f30 2use warnings;
5ef62e9f 3use Test::More;
4use Test::Warn;
5
50891152 6use lib 't/cdbi/testlib';
5ef62e9f 7use Film;
8
9my $waves = Film->insert({
10 Title => "Breaking the Waves",
11 Director => 'Lars von Trier',
12 Rating => 'R'
13});
14
8ed9eec6 15local $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
46warning_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 88SKIP: {
49b3a264 89 unless (eval { require MyFoo }) {
90 my ($err) = $@ =~ /([^\n]+)/;
91 skip $err, 3
92 }
93
1c779eb2 94 my $foo = MyFoo->insert({
95 name => 'Whatever',
96 tdate => '1949-02-01',
97 });
98 isa_ok $foo, 'MyFoo';
8273e845 99
1c779eb2 100 isa_ok $foo->{tdate}, 'Date::Simple';
101 is $foo->{tdate}->year, 1949;
89bddb49 102}
103
104done_testing;