Take a copy instead of weakening in 5.8 leak workaround
[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
2c2bc4e5 27require DBICTest::Schema;
199fbc45 28DBICTest::Schema->load_classes('EventSmallDT');
68de9438 29
d867eeda 30my @storage_types = (
95787afe 31 'DBI::Sybase::ASE',
32 'DBI::Sybase::ASE::NoBindVars',
d867eeda 33);
34my $schema;
35
36for my $storage_type (@storage_types) {
37 $schema = DBICTest::Schema->clone;
38
95787afe 39 unless ($storage_type eq 'DBI::Sybase::ASE') { # autodetect
d867eeda 40 $schema->storage_type("::$storage_type");
41 }
42 $schema->connection($dsn, $user, $pass, {
3d98c75e 43 on_connect_call => 'datetime_setup',
d867eeda 44 });
45
65d35121 46 my $guard = Scope::Guard->new(sub { cleanup($schema) } );
3d98c75e 47
d867eeda 48 $schema->storage->ensure_connected;
49
50 isa_ok( $schema->storage, "DBIx::Class::Storage::$storage_type" );
51
3d98c75e 52 eval { $schema->storage->dbh->do("DROP TABLE track") };
53 $schema->storage->dbh->do(<<"SQL");
54CREATE TABLE track (
55 trackid INT IDENTITY PRIMARY KEY,
56 cd INT NULL,
57 position INT NULL,
58 last_updated_at DATETIME NULL
59)
60SQL
61 eval { $schema->storage->dbh->do("DROP TABLE event_small_dt") };
62 $schema->storage->dbh->do(<<"SQL");
63CREATE TABLE event_small_dt (
64 id INT IDENTITY PRIMARY KEY,
65 small_dt SMALLDATETIME NULL,
66)
67SQL
68
69# coltype, column, source, pk, create_extra, datehash
d867eeda 70 my @dt_types = (
3d98c75e 71 ['DATETIME',
72 'last_updated_at',
73 'Track',
74 'trackid',
75 { cd => 1 },
76 {
77 year => 2004,
78 month => 8,
79 day => 21,
80 hour => 14,
81 minute => 36,
82 second => 48,
83 nanosecond => 500000000,
84 }],
85 ['SMALLDATETIME', # minute precision
86 'small_dt',
87 'EventSmallDT',
88 'id',
89 {},
90 {
91 year => 2004,
92 month => 8,
93 day => 21,
94 hour => 14,
95 minute => 36,
96 }],
d867eeda 97 );
68de9438 98
d867eeda 99 for my $dt_type (@dt_types) {
3d98c75e 100 my ($type, $col, $source, $pk, $create_extra, $sample_dt) = @$dt_type;
d867eeda 101
3d98c75e 102 ok(my $dt = DateTime->new($sample_dt));
d867eeda 103
104 my $row;
3d98c75e 105 ok( $row = $schema->resultset($source)->create({
d867eeda 106 $col => $dt,
3d98c75e 107 %$create_extra,
d867eeda 108 }));
3d98c75e 109 ok( $row = $schema->resultset($source)
110 ->search({ $pk => $row->$pk }, { select => [$col] })
d867eeda 111 ->first
112 );
fb95dc4d 113 is( $row->$col, $dt, "$type roundtrip" );
114
3d98c75e 115 cmp_ok( $row->$col->nanosecond, '==', $sample_dt->{nanosecond},
116 'DateTime fractional portion roundtrip' )
117 if exists $sample_dt->{nanosecond};
d867eeda 118 }
6469dabf 119
120 # test a computed datetime column
121 eval { $schema->storage->dbh->do("DROP TABLE track") };
122 $schema->storage->dbh->do(<<"SQL");
123CREATE TABLE track (
3d98c75e 124 trackid INT IDENTITY PRIMARY KEY,
125 cd INT NULL,
126 position INT NULL,
127 title VARCHAR(100) NULL,
128 last_updated_on DATETIME NULL,
129 last_updated_at AS getdate(),
6469dabf 130)
131SQL
132
3d98c75e 133 my $now = DateTime->now;
6469dabf 134 sleep 1;
135 my $new_row = $schema->resultset('Track')->create({});
136 $new_row->discard_changes;
137
138 lives_and {
139 cmp_ok (($new_row->last_updated_at - $now)->seconds, '>=', 1)
140 } 'getdate() computed column works';
d867eeda 141}
142
6469dabf 143done_testing;
144
d867eeda 145# clean up our mess
3d98c75e 146sub cleanup {
65d35121 147 my $schema = shift;
3d98c75e 148 if (my $dbh = eval { $schema->storage->dbh }) {
d867eeda 149 $dbh->do('DROP TABLE track');
3d98c75e 150 $dbh->do('DROP TABLE event_small_dt');
d867eeda 151 }
152}