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