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