Commit | Line | Data |
d867eeda |
1 | use strict; |
68de9438 |
2 | use warnings; |
d867eeda |
3 | |
4 | use Test::More; |
5 | use Test::Exception; |
6 | use lib qw(t/lib); |
7 | use DBICTest; |
8 | |
9 | my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_SYBASE_${_}" } qw/DSN USER PASS/}; |
10 | |
11 | if (not ($dsn && $user)) { |
12 | plan skip_all => |
13 | 'Set $ENV{DBICTEST_SYBASE_DSN}, _USER and _PASS to run this test' . |
14 | "\nWarning: This test drops and creates a table called 'track'"; |
d867eeda |
15 | } |
16 | |
68de9438 |
17 | plan skip_all => 'Test needs ' . DBIx::Class::Optional::Dependencies->req_missing_for ('test_rdbms_ase') |
18 | unless DBIx::Class::Optional::Dependencies->req_ok_for ('test_rdbms_ase'); |
19 | |
d867eeda |
20 | my @storage_types = ( |
95787afe |
21 | 'DBI::Sybase::ASE', |
22 | 'DBI::Sybase::ASE::NoBindVars', |
d867eeda |
23 | ); |
24 | my $schema; |
25 | |
26 | for my $storage_type (@storage_types) { |
27 | $schema = DBICTest::Schema->clone; |
28 | |
95787afe |
29 | unless ($storage_type eq 'DBI::Sybase::ASE') { # autodetect |
d867eeda |
30 | $schema->storage_type("::$storage_type"); |
31 | } |
32 | $schema->connection($dsn, $user, $pass, { |
33 | AutoCommit => 1, |
34 | on_connect_call => [ 'datetime_setup' ], |
35 | }); |
36 | |
37 | $schema->storage->ensure_connected; |
38 | |
39 | isa_ok( $schema->storage, "DBIx::Class::Storage::$storage_type" ); |
40 | |
41 | # coltype, col, date |
42 | my @dt_types = ( |
43 | ['DATETIME', 'last_updated_at', '2004-08-21T14:36:48.080Z'], |
44 | # minute precision |
45 | ['SMALLDATETIME', 'small_dt', '2004-08-21T14:36:00.000Z'], |
46 | ); |
68de9438 |
47 | |
d867eeda |
48 | for my $dt_type (@dt_types) { |
49 | my ($type, $col, $sample_dt) = @$dt_type; |
50 | |
51 | eval { $schema->storage->dbh->do("DROP TABLE track") }; |
52 | $schema->storage->dbh->do(<<"SQL"); |
53 | CREATE TABLE track ( |
54 | trackid INT IDENTITY PRIMARY KEY, |
6469dabf |
55 | cd INT NULL, |
56 | position INT NULL, |
57 | $col $type NULL |
d867eeda |
58 | ) |
59 | SQL |
60 | ok(my $dt = DateTime::Format::Sybase->parse_datetime($sample_dt)); |
61 | |
62 | my $row; |
63 | ok( $row = $schema->resultset('Track')->create({ |
64 | $col => $dt, |
65 | cd => 1, |
66 | })); |
67 | ok( $row = $schema->resultset('Track') |
68 | ->search({ trackid => $row->trackid }, { select => [$col] }) |
69 | ->first |
70 | ); |
fb95dc4d |
71 | is( $row->$col, $dt, "$type roundtrip" ); |
72 | |
73 | is( $row->$col->nanosecond, $dt->nanosecond, |
74 | 'fractional DateTime portion roundtrip' ) |
75 | if $dt->nanosecond > 0; |
d867eeda |
76 | } |
6469dabf |
77 | |
78 | # test a computed datetime column |
79 | eval { $schema->storage->dbh->do("DROP TABLE track") }; |
80 | $schema->storage->dbh->do(<<"SQL"); |
81 | CREATE TABLE track ( |
82 | trackid INT IDENTITY PRIMARY KEY, |
83 | cd INT NULL, |
84 | position INT NULL, |
85 | title VARCHAR(100) NULL, |
86 | last_updated_on DATETIME NULL, |
87 | last_updated_at AS getdate(), |
88 | small_dt SMALLDATETIME NULL |
89 | ) |
90 | SQL |
91 | |
92 | my $now = DateTime->now; |
93 | sleep 1; |
94 | my $new_row = $schema->resultset('Track')->create({}); |
95 | $new_row->discard_changes; |
96 | |
97 | lives_and { |
98 | cmp_ok (($new_row->last_updated_at - $now)->seconds, '>=', 1) |
99 | } 'getdate() computed column works'; |
d867eeda |
100 | } |
101 | |
6469dabf |
102 | done_testing; |
103 | |
d867eeda |
104 | # clean up our mess |
105 | END { |
106 | if (my $dbh = eval { $schema->storage->_dbh }) { |
107 | $dbh->do('DROP TABLE track'); |
108 | } |
109 | } |