f9b321036e3e9c0b0242742e1b6eeb1b78d7c476
[dbsrgits/DBIx-Class.git] / t / icdt / engine_specific / sqlite.t
1 use DBIx::Class::Optional::Dependencies -skip_all_without => qw( ic_dt test_rdbms_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 # Test offline parser determination (formerly t/inflate/datetime_determine_parser.t)
13 {
14   my $schema = DBICTest->init_schema(
15     no_deploy => 1, # Deploying would cause an early rebless
16   );
17
18   my $storage = $schema->storage;
19
20   if ($ENV{DBICTEST_VIA_REPLICATED}) {
21     $storage = $storage->master;
22   }
23   else {
24     is(
25       ref $storage, 'DBIx::Class::Storage::DBI',
26       'Starting with generic storage'
27     );
28   }
29
30   # Calling date_time_parser should cause the storage to be reblessed,
31   # so that we can pick up datetime_parser_type from subclasses
32   my $parser = $storage->datetime_parser();
33
34   is($parser, 'DateTime::Format::SQLite', 'Got expected storage-set datetime_parser');
35   isa_ok($storage, 'DBIx::Class::Storage::DBI::SQLite', 'storage');
36
37   ok(! $storage->connected, 'Not yet connected');
38 }
39
40 # so user's env doesn't screw us
41 delete $ENV{DBIC_DT_SEARCH_OK};
42
43 my $schema = DBICTest->init_schema();
44
45 # inflation test
46 my $event = $schema->resultset("Event")->find(1);
47
48 isa_ok($event->starts_at, 'DateTime', 'DateTime returned');
49
50 # klunky, but makes older Test::More installs happy
51 my $starts = $event->starts_at;
52 is("$starts", '2006-04-25T22:24:33', 'Correct date/time');
53
54 my $dt_warn_re = qr/DateTime objects.+not supported properly/;
55
56 my $row;
57
58 {
59   local $ENV{DBIC_DT_SEARCH_OK} = 1;
60   local $SIG{__WARN__} = sub {
61     fail('Disabled warning still issued') if $_[0] =~ $dt_warn_re;
62     warn @_;
63   };
64   $row = $schema->resultset('Event')->search({ starts_at => $starts })->single
65 }
66
67 warnings_exist {
68   $row = $schema->resultset('Event')->search({ starts_at => $starts })->single
69 } [$dt_warn_re],
70   'using a DateTime object in ->search generates a warning';
71
72 {
73   local $TODO = "This stuff won't work without a -dt operator of some sort"
74     unless eval { require DBIx::Class::SQLMaker::DateOps };
75
76   is(eval { $row->id }, 1, 'DT in search');
77
78   local $ENV{DBIC_DT_SEARCH_OK} = 1;
79
80   ok($row =
81     $schema->resultset('Event')->search({ starts_at => { '>=' => $starts } })
82     ->single);
83
84   is(eval { $row->id }, 1, 'DT in search with condition');
85 }
86
87 # create using DateTime
88 my $created = $schema->resultset('Event')->create({
89     starts_at => DateTime->new(year=>2006, month=>6, day=>18),
90     created_on => DateTime->new(year=>2006, month=>6, day=>23)
91 });
92 my $created_start = $created->starts_at;
93
94 isa_ok($created->starts_at, 'DateTime', 'DateTime returned');
95 is("$created_start", '2006-06-18T00:00:00', 'Correct date/time');
96
97 ## timestamp field
98 isa_ok($event->created_on, 'DateTime', 'DateTime returned');
99
100 ## varchar fields
101 isa_ok($event->varchar_date, 'DateTime', 'DateTime returned');
102 isa_ok($event->varchar_datetime, 'DateTime', 'DateTime returned');
103
104 ## skip inflation field
105 isnt(ref($event->skip_inflation), 'DateTime', 'No DateTime returned for skip inflation column');
106
107 # klunky, but makes older Test::More installs happy
108 my $createo = $event->created_on;
109 is("$createo", '2006-06-22T21:00:05', 'Correct date/time');
110
111 my $created_cron = $created->created_on;
112
113 isa_ok($created->created_on, 'DateTime', 'DateTime returned');
114 is("$created_cron", '2006-06-23T00:00:00', 'Correct date/time');
115
116 ## varchar field using inflate_date => 1
117 my $varchar_date = $event->varchar_date;
118 is("$varchar_date", '2006-07-23T00:00:00', 'Correct date/time');
119
120 ## varchar field using inflate_datetime => 1
121 my $varchar_datetime = $event->varchar_datetime;
122 is("$varchar_datetime", '2006-05-22T19:05:07', 'Correct date/time');
123
124 ## skip inflation field
125 my $skip_inflation = $event->skip_inflation;
126 is ("$skip_inflation", '2006-04-21 18:04:06', 'Correct date/time');
127
128 # extra accessor tests with update_or_insert
129 {
130   my $new = $schema->resultset("Track")->new( {
131     trackid => 100,
132     cd => 1,
133     title => 'Insert or Update',
134     last_updated_on => '1973-07-19 12:01:02'
135   } );
136   $new->update_or_insert;
137   ok($new->in_storage, 'update_or_insert insert ok');
138
139   # test in update mode
140   $new->title('Insert or Update - updated');
141   $new->update_or_insert;
142   is( $schema->resultset("Track")->find(100)->title, 'Insert or Update - updated', 'update_or_insert update ok');
143
144   # test get_inflated_columns with objects
145   my $event = $schema->resultset('Event')->search->first;
146   my %edata = $event->get_inflated_columns;
147   is($edata{'id'}, $event->id, 'got id');
148   isa_ok($edata{'starts_at'}, 'DateTime', 'start_at is DateTime object');
149   isa_ok($edata{'created_on'}, 'DateTime', 'create_on DateTime object');
150   is($edata{'starts_at'}, $event->starts_at, 'got start date');
151   is($edata{'created_on'}, $event->created_on, 'got created date');
152
153   # get_inflated_columns w/relation and accessor alias
154   isa_ok($new->updated_date, 'DateTime', 'have inflated object via accessor');
155   my %tdata = $new->get_inflated_columns;
156   is($tdata{'trackid'}, 100, 'got id');
157   isa_ok($tdata{'cd'}, 'DBICTest::CD', 'cd is CD object');
158   is($tdata{'cd'}->id, 1, 'cd object is id 1');
159   is(
160     $tdata{'position'},
161     $schema->resultset ('Track')->search ({cd => 1})->count,
162     'Ordered assigned proper position',
163   );
164   is($tdata{'title'}, 'Insert or Update - updated');
165   is($tdata{'last_updated_on'}, '1973-07-19T12:01:02');
166   isa_ok($tdata{'last_updated_on'}, 'DateTime', 'inflated accessored column');
167 }
168
169 # create and update with literals
170 {
171   my $d = {
172     created_on => \ '2001-09-11',
173     starts_at => \[ '?' => '2001-10-26' ],
174   };
175
176   my $ev = $schema->resultset('Event')->create($d);
177
178   for my $col (qw(created_on starts_at)) {
179     ok (ref $ev->$col, "literal untouched in $col");
180     is_deeply( $ev->$col, $d->{$col});
181     is_deeply( $ev->get_inflated_column($col), $d->{$col});
182     is_deeply( $ev->get_column($col), $d->{$col});
183   }
184
185   $ev->discard_changes;
186
187   is_deeply(
188     { $ev->get_dirty_columns },
189     {}
190   );
191
192   for my $col (qw(created_on starts_at)) {
193     isa_ok ($ev->$col, "DateTime", "$col properly inflated on retrieve");
194   }
195
196   for my $meth (qw(set_inflated_columns set_columns)) {
197
198     $ev->$meth({%$d});
199
200     is_deeply(
201       { $ev->get_dirty_columns },
202       $d,
203       "Expected dirty cols after setting literals via $meth",
204     );
205
206     $ev->update;
207
208     for my $col (qw(created_on starts_at)) {
209       ok (ref $ev->$col, "literal untouched in $col updated via $meth");
210       is_deeply( $ev->$col, $d->{$col});
211       is_deeply( $ev->get_inflated_column($col), $d->{$col});
212       is_deeply( $ev->get_column($col), $d->{$col});
213     }
214   }
215 }
216
217 done_testing;