Only load DBICTest::Schema when needed in tests
[dbsrgits/DBIx-Class.git] / t / inflate / datetime_sybase.t
1 use strict;
2 use warnings;
3
4 use Test::More;
5 use Test::Exception;
6 use Scope::Guard ();
7 use Try::Tiny;
8 use DBIx::Class::Optional::Dependencies ();
9 use lib qw(t/lib);
10 use DBICTest;
11
12 plan skip_all => 'Test needs ' . DBIx::Class::Optional::Dependencies->req_missing_for ('test_dt')
13 . ' and ' .
14 DBIx::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');
17
18 my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_SYBASE_${_}" } qw/DSN USER PASS/};
19
20 if (not ($dsn && $user)) {
21   plan skip_all =>
22     'Set $ENV{DBICTEST_SYBASE_DSN}, _USER and _PASS to run this test' .
23     "\nWarning: This test drops and creates a table called 'track' and " .
24     "'event_small_dt'";
25 }
26
27 require DBICTest::Schema;
28 DBICTest::Schema->load_classes('EventSmallDT');
29
30 my @storage_types = (
31   'DBI::Sybase::ASE',
32   'DBI::Sybase::ASE::NoBindVars',
33 );
34 my $schema;
35
36 for my $storage_type (@storage_types) {
37   $schema = DBICTest::Schema->clone;
38
39   unless ($storage_type eq 'DBI::Sybase::ASE') { # autodetect
40     $schema->storage_type("::$storage_type");
41   }
42   $schema->connection($dsn, $user, $pass, {
43     on_connect_call => 'datetime_setup',
44   });
45
46   my $guard = Scope::Guard->new(sub { cleanup($schema) } );
47
48   $schema->storage->ensure_connected;
49
50   isa_ok( $schema->storage, "DBIx::Class::Storage::$storage_type" );
51
52   eval { $schema->storage->dbh->do("DROP TABLE track") };
53   $schema->storage->dbh->do(<<"SQL");
54 CREATE TABLE track (
55     trackid INT IDENTITY PRIMARY KEY,
56     cd INT NULL,
57     position INT NULL,
58     last_updated_at DATETIME NULL
59 )
60 SQL
61   eval { $schema->storage->dbh->do("DROP TABLE event_small_dt") };
62   $schema->storage->dbh->do(<<"SQL");
63 CREATE TABLE event_small_dt (
64     id INT IDENTITY PRIMARY KEY,
65     small_dt SMALLDATETIME NULL,
66 )
67 SQL
68
69 # coltype, column, source, pk, create_extra, datehash
70   my @dt_types = (
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     }],
97   );
98
99   for my $dt_type (@dt_types) {
100     my ($type, $col, $source, $pk, $create_extra, $sample_dt) = @$dt_type;
101
102     ok(my $dt = DateTime->new($sample_dt));
103
104     my $row;
105     ok( $row = $schema->resultset($source)->create({
106           $col => $dt,
107           %$create_extra,
108         }));
109     ok( $row = $schema->resultset($source)
110       ->search({ $pk => $row->$pk }, { select => [$col] })
111       ->first
112     );
113     is( $row->$col, $dt, "$type roundtrip" );
114
115     cmp_ok( $row->$col->nanosecond, '==', $sample_dt->{nanosecond},
116       'DateTime fractional portion roundtrip' )
117       if exists $sample_dt->{nanosecond};
118   }
119
120   # test a computed datetime column
121   eval { $schema->storage->dbh->do("DROP TABLE track") };
122   $schema->storage->dbh->do(<<"SQL");
123 CREATE TABLE track (
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(),
130 )
131 SQL
132
133   my $now = DateTime->now;
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';
141 }
142
143 done_testing;
144
145 # clean up our mess
146 sub cleanup {
147   my $schema = shift;
148   if (my $dbh = eval { $schema->storage->dbh }) {
149     $dbh->do('DROP TABLE track');
150     $dbh->do('DROP TABLE event_small_dt');
151   }
152 }