0019e293d049708c5ab7f24246ebd5a086ce7b6d
[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;";
7   plan skip_all => "Class::Trigger and DBIx::ContextualFetch required"
8     if $@;
9
10   eval { require DateTime };
11   plan skip_all => "Need DateTime for inflation tests" if $@;
12
13   eval { require Clone };
14   plan skip_all => "Need Clone for CDBICompat inflation tests" if $@;
15 }
16
17 plan tests => 6;
18
19 use lib qw(t/lib);
20 use DBICTest;
21
22 my $schema = DBICTest->init_schema();
23
24 DBICTest::Schema::CD->load_components(qw/CDBICompat::Relationships/);
25
26 DBICTest::Schema::CD->has_a( 'year', 'DateTime',
27       inflate => sub { DateTime->new( year => shift ) },
28       deflate => sub { shift->year }
29 );
30 Class::C3->reinitialize;
31
32 # inflation test
33 my $cd = $schema->resultset("CD")->find(3);
34
35 is( ref($cd->year), 'DateTime', 'year is a DateTime, ok' );
36
37 is( $cd->year->month, 1, 'inflated month ok' );
38
39 # deflate test
40 my $now = DateTime->now;
41 $cd->year( $now );
42 $cd->update;
43
44 ($cd) = $schema->resultset("CD")->search( year => $now->year );
45 is( $cd->year->year, $now->year, 'deflate ok' );
46
47 # re-test using alternate deflate syntax
48 $schema->class("CD")->has_a( 'year', 'DateTime',
49       inflate => sub { DateTime->new( year => shift ) },
50       deflate => 'year'
51 );
52
53 # inflation test
54 $cd = $schema->resultset("CD")->find(3);
55
56 is( ref($cd->year), 'DateTime', 'year is a DateTime, ok' );
57
58 is( $cd->year->month, 1, 'inflated month ok' );
59
60 # deflate test
61 $now = DateTime->now;
62 $cd->year( $now );
63 $cd->update;
64
65 ($cd) = $schema->resultset("CD")->search( year => $now->year );
66 is( $cd->year->year, $now->year, 'deflate ok' );
67