7842f0a6ce424c5b0c798d590593e24700f2ab1a
[dbsrgits/DBIx-Class.git] / t / cdbi / 68-inflate_has_a.t
1 use strict;
2 use warnings;
3 use Test::More;
4
5 BEGIN {
6   eval "use DBIx::Class::CDBICompat; use DateTime 0.55; use Clone;";
7   plan skip_all => "Clone, DateTime 0.55, Class::Trigger and DBIx::ContextualFetch required"
8     if $@;
9 }
10
11 plan tests => 6;
12
13 use lib qw(t/lib);
14 use DBICTest;
15
16 my $schema = DBICTest->init_schema();
17
18 DBICTest::Schema::CD->load_components(qw/CDBICompat::Relationships/);
19
20 DBICTest::Schema::CD->has_a( 'year', 'DateTime',
21       inflate => sub { DateTime->new( year => shift ) },
22       deflate => sub { shift->year }
23 );
24 Class::C3->reinitialize;
25
26 # inflation test
27 my $cd = $schema->resultset("CD")->find(3);
28
29 is( ref($cd->year), 'DateTime', 'year is a DateTime, ok' );
30
31 is( $cd->year->month, 1, 'inflated month ok' );
32
33 # deflate test
34 my $now = DateTime->now;
35 $cd->year( $now );
36 $cd->update;
37
38 ($cd) = $schema->resultset("CD")->search( year => $now->year );
39 is( $cd->year->year, $now->year, 'deflate ok' );
40
41 # re-test using alternate deflate syntax
42 $schema->class("CD")->has_a( 'year', 'DateTime',
43       inflate => sub { DateTime->new( year => shift ) },
44       deflate => 'year'
45 );
46
47 # inflation test
48 $cd = $schema->resultset("CD")->find(3);
49
50 is( ref($cd->year), 'DateTime', 'year is a DateTime, ok' );
51
52 is( $cd->year->month, 1, 'inflated month ok' );
53
54 # deflate test
55 $now = DateTime->now;
56 $cd->year( $now );
57 $cd->update;
58
59 ($cd) = $schema->resultset("CD")->search( year => $now->year );
60 is( $cd->year->year, $now->year, 'deflate ok' );
61