From 19591d37d4a2bea611fdd4c6efa94691e685d4db Mon Sep 17 00:00:00 2001 From: Colin Kuskie Date: Fri, 15 Feb 2008 23:17:52 +0000 Subject: [PATCH] Fix a bug in the upgrade script, with a default applied to the wrong column. Built the add method, and tested it, too. --- docs/upgrades/upgrade_7.5.2-7.5.3.pl | 4 +- lib/WebGUI/Shop/Tax.pm | 37 +++++++++++++++ t/Shop/Tax.t | 70 +++++++++++++++++++++++++++- 3 files changed, 107 insertions(+), 4 deletions(-) diff --git a/docs/upgrades/upgrade_7.5.2-7.5.3.pl b/docs/upgrades/upgrade_7.5.2-7.5.3.pl index c1077bcf7..acfc3936e 100644 --- a/docs/upgrades/upgrade_7.5.2-7.5.3.pl +++ b/docs/upgrades/upgrade_7.5.2-7.5.3.pl @@ -62,8 +62,8 @@ sub insertCommerceTaxTable { CREATE TABLE tax ( taxId VARCHAR(22) binary NOT NULL, field VARCHAR(100) NOT NULL, - value VARCHAR(100) DEFAULT 0.0, - taxRate FLOAT NOT NULL, + value VARCHAR(100), + taxRate FLOAT NOT NULL DEFAULT 0.0, PRIMARY KEY (taxId), UNIQUE KEY (field, value) ) diff --git a/lib/WebGUI/Shop/Tax.pm b/lib/WebGUI/Shop/Tax.pm index 9d61ef25c..2e3ed32af 100644 --- a/lib/WebGUI/Shop/Tax.pm +++ b/lib/WebGUI/Shop/Tax.pm @@ -30,6 +30,43 @@ readonly session => my %session; #------------------------------------------------------------------- +=head2 add ( [$params] ) + +Add tax information to the table. Returns the taxId of the newly created tax information. + +=head3 $params + +A hash ref of the geographic and rate information. All parameters are required. + +=head4 field + +field denotes what kind of location the tax information is for. This should +be country, state, or code. The combination of field and value is unique +in the database. + +=head4 value + +value is the value of the field to be added. For example, appropriate values +for a field of country might be China, United States, Mexico. If the field +is state, it could be British Colombia, Oregon or Maine. + +=head4 taxRate + +This is the tax rate for the location, as specified by field and value. The tax rate is stored +as a percentage, like 5.5 . + +=cut + +sub add { + my $self = shift; + my $params = shift; + my $id = $self->session->id->generate(); + $self->session->db->write('insert into tax (taxId, field, value, taxRate) VALUES (?,?,?,?)', [$id, @{ $params }{qw[ field value taxRate ]}]); + return $id; +} + +#------------------------------------------------------------------- + =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 fd2f69d6c..d41dc2574 100644 --- a/t/Shop/Tax.t +++ b/t/Shop/Tax.t @@ -17,6 +17,7 @@ use FindBin; use strict; use lib "$FindBin::Bin/../lib"; use Test::More; +use Test::Deep; use WebGUI::Test; # Must use this before any other WebGUI modules use WebGUI::Session; @@ -27,7 +28,7 @@ my $session = WebGUI::Test->session; #---------------------------------------------------------------------------- # Tests -my $tests = 5; +my $tests = 11; plan tests => 1 + $tests; #---------------------------------------------------------------------------- @@ -39,6 +40,12 @@ SKIP: { skip 'Unable to load module WebGUI::Shop::Tax', $tests unless $loaded; +####################################################################### +# +# new +# +####################################################################### + my $taxer = WebGUI::Shop::Tax->new($session); isa_ok($taxer, 'WebGUI::Shop::Tax'); @@ -47,17 +54,76 @@ isa_ok($taxer->session, 'WebGUI::Session', 'session method returns a session obj is($session->getId, $taxer->session->getId, 'session method returns OUR session object'); +####################################################################### +# +# getItems +# +####################################################################### + my $taxIterator = $taxer->getItems; isa_ok($taxIterator, 'WebGUI::SQL::ResultSet'); is($taxIterator->rows, 0, 'WebGUI ships with no predefined tax data'); +####################################################################### +# +# add +# +####################################################################### + +my $taxData = { + field => 'state', + value => 'Oregon', + taxRate => '0', +}; + +my $oregonTaxId = $taxer->add($taxData); + +ok($session->id->valid($oregonTaxId), 'add method returns a valid GUID'); + +$taxIterator = $taxer->getItems; +is($taxIterator->rows, 1, 'add added only 1 row to the tax table'); + +my $addedData = $taxIterator->hashRef; +$taxData->{taxId} = $oregonTaxId; + +cmp_deeply($taxData, $addedData, 'add put the right data into the database for Oregon'); + +$taxData = { + field => 'state', + value => 'Wisconsin', + taxRate => '5', +}; + +my $wisconsinTaxId = $taxer->add($taxData); + +$taxIterator = $taxer->getItems; +is($taxIterator->rows, 2, 'add added another row to the tax table'); + +$taxData = { + field => 'state', + value => 'Oregon', + taxRate => '0.1', +}; + +eval {$taxer->add($taxData)}; + +ok($@, 'add threw an exception to having taxes in Oregon when they were defined as 0 initially'); + +$taxIterator = $taxer->getItems; +is($taxIterator->rows, 2, 'add did not add another row since it would be a duplicate'); + +##Madison zip codes: +##53701-53709 +##city rate: 0.5% +##Wisconsin rate 5.0% + } #---------------------------------------------------------------------------- # Cleanup END { - + $session->db->write('delete from tax'); }