Multiple optimizations of $rs->populate
[dbsrgits/DBIx-Class.git] / t / inflate / datetime_sybase.t
CommitLineData
d867eeda 1use strict;
68de9438 2use warnings;
d867eeda 3
4use Test::More;
5use Test::Exception;
3d98c75e 6use Scope::Guard ();
7use Try::Tiny;
d867eeda 8use lib qw(t/lib);
9use DBICTest;
10
3d98c75e 11DBICTest::Schema->load_classes('EventSmallDT');
12
d867eeda 13my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_SYBASE_${_}" } qw/DSN USER PASS/};
14
15if (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'";
d867eeda 19}
20
68de9438 21plan 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
d867eeda 24my @storage_types = (
95787afe 25 'DBI::Sybase::ASE',
26 'DBI::Sybase::ASE::NoBindVars',
d867eeda 27);
28my $schema;
29
30for my $storage_type (@storage_types) {
31 $schema = DBICTest::Schema->clone;
32
95787afe 33 unless ($storage_type eq 'DBI::Sybase::ASE') { # autodetect
d867eeda 34 $schema->storage_type("::$storage_type");
35 }
36 $schema->connection($dsn, $user, $pass, {
3d98c75e 37 on_connect_call => 'datetime_setup',
d867eeda 38 });
39
3d98c75e 40 my $guard = Scope::Guard->new(\&cleanup);
41
d867eeda 42 $schema->storage->ensure_connected;
43
44 isa_ok( $schema->storage, "DBIx::Class::Storage::$storage_type" );
45
3d98c75e 46 eval { $schema->storage->dbh->do("DROP TABLE track") };
47 $schema->storage->dbh->do(<<"SQL");
48CREATE TABLE track (
49 trackid INT IDENTITY PRIMARY KEY,
50 cd INT NULL,
51 position INT NULL,
52 last_updated_at DATETIME NULL
53)
54SQL
55 eval { $schema->storage->dbh->do("DROP TABLE event_small_dt") };
56 $schema->storage->dbh->do(<<"SQL");
57CREATE TABLE event_small_dt (
58 id INT IDENTITY PRIMARY KEY,
59 small_dt SMALLDATETIME NULL,
60)
61SQL
62
63# coltype, column, source, pk, create_extra, datehash
d867eeda 64 my @dt_types = (
3d98c75e 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 }],
d867eeda 91 );
68de9438 92
d867eeda 93 for my $dt_type (@dt_types) {
3d98c75e 94 my ($type, $col, $source, $pk, $create_extra, $sample_dt) = @$dt_type;
d867eeda 95
3d98c75e 96 ok(my $dt = DateTime->new($sample_dt));
d867eeda 97
98 my $row;
3d98c75e 99 ok( $row = $schema->resultset($source)->create({
d867eeda 100 $col => $dt,
3d98c75e 101 %$create_extra,
d867eeda 102 }));
3d98c75e 103 ok( $row = $schema->resultset($source)
104 ->search({ $pk => $row->$pk }, { select => [$col] })
d867eeda 105 ->first
106 );
fb95dc4d 107 is( $row->$col, $dt, "$type roundtrip" );
108
3d98c75e 109 cmp_ok( $row->$col->nanosecond, '==', $sample_dt->{nanosecond},
110 'DateTime fractional portion roundtrip' )
111 if exists $sample_dt->{nanosecond};
d867eeda 112 }
6469dabf 113
114 # test a computed datetime column
115 eval { $schema->storage->dbh->do("DROP TABLE track") };
116 $schema->storage->dbh->do(<<"SQL");
117CREATE TABLE track (
3d98c75e 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(),
6469dabf 124)
125SQL
126
3d98c75e 127 my $now = DateTime->now;
6469dabf 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';
d867eeda 135}
136
6469dabf 137done_testing;
138
d867eeda 139# clean up our mess
3d98c75e 140sub cleanup {
141 if (my $dbh = eval { $schema->storage->dbh }) {
d867eeda 142 $dbh->do('DROP TABLE track');
3d98c75e 143 $dbh->do('DROP TABLE event_small_dt');
d867eeda 144 }
145}