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