Massive cleanup of DateTime test dependencies, other interim
[dbsrgits/DBIx-Class.git] / t / inflate / datetime_mysql.t
1 use strict;
2 use warnings;
3
4 use Test::More;
5 use Test::Exception;
6 use Test::Warn;
7 use lib qw(t/lib);
8 use DBICTest;
9 use DBICTest::Schema;
10
11 plan skip_all => 'Inflation tests need ' . DBIx::Class::Optional::Dependencies->req_missing_for ('test_dt_mysql')
12   unless DBIx::Class::Optional::Dependencies->req_ok_for ('test_dt_mysql');
13
14 {
15   local $SIG{__WARN__} = sub { warn @_ if $_[0] !~ /extra \=\> .+? has been deprecated/ };
16   DBICTest::Schema->load_classes('EventTZ');
17   DBICTest::Schema->load_classes('EventTZDeprecated');
18 }
19
20 my $schema = DBICTest->init_schema();
21
22 # Test "timezone" parameter
23 foreach my $tbl (qw/EventTZ EventTZDeprecated/) {
24   my $event_tz = $schema->resultset($tbl)->create({
25       starts_at => DateTime->new(year=>2007, month=>12, day=>31, time_zone => "America/Chicago" ),
26       created_on => DateTime->new(year=>2006, month=>1, day=>31,
27           hour => 13, minute => 34, second => 56, time_zone => "America/New_York" ),
28   });
29
30   is ($event_tz->starts_at->day_name, "Montag", 'Locale de_DE loaded: day_name');
31   is ($event_tz->starts_at->month_name, "Dezember", 'Locale de_DE loaded: month_name');
32   is ($event_tz->created_on->day_name, "Tuesday", 'Default locale loaded: day_name');
33   is ($event_tz->created_on->month_name, "January", 'Default locale loaded: month_name');
34
35   my $starts_at = $event_tz->starts_at;
36   is("$starts_at", '2007-12-31T00:00:00', 'Correct date/time using timezone');
37
38   my $created_on = $event_tz->created_on;
39   is("$created_on", '2006-01-31T12:34:56', 'Correct timestamp using timezone');
40   is($event_tz->created_on->time_zone->name, "America/Chicago", "Correct timezone");
41
42   my $loaded_event = $schema->resultset($tbl)->find( $event_tz->id );
43
44   isa_ok($loaded_event->starts_at, 'DateTime', 'DateTime returned');
45   $starts_at = $loaded_event->starts_at;
46   is("$starts_at", '2007-12-31T00:00:00', 'Loaded correct date/time using timezone');
47   is($starts_at->time_zone->name, 'America/Chicago', 'Correct timezone');
48
49   isa_ok($loaded_event->created_on, 'DateTime', 'DateTime returned');
50   $created_on = $loaded_event->created_on;
51   is("$created_on", '2006-01-31T12:34:56', 'Loaded correct timestamp using timezone');
52   is($created_on->time_zone->name, 'America/Chicago', 'Correct timezone');
53
54   # Test floating timezone warning
55   # We expect one warning
56   SKIP: {
57     skip "ENV{DBIC_FLOATING_TZ_OK} was set, skipping", 1 if $ENV{DBIC_FLOATING_TZ_OK};
58     warnings_exist (
59       sub {
60         $schema->resultset($tbl)->create({
61           starts_at => DateTime->new(year=>2007, month=>12, day=>31 ),
62           created_on => DateTime->new(year=>2006, month=>1, day=>31, hour => 13, minute => 34, second => 56 ),
63         });
64       },
65       qr/You're using a floating timezone, please see the documentation of DBIx::Class::InflateColumn::DateTime for an explanation/,
66       'Floating timezone warning'
67     );
68   };
69
70   # This should fail to set
71   my $prev_str = "$created_on";
72   $loaded_event->update({ created_on => '0000-00-00' });
73   is("$created_on", $prev_str, "Don't update invalid dates");
74 }
75
76 # Test invalid DT
77 my $invalid = $schema->resultset('EventTZ')->create({
78   starts_at  => '0000-00-00',
79   created_on => DateTime->now,
80 });
81
82 is( $invalid->get_column('starts_at'), '0000-00-00', "Invalid date stored" );
83 is( $invalid->starts_at, undef, "Inflate to undef" );
84
85 $invalid->created_on('0000-00-00');
86 $invalid->update;
87
88 throws_ok (
89   sub { $invalid->created_on },
90   qr/invalid date format/i,
91   "Invalid date format exception"
92 );
93
94 done_testing;