Add import-time-skip support to OptDeps, switch most tests over to that
[dbsrgits/DBIx-Class.git] / t / inflate / datetime.t
CommitLineData
cb551b07 1use DBIx::Class::Optional::Dependencies -skip_all_without => 'test_dt_sqlite';
2
40f75181 3use strict;
4use warnings;
5
6use Test::More;
e1038213 7use Test::Warn;
8use Try::Tiny;
40f75181 9use lib qw(t/lib);
10use DBICTest;
11
07d6018a 12# so user's env doesn't screw us
273f4cea 13delete $ENV{DBIC_DT_SEARCH_OK};
07d6018a 14
40f75181 15my $schema = DBICTest->init_schema();
16
40f75181 17# inflation test
18my $event = $schema->resultset("Event")->find(1);
19
20isa_ok($event->starts_at, 'DateTime', 'DateTime returned');
21
22# klunky, but makes older Test::More installs happy
23my $starts = $event->starts_at;
24is("$starts", '2006-04-25T22:24:33', 'Correct date/time');
25
07d6018a 26my $dt_warn_re = qr/DateTime objects.+not supported properly/;
27
e1038213 28my $row;
29
07d6018a 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 @_;
e1038213 35 };
07d6018a 36 $row = $schema->resultset('Event')->search({ starts_at => $starts })->single
37}
38
39warnings_exist {
40 $row = $schema->resultset('Event')->search({ starts_at => $starts })->single
41} [$dt_warn_re],
e1038213 42 'using a DateTime object in ->search generates a warning';
43
4ca1fd6f 44{
5ad836e4 45 local $TODO = "This stuff won't work without a -dt operator of some sort"
46 unless eval { require DBIx::Class::SQLMaker::DateOps };
40f75181 47
40f75181 48 is(eval { $row->id }, 1, 'DT in search');
49
07d6018a 50 local $ENV{DBIC_DT_SEARCH_OK} = 1;
e1038213 51
40f75181 52 ok($row =
e1038213 53 $schema->resultset('Event')->search({ starts_at => { '>=' => $starts } })
54 ->single);
55
40f75181 56 is(eval { $row->id }, 1, 'DT in search with condition');
57}
58
59# create using DateTime
60my $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});
64my $created_start = $created->starts_at;
65
66isa_ok($created->starts_at, 'DateTime', 'DateTime returned');
67is("$created_start", '2006-06-18T00:00:00', 'Correct date/time');
68
69## timestamp field
70isa_ok($event->created_on, 'DateTime', 'DateTime returned');
71
72## varchar fields
73isa_ok($event->varchar_date, 'DateTime', 'DateTime returned');
74isa_ok($event->varchar_datetime, 'DateTime', 'DateTime returned');
75
76## skip inflation field
77isnt(ref($event->skip_inflation), 'DateTime', 'No DateTime returned for skip inflation column');
78
79# klunky, but makes older Test::More installs happy
80my $createo = $event->created_on;
81is("$createo", '2006-06-22T21:00:05', 'Correct date/time');
82
83my $created_cron = $created->created_on;
84
85isa_ok($created->created_on, 'DateTime', 'DateTime returned');
86is("$created_cron", '2006-06-23T00:00:00', 'Correct date/time');
87
88## varchar field using inflate_date => 1
89my $varchar_date = $event->varchar_date;
90is("$varchar_date", '2006-07-23T00:00:00', 'Correct date/time');
91
92## varchar field using inflate_datetime => 1
93my $varchar_datetime = $event->varchar_datetime;
94is("$varchar_datetime", '2006-05-22T19:05:07', 'Correct date/time');
95
96## skip inflation field
97my $skip_inflation = $event->skip_inflation;
98is ("$skip_inflation", '2006-04-21 18:04:06', 'Correct date/time');
68de9438 99
3705e3b2 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
68de9438 148done_testing;