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