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