Reformat the JSON test for Shop/Tax.t

Add JSON tets to Shop/Transaction.t
Reindent several methods in SQL/ResultSet.pm
add a method for returning a YUI Data Table structure to SQL.pm
This commit is contained in:
Colin Kuskie 2008-03-19 04:33:24 +00:00
parent 94b03aa3f5
commit 4db53bb8ed
5 changed files with 94 additions and 13 deletions

View file

@ -225,8 +225,7 @@ sub buildArrayRefOfHashRefs {
my $sql = shift; my $sql = shift;
my $params = shift; my $params = shift;
my $sth = $class->read($sql,$params); my $sth = $class->read($sql,$params);
my $data; while (my $data = $sth->hashRef) {
while ($data = $sth->hashRef) {
push(@array,$data); push(@array,$data);
} }
$sth->finish; $sth->finish;
@ -236,7 +235,47 @@ sub buildArrayRefOfHashRefs {
#------------------------------------------------------------------- #-------------------------------------------------------------------
=head2 buildHashRefOfHashRefs ( sql ) =head2 buildDataTableStructure ( sql, params )
Builds a data structure that can be converted to JSON and sent
to a YUI Data Table. This is basically a hash of information about
the results, with one of the keys being an array ref of hashrefs. It also
calculates the total records that could have been matched without a limit
statement, as well as how many were actually matched.
=head3 sql
An SQL query. The query may select as many columns of data as you wish. The query
should contain a SQL_CALC_ROWS_FOUND entry so that the total number of available
rows can be sent to the Data Table.
=head3 params
An array reference containing values for any placeholder params used in the SQL query.
=cut
sub buildDataTableStructure {
my $self = shift;
my $sql = shift;
my $params = shift;
my %hash;
my @array;
##Note, I need a valid statement handle for doing the rows method on.
my $sth = $self->read($sql,$params);
while (my $data = $sth->hashRef) {
push(@array,$data);
}
$hash{records} = \@array;
$hash{totalRecords} = $self->quickScalar('select found_rows()') + 0; ##Convert to numeric
$hash{recordsReturned} = $sth->rows();
$sth->finish;
return \%hash;
}
#-------------------------------------------------------------------
=head2 buildHashRefOfHashRefs ( sql, params, key )
Builds a hash reference of hash references of data Builds a hash reference of hash references of data
from a series of rows. Useful for returning many rows at once. from a series of rows. Useful for returning many rows at once.

View file

@ -68,8 +68,8 @@ Returns the next row of data as an array reference. Note that this is 12% faster
=cut =cut
sub arrayRef { sub arrayRef {
my $self = shift; my $self = shift;
return $self->sth->fetchrow_arrayref() or $self->db->session->errorHandler->fatal("Couldn't fetch array. ".$self->errorMessage); return $self->sth->fetchrow_arrayref() or $self->db->session->errorHandler->fatal("Couldn't fetch array. ".$self->errorMessage);
} }
@ -82,8 +82,8 @@ A reference to the current WebGUI::SQL object.
=cut =cut
sub db { sub db {
my $self = shift; my $self = shift;
return $self->{_db}; return $self->{_db};
} }
#------------------------------------------------------------------- #-------------------------------------------------------------------

View file

@ -9,6 +9,7 @@ use WebGUI::Exception::Shop;
use WebGUI::Form; use WebGUI::Form;
use WebGUI::International; use WebGUI::International;
use WebGUI::Paginator; use WebGUI::Paginator;
use WebGUI::Shop::Admin;
use WebGUI::Shop::AddressBook; use WebGUI::Shop::AddressBook;
use WebGUI::Shop::TransactionItem; use WebGUI::Shop::TransactionItem;

View file

@ -607,7 +607,6 @@ is($taxer->calculate($cart), 5.5, 'calculate: simple tax calculation on 2 items
$session->user({userId=>3}); $session->user({userId=>3});
my $json = $taxer->www_getTaxesAsJson(); my $json = $taxer->www_getTaxesAsJson();
diag $json;
ok($json, 'www_getTaxesAsJson returned something'); ok($json, 'www_getTaxesAsJson returned something');
my $jsonTax = JSON::from_json($json); my $jsonTax = JSON::from_json($json);
cmp_deeply( cmp_deeply(
@ -618,9 +617,16 @@ cmp_deeply(
totalRecords => 1778, totalRecords => 1778,
recordsReturned => 25, recordsReturned => 25,
dir => 'desc', dir => 'desc',
records => array_each({taxId=>ignore, country => 'USA', state=>ignore, city=>ignore, code=>ignore, taxRate=>re('^\d+\.\d+$')}), records => array_each({
taxId=>ignore,
country => 'USA',
state=>ignore,
city=>ignore,
code=>ignore,
taxRate=>re('^\d+\.\d+$')
}),
}, },
'Check major elements of JSON', 'Check major elements of tax JSON',
); );
$taxableDonation->purge; $taxableDonation->purge;

View file

@ -30,7 +30,7 @@ my $session = WebGUI::Test->session;
#---------------------------------------------------------------------------- #----------------------------------------------------------------------------
# Tests # Tests
plan tests => 64; # Increment this number for each test you create plan tests => 66; # Increment this number for each test you create
#---------------------------------------------------------------------------- #----------------------------------------------------------------------------
# put your tests here # put your tests here
@ -183,13 +183,48 @@ my $icopy = $transaction->getItem($item->getId);
isa_ok($icopy, "WebGUI::Shop::TransactionItem"); isa_ok($icopy, "WebGUI::Shop::TransactionItem");
is($icopy->getId, $item->getId, "items are the same"); is($icopy->getId, $item->getId, "items are the same");
# get itmes # get items
is(scalar @{$transaction->getItems}, 1, "can retrieve items"); is(scalar @{$transaction->getItems}, 1, "can retrieve items");
# delete # delete
$item->delete; $item->delete;
is(scalar @{$transaction->getItems}, 0, "can delete items"); is(scalar @{$transaction->getItems}, 0, "can delete items");
#######################################################################
#
# www_getTaxesAsJson
#
#######################################################################
$session->user({userId=>3});
my $json = WebGUI::Shop::Transaction->www_getTransactionsAsJson($session);
ok($json, 'www_getTransactionsAsJson returned something');
my $jsonTransactions = JSON::from_json($json);
cmp_deeply(
$jsonTransactions,
{
sort => undef,
startIndex => 0,
totalRecords => 1,
recordsReturned => 1,
dir => 'desc',
records => array_each({
orderNumber=>ignore,
transactionId=>ignore,
transactionCode=>ignore,
paymentDriverLabel=>ignore,
dateOfPurchase=>ignore,
username=>ignore,
amount=>ignore,
isSuccessful=>ignore,
statusCode=>ignore,
statusMessage=>ignore,
}),
},
'Check major elements of transaction JSON',
);
$transaction->delete; $transaction->delete;
is($session->db->quickScalar("select transactionId from transaction where transactionId=?",[$transaction->getId]), undef, "can delete transactions"); is($session->db->quickScalar("select transactionId from transaction where transactionId=?",[$transaction->getId]), undef, "can delete transactions");
@ -198,5 +233,5 @@ is($session->db->quickScalar("select transactionId from transaction where transa
#---------------------------------------------------------------------------- #----------------------------------------------------------------------------
# Cleanup # Cleanup
END { END {
$session->db->write('delete from transaction');
} }