slightly different testing strategy
[dbsrgits/DBIx-Class-DateTime-Epoch.git] / t / 02-schema.t
1 use strict;
2 use warnings;
3
4 use lib 't/lib';
5
6 use Test::More tests => 19;
7
8 use DBICx::TestDatabase;
9 use DateTime;
10 my $schema = DBICx::TestDatabase->new( 'MySchema' );
11
12 my $rs = $schema->resultset( 'Foo' );
13
14 {
15     my $now = DateTime->now;
16     my $epoch = $now->epoch;
17
18     $schema->populate( 'Foo', [ [ qw( id bar baz creation_time modification_time dt ) ], [ 1, ( $epoch ) x 4, $now ] ] );
19
20     {
21         my $row = $rs->find( 1 );
22
23         isa_ok( $row->bar, 'DateTime' );
24         isa_ok( $row->baz, 'DateTime' );
25         isa_ok( $row->dt, 'DateTime' );
26         ok( $row->bar == $now, 'inflate: epoch as int' ); 
27         ok( $row->baz == $now, 'inflate: epoch as varchar' ); 
28         ok( $row->dt == $now, 'inflate: regular datetime column' ); 
29     }
30
31     {
32         $rs->create( { bar => $now, baz => $now, dt => $now } );
33         my $row = $rs->find( 2 );
34
35         isa_ok( $row->bar, 'DateTime' );
36         isa_ok( $row->baz, 'DateTime' );
37         isa_ok( $row->dt, 'DateTime' );
38         is( $row->get_column( 'bar' ), $epoch, 'deflate: epoch as int' );
39         is( $row->get_column( 'baz' ), $epoch, 'deflate: epoch as varchar' );
40         is( $row->get_column( 'dt' ), join( ' ', $now->ymd, $now->hms ), 'deflate: regular datetime column' );
41
42         # courtesy of TimeStamp
43         isa_ok( $row->creation_time, 'DateTime' ); # courtesy of TimeStamp
44         isa_ok( $row->modification_time, 'DateTime' );
45         like( $row->get_column( 'creation_time' ), qr/^\d+$/, 'TimeStamp as epoch' );
46         like( $row->get_column( 'modification_time' ), qr/^\d+$/, 'TimeStamp as epoch' );
47
48         my $mtime = $row->modification_time;
49         sleep( 1 );
50         $row->update( { name => 'test' } );
51
52         $row = $rs->find( 2 );
53         isa_ok( $row->modification_time, 'DateTime' );
54         like( $row->get_column( 'modification_time' ), qr/^\d+$/, 'TimeStamp as epoch' );
55         ok( $row->modification_time > $mtime, 'mtime column was updated' );
56     }
57 }