Add a proof of concept test for copy() with assymetric IC::DT
[dbsrgits/DBIx-Class.git] / t / icdt / engine_specific / sybase.t
CommitLineData
c0329273 1BEGIN { do "./t/lib/ANFANG.pm" or die ( $@ || $! ) }
54a9a088 2use DBIx::Class::Optional::Dependencies -skip_all_without => qw( ic_dt test_rdbms_ase );
cb551b07 3
d867eeda 4use strict;
68de9438 5use warnings;
d867eeda 6
7use Test::More;
8use Test::Exception;
bbf6a9a5 9use DBIx::Class::_Util 'scope_guard';
11f335cd 10use Sub::Name;
c0329273 11
d867eeda 12use DBICTest;
13
14my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_SYBASE_${_}" } qw/DSN USER PASS/};
15
199fbc45 16DBICTest::Schema->load_classes('EventSmallDT');
68de9438 17
d867eeda 18my @storage_types = (
95787afe 19 'DBI::Sybase::ASE',
20 'DBI::Sybase::ASE::NoBindVars',
d867eeda 21);
22my $schema;
23
24for my $storage_type (@storage_types) {
25 $schema = DBICTest::Schema->clone;
26
95787afe 27 unless ($storage_type eq 'DBI::Sybase::ASE') { # autodetect
d867eeda 28 $schema->storage_type("::$storage_type");
29 }
30 $schema->connection($dsn, $user, $pass, {
3d98c75e 31 on_connect_call => 'datetime_setup',
d867eeda 32 });
33
bbf6a9a5 34 my $guard = scope_guard { cleanup($schema) };
3d98c75e 35
d867eeda 36 $schema->storage->ensure_connected;
37
38 isa_ok( $schema->storage, "DBIx::Class::Storage::$storage_type" );
39
3d98c75e 40 eval { $schema->storage->dbh->do("DROP TABLE track") };
41 $schema->storage->dbh->do(<<"SQL");
42CREATE TABLE track (
43 trackid INT IDENTITY PRIMARY KEY,
44 cd INT NULL,
45 position INT NULL,
46 last_updated_at DATETIME NULL
47)
48SQL
49 eval { $schema->storage->dbh->do("DROP TABLE event_small_dt") };
50 $schema->storage->dbh->do(<<"SQL");
51CREATE TABLE event_small_dt (
52 id INT IDENTITY PRIMARY KEY,
53 small_dt SMALLDATETIME NULL,
54)
55SQL
56
57# coltype, column, source, pk, create_extra, datehash
d867eeda 58 my @dt_types = (
3d98c75e 59 ['DATETIME',
60 'last_updated_at',
61 'Track',
62 'trackid',
63 { cd => 1 },
64 {
65 year => 2004,
66 month => 8,
67 day => 21,
68 hour => 14,
69 minute => 36,
70 second => 48,
71 nanosecond => 500000000,
72 }],
73 ['SMALLDATETIME', # minute precision
74 'small_dt',
75 'EventSmallDT',
76 'id',
77 {},
78 {
79 year => 2004,
80 month => 8,
81 day => 21,
82 hour => 14,
83 minute => 36,
84 }],
d867eeda 85 );
68de9438 86
d867eeda 87 for my $dt_type (@dt_types) {
3d98c75e 88 my ($type, $col, $source, $pk, $create_extra, $sample_dt) = @$dt_type;
d867eeda 89
3d98c75e 90 ok(my $dt = DateTime->new($sample_dt));
d867eeda 91
92 my $row;
3d98c75e 93 ok( $row = $schema->resultset($source)->create({
d867eeda 94 $col => $dt,
3d98c75e 95 %$create_extra,
d867eeda 96 }));
3d98c75e 97 ok( $row = $schema->resultset($source)
11f335cd 98 ->search({ $pk => $row->$pk }, { select => [$pk, $col] })
d867eeda 99 ->first
100 );
fb95dc4d 101 is( $row->$col, $dt, "$type roundtrip" );
102
3d98c75e 103 cmp_ok( $row->$col->nanosecond, '==', $sample_dt->{nanosecond},
104 'DateTime fractional portion roundtrip' )
105 if exists $sample_dt->{nanosecond};
11f335cd 106
107 # Testing an ugly half-solution
108 #
109 # copy() uses get_columns()
110 #
111 # The values should survive a roundtrip also, but they don't
112 # because the Sybase ICDT setup is asymmetric
113 # One *has* to force an inflation/deflation cycle to make the
114 # values usable to the database
115 #
116 # This can be done by marking the columns as dirty, and there
117 # are tests for this already in t/inflate/serialize.t
118 #
119 # But even this isn't enough - one has to reload the RDBMS-formatted
120 # values once done, otherwise the copy is just as useless... sigh
121 #
122 # Adding the test here to validate the technique works
123 # UGH!
124 {
125 no warnings 'once';
126 local *DBICTest::BaseResult::copy = subname 'DBICTest::BaseResult::copy' => sub {
127 my $self = shift;
128
129 $self->make_column_dirty($_) for keys %{{ $self->get_inflated_columns }};
130
131 my $cp = $self->next::method(@_);
132
133 $cp->discard_changes({ columns => [ keys %{{ $cp->get_columns }} ] });
134 };
135 Class::C3->reinitialize if DBIx::Class::_ENV_::OLD_MRO;
136
137 my $cp = $row->copy;
138 ok( $cp->in_storage );
139 is( $cp->$col, $dt, "$type copy logical roundtrip" );
140
141 $cp->discard_changes({ select => [ $pk, $col ] });
142 is( $cp->$col, $dt, "$type copy server roundtrip" );
143 }
144
145 Class::C3->reinitialize if DBIx::Class::_ENV_::OLD_MRO;
d867eeda 146 }
6469dabf 147
148 # test a computed datetime column
149 eval { $schema->storage->dbh->do("DROP TABLE track") };
150 $schema->storage->dbh->do(<<"SQL");
151CREATE TABLE track (
3d98c75e 152 trackid INT IDENTITY PRIMARY KEY,
153 cd INT NULL,
154 position INT NULL,
155 title VARCHAR(100) NULL,
156 last_updated_on DATETIME NULL,
157 last_updated_at AS getdate(),
6469dabf 158)
159SQL
160
3d98c75e 161 my $now = DateTime->now;
6469dabf 162 sleep 1;
163 my $new_row = $schema->resultset('Track')->create({});
164 $new_row->discard_changes;
165
166 lives_and {
167 cmp_ok (($new_row->last_updated_at - $now)->seconds, '>=', 1)
168 } 'getdate() computed column works';
d867eeda 169}
170
6469dabf 171done_testing;
172
d867eeda 173# clean up our mess
3d98c75e 174sub cleanup {
65d35121 175 my $schema = shift;
3d98c75e 176 if (my $dbh = eval { $schema->storage->dbh }) {
d867eeda 177 $dbh->do('DROP TABLE track');
3d98c75e 178 $dbh->do('DROP TABLE event_small_dt');
d867eeda 179 }
180}