1 use DBIx::Class::Optional::Dependencies -skip_all_without => qw( icdt test_rdbms_sqlite );
12 # Test offline parser determination (formerly t/inflate/datetime_determine_parser.t)
14 my $schema = DBICTest->init_schema(
15 no_deploy => 1, # Deploying would cause an early rebless
18 my $storage = $schema->storage;
20 if ($ENV{DBICTEST_VIA_REPLICATED}) {
21 $storage = $storage->master;
25 ref $storage, 'DBIx::Class::Storage::DBI',
26 'Starting with generic storage'
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();
34 is($parser, 'DateTime::Format::SQLite', 'Got expected storage-set datetime_parser');
35 isa_ok($storage, 'DBIx::Class::Storage::DBI::SQLite', 'storage');
37 ok(! $storage->connected, 'Not yet connected');
40 # so user's env doesn't screw us
41 delete $ENV{DBIC_DT_SEARCH_OK};
43 my $schema = DBICTest->init_schema();
46 my $event = $schema->resultset("Event")->find(1);
48 isa_ok($event->starts_at, 'DateTime', 'DateTime returned');
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');
54 my $dt_warn_re = qr/DateTime objects.+not supported properly/;
59 local $ENV{DBIC_DT_SEARCH_OK} = 1;
60 local $SIG{__WARN__} = sub {
61 fail('Disabled warning still issued') if $_[0] =~ $dt_warn_re;
64 $row = $schema->resultset('Event')->search({ starts_at => $starts })->single
68 $row = $schema->resultset('Event')->search({ starts_at => $starts })->single
70 'using a DateTime object in ->search generates a warning';
73 local $TODO = "This stuff won't work without a -dt operator of some sort"
74 unless eval { require DBIx::Class::SQLMaker::DateOps };
76 is(eval { $row->id }, 1, 'DT in search');
78 local $ENV{DBIC_DT_SEARCH_OK} = 1;
81 $schema->resultset('Event')->search({ starts_at => { '>=' => $starts } })
84 is(eval { $row->id }, 1, 'DT in search with condition');
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)
92 my $created_start = $created->starts_at;
94 isa_ok($created->starts_at, 'DateTime', 'DateTime returned');
95 is("$created_start", '2006-06-18T00:00:00', 'Correct date/time');
98 isa_ok($event->created_on, 'DateTime', 'DateTime returned');
101 isa_ok($event->varchar_date, 'DateTime', 'DateTime returned');
102 isa_ok($event->varchar_datetime, 'DateTime', 'DateTime returned');
104 ## skip inflation field
105 isnt(ref($event->skip_inflation), 'DateTime', 'No DateTime returned for skip inflation column');
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');
111 my $created_cron = $created->created_on;
113 isa_ok($created->created_on, 'DateTime', 'DateTime returned');
114 is("$created_cron", '2006-06-23T00:00:00', 'Correct date/time');
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');
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');
124 ## skip inflation field
125 my $skip_inflation = $event->skip_inflation;
126 is ("$skip_inflation", '2006-04-21 18:04:06', 'Correct date/time');
128 # extra accessor tests with update_or_insert
130 my $new = $schema->resultset("Track")->new( {
133 title => 'Insert or Update',
134 last_updated_on => '1973-07-19 12:01:02'
136 $new->update_or_insert;
137 ok($new->in_storage, 'update_or_insert insert ok');
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');
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');
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');
161 $schema->resultset ('Track')->search ({cd => 1})->count,
162 'Ordered assigned proper position',
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');
169 # create and update with literals
172 created_on => \ '2001-09-11',
173 starts_at => \[ '?' => '2001-10-26' ],
176 my $ev = $schema->resultset('Event')->create($d);
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});
185 $ev->discard_changes;
188 { $ev->get_dirty_columns },
192 for my $col (qw(created_on starts_at)) {
193 isa_ok ($ev->$col, "DateTime", "$col properly inflated on retrieve");
196 for my $meth (qw(set_inflated_columns set_columns)) {
201 { $ev->get_dirty_columns },
203 "Expected dirty cols after setting literals via $meth",
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});