Fix overspecified msaccess test optdep (should not include icdt)
[dbsrgits/DBIx-Class.git] / t / inflate / datetime.t
1 use DBIx::Class::Optional::Dependencies -skip_all_without => 'test_dt_sqlite';
2
3 use strict;
4 use warnings;
5
6 use Test::More;
7 use Test::Warn;
8 use Try::Tiny;
9 use lib qw(t/lib);
10 use DBICTest;
11
12 # so user's env doesn't screw us
13 delete $ENV{DBIC_DT_SEARCH_OK};
14
15 my $schema = DBICTest->init_schema();
16
17 # inflation test
18 my $event = $schema->resultset("Event")->find(1);
19
20 isa_ok($event->starts_at, 'DateTime', 'DateTime returned');
21
22 # klunky, but makes older Test::More installs happy
23 my $starts = $event->starts_at;
24 is("$starts", '2006-04-25T22:24:33', 'Correct date/time');
25
26 my $dt_warn_re = qr/DateTime objects.+not supported properly/;
27
28 my $row;
29
30 {
31   local $ENV{DBIC_DT_SEARCH_OK} = 1;
32   local $SIG{__WARN__} = sub {
33     fail('Disabled warning still issued') if $_[0] =~ $dt_warn_re;
34     warn @_;
35   };
36   $row = $schema->resultset('Event')->search({ starts_at => $starts })->single
37 }
38
39 warnings_exist {
40   $row = $schema->resultset('Event')->search({ starts_at => $starts })->single
41 } [$dt_warn_re],
42   'using a DateTime object in ->search generates a warning';
43
44 {
45   local $TODO = "This stuff won't work without a -dt operator of some sort"
46     unless eval { require DBIx::Class::SQLMaker::DateOps };
47
48   is(eval { $row->id }, 1, 'DT in search');
49
50   local $ENV{DBIC_DT_SEARCH_OK} = 1;
51
52   ok($row =
53     $schema->resultset('Event')->search({ starts_at => { '>=' => $starts } })
54     ->single);
55
56   is(eval { $row->id }, 1, 'DT in search with condition');
57 }
58
59 # create using DateTime
60 my $created = $schema->resultset('Event')->create({
61     starts_at => DateTime->new(year=>2006, month=>6, day=>18),
62     created_on => DateTime->new(year=>2006, month=>6, day=>23)
63 });
64 my $created_start = $created->starts_at;
65
66 isa_ok($created->starts_at, 'DateTime', 'DateTime returned');
67 is("$created_start", '2006-06-18T00:00:00', 'Correct date/time');
68
69 ## timestamp field
70 isa_ok($event->created_on, 'DateTime', 'DateTime returned');
71
72 ## varchar fields
73 isa_ok($event->varchar_date, 'DateTime', 'DateTime returned');
74 isa_ok($event->varchar_datetime, 'DateTime', 'DateTime returned');
75
76 ## skip inflation field
77 isnt(ref($event->skip_inflation), 'DateTime', 'No DateTime returned for skip inflation column');
78
79 # klunky, but makes older Test::More installs happy
80 my $createo = $event->created_on;
81 is("$createo", '2006-06-22T21:00:05', 'Correct date/time');
82
83 my $created_cron = $created->created_on;
84
85 isa_ok($created->created_on, 'DateTime', 'DateTime returned');
86 is("$created_cron", '2006-06-23T00:00:00', 'Correct date/time');
87
88 ## varchar field using inflate_date => 1
89 my $varchar_date = $event->varchar_date;
90 is("$varchar_date", '2006-07-23T00:00:00', 'Correct date/time');
91
92 ## varchar field using inflate_datetime => 1
93 my $varchar_datetime = $event->varchar_datetime;
94 is("$varchar_datetime", '2006-05-22T19:05:07', 'Correct date/time');
95
96 ## skip inflation field
97 my $skip_inflation = $event->skip_inflation;
98 is ("$skip_inflation", '2006-04-21 18:04:06', 'Correct date/time');
99
100 # create and update with literals
101 {
102   my $d = {
103     created_on => \ '2001-09-11',
104     starts_at => \[ '?' => '2001-10-26' ],
105   };
106
107   my $ev = $schema->resultset('Event')->create($d);
108
109   for my $col (qw(created_on starts_at)) {
110     ok (ref $ev->$col, "literal untouched in $col");
111     is_deeply( $ev->$col, $d->{$col});
112     is_deeply( $ev->get_inflated_column($col), $d->{$col});
113     is_deeply( $ev->get_column($col), $d->{$col});
114   }
115
116   $ev->discard_changes;
117
118   is_deeply(
119     { $ev->get_dirty_columns },
120     {}
121   );
122
123   for my $col (qw(created_on starts_at)) {
124     isa_ok ($ev->$col, "DateTime", "$col properly inflated on retrieve");
125   }
126
127   for my $meth (qw(set_inflated_columns set_columns)) {
128
129     $ev->$meth({%$d});
130
131     is_deeply(
132       { $ev->get_dirty_columns },
133       $d,
134       "Expected dirty cols after setting literals via $meth",
135     );
136
137     $ev->update;
138
139     for my $col (qw(created_on starts_at)) {
140       ok (ref $ev->$col, "literal untouched in $col updated via $meth");
141       is_deeply( $ev->$col, $d->{$col});
142       is_deeply( $ev->get_inflated_column($col), $d->{$col});
143       is_deeply( $ev->get_column($col), $d->{$col});
144     }
145   }
146 }
147
148 done_testing;