don't mask missing deps with datetime_undef_if_invalid
[dbsrgits/DBIx-Class.git] / t / inflate / datetime_sybase.t
CommitLineData
d867eeda 1use strict;
68de9438 2use warnings;
d867eeda 3
4use Test::More;
5use Test::Exception;
3d98c75e 6use Scope::Guard ();
7use Try::Tiny;
d867eeda 8use lib qw(t/lib);
9use DBICTest;
10
3d98c75e 11DBICTest::Schema->load_classes('EventSmallDT');
12
d867eeda 13my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_SYBASE_${_}" } qw/DSN USER PASS/};
14
15if (not ($dsn && $user)) {
16 plan skip_all =>
17 'Set $ENV{DBICTEST_SYBASE_DSN}, _USER and _PASS to run this test' .
18 "\nWarning: This test drops and creates a table called 'track'";
d867eeda 19}
20
c6b7885f 21for (qw/test_rdbms_ase test_dt/) {
22 plan skip_all => 'Test needs ' . DBIx::Class::Optional::Dependencies->req_missing_for ($_)
23 unless DBIx::Class::Optional::Dependencies->req_ok_for ($_);
24}
68de9438 25
d867eeda 26my @storage_types = (
95787afe 27 'DBI::Sybase::ASE',
28 'DBI::Sybase::ASE::NoBindVars',
d867eeda 29);
30my $schema;
31
32for my $storage_type (@storage_types) {
33 $schema = DBICTest::Schema->clone;
34
95787afe 35 unless ($storage_type eq 'DBI::Sybase::ASE') { # autodetect
d867eeda 36 $schema->storage_type("::$storage_type");
37 }
38 $schema->connection($dsn, $user, $pass, {
3d98c75e 39 on_connect_call => 'datetime_setup',
d867eeda 40 });
41
3d98c75e 42 my $guard = Scope::Guard->new(\&cleanup);
43
d867eeda 44 $schema->storage->ensure_connected;
45
46 isa_ok( $schema->storage, "DBIx::Class::Storage::$storage_type" );
47
3d98c75e 48 eval { $schema->storage->dbh->do("DROP TABLE track") };
49 $schema->storage->dbh->do(<<"SQL");
50CREATE TABLE track (
51 trackid INT IDENTITY PRIMARY KEY,
52 cd INT NULL,
53 position INT NULL,
54 last_updated_at DATETIME NULL
55)
56SQL
57 eval { $schema->storage->dbh->do("DROP TABLE event_small_dt") };
58 $schema->storage->dbh->do(<<"SQL");
59CREATE TABLE event_small_dt (
60 id INT IDENTITY PRIMARY KEY,
61 small_dt SMALLDATETIME NULL,
62)
63SQL
64
65# coltype, column, source, pk, create_extra, datehash
d867eeda 66 my @dt_types = (
3d98c75e 67 ['DATETIME',
68 'last_updated_at',
69 'Track',
70 'trackid',
71 { cd => 1 },
72 {
73 year => 2004,
74 month => 8,
75 day => 21,
76 hour => 14,
77 minute => 36,
78 second => 48,
79 nanosecond => 500000000,
80 }],
81 ['SMALLDATETIME', # minute precision
82 'small_dt',
83 'EventSmallDT',
84 'id',
85 {},
86 {
87 year => 2004,
88 month => 8,
89 day => 21,
90 hour => 14,
91 minute => 36,
92 }],
d867eeda 93 );
68de9438 94
d867eeda 95 for my $dt_type (@dt_types) {
3d98c75e 96 my ($type, $col, $source, $pk, $create_extra, $sample_dt) = @$dt_type;
d867eeda 97
3d98c75e 98 ok(my $dt = DateTime->new($sample_dt));
d867eeda 99
100 my $row;
3d98c75e 101 ok( $row = $schema->resultset($source)->create({
d867eeda 102 $col => $dt,
3d98c75e 103 %$create_extra,
d867eeda 104 }));
3d98c75e 105 ok( $row = $schema->resultset($source)
106 ->search({ $pk => $row->$pk }, { select => [$col] })
d867eeda 107 ->first
108 );
fb95dc4d 109 is( $row->$col, $dt, "$type roundtrip" );
110
3d98c75e 111 cmp_ok( $row->$col->nanosecond, '==', $sample_dt->{nanosecond},
112 'DateTime fractional portion roundtrip' )
113 if exists $sample_dt->{nanosecond};
d867eeda 114 }
6469dabf 115
116 # test a computed datetime column
117 eval { $schema->storage->dbh->do("DROP TABLE track") };
118 $schema->storage->dbh->do(<<"SQL");
119CREATE TABLE track (
3d98c75e 120 trackid INT IDENTITY PRIMARY KEY,
121 cd INT NULL,
122 position INT NULL,
123 title VARCHAR(100) NULL,
124 last_updated_on DATETIME NULL,
125 last_updated_at AS getdate(),
6469dabf 126)
127SQL
128
3d98c75e 129 my $now = DateTime->now;
6469dabf 130 sleep 1;
131 my $new_row = $schema->resultset('Track')->create({});
132 $new_row->discard_changes;
133
134 lives_and {
135 cmp_ok (($new_row->last_updated_at - $now)->seconds, '>=', 1)
136 } 'getdate() computed column works';
d867eeda 137}
138
6469dabf 139done_testing;
140
d867eeda 141# clean up our mess
3d98c75e 142sub cleanup {
143 if (my $dbh = eval { $schema->storage->dbh }) {
d867eeda 144 $dbh->do('DROP TABLE track');
3d98c75e 145 $dbh->do('DROP TABLE event_small_dt');
d867eeda 146 }
147}