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