Remove small_datetime from the main schema - it is not a standard datatype
[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 lib qw(t/lib);
9 use DBICTest;
10
11 DBICTest::Schema->load_classes('EventSmallDT');
12
13 my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_SYBASE_${_}" } qw/DSN USER PASS/};
14
15 if (not ($dsn && $user)) {
16   plan skip_all =>
17     'Set $ENV{DBICTEST_SYBASE_DSN}, _USER and _PASS to run this test' .
18     "\nWarning: This test drops and creates a table called 'track'";
19 }
20
21 plan skip_all => 'Test needs ' . DBIx::Class::Optional::Dependencies->req_missing_for ('test_rdbms_ase')
22   unless DBIx::Class::Optional::Dependencies->req_ok_for ('test_rdbms_ase');
23
24 my @storage_types = (
25   'DBI::Sybase::ASE',
26   'DBI::Sybase::ASE::NoBindVars',
27 );
28 my $schema;
29
30 for my $storage_type (@storage_types) {
31   $schema = DBICTest::Schema->clone;
32
33   unless ($storage_type eq 'DBI::Sybase::ASE') { # autodetect
34     $schema->storage_type("::$storage_type");
35   }
36   $schema->connection($dsn, $user, $pass, {
37     on_connect_call => 'datetime_setup',
38   });
39
40   my $guard = Scope::Guard->new(\&cleanup);
41
42   $schema->storage->ensure_connected;
43
44   isa_ok( $schema->storage, "DBIx::Class::Storage::$storage_type" );
45
46   eval { $schema->storage->dbh->do("DROP TABLE track") };
47   $schema->storage->dbh->do(<<"SQL");
48 CREATE TABLE track (
49     trackid INT IDENTITY PRIMARY KEY,
50     cd INT NULL,
51     position INT NULL,
52     last_updated_at DATETIME NULL
53 )
54 SQL
55   eval { $schema->storage->dbh->do("DROP TABLE event_small_dt") };
56   $schema->storage->dbh->do(<<"SQL");
57 CREATE TABLE event_small_dt (
58     id INT IDENTITY PRIMARY KEY,
59     small_dt SMALLDATETIME NULL,
60 )
61 SQL
62
63 # coltype, column, source, pk, create_extra, datehash
64   my @dt_types = (
65     ['DATETIME',
66      'last_updated_at',
67      'Track',
68      'trackid',
69      { cd => 1 },
70      {
71       year => 2004,
72       month => 8,
73       day => 21,
74       hour => 14,
75       minute => 36,
76       second => 48,
77       nanosecond => 500000000,
78     }],
79     ['SMALLDATETIME', # minute precision
80      'small_dt',
81      'EventSmallDT',
82      'id',
83      {},
84      {
85       year => 2004,
86       month => 8,
87       day => 21,
88       hour => 14,
89       minute => 36,
90     }],
91   );
92
93   for my $dt_type (@dt_types) {
94     my ($type, $col, $source, $pk, $create_extra, $sample_dt) = @$dt_type;
95
96     ok(my $dt = DateTime->new($sample_dt));
97
98     my $row;
99     ok( $row = $schema->resultset($source)->create({
100           $col => $dt,
101           %$create_extra,
102         }));
103     ok( $row = $schema->resultset($source)
104       ->search({ $pk => $row->$pk }, { select => [$col] })
105       ->first
106     );
107     is( $row->$col, $dt, "$type roundtrip" );
108
109     cmp_ok( $row->$col->nanosecond, '==', $sample_dt->{nanosecond},
110       'DateTime fractional portion roundtrip' )
111       if exists $sample_dt->{nanosecond};
112   }
113
114   # test a computed datetime column
115   eval { $schema->storage->dbh->do("DROP TABLE track") };
116   $schema->storage->dbh->do(<<"SQL");
117 CREATE TABLE track (
118     trackid INT IDENTITY PRIMARY KEY,
119     cd INT NULL,
120     position INT NULL,
121     title VARCHAR(100) NULL,
122     last_updated_on DATETIME NULL,
123     last_updated_at AS getdate(),
124 )
125 SQL
126
127   my $now = DateTime->now;
128   sleep 1;
129   my $new_row = $schema->resultset('Track')->create({});
130   $new_row->discard_changes;
131
132   lives_and {
133     cmp_ok (($new_row->last_updated_at - $now)->seconds, '>=', 1)
134   } 'getdate() computed column works';
135 }
136
137 done_testing;
138
139 # clean up our mess
140 sub cleanup {
141   if (my $dbh = eval { $schema->storage->dbh }) {
142     $dbh->do('DROP TABLE track');
143     $dbh->do('DROP TABLE event_small_dt');
144   }
145 }