From c613e389958994e462fc49b449c44cf415062568 Mon Sep 17 00:00:00 2001 From: Colin Kuskie Date: Mon, 18 Feb 2008 21:23:43 +0000 Subject: [PATCH] Input validation tests for importTaxData. --- lib/WebGUI/Shop/Tax.pm | 16 +++- t/Shop/Tax.t | 88 ++++++++++++++++++- .../taxTables/badTaxTable.csv | 3 + .../taxTables/commentedTaxTable.csv | 8 ++ .../taxTables/emptyTaxTable.csv | 3 + .../taxTables/goodTaxTable.csv | 2 - .../taxTables/orderedTaxTable.csv | 3 + 7 files changed, 115 insertions(+), 8 deletions(-) create mode 100644 t/supporting_collateral/taxTables/badTaxTable.csv create mode 100644 t/supporting_collateral/taxTables/commentedTaxTable.csv create mode 100644 t/supporting_collateral/taxTables/emptyTaxTable.csv create mode 100644 t/supporting_collateral/taxTables/orderedTaxTable.csv diff --git a/lib/WebGUI/Shop/Tax.pm b/lib/WebGUI/Shop/Tax.pm index 6c5692b49..f00c0e08e 100644 --- a/lib/WebGUI/Shop/Tax.pm +++ b/lib/WebGUI/Shop/Tax.pm @@ -147,8 +147,15 @@ sub getItems { Import tax information from the specified file in CSV format. The first line of the file should contain the name of the columns, in -any order. The following lines will contain tax information. Blank -lines and anything following a '#' sign will be ignored. +any order. The first line may not contain comments in it, or +before it. + +The following lines will contain tax information. Blank +lines and anything following a '#' sign will be ignored from +the second line of the file, on to the end. + +Returns 1 if the import has taken place. This is to help you know +if old data has been deleted and new has been inserted. =cut @@ -174,12 +181,15 @@ sub importTaxData { my $line = 1; while (my $taxRow = <$table>) { chomp $taxRow; + $taxRow =~ s/\s*#.+$//; + next unless $taxRow; my @taxRow = WebGUI::Text::splitCSV($taxRow); croak qq{Error on line $line in file $filePath} unless scalar @taxRow == 3; push @taxData, [ @taxRow ]; } ##Okay, if we got this far, then the data looks fine. + return unless scalar @taxData; $self->session->db->beginTransaction; $self->session->db->write('delete from tax'); foreach my $taxRow (@taxData) { @@ -188,7 +198,7 @@ sub importTaxData { $self->add(\%taxRow); } $self->session->db->commit; - return; + return 1; } #------------------------------------------------------------------- diff --git a/t/Shop/Tax.t b/t/Shop/Tax.t index 6b67ee3df..7b2259a0d 100644 --- a/t/Shop/Tax.t +++ b/t/Shop/Tax.t @@ -29,7 +29,7 @@ my $session = WebGUI::Test->session; #---------------------------------------------------------------------------- # Tests -my $tests = 34; +my $tests = 45; plan tests => 1 + $tests; #---------------------------------------------------------------------------- @@ -244,13 +244,95 @@ SKIP: { } -$taxer->importTaxData($taxFile); +my $expectedTaxData = [ + { + field => 'state', + value => 'Wisconsin', + taxRate => 5.0, + }, + { + field => 'code', + value => 53701, + taxRate => 0.5, + }, +]; + +ok( + $taxer->importTaxData( + $taxFile + ), + 'Good tax data inserted', +); $taxIterator = $taxer->getItems; -is($taxIterator->rows, 4, 'import: Old data deleted, new data imported'); +is($taxIterator->rows, 2, 'import: Old data deleted, new data imported'); +my @goodTaxData = _grabTaxData($taxIterator); +cmp_bag( + \@goodTaxData, + $expectedTaxData, + 'Correct data inserted.', +); + +ok( + $taxer->importTaxData( + WebGUI::Test->getTestCollateralPath('taxTables/orderedTaxTable.csv') + ), + 'Reordered tax data inserted', +); + +$taxIterator = $taxer->getItems; +is($taxIterator->rows, 2, 'import: Old data deleted, new data imported again'); +my @orderedTaxData = _grabTaxData($taxIterator); +cmp_bag( + \@orderedTaxData, + $expectedTaxData, + 'Correct data inserted, with CSV in different columnar order.', +); + +ok( + $taxer->importTaxData( + WebGUI::Test->getTestCollateralPath('taxTables/commentedTaxTable.csv') + ), + 'Commented tax data inserted', +); + +$taxIterator = $taxer->getItems; +is($taxIterator->rows, 2, 'import: Old data deleted, new data imported the third time'); +my @orderedTaxData = _grabTaxData($taxIterator); +cmp_bag( + \@orderedTaxData, + $expectedTaxData, + 'Correct data inserted, with comments in the CSV file', +); + +ok( + ! $taxer->importTaxData( + WebGUI::Test->getTestCollateralPath('taxTables/emptyTaxTable.csv') + ), + 'Empty tax data not inserted', +); + +my $failure; +eval { + $failure = $taxer->importTaxData( + WebGUI::Test->getTestCollateralPath('taxTables/badTaxTable.csv') + ); +}; +ok (!$failure, 'Tax data not imported'); +like($@, qr{Error on line \d+ in file}, + 'importTaxData: error handling when the CSV data is missing an entry on 1 line'); } +sub _grabTaxData { + my $tax = shift; + my @taxData = (); + while (my $taxRow = $tax->hashRef) { + delete $taxRow->{'taxId'}; + push @taxData, $taxRow; + } + return @taxData; +} #---------------------------------------------------------------------------- # Cleanup diff --git a/t/supporting_collateral/taxTables/badTaxTable.csv b/t/supporting_collateral/taxTables/badTaxTable.csv new file mode 100644 index 000000000..bcd03dd13 --- /dev/null +++ b/t/supporting_collateral/taxTables/badTaxTable.csv @@ -0,0 +1,3 @@ +field,value,taxRate +state,5.0 +code,53701,0.5 diff --git a/t/supporting_collateral/taxTables/commentedTaxTable.csv b/t/supporting_collateral/taxTables/commentedTaxTable.csv new file mode 100644 index 000000000..4b20147a0 --- /dev/null +++ b/t/supporting_collateral/taxTables/commentedTaxTable.csv @@ -0,0 +1,8 @@ +taxRate,value,field +#header lines above + +#This is just a zip code. +0.5,53701,code + + +5.0,Wisconsin,state #Wisconsin is expensive! diff --git a/t/supporting_collateral/taxTables/emptyTaxTable.csv b/t/supporting_collateral/taxTables/emptyTaxTable.csv new file mode 100644 index 000000000..6fdd30d12 --- /dev/null +++ b/t/supporting_collateral/taxTables/emptyTaxTable.csv @@ -0,0 +1,3 @@ +field,value,taxRate +#state,Wisconsin,5.0 +#code,53701,0.5 diff --git a/t/supporting_collateral/taxTables/goodTaxTable.csv b/t/supporting_collateral/taxTables/goodTaxTable.csv index c7f627eae..bef2e91c7 100644 --- a/t/supporting_collateral/taxTables/goodTaxTable.csv +++ b/t/supporting_collateral/taxTables/goodTaxTable.csv @@ -1,5 +1,3 @@ field,value,taxRate -country,USA,0.1 state,Wisconsin,5.0 -city,Madcity-Baby,0.5 code,53701,0.5 diff --git a/t/supporting_collateral/taxTables/orderedTaxTable.csv b/t/supporting_collateral/taxTables/orderedTaxTable.csv new file mode 100644 index 000000000..08a6a69ec --- /dev/null +++ b/t/supporting_collateral/taxTables/orderedTaxTable.csv @@ -0,0 +1,3 @@ +taxRate,value,field +5.0,Wisconsin,state +0.5,53701,code