Remove all uses of Scope::Guard from the tests, use our own version
[dbsrgits/DBIx-Class.git] / t / icdt / engine_specific / sqlanywhere.t
1 use DBIx::Class::Optional::Dependencies -skip_all_without => qw( ic_dt _rdbms_sqlanywhere_common );
2
3 use strict;
4 use warnings;
5
6 use Test::More;
7 use DBIx::Class::_Util 'scope_guard';
8 use lib qw(t/lib);
9 use DBICTest;
10
11 my @tdeps = qw( test_rdbms_sqlanywhere test_rdbms_sqlanywhere_odbc );
12 plan skip_all => 'Test needs  ' . (join '  OR  ', map
13   { "[ @{[ DBIx::Class::Optional::Dependencies->req_missing_for( $_ ) ]} ]" }
14   @tdeps
15 ) unless scalar grep
16   { DBIx::Class::Optional::Dependencies->req_ok_for( $_ ) }
17   @tdeps
18 ;
19
20 my ($dsn, $user, $pass)    = @ENV{map { "DBICTEST_SQLANYWHERE_${_}" }      qw/DSN USER PASS/};
21 my ($dsn2, $user2, $pass2) = @ENV{map { "DBICTEST_SQLANYWHERE_ODBC_${_}" } qw/DSN USER PASS/};
22
23 my @info = (
24   [ $dsn,  $user,  $pass  ],
25   [ $dsn2, $user2, $pass2 ],
26 );
27
28 my $schema;
29
30 foreach my $info (@info) {
31   my ($dsn, $user, $pass) = @$info;
32
33   next unless $dsn;
34
35   $schema = DBICTest::Schema->clone;
36
37   $schema->connection($dsn, $user, $pass, {
38     on_connect_call => 'datetime_setup',
39   });
40
41   my $sg = scope_guard { cleanup($schema) };
42
43   eval { $schema->storage->dbh->do('DROP TABLE event') };
44   $schema->storage->dbh->do(<<"SQL");
45   CREATE TABLE event (
46     id INT IDENTITY PRIMARY KEY,
47     created_on TIMESTAMP,
48     starts_at DATE
49   )
50 SQL
51
52 # coltype, col, date
53   my @dt_types = (
54     [
55       'TIMESTAMP',
56       'created_on',
57       '2004-08-21 14:36:48.080445',
58     ],
59 # date only (but minute precision according to ASA docs)
60     [
61       'DATE',
62       'starts_at',
63       '2004-08-21 00:00:00.000000',
64     ],
65   );
66
67   for my $dt_type (@dt_types) {
68     my ($type, $col, $sample_dt) = @$dt_type;
69
70     ok(my $dt = $schema->storage->datetime_parser->parse_datetime($sample_dt));
71
72     my $row;
73     ok( $row = $schema->resultset('Event')->create({ $col => $dt, }));
74     ok( $row = $schema->resultset('Event')
75       ->search({ id => $row->id }, { select => [$col] })
76       ->first
77     );
78     is( $row->$col, $dt, "$type roundtrip" );
79
80     is $row->$col->nanosecond, $dt->nanosecond,
81         'nanoseconds survived' if 0+$dt->nanosecond;
82   }
83 }
84
85 done_testing;
86
87 # clean up our mess
88 sub cleanup {
89   my $schema = shift;
90   if (my $dbh = $schema->storage->dbh) {
91     eval { $dbh->do("DROP TABLE $_") } for qw/event/;
92   }
93 }