When emulating $obj->{column} do not call any custom column method, just
[dbsrgits/DBIx-Class.git] / t / cdbi-t / columns_as_hashes.t
CommitLineData
5ef62e9f 1#!/usr/bin/perl -w
2
3use strict;
4use Test::More;
5use Test::Warn;
6
7BEGIN {
8 eval "use DBIx::Class::CDBICompat;";
9 plan $@ ? (skip_all => "Class::Trigger and DBIx::ContextualFetch required: $@")
92a23d90 10 : (tests=> 10);
5ef62e9f 11}
12
13use lib 't/testlib';
14use Film;
15
16my $waves = Film->insert({
17 Title => "Breaking the Waves",
18 Director => 'Lars von Trier',
19 Rating => 'R'
20});
21
10221b79 22local $ENV{DBIC_CDBICOMPAT_HASH_WARN} = 1;
23
5ef62e9f 24warnings_like {
ebe790db 25 my $rating = $waves->{rating};
26 $waves->Rating("PG");
27 is $rating, "R", 'evaluation of column value is not deferred';
28} qr{^Column 'rating' of 'Film/$waves' was fetched as a hash at \Q$0};
29
30warnings_like {
5ef62e9f 31 is $waves->{title}, $waves->Title, "columns can be accessed as hashes";
ebe790db 32} qr{^Column 'title' of 'Film/$waves' was fetched as a hash at\b};
5ef62e9f 33
34$waves->Rating("G");
35
36warnings_like {
37 is $waves->{rating}, "G", "updating via the accessor updates the hash";
ebe790db 38} qr{^Column 'rating' of 'Film/$waves' was fetched as a hash at\b};
5ef62e9f 39
5ef62e9f 40
41warnings_like {
ebe790db 42 $waves->{rating} = "PG";
43} qr{^Column 'rating' of 'Film/$waves' was stored as a hash at\b};
5ef62e9f 44
ebe790db 45$waves->update;
5ef62e9f 46my @films = Film->search( Rating => "PG", Title => "Breaking the Waves" );
47is @films, 1, "column updated as hash was saved";
10221b79 48
49
50warning_is {
51 local $ENV{DBIC_CDBICOMPAT_HASH_WARN} = 0;
52 $waves->{rating}
92a23d90 53} '', 'DBIC_CDBICOMPAT_HASH_WARN controls warnings';
54
55
56{
57 local $ENV{DBIC_CDBICOMPAT_HASH_WARN} = 0;
58
59 $waves->rating("R");
60 $waves->update;
61
62 no warnings 'redefine';
63 local *Film::rating = sub {
64 return "wibble";
65 };
66
67 is $waves->{rating}, "R";
68}