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