Promote the getAllProducts to Asset.pm as getIsa. Move the tests

from Asset/Sku/Product.t into Asset/Asset.t, and add tests to check
that inheritance is respected.
This commit is contained in:
Colin Kuskie 2008-05-28 16:06:52 +00:00
parent 4aa428e927
commit 3ad1668a21
4 changed files with 73 additions and 52 deletions

View file

@ -1058,6 +1058,40 @@ sub getImportNode {
#-------------------------------------------------------------------
=head2 getIsa ( $session )
A class method to return an iterator for getting all Assets by class (and all sub-classes)
as Asset objects, one at a time. When the end of the assets is reached, then the iterator
will close the database handle that it uses and return undef.
It should be used like this:
my $productIterator = WebGUI::Asset::Product->getIsa($session);
while (my $product = $productIterator->()) {
##Do something useful with $product
}
=cut
sub getIsa {
my $class = shift;
my $session = shift;
my $def = $class->definition($session);
my $tableName = $def->[0]->{tableName};
my $sth = $session->db->read("select distinct(assetId) from $tableName");
return sub {
my ($assetId) = $sth->array;
if (!$assetId) {
$sth->finish;
return undef;
}
return WebGUI::Asset->newPending($session, $assetId);
};
}
#-------------------------------------------------------------------
=head2 getMedia ( session )

View file

@ -272,38 +272,6 @@ sub getAllCollateral {
}
#-------------------------------------------------------------------
=head2 getAllProducts ( $session )
A class method to return an iterator for getting all Products (and Product sub-classes)
as Asset objects, one at a time. The iterator will return undef when you've reached
the end of the list of products.
It should be used like this:
my $productIterator = WebGUI::Asset::Product->getAllProducts($session);
while (my $product = $productIterator->()) {
##Do something useful with $product
}
=cut
sub getAllProducts {
my $class = shift;
my $session = shift;
my $sth = $session->db->read('select distinct(assetId) from Product');
return sub {
my ($assetId) = $sth->array;
if (!$assetId) {
$sth->finish;
return undef;
}
return WebGUI::Asset->newPending($session, $assetId);
};
}
#-------------------------------------------------------------------
=head2 getCollateral ( tableName, keyName, keyValue )

View file

@ -19,6 +19,8 @@ use WebGUI::Asset;
use WebGUI::User;
use WebGUI::Asset::Wobject::Navigation;
use WebGUI::Asset::Wobject::Folder;
use WebGUI::Asset::Sku;
use WebGUI::Asset::Sku::Product;
use WebGUI::AssetVersioning;
use WebGUI::VersionTag;
@ -144,7 +146,7 @@ $canViewMaker->prepare(
},
);
plan tests => 89
plan tests => 94
+ scalar(@fixIdTests)
+ scalar(@fixTitleTests)
+ 2*scalar(@getTitleTests) #same tests used for getTitle and getMenuTitle
@ -701,6 +703,42 @@ diag $assetProps->{title};
isnt( $rootAsset->get('title'), $funkyTitle, 'get returns a safe copy of the Asset properties');
################################################################
#
# getIsa
#
################################################################
my $node = WebGUI::Asset::Sku::Product->getProductImportNode($session);
my $product1 = $node->addChild({ className => 'WebGUI::Asset::Sku::Product'});
my $product2 = $node->addChild({ className => 'WebGUI::Asset::Sku::Product'});
my $product3 = $node->addChild({ className => 'WebGUI::Asset::Sku::Product'});
my $getAProduct = WebGUI::Asset::Sku::Product->getIsa($session);
isa_ok($getAProduct, 'CODE', 'getIsa returns a sub ref');
my $counter = 0;
my $productIds = [];
while( my $product = $getAProduct->()) {
++$counter;
push @{ $productIds }, $product->getId;
}
is($counter, 3, 'getIsa: returned only 3 Products');
cmp_bag($productIds, [$product1->getId, $product2->getId, $product3->getId], 'getIsa returned the correct 3 products');
my $getASku = WebGUI::Asset::Sku->getIsa($session);
$counter = 0;
my $skuIds = [];
while( my $sku = $getASku->()) {
++$counter;
push @{ $skuIds }, $sku->getId;
}
is($counter, 3, 'getIsa: returned only 3 Products for a parent class');
cmp_bag($skuIds, [$product1->getId, $product2->getId, $product3->getId], 'getIsa returned the correct 3 products for a parent class');
$product1->purge;
$product2->purge;
$product3->purge;
END {
$session->config->set( 'extrasURL', $origExtras);
$session->config->set( 'uploadsURL', $origUploads);

View file

@ -41,25 +41,6 @@ my $node = WebGUI::Asset::Sku::Product->getProductImportNode($session);
isa_ok($node, 'WebGUI::Asset::Wobject::Folder', 'getProductImportNode returns a Folder');
is($node->getId, 'PBproductimportnode001', 'Product Import Node has the correct GUID');
my $product1 = $node->addChild({ className => 'WebGUI::Asset::Sku::Product'});
my $product2 = $node->addChild({ className => 'WebGUI::Asset::Sku::Product'});
my $product3 = $node->addChild({ className => 'WebGUI::Asset::Sku::Product'});
my $getAProduct = WebGUI::Asset::Sku::Product->getAllProducts($session);
isa_ok($getAProduct, 'CODE', 'getAllProducts returns a sub ref');
my $counter = 0;
my $productIds = [];
while( my $product = $getAProduct->()) {
++$counter;
push @{ $productIds }, $product->getId;
}
is($counter, 3, 'getAllProducts: returned only 3 Products');
cmp_bag($productIds, [$product1->getId, $product2->getId, $product3->getId], 'getAllProduct returned the correct 3 products');
$product1->purge;
$product2->purge;
$product3->purge;
#----------------------------------------------------------------------------
# Cleanup
END {