Only load DBICTest::Schema when needed in tests
[dbsrgits/DBIx-Class.git] / t / inflate / datetime_sqlanywhere.t
CommitLineData
ed720bc5 1use strict;
8273e845 2use warnings;
ed720bc5 3
4use Test::More;
592ff7c4 5use Scope::Guard ();
199fbc45 6use DBIx::Class::Optional::Dependencies ();
ed720bc5 7use lib qw(t/lib);
8use DBICTest;
9
374f18f2 10my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_SQLANYWHERE_${_}" } qw/DSN USER PASS/};
11my ($dsn2, $user2, $pass2) = @ENV{map { "DBICTEST_SQLANYWHERE_ODBC_${_}" } qw/DSN USER PASS/};
ed720bc5 12
199fbc45 13plan skip_all => 'Test needs ' .
14 (join ' and ', map { $_ ? $_ : () }
15 DBIx::Class::Optional::Dependencies->req_missing_for('test_dt'),
16 (join ' or ', map { $_ ? $_ : () }
17 DBIx::Class::Optional::Dependencies->req_missing_for('test_rdbms_sqlanywhere'),
18 DBIx::Class::Optional::Dependencies->req_missing_for('test_rdbms_sqlanywhere_odbc')))
19 unless
20 DBIx::Class::Optional::Dependencies->req_ok_for ('test_dt') && (
21 $dsn && DBIx::Class::Optional::Dependencies->req_ok_for('test_rdbms_sqlanywhere')
22 or
23 $dsn2 && DBIx::Class::Optional::Dependencies->req_ok_for('test_rdbms_sqlanywhere_odbc'))
24 or (not $dsn || $dsn2);
25
5be953ff 26if (not ($dsn || $dsn2)) {
27 plan skip_all => <<'EOF';
374f18f2 28Set $ENV{DBICTEST_SQLANYWHERE_DSN} and/or $ENV{DBICTEST_SQLANYWHERE_ODBC_DSN}
5be953ff 29_USER and _PASS to run this test'.
7bef80f1 30Warning: This test drops and creates a table called 'event'";
5be953ff 31EOF
ed720bc5 32}
33
5be953ff 34my @info = (
35 [ $dsn, $user, $pass ],
36 [ $dsn2, $user2, $pass2 ],
37);
38
592ff7c4 39my $schema;
5be953ff 40
41foreach my $info (@info) {
42 my ($dsn, $user, $pass) = @$info;
ed720bc5 43
5be953ff 44 next unless $dsn;
ed720bc5 45
2c2bc4e5 46 $schema = DBICTest->connect_schema($dsn, $user, $pass, {
7bef80f1 47 on_connect_call => 'datetime_setup',
5be953ff 48 });
49
65d35121 50 my $sg = Scope::Guard->new(sub { cleanup($schema) } );
ed720bc5 51
7bef80f1 52 eval { $schema->storage->dbh->do('DROP TABLE event') };
53 $schema->storage->dbh->do(<<"SQL");
54 CREATE TABLE event (
55 id INT IDENTITY PRIMARY KEY,
56 created_on TIMESTAMP,
57 starts_at DATE
58 )
59SQL
60
ed720bc5 61# coltype, col, date
5be953ff 62 my @dt_types = (
7bef80f1 63 [
64 'TIMESTAMP',
65 'created_on',
66 '2004-08-21 14:36:48.080445',
67 ],
2b0076be 68# date only (but minute precision according to ASA docs)
7bef80f1 69 [
70 'DATE',
71 'starts_at',
72 '2004-08-21 00:00:00.000000',
73 ],
5be953ff 74 );
75
76 for my $dt_type (@dt_types) {
77 my ($type, $col, $sample_dt) = @$dt_type;
ed720bc5 78
5be953ff 79 ok(my $dt = $schema->storage->datetime_parser->parse_datetime($sample_dt));
80
81 my $row;
7bef80f1 82 ok( $row = $schema->resultset('Event')->create({ $col => $dt, }));
83 ok( $row = $schema->resultset('Event')
84 ->search({ id => $row->id }, { select => [$col] })
5be953ff 85 ->first
86 );
592ff7c4 87 is( $row->$col, $dt, "$type roundtrip" );
51b1115f 88
89 is $row->$col->nanosecond, $dt->nanosecond,
90 'nanoseconds survived' if 0+$dt->nanosecond;
5be953ff 91 }
ed720bc5 92}
93
94done_testing;
95
96# clean up our mess
592ff7c4 97sub cleanup {
65d35121 98 my $schema = shift;
592ff7c4 99 if (my $dbh = $schema->storage->dbh) {
7bef80f1 100 eval { $dbh->do("DROP TABLE $_") } for qw/event/;
ed720bc5 101 }
102}