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