Centralize remaining uses of Sub::Name within _Util
[dbsrgits/DBIx-Class.git] / t / icdt / engine_specific / sybase.t
1 BEGIN { do "./t/lib/ANFANG.pm" or die ( $@ || $! ) }
2 use DBIx::Class::Optional::Dependencies -skip_all_without => qw( ic_dt test_rdbms_ase );
3
4 use strict;
5 use warnings;
6
7 use Test::More;
8 use Test::Exception;
9 use DBIx::Class::_Util qw( scope_guard set_subname );
10
11 use DBICTest;
12
13 my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_SYBASE_${_}" } qw/DSN USER PASS/};
14
15 DBICTest::Schema->load_classes('EventSmallDT');
16
17 my @storage_types = (
18   'DBI::Sybase::ASE',
19   'DBI::Sybase::ASE::NoBindVars',
20 );
21 my $schema;
22
23 for my $storage_type (@storage_types) {
24   $schema = DBICTest::Schema->clone;
25
26   unless ($storage_type eq 'DBI::Sybase::ASE') { # autodetect
27     $schema->storage_type("::$storage_type");
28   }
29   $schema->connection($dsn, $user, $pass, {
30     on_connect_call => 'datetime_setup',
31   });
32
33   my $guard = scope_guard { cleanup($schema) };
34
35   $schema->storage->ensure_connected;
36
37   isa_ok( $schema->storage, "DBIx::Class::Storage::$storage_type" );
38
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
57   my @dt_types = (
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     }],
84   );
85
86   for my $dt_type (@dt_types) {
87     my ($type, $col, $source, $pk, $create_extra, $sample_dt) = @$dt_type;
88
89     ok(my $dt = DateTime->new($sample_dt));
90
91     my $row;
92     ok( $row = $schema->resultset($source)->create({
93           $col => $dt,
94           %$create_extra,
95         }));
96     ok( $row = $schema->resultset($source)
97       ->search({ $pk => $row->$pk }, { select => [$pk, $col] })
98       ->first
99     );
100     is( $row->$col, $dt, "$type roundtrip" );
101
102     cmp_ok( $row->$col->nanosecond, '==', $sample_dt->{nanosecond},
103       'DateTime fractional portion roundtrip' )
104       if exists $sample_dt->{nanosecond};
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';
125       local *DBICTest::BaseResult::copy = set_subname 'DBICTest::BaseResult::copy' => sub {
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;
145   }
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 (
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(),
157 )
158 SQL
159
160   my $now = DateTime->now;
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';
168 }
169
170 done_testing;
171
172 # clean up our mess
173 sub cleanup {
174   my $schema = shift;
175   if (my $dbh = eval { $schema->storage->dbh }) {
176     $dbh->do('DROP TABLE track');
177     $dbh->do('DROP TABLE event_small_dt');
178   }
179 }