File: columns_as_hashes.t

package info (click to toggle)
libdbix-class-perl 0.082843-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 5,320 kB
  • sloc: perl: 27,215; sql: 322; sh: 29; makefile: 16
file content (104 lines) | stat: -rw-r--r-- 2,339 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
use strict;
use warnings;
use Test::More;
use Test::Warn;

use lib 't/cdbi/testlib';
use Film;

my $waves = Film->insert({
    Title     => "Breaking the Waves",
    Director  => 'Lars von Trier',
    Rating    => 'R'
});

local $ENV{DBIC_CDBICOMPAT_HASH_WARN} = 0;

{
    local $ENV{DBIC_CDBICOMPAT_HASH_WARN} = 1;

    warnings_like {
        my $rating = $waves->{rating};
        $waves->Rating("PG");
        is $rating, "R", 'evaluation of column value is not deferred';
    } qr{^Column 'rating' of 'Film/$waves' was fetched as a hash at\b};

    warnings_like {
        is $waves->{title}, $waves->Title, "columns can be accessed as hashes";
    } qr{^Column 'title' of 'Film/$waves' was fetched as a hash at\b};

    $waves->Rating("G");

    warnings_like {
        is $waves->{rating}, "G", "updating via the accessor updates the hash";
    } qr{^Column 'rating' of 'Film/$waves' was fetched as a hash at\b};


    warnings_like {
        $waves->{rating} = "PG";
    } qr{^Column 'rating' of 'Film/$waves' was stored as a hash at\b};

    $waves->update;
    my @films = Film->search( Rating => "PG", Title => "Breaking the Waves" );
    is @films, 1, "column updated as hash was saved";
}

warning_is {
    $waves->{rating}
} '', 'DBIC_CDBICOMPAT_HASH_WARN controls warnings';


{
    $waves->rating("R");
    $waves->update;

    no warnings 'redefine';
    local *Film::rating = sub {
        return "wibble";
    };

    is $waves->{rating}, "R";
}


{
    no warnings 'redefine';
    no warnings 'once';
    local *Actor::accessor_name_for = sub {
        my($class, $col) = @_;
        return "movie" if lc $col eq "film";
        return $col;
    };

    require Actor;
    Actor->has_a( film => "Film" );

    my $actor = Actor->insert({
        name    => 'Emily Watson',
        film    => $waves,
    });

    ok !eval { $actor->film };
    is $actor->{film}->id, $waves->id,
       'hash access still works despite lack of accessor';
}


# Emulate that Class::DBI inflates immediately
SKIP: {
    unless (eval { require MyFoo }) {
      my ($err) = $@ =~ /([^\n]+)/;
      skip $err, 3
    }

    my $foo = MyFoo->insert({
        name    => 'Whatever',
        tdate   => '1949-02-01',
    });
    isa_ok $foo, 'MyFoo';

    isa_ok $foo->{tdate}, 'Date::Simple';
    is $foo->{tdate}->year, 1949;
}

done_testing;