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