Renamed the import and export methods to prevent Perl from
calling them. Added code and tests for import method. It needs more testing for user input validation.
This commit is contained in:
parent
76f3e986b7
commit
215031bb4e
2 changed files with 96 additions and 8 deletions
|
|
@ -104,7 +104,7 @@ sub delete {
|
|||
|
||||
#-------------------------------------------------------------------
|
||||
|
||||
=head2 export ( )
|
||||
=head2 exportTaxData ( )
|
||||
|
||||
Creates a tab deliniated file containing all the information from
|
||||
the tax table. Returns a temporary WebGUI::Storage object containing
|
||||
|
|
@ -112,7 +112,7 @@ the file. The file will be named "siteTaxData.csv".
|
|||
|
||||
=cut
|
||||
|
||||
sub export {
|
||||
sub exportTaxData {
|
||||
my $self = shift;
|
||||
my $taxIterator = $self->getItems;
|
||||
my @columns = qw{ field value taxRate };
|
||||
|
|
@ -143,6 +143,56 @@ sub getItems {
|
|||
|
||||
#-------------------------------------------------------------------
|
||||
|
||||
=head2 importTaxData ( $filePath )
|
||||
|
||||
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.
|
||||
|
||||
=cut
|
||||
|
||||
sub importTaxData {
|
||||
my $self = shift;
|
||||
my $filePath = shift;
|
||||
croak q{Must provide the path to a file}
|
||||
unless $filePath;
|
||||
croak qq{$filePath could not be found}
|
||||
unless -e $filePath;
|
||||
croak qq{$filePath is not readable}
|
||||
unless -r $filePath;
|
||||
open my $table, '<', $filePath or
|
||||
croak "Unable to open $filePath for reading: $!\n";
|
||||
my $headers;
|
||||
$headers = <$table>;
|
||||
chomp $headers;
|
||||
my @headers = WebGUI::Text::splitCSV($headers);
|
||||
croak qq{Bad header found in the CSV file}
|
||||
unless (join(q{-}, sort @headers) eq 'field-taxRate-value')
|
||||
and (scalar @headers == 3);
|
||||
my @taxData = ();
|
||||
my $line = 1;
|
||||
while (my $taxRow = <$table>) {
|
||||
chomp $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.
|
||||
$self->session->db->beginTransaction;
|
||||
$self->session->db->write('delete from tax');
|
||||
foreach my $taxRow (@taxData) {
|
||||
my %taxRow;
|
||||
@taxRow{ @headers } = @{ $taxRow }; ##Must correspond 1:1, or else...
|
||||
$self->add(\%taxRow);
|
||||
}
|
||||
$self->session->db->commit;
|
||||
return;
|
||||
}
|
||||
|
||||
#-------------------------------------------------------------------
|
||||
|
||||
=head2 new ( $session )
|
||||
|
||||
Constructor for the WebGUI::Shop::Tax. Returns a WebGUI::Shop::Tax object.
|
||||
|
|
|
|||
50
t/Shop/Tax.t
50
t/Shop/Tax.t
|
|
@ -29,7 +29,7 @@ my $session = WebGUI::Test->session;
|
|||
#----------------------------------------------------------------------------
|
||||
# Tests
|
||||
|
||||
my $tests = 30;
|
||||
my $tests = 34;
|
||||
plan tests => 1 + $tests;
|
||||
|
||||
#----------------------------------------------------------------------------
|
||||
|
|
@ -193,24 +193,62 @@ is($taxIterator->hashRef->{taxId}, $wisconsinTaxId, 'The correct tax information
|
|||
|
||||
#######################################################################
|
||||
#
|
||||
# export
|
||||
# exportTaxData
|
||||
#
|
||||
#######################################################################
|
||||
|
||||
$storage = $taxer->export();
|
||||
isa_ok($storage, 'WebGUI::Storage', 'export returns a WebGUI::Storage object');
|
||||
$storage = $taxer->exportTaxData();
|
||||
isa_ok($storage, 'WebGUI::Storage', 'exportTaxData returns a WebGUI::Storage object');
|
||||
is($storage->{_part1}, 'temp', 'The storage object is in the temporary area');
|
||||
ok(-e $storage->getPath('siteTaxData.csv'), 'siteTaxData.csv file exists in the storage object');
|
||||
cmp_ok($storage->getFileSize('siteTaxData.csv'), '!=', 0, 'CSV file is not empty');
|
||||
my @fileLines = split /\n+/, $storage->getFileContentsAsScalar('siteTaxData.csv');
|
||||
#my @fileLines = ();
|
||||
my @header = WebGUI::Text::splitCSV($fileLines[0]);
|
||||
my @expectedHeader = qw/field value taxRate/;
|
||||
cmp_deeply(\@header, \@expectedHeader, 'export: header line is correct');
|
||||
cmp_deeply(\@header, \@expectedHeader, 'exportTaxData: header line is correct');
|
||||
my @row1 = WebGUI::Text::splitCSV($fileLines[1]);
|
||||
use Data::Dumper;
|
||||
my $wiData = $taxer->getItems->hashRef;
|
||||
##Need to ignore the taxId from the database
|
||||
cmp_bag([ @{ $wiData }{ @expectedHeader } ], \@row1, 'export: first line of data is correct');
|
||||
cmp_bag([ @{ $wiData }{ @expectedHeader } ], \@row1, 'exportTaxData: first line of data is correct');
|
||||
|
||||
#######################################################################
|
||||
#
|
||||
# import
|
||||
#
|
||||
#######################################################################
|
||||
|
||||
eval { $taxer->importTaxData(); };
|
||||
like($@, qr{Must provide the path to a file},
|
||||
'importTaxData: error handling for an undefined taxId value');
|
||||
|
||||
eval { $taxer->importTaxData('/path/to/nowhere'); };
|
||||
like($@, qr{/path/to/nowhere could not be found},
|
||||
'importTaxData: error handling for file that does not exist in the filesystem');
|
||||
|
||||
my $taxFile = WebGUI::Test->getTestCollateralPath('taxTable.csv');
|
||||
|
||||
SKIP: {
|
||||
skip 'Root will cause this test to fail since it does not obey file permissions', 1
|
||||
if $< == 0;
|
||||
|
||||
my $originalChmod = (stat $taxFile)[2];
|
||||
chmod oct(0000), $taxFile;
|
||||
|
||||
eval { $taxer->importTaxData($taxFile); };
|
||||
like($@, qr{is not readable},
|
||||
'importTaxData: error handling for file that cannot be read');
|
||||
|
||||
chmod $originalChmod, $taxFile;
|
||||
|
||||
}
|
||||
|
||||
$taxer->importTaxData($taxFile);
|
||||
|
||||
$taxIterator = $taxer->getItems;
|
||||
is($taxIterator->rows, 4, 'import: Old data deleted, new data imported');
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue