Add coverage tests, convert croaks to exceptions.

This commit is contained in:
Colin Kuskie 2008-03-05 23:34:53 +00:00
parent cd55ff1a9e
commit d27ac7a629
4 changed files with 55 additions and 10 deletions

View file

@ -2,7 +2,6 @@ package WebGUI::Shop::Ship;
use strict;
use Carp qw(croak);
use WebGUI::International;
use WebGUI::Shop::ShipDriver;
use WebGUI::Pluggable;
@ -104,8 +103,9 @@ A WebGUI::Session object. A WebGUI::Error::InvalidParam exception will be throw
sub getOptions {
my $class = shift;
my $session = shift;
croak "Definition requires a session object"
WebGUI::Error::InvalidParam->throw(error => q{Must provide a session variable})
unless ref $session eq 'WebGUI::Session';
return;
}
#-------------------------------------------------------------------

View file

@ -273,8 +273,11 @@ sub new {
my $properties = $session->db->quickHashRef('select * from shipper where shipperId=?',[$shipperId]);
WebGUI::Error::ObjectNotFound->throw(error => q{shipperId not found in db}, id => $shipperId)
unless scalar keys %{ $properties };
croak "Somehow, the options property of this object, $shipperId, got broken in the db"
unless exists $properties->{options} and $properties->{options};
##This check is just to guardband the from_json call below.
WebGUI::Error::InvalidParam->throw(
error => qq{Options property for $shipperId was broken in the db},
param => $properties->{options},
) unless $properties->{options}; ##Note, existence is controlled by the columns in the db
my $options = from_json($properties->{options});
my $self = WebGUI::Shop::ShipDriver->_buildObj($session, $class, $shipperId, $options);
return $self;

View file

@ -31,7 +31,7 @@ my $session = WebGUI::Test->session;
#----------------------------------------------------------------------------
# Tests
my $tests = 27;
my $tests = 29;
plan tests => 1 + $tests;
#----------------------------------------------------------------------------
@ -220,6 +220,23 @@ cmp_bag(
'Returned shippers have the right data'
);
#######################################################################
#
# getOptions
#
#######################################################################
eval { $shippers = WebGUI::Shop::Ship->getOptions(); };
$e = Exception::Class->caught();
isa_ok($e, 'WebGUI::Error::InvalidParam', 'getOptions takes exception to not giving it a session object');
cmp_deeply(
$e,
methods(
error => 'Must provide a session variable',
),
'getOptions takes exception to not giving it a session object',
);
}
#----------------------------------------------------------------------------

View file

@ -31,7 +31,7 @@ my $session = WebGUI::Test->session;
#----------------------------------------------------------------------------
# Tests
my $tests = 39;
my $tests = 42;
plan tests => 1 + $tests;
#----------------------------------------------------------------------------
@ -302,10 +302,35 @@ is($driver->getId, $driverCopy->getId, 'same id');
is($driver->className, $driverCopy->className, 'same className');
cmp_deeply($driver->options, $driverCopy->options, 'same options');
TODO: {
local $TODO = 'tests for new';
ok(0, 'Test broken options in the db');
}
my $brokenDriver = WebGUI::Shop::ShipDriver->create($session, {label=>'to be broken', enabled=>'0'});
$session->db->write('update shipper set options=NULL where shipperId=?',[$brokenDriver->getId]);
eval { $oldDriver = WebGUI::Shop::ShipDriver->new($session, $brokenDriver->getId); };
$e = Exception::Class->caught();
isa_ok($e, 'WebGUI::Error::InvalidParam', 'new croaks if the options column in the db is null');
cmp_deeply(
$e,
methods(
error => re('Options property for \S{22} was broken in the db'),
param => undef,
),
'new croaks if the options column in the db is null',
);
$session->db->write(q{update shipper set options='' where shipperId=?},[$brokenDriver->getId]);
eval { $oldDriver = WebGUI::Shop::ShipDriver->new($session, $brokenDriver->getId); };
$e = Exception::Class->caught();
isa_ok($e, 'WebGUI::Error::InvalidParam', 'new croaks if the options column in the db is empty string');
cmp_deeply(
$e,
methods(
error => re('Options property for \S{22} was broken in the db'),
param => '',
),
'new croaks if the options column in the db is empty string',
);
#######################################################################
#