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