From 18dd9a459ded982dbc8e29166c26b37145885497 Mon Sep 17 00:00:00 2001 From: Doug Bell Date: Thu, 5 May 2011 19:27:22 -0500 Subject: [PATCH] move export product tests and fix the export product helper --- lib/WebGUI/AssetHelper/Product/ExportCSV.pm | 2 +- t/AssetHelper/Product/ExportCSV.t | 120 ++++++++++++++++++++ 2 files changed, 121 insertions(+), 1 deletion(-) create mode 100644 t/AssetHelper/Product/ExportCSV.t diff --git a/lib/WebGUI/AssetHelper/Product/ExportCSV.pm b/lib/WebGUI/AssetHelper/Product/ExportCSV.pm index b9be0d258..afc088dca 100644 --- a/lib/WebGUI/AssetHelper/Product/ExportCSV.pm +++ b/lib/WebGUI/AssetHelper/Product/ExportCSV.pm @@ -54,7 +54,7 @@ sub exportProducts { # This should be perhaps genericized and placed into WebGUI::Asset my $tableName = $session->db->dbh->quote_identifier( WebGUI::Asset::Sku::Product->tableName ); my $productIds = $session->db->buildArrayRef( - "SELECT assetId FROM asset JOIN assetData USING (assetId) JOIN $tableName USING (assetId, revisionDate) WHERE status=? OR status=? HAVING MAX(revisionDate)", + "SELECT assetId FROM asset JOIN assetData USING (assetId) JOIN $tableName USING (assetId, revisionDate) WHERE status=? OR status=? GROUP BY (assetId) HAVING MAX(revisionDate)", ['approved','archived'], ); diff --git a/t/AssetHelper/Product/ExportCSV.t b/t/AssetHelper/Product/ExportCSV.t new file mode 100644 index 000000000..9ae6cee07 --- /dev/null +++ b/t/AssetHelper/Product/ExportCSV.t @@ -0,0 +1,120 @@ + +# vim:syntax=perl +#------------------------------------------------------------------- +# WebGUI is Copyright 2001-2009 Plain Black Corporation. +#------------------------------------------------------------------- +# Please read the legal notices (docs/legal.txt) and the license +# (docs/license.txt) that came with this distribution before using +# this software. +#------------------------------------------------------------------ +# http://www.plainblack.com info@plainblack.com +#------------------------------------------------------------------ + +# Write a little about what this script tests. +# +# + +use FindBin; +use strict; +use lib "$FindBin::Bin/lib"; +use File::Slurp qw( read_file ); +use File::Spec::Functions qw( catfile ); +use Test::More; +use Test::Deep; +use WebGUI::Test; # Must use this before any other WebGUI modules +use WebGUI::Session; +use WebGUI::Storage; +use WebGUI::AssetHelper::Product::ExportCSV; + +#---------------------------------------------------------------------------- +# Init +my $session = WebGUI::Test->session; + + +#---------------------------------------------------------------------------- +# Tests + +my $root = WebGUI::Test->asset; +my $class = 'WebGUI::Asset::Wobject::Shelf'; +my $shelf = $root->addChild({className => $class}); + +my $soda = [ + { + varSku => 'soda-sweet', + shortdesc => 'Sweet Soda', + price => 0.95, + weight => 0.95, + quantity => 500, + variantId => $session->id->generate, + }, + ]; +my $shirts = [ + { + varSku => 'red-t-shirt', + shortdesc => 'Red T-Shirt', + price => '5.00', + weight => '1.33', + quantity => '1000', + variantId => $session->id->generate, + }, + { + varSku => 'blue-t-shirt', + shortdesc => 'Blue T-Shirt', + price => '5.25', + weight => '1.33', + quantity => '2000', + variantId => $session->id->generate, + }, + ]; +$shelf->addChild({ + className => 'WebGUI::Asset::Sku::Product', + variantsJSON => JSON->new->encode( $soda ), + title => 'Sweet Soda-bottled in Oregon', + sku => 'soda', + }); +$shelf->addChild({ + className => 'WebGUI::Asset::Sku::Product', + variantsJSON => JSON->new->encode( $shirts ), + title => 'Shirts', + sku => 't-shirt', + }); +my $helper = WebGUI::AssetHelper::Product::ExportCSV->new( + id => 'exportProducts', + session => $session, + asset => $shelf, +); +my $exportProducts = \&WebGUI::AssetHelper::Product::ExportCSV::exportProducts; +my $process = Test::MockObject::Extends->new( WebGUI::Fork->create( $session ) ); +addToCleanup( sub { $process->delete } ); + +$exportProducts->($process, {}); +# Determine the storage location from the URL +my $status = JSON->new->decode( $process->{delay}->() ); +my ( $filePath )= $status->{ redirect } =~ m!^/uploads/(.+)$!; + +my $productData = read_file catfile( $session->config->get('uploadsPath'), $filePath); +my @productData = split /\n/, $productData; +is(scalar @productData, 4, 'productData should have 4 entries, 1 header + 3 data'); +is($productData[0], 'mastersku,title,varSku,shortdescription,price,weight,quantity', 'header line is okay'); +@productData = map { [ WebGUI::Text::splitCSV($_) ] } @productData[1..3]; +my ($sodas, $shirts) = ([], []); +foreach my $productData (@productData) { + if ($productData->[0] eq 'soda') { + push @{ $sodas }, $productData; + } + elsif ($productData->[0] eq 't-shirt') { + push @{ $shirts }, $productData; + } +} +is(scalar @{ $sodas }, 1, 'just 1 soda'); +is(scalar @{ $shirts }, 2, '2 shirts'); + +cmp_deeply( + $sodas, + [ ['soda', 'Sweet Soda-bottled in Oregon', + 'soda-sweet', 'Sweet Soda', 0.95, 0.95, 500] ], + 'soda data is okay' +); + +done_testing; +#vim:ft=perl