Fix populate with an emply ([]) has_many
[dbsrgits/DBIx-Class.git] / t / inflate / datetime_mssql.t
CommitLineData
5a77aa8b 1use strict;
dd572bad 2use warnings;
5a77aa8b 3
4use Test::More;
5use Test::Exception;
fb95dc4d 6use Scope::Guard ();
3d98c75e 7use Try::Tiny;
5a77aa8b 8use lib qw(t/lib);
9use DBICTest;
10
3d98c75e 11DBICTest::Schema->load_classes('EventSmallDT');
12
fb95dc4d 13# use this if you keep a copy of DBD::Sybase linked to FreeTDS somewhere else
14BEGIN {
15 if (my $lib_dirs = $ENV{DBICTEST_MSSQL_PERL5LIB}) {
16 unshift @INC, $_ for split /:/, $lib_dirs;
17 }
18}
5a77aa8b 19
fb95dc4d 20my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_MSSQL_ODBC_${_}" } qw/DSN USER PASS/};
21my ($dsn2, $user2, $pass2) = @ENV{map { "DBICTEST_MSSQL_${_}" } qw/DSN USER PASS/};
22
23if (not ($dsn || $dsn2)) {
5a77aa8b 24 plan skip_all =>
fb95dc4d 25 'Set $ENV{DBICTEST_MSSQL_ODBC_DSN} and/or $ENV{DBICTEST_MSSQL_DSN} _USER '
26 .'and _PASS to run this test' .
3d98c75e 27 "\nWarning: This test drops and creates a table called 'small_dt'";
5a77aa8b 28}
29
68de9438 30plan skip_all => 'Test needs ' . DBIx::Class::Optional::Dependencies->req_missing_for ('test_dt')
31 unless DBIx::Class::Optional::Dependencies->req_ok_for ('test_dt');
32
fb95dc4d 33my @connect_info = (
34 [ $dsn, $user, $pass ],
35 [ $dsn2, $user2, $pass2 ],
36);
37
38my $schema;
39
dd572bad 40SKIP:
fb95dc4d 41for my $connect_info (@connect_info) {
42 my ($dsn, $user, $pass) = @$connect_info;
43
44 next unless $dsn;
5a77aa8b 45
fb95dc4d 46 $schema = DBICTest::Schema->connect($dsn, $user, $pass, {
47 on_connect_call => 'datetime_setup'
48 });
49
dd572bad 50 {
51 my $w;
52 local $SIG{__WARN__} = sub { $w = shift };
53 $schema->storage->ensure_connected;
54 if ($w =~ /Your DBD::Sybase is too old to support DBIx::Class::InflateColumn::DateTime/) {
55 skip "Skipping tests on old DBD::Sybase " . DBD::Sybase->VERSION, 1;
56 }
57 }
58
fb95dc4d 59 my $guard = Scope::Guard->new(\&cleanup);
5a77aa8b 60
3d98c75e 61 try { $schema->storage->dbh->do("DROP TABLE track") };
62 $schema->storage->dbh->do(<<"SQL");
63CREATE TABLE track (
64 trackid INT IDENTITY PRIMARY KEY,
65 cd INT,
66 position INT,
67 last_updated_at DATETIME,
68)
69SQL
70 try { $schema->storage->dbh->do("DROP TABLE event_small_dt") };
71 $schema->storage->dbh->do(<<"SQL");
72CREATE TABLE event_small_dt (
73 id INT IDENTITY PRIMARY KEY,
74 small_dt SMALLDATETIME,
75)
76SQL
77
78# coltype, column, source, pk, create_extra, datehash
fb95dc4d 79 my @dt_types = (
80 ['DATETIME',
81 'last_updated_at',
3d98c75e 82 'Track',
83 'trackid',
84 { cd => 1 },
fb95dc4d 85 {
86 year => 2004,
87 month => 8,
88 day => 21,
89 hour => 14,
90 minute => 36,
91 second => 48,
92 nanosecond => 500000000,
93 }],
94 ['SMALLDATETIME', # minute precision
95 'small_dt',
3d98c75e 96 'EventSmallDT',
97 'id',
98 {},
fb95dc4d 99 {
100 year => 2004,
101 month => 8,
102 day => 21,
103 hour => 14,
104 minute => 36,
105 }],
106 );
5a77aa8b 107
fb95dc4d 108 for my $dt_type (@dt_types) {
3d98c75e 109 my ($type, $col, $source, $pk, $create_extra, $sample_dt) = @$dt_type;
5a77aa8b 110
fb95dc4d 111 ok(my $dt = DateTime->new($sample_dt));
112
113 my $row;
3d98c75e 114 ok( $row = $schema->resultset($source)->create({
fb95dc4d 115 $col => $dt,
3d98c75e 116 %$create_extra,
fb95dc4d 117 }));
3d98c75e 118 ok( $row = $schema->resultset($source)
119 ->search({ $pk => $row->$pk }, { select => [$col] })
fb95dc4d 120 ->first
121 );
122 is( $row->$col, $dt, "$type roundtrip" );
0f72fb47 123
124 cmp_ok( $row->$col->nanosecond, '==', $sample_dt->{nanosecond},
fb95dc4d 125 'DateTime fractional portion roundtrip' )
126 if exists $sample_dt->{nanosecond};
127 }
5a77aa8b 128}
129
fb95dc4d 130done_testing;
131
5a77aa8b 132# clean up our mess
fb95dc4d 133sub cleanup {
134 if (my $dbh = eval { $schema->storage->dbh }) {
5a77aa8b 135 $dbh->do('DROP TABLE track');
3d98c75e 136 $dbh->do('DROP TABLE event_small_dt');
5a77aa8b 137 }
138}