1 BEGIN { do "./t/lib/ANFANG.pm" or die ( $@ || $! ) }
2 use DBIx::Class::Optional::Dependencies -skip_all_without => qw( ic_dt test_rdbms_sqlite );
13 # Test offline parser determination (formerly t/inflate/datetime_determine_parser.t)
15 my $schema = DBICTest->init_schema(
16 no_deploy => 1, # Deploying would cause an early rebless
19 my $storage = $schema->storage;
21 if( $storage->isa('DBIx::Class::Storage::DBI::Replicated') ) {
22 $storage = $storage->master;
26 ref $storage, 'DBIx::Class::Storage::DBI',
27 'Starting with generic storage'
31 # Calling date_time_parser should cause the storage to be reblessed,
32 # so that we can pick up datetime_parser_type from subclasses
33 my $parser = $storage->datetime_parser();
35 is($parser, 'DateTime::Format::SQLite', 'Got expected storage-set datetime_parser');
36 isa_ok($storage, 'DBIx::Class::Storage::DBI::SQLite', 'storage');
38 ok(! $storage->connected, 'Not yet connected');
41 # so user's env doesn't screw us
42 delete $ENV{DBIC_DT_SEARCH_OK};
44 my $schema = DBICTest->init_schema();
47 my $event = $schema->resultset("Event")->find(1);
49 isa_ok($event->starts_at, 'DateTime', 'DateTime returned');
51 # klunky, but makes older Test::More installs happy
52 my $starts = $event->starts_at;
53 is("$starts", '2006-04-25T22:24:33', 'Correct date/time');
55 my $dt_warn_re = qr/DateTime objects.+not supported properly/;
60 local $ENV{DBIC_DT_SEARCH_OK} = 1;
61 local $SIG{__WARN__} = sub {
62 fail('Disabled warning still issued') if $_[0] =~ $dt_warn_re;
65 $row = $schema->resultset('Event')->search({ starts_at => $starts })->single
69 $row = $schema->resultset('Event')->search({ starts_at => $starts })->single
71 'using a DateTime object in ->search generates a warning';
74 local $TODO = "This stuff won't work without a -dt operator of some sort"
75 unless eval { require DBIx::Class::SQLMaker::DateOps };
77 is(eval { $row->id }, 1, 'DT in search');
79 local $ENV{DBIC_DT_SEARCH_OK} = 1;
82 $schema->resultset('Event')->search({ starts_at => { '>=' => $starts } })
85 is(eval { $row->id }, 1, 'DT in search with condition');
88 # create using DateTime
89 my $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)
93 my $created_start = $created->starts_at;
95 isa_ok($created->starts_at, 'DateTime', 'DateTime returned');
96 is("$created_start", '2006-06-18T00:00:00', 'Correct date/time');
99 isa_ok($event->created_on, 'DateTime', 'DateTime returned');
102 isa_ok($event->varchar_date, 'DateTime', 'DateTime returned');
103 isa_ok($event->varchar_datetime, 'DateTime', 'DateTime returned');
105 ## skip inflation field
106 isnt(ref($event->skip_inflation), 'DateTime', 'No DateTime returned for skip inflation column');
108 # klunky, but makes older Test::More installs happy
109 my $createo = $event->created_on;
110 is("$createo", '2006-06-22T21:00:05', 'Correct date/time');
112 my $created_cron = $created->created_on;
114 isa_ok($created->created_on, 'DateTime', 'DateTime returned');
115 is("$created_cron", '2006-06-23T00:00:00', 'Correct date/time');
117 ## varchar field using inflate_date => 1
118 my $varchar_date = $event->varchar_date;
119 is("$varchar_date", '2006-07-23T00:00:00', 'Correct date/time');
121 ## varchar field using inflate_datetime => 1
122 my $varchar_datetime = $event->varchar_datetime;
123 is("$varchar_datetime", '2006-05-22T19:05:07', 'Correct date/time');
125 ## skip inflation field
126 my $skip_inflation = $event->skip_inflation;
127 is ("$skip_inflation", '2006-04-21 18:04:06', 'Correct date/time');
129 # extra accessor tests with update_or_insert
131 my $new = $schema->resultset("Track")->new( {
134 title => 'Insert or Update',
135 last_updated_on => '1973-07-19 12:01:02'
137 $new->update_or_insert;
138 ok($new->in_storage, 'update_or_insert insert ok');
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');
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');
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');
162 $schema->resultset ('Track')->search ({cd => 1})->count,
163 'Ordered assigned proper position',
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');
170 # create and update with literals
173 created_on => \ '2001-09-11',
174 starts_at => \[ '?' => '2001-10-26' ],
177 my $ev = $schema->resultset('Event')->create($d);
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});
186 $ev->discard_changes;
189 { $ev->get_dirty_columns },
193 for my $col (qw(created_on starts_at)) {
194 isa_ok ($ev->$col, "DateTime", "$col properly inflated on retrieve");
197 for my $meth (qw(set_inflated_columns set_columns)) {
202 { $ev->get_dirty_columns },
204 "Expected dirty cols after setting literals via $meth",
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});