[Jifty-commit] r5042 - in Jifty-DBI/branches/tisql: lib/Jifty/DBI/Filter t
Jifty commits
jifty-commit at lists.jifty.org
Tue Feb 5 21:18:50 EST 2008
Author: ruz
Date: Tue Feb 5 21:18:50 2008
New Revision: 5042
Modified:
Jifty-DBI/branches/tisql/ (props changed)
Jifty-DBI/branches/tisql/lib/Jifty/DBI/Filter/Duration.pm
Jifty-DBI/branches/tisql/t/06filter_duration.t
Log:
r4825 at cubic-pc (orig r4824): trs | 2008-01-11 23:16:23 +0300
r30707 at zot: tom | 2008-01-11 15:15:56 -0500
Better error checking
Modified: Jifty-DBI/branches/tisql/lib/Jifty/DBI/Filter/Duration.pm
==============================================================================
--- Jifty-DBI/branches/tisql/lib/Jifty/DBI/Filter/Duration.pm (original)
+++ Jifty-DBI/branches/tisql/lib/Jifty/DBI/Filter/Duration.pm Tue Feb 5 21:18:50 2008
@@ -16,7 +16,8 @@
=head2 encode
If value is defined, then encode it using
-L<Time::Duration::Parse/parse_duration>, otherwise do nothing.
+L<Time::Duration::Parse/parse_duration>, otherwise do nothing. If the value
+can't be parsed, then set it to undef.
=cut
@@ -26,13 +27,20 @@
my $value_ref = $self->value_ref;
return unless defined $$value_ref and length $$value_ref;
- # Convert hh:mm(::ss)? to something Time::Duration::Parse understands
+ # Convert hh:mm(:ss)? to something Time::Duration::Parse understands
$$value_ref =~ s/\b(\d+):(\d\d):(\d\d)\b/$1h $2m $3s/g;
$$value_ref =~ s/\b(\d+):(\d\d)\b/$1h $2m/g;
- $$value_ref = Time::Duration::Parse::parse_duration($$value_ref);
+ my ($parsed) = eval { Time::Duration::Parse::parse_duration($$value_ref) };
- return 1;
+ if ( not $@ ) {
+ $$value_ref = $parsed;
+ return 1;
+ }
+ else {
+ $$value_ref = undef;
+ return;
+ }
}
=head2 decode
@@ -47,7 +55,8 @@
my $self = shift;
my $value_ref = $self->value_ref;
- return unless defined $$value_ref and length $$value_ref;
+ return unless defined $$value_ref and length $$value_ref
+ and $$value_ref =~ /^\s*\d+\s*$/;
$$value_ref = Time::Duration::concise(Time::Duration::duration_exact($$value_ref));
}
Modified: Jifty-DBI/branches/tisql/t/06filter_duration.t
==============================================================================
--- Jifty-DBI/branches/tisql/t/06filter_duration.t (original)
+++ Jifty-DBI/branches/tisql/t/06filter_duration.t Tue Feb 5 21:18:50 2008
@@ -6,7 +6,7 @@
BEGIN { require "t/utils.pl" }
our (@available_drivers);
-use constant TESTS_PER_DRIVER => 37;
+use constant TESTS_PER_DRIVER => 42;
eval "use Time::Duration ()";
if ($@) {
@@ -21,6 +21,7 @@
my $total = scalar(@available_drivers) * TESTS_PER_DRIVER;
plan tests => $total;
+my @bad_input = ('foo');
my @duration_input = ('3h5m', '3:05', '3:04:60', '3h 0:05', '1h 2:04:60');
my $duration_output = '3h5m';
my $duration_seconds = 11100;
@@ -46,6 +47,18 @@
isa_ok($ret, 'DBI::st', 'init schema');
}
+ for my $input ( @bad_input ) {
+ my $rec = TestApp::User->new( handle => $handle );
+ isa_ok($rec, 'Jifty::DBI::Record');
+
+ my ($id) = $rec->create( my_data => $input );
+ ok($id, 'created record');
+ ok($rec->load($id), 'loaded record');
+ is($rec->id, $id, 'record id matches');
+
+ is($rec->my_data, undef, 'my_data output is undef');
+ }
+
for my $input ( @duration_input ) {
my $rec = TestApp::User->new( handle => $handle );
isa_ok($rec, 'Jifty::DBI::Record');
More information about the Jifty-commit
mailing list