Commit | Line | Data |
c0329273 |
1 | BEGIN { do "./t/lib/ANFANG.pm" or die ( $@ || $! ) } |
54a9a088 |
2 | use DBIx::Class::Optional::Dependencies -skip_all_without => qw( ic_dt test_rdbms_ase ); |
cb551b07 |
3 | |
d867eeda |
4 | use strict; |
68de9438 |
5 | use warnings; |
d867eeda |
6 | |
7 | use Test::More; |
8 | use Test::Exception; |
514b84f6 |
9 | use DBIx::Class::_Util qw( scope_guard set_subname ); |
c0329273 |
10 | |
d867eeda |
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 | |
bbf6a9a5 |
33 | my $guard = scope_guard { 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) |
11f335cd |
97 | ->search({ $pk => $row->$pk }, { select => [$pk, $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}; |
11f335cd |
105 | |
106 | # Testing an ugly half-solution |
107 | # |
108 | # copy() uses get_columns() |
109 | # |
110 | # The values should survive a roundtrip also, but they don't |
111 | # because the Sybase ICDT setup is asymmetric |
112 | # One *has* to force an inflation/deflation cycle to make the |
113 | # values usable to the database |
114 | # |
115 | # This can be done by marking the columns as dirty, and there |
116 | # are tests for this already in t/inflate/serialize.t |
117 | # |
118 | # But even this isn't enough - one has to reload the RDBMS-formatted |
119 | # values once done, otherwise the copy is just as useless... sigh |
120 | # |
121 | # Adding the test here to validate the technique works |
122 | # UGH! |
123 | { |
124 | no warnings 'once'; |
514b84f6 |
125 | local *DBICTest::BaseResult::copy = set_subname 'DBICTest::BaseResult::copy' => sub { |
11f335cd |
126 | my $self = shift; |
127 | |
128 | $self->make_column_dirty($_) for keys %{{ $self->get_inflated_columns }}; |
129 | |
130 | my $cp = $self->next::method(@_); |
131 | |
132 | $cp->discard_changes({ columns => [ keys %{{ $cp->get_columns }} ] }); |
133 | }; |
134 | Class::C3->reinitialize if DBIx::Class::_ENV_::OLD_MRO; |
135 | |
136 | my $cp = $row->copy; |
137 | ok( $cp->in_storage ); |
138 | is( $cp->$col, $dt, "$type copy logical roundtrip" ); |
139 | |
140 | $cp->discard_changes({ select => [ $pk, $col ] }); |
141 | is( $cp->$col, $dt, "$type copy server roundtrip" ); |
142 | } |
143 | |
144 | Class::C3->reinitialize if DBIx::Class::_ENV_::OLD_MRO; |
d867eeda |
145 | } |
6469dabf |
146 | |
147 | # test a computed datetime column |
148 | eval { $schema->storage->dbh->do("DROP TABLE track") }; |
149 | $schema->storage->dbh->do(<<"SQL"); |
150 | CREATE TABLE track ( |
3d98c75e |
151 | trackid INT IDENTITY PRIMARY KEY, |
152 | cd INT NULL, |
153 | position INT NULL, |
154 | title VARCHAR(100) NULL, |
155 | last_updated_on DATETIME NULL, |
156 | last_updated_at AS getdate(), |
6469dabf |
157 | ) |
158 | SQL |
159 | |
3d98c75e |
160 | my $now = DateTime->now; |
6469dabf |
161 | sleep 1; |
162 | my $new_row = $schema->resultset('Track')->create({}); |
163 | $new_row->discard_changes; |
164 | |
165 | lives_and { |
166 | cmp_ok (($new_row->last_updated_at - $now)->seconds, '>=', 1) |
167 | } 'getdate() computed column works'; |
d867eeda |
168 | } |
169 | |
6469dabf |
170 | done_testing; |
171 | |
d867eeda |
172 | # clean up our mess |
3d98c75e |
173 | sub cleanup { |
65d35121 |
174 | my $schema = shift; |
3d98c75e |
175 | if (my $dbh = eval { $schema->storage->dbh }) { |
d867eeda |
176 | $dbh->do('DROP TABLE track'); |
3d98c75e |
177 | $dbh->do('DROP TABLE event_small_dt'); |
d867eeda |
178 | } |
179 | } |