From 645c21bc3b9e50ca8c9c93cbab27eec5c191182c Mon Sep 17 00:00:00 2001 From: Colin Kuskie Date: Sat, 16 Feb 2008 05:36:25 +0000 Subject: [PATCH] Tax: add the delete method, with tests --- lib/WebGUI/Shop/Tax.pm | 28 ++++++++++++++++++++++++++++ t/Shop/Tax.t | 31 ++++++++++++++++++++++++++++++- 2 files changed, 58 insertions(+), 1 deletion(-) diff --git a/lib/WebGUI/Shop/Tax.pm b/lib/WebGUI/Shop/Tax.pm index 2e3ed32af..ef660a73b 100644 --- a/lib/WebGUI/Shop/Tax.pm +++ b/lib/WebGUI/Shop/Tax.pm @@ -3,6 +3,7 @@ package WebGUI::Shop::Tax; use strict; use Class::InsideOut qw{ :std }; +use Carp qw(croak); =head1 NAME @@ -67,6 +68,33 @@ sub add { #------------------------------------------------------------------- +=head2 delete ( [$params] ) + +Deletes data from the tax table by taxId. + +=head3 $params + +A hashref containing the taxId of the data to delete from the table. + +=head4 taxId + +The taxId of the data to delete from the table. + +=cut + +sub delete { + my $self = shift; + my $params = shift; + croak "Must pass in a hashref" + unless ref($params) eq 'HASH'; + croak "Hash ref must contain a taxId key with a defined value" + unless exists($params->{taxId}) and defined $params->{taxId}; + $self->session->db->write('delete from tax where taxId=?', [$params->{taxId}]); + return; +} + +#------------------------------------------------------------------- + =head2 getItems ( ) Returns a WebGUI::SQL::Result object for accessing all of the data in the tax table. This diff --git a/t/Shop/Tax.t b/t/Shop/Tax.t index d41dc2574..d2824be4e 100644 --- a/t/Shop/Tax.t +++ b/t/Shop/Tax.t @@ -28,7 +28,7 @@ my $session = WebGUI::Test->session; #---------------------------------------------------------------------------- # Tests -my $tests = 11; +my $tests = 17; plan tests => 1 + $tests; #---------------------------------------------------------------------------- @@ -119,6 +119,35 @@ is($taxIterator->rows, 2, 'add did not add another row since it would be a dupli ##city rate: 0.5% ##Wisconsin rate 5.0% +####################################################################### +# +# delete +# +####################################################################### + +eval{$taxer->delete()}; +like($@, qr{Must pass in a hashref}, + 'delete: error handling for missing hashref'); + +eval{$taxer->delete({})}; +like($@, qr{Hash ref must contain a taxId key with a defined value}, + 'delete: error handling for missing key in hashref'); + +eval{$taxer->delete({ taxId => undef })}; +like($@, qr{Hash ref must contain a taxId key with a defined value}, + 'delete: error handling for an undefined taxId value'); + +$taxer->delete({ taxId => $oregonTaxId }); + +$taxIterator = $taxer->getItems; +is($taxIterator->rows, 1, 'One row was deleted from the tax table'); + +$taxer->delete({ taxId => $session->id->generate }); + +$taxIterator = $taxer->getItems; +is($taxIterator->rows, 1, 'No rows were deleted from the table since the requested id does not exist'); +is($taxIterator->hashRef->{taxId}, $wisconsinTaxId, 'The correct tax information was deleted'); + }