Add input validation for the add method, and test it.

Now it's time for the import and export methods.
This commit is contained in:
Colin Kuskie 2008-02-16 06:48:00 +00:00
parent 645c21bc3b
commit 0701adf7e0
2 changed files with 49 additions and 2 deletions

View file

@ -37,7 +37,8 @@ Add tax information to the table. Returns the taxId of the newly created tax in
=head3 $params
A hash ref of the geographic and rate information. All parameters are required.
A hash ref of the geographic and rate information. All parameters are required and
must have defined values.
=head4 field
@ -62,6 +63,12 @@ sub add {
my $self = shift;
my $params = shift;
my $id = $self->session->id->generate();
croak "Must pass in a hashref"
unless ref($params) eq 'HASH';
foreach my $key (qw/field value taxRate/) {
croak "Hash ref must contain a $key key with a defined value"
unless exists($params->{$key}) and defined $params->{$key};
}
$self->session->db->write('insert into tax (taxId, field, value, taxRate) VALUES (?,?,?,?)', [$id, @{ $params }{qw[ field value taxRate ]}]);
return $id;
}

View file

@ -28,7 +28,7 @@ my $session = WebGUI::Test->session;
#----------------------------------------------------------------------------
# Tests
my $tests = 17;
my $tests = 24;
plan tests => 1 + $tests;
#----------------------------------------------------------------------------
@ -72,6 +72,46 @@ is($taxIterator->rows, 0, 'WebGUI ships with no predefined tax data');
#
#######################################################################
eval{$taxer->add()};
like($@, qr{Must pass in a hashref},
'add: error handling for missing hashref');
eval{$taxer->add({})};
like($@, qr{Hash ref must contain a field key with a defined value},
'add: error handling for missing field hashref key');
my $taxData = {
field => undef,
};
eval{$taxer->add($taxData)};
like($@, qr{Hash ref must contain a field key with a defined value},
'add: error handling for undefined field key');
$taxData->{field} = 'state';
eval{$taxer->add($taxData)};
like($@, qr{Hash ref must contain a value key with a defined value},
'add: error handling for missing value key');
$taxData->{value} = undef;
eval{$taxer->add($taxData)};
like($@, qr{Hash ref must contain a value key with a defined value},
'add: error handling for undefined value key');
$taxData->{value} = 'Oregon';
eval{$taxer->add($taxData)};
like($@, qr{Hash ref must contain a taxRate key with a defined value},
'add: error handling for missing taxRate key');
$taxData->{taxRate} = undef;
eval{$taxer->add($taxData)};
like($@, qr{Hash ref must contain a taxRate key with a defined value},
'add: error handling for undefined taxRate key');
my $taxData = {
field => 'state',
value => 'Oregon',