Test that DateTime objects can safely be updated twice (which is what triggers
[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: $@")
d656262b 10 : ('no_plan');
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
8ed9eec6 22local $ENV{DBIC_CDBICOMPAT_HASH_WARN} = 0;
10221b79 23
8ed9eec6 24{
25 local $ENV{DBIC_CDBICOMPAT_HASH_WARN} = 1;
ebe790db 26
8ed9eec6 27 warnings_like {
28 my $rating = $waves->{rating};
29 $waves->Rating("PG");
30 is $rating, "R", 'evaluation of column value is not deferred';
31 } qr{^Column 'rating' of 'Film/$waves' was fetched as a hash at \Q$0};
5ef62e9f 32
8ed9eec6 33 warnings_like {
34 is $waves->{title}, $waves->Title, "columns can be accessed as hashes";
35 } qr{^Column 'title' of 'Film/$waves' was fetched as a hash at\b};
5ef62e9f 36
8ed9eec6 37 $waves->Rating("G");
5ef62e9f 38
8ed9eec6 39 warnings_like {
40 is $waves->{rating}, "G", "updating via the accessor updates the hash";
41 } qr{^Column 'rating' of 'Film/$waves' was fetched as a hash at\b};
5ef62e9f 42
5ef62e9f 43
8ed9eec6 44 warnings_like {
45 $waves->{rating} = "PG";
46 } qr{^Column 'rating' of 'Film/$waves' was stored as a hash at\b};
10221b79 47
8ed9eec6 48 $waves->update;
49 my @films = Film->search( Rating => "PG", Title => "Breaking the Waves" );
50 is @films, 1, "column updated as hash was saved";
51}
10221b79 52
53warning_is {
10221b79 54 $waves->{rating}
92a23d90 55} '', 'DBIC_CDBICOMPAT_HASH_WARN controls warnings';
56
57
8ed9eec6 58{
92a23d90 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}
d656262b 69
70
71{
72 no warnings 'redefine';
73 no warnings 'once';
74 local *Actor::accessor_name_for = sub {
75 my($class, $col) = @_;
76 return "movie" if lc $col eq "film";
77 return $col;
78 };
79
80 require Actor;
81
82 my $actor = Actor->insert({
83 name => 'Emily Watson',
84 film => $waves,
85 });
86
87 ok !eval { $actor->film };
88 is $actor->{film}->id, $waves->id,
89 'hash access still works despite lack of accessor';
1c779eb2 90}
91
92
93# Emulate that Class::DBI inflates immediately
94{
95 require_ok 'MyFoo';
96
97 my $foo = MyFoo->insert({
98 name => 'Whatever',
99 tdate => '1949-02-01',
100 });
101 isa_ok $foo, 'MyFoo';
102
103 isa_ok $foo->{tdate}, 'Date::Simple';
104 is $foo->{tdate}->year, 1949;
d656262b 105}