From 6ad6cd3e5ebcd0d1e648d118367b95bcaa4e747c Mon Sep 17 00:00:00 2001 From: Colin Kuskie Date: Sat, 3 May 2008 21:25:19 +0000 Subject: [PATCH] move* with tests --- lib/WebGUI/Asset/Sku/Product.pm | 96 ++++++++++----------------------- t/Asset/Sku/ProductCollateral.t | 70 +++++++++++++++++++++++- 2 files changed, 95 insertions(+), 71 deletions(-) diff --git a/lib/WebGUI/Asset/Sku/Product.pm b/lib/WebGUI/Asset/Sku/Product.pm index f0dee313d..71e423504 100644 --- a/lib/WebGUI/Asset/Sku/Product.pm +++ b/lib/WebGUI/Asset/Sku/Product.pm @@ -333,29 +333,18 @@ sub indexContent { #------------------------------------------------------------------- -=head2 moveCollateralDown ( tableName, keyName, keyValue [ , setName, setValue ] ) +=head2 moveCollateralDown ( tableName, index ) -Moves a collateral data item down one position. This assumes that the collateral data table has a column called "assetId" that identifies the wobject, and a column called "sequenceNumber" that determines the position of the data item. +Moves a collateral data item down one position. If called on the last element of the +collateral array then it does nothing. =head3 tableName A string indicating the table that contains the collateral data. -=head3 keyName +=head3 index -A string indicating the name of the column that uniquely identifies this collateral data item. - -=head3 keyValue - -An iid that uniquely identifies this collateral data item. - -=head3 setName - -By default this method assumes that the collateral will have an assetId in the table. However, since there is not always a assetId to separate one data set from another, you may specify another field to do that. - -=head3 setValue - -The value of the column defined by "setName" to select a data set from. +The index of the collateral data to move down. =cut @@ -363,51 +352,31 @@ The value of the column defined by "setName" to select a data set from. ### two different types of collateral data. sub moveCollateralDown { - my $self = shift; - my $table = shift; - my $keyName = shift; - my $keyValue = shift; - my $setName = shift || "assetId"; - my $setValue = shift; - unless (defined $setValue) { - $setValue = $self->get($setName); - } - $self->session->db->beginTransaction; - my ($seq) = $self->session->db->quickArray("select sequenceNumber from $table where $keyName=".$self->session->db->quote($keyValue)." and $setName=".$self->session->db->quote($setValue)); - my ($id) = $self->session->db->quickArray("select $keyName from $table where $setName=".$self->session->db->quote($setValue)." and sequenceNumber=$seq+1"); - if ($id ne "") { - $self->session->db->write("update $table set sequenceNumber=sequenceNumber+1 where $keyName=".$self->session->db->quote($keyValue)." and $setName=" .$self->session->db->quote($setValue)); - $self->session->db->write("update $table set sequenceNumber=sequenceNumber-1 where $keyName=".$self->session->db->quote($id)." and $setName=" .$self->session->db->quote($setValue)); - } - $self->session->db->commit; + my $self = shift; + my $tableName = shift; + my $index = shift; + + my $table = $self->getAllCollateral($tableName); + return unless (abs($index) < $#{$table}); + @{ $table }[$index,$index+1] = @{ $table }[$index+1,$index]; + $self->setAllCollateral($tableName); } #------------------------------------------------------------------- -=head2 moveCollateralUp ( tableName, keyName, keyValue [ , setName, setValue ] ) +=head2 moveCollateralDown ( tableName, index ) -Moves a collateral data item up one position. This assumes that the collateral data table has a column called "assetId" that identifies the wobject, and a column called "sequenceNumber" that determines the position of the data item. +Moves a collateral data item up one position. If called on the first element of the +collateral array then it does nothing. =head3 tableName A string indicating the table that contains the collateral data. -=head3 keyName +=head3 index -A string indicating the name of the column that uniquely identifies this collateral data item. - -=head3 keyValue - -An id that uniquely identifies this collateral data item. - -=head3 setName - -By default this method assumes that the collateral will have a asset in the table. However, since there is not always a assetId to separate one data set from another, you may specify another field to do that. - -=head3 setValue - -The value of the column defined by "setName" to select a data set from. +The index of the collateral data to move up. =cut @@ -415,28 +384,17 @@ The value of the column defined by "setName" to select a data set from. ### two different types of collateral data. sub moveCollateralUp { - my $self = shift; - my $table = shift; - my $keyName = shift; - my $keyValue = shift; - my $setName = shift || "assetId"; - my $setValue = shift; - unless (defined $setValue) { - $setValue = $self->get($setName); - } - $self->session->db->beginTransaction; - my ($seq) = $self->session->db->quickArray("select sequenceNumber from $table where $keyName=".$self->session->db->quote($keyValue)." and $setName=".$self->session->db->quote($setValue)); - my ($id) = $self->session->db->quickArray("select $keyName from $table where $setName=".$self->session->db->quote($setValue) - ." and sequenceNumber=$seq-1"); - if ($id ne "") { - $self->session->db->write("update $table set sequenceNumber=sequenceNumber-1 where $keyName=".$self->session->db->quote($keyValue)." and $setName=" - .$self->session->db->quote($setValue)); - $self->session->db->write("update $table set sequenceNumber=sequenceNumber+1 where $keyName=".$self->session->db->quote($id)." and $setName=" - .$self->session->db->quote($setValue)); - } - $self->session->db->commit; + my $self = shift; + my $tableName = shift; + my $index = shift; + + my $table = $self->getAllCollateral($tableName); + return unless $index && (abs($index) <= $#{$table}); + @{ $table }[$index-1,$index] = @{ $table }[$index,$index-1]; + $self->setAllCollateral($tableName); } + #------------------------------------------------------------------- =head2 prepareView ( ) diff --git a/t/Asset/Sku/ProductCollateral.t b/t/Asset/Sku/ProductCollateral.t index 71d767a66..d54ce439b 100644 --- a/t/Asset/Sku/ProductCollateral.t +++ b/t/Asset/Sku/ProductCollateral.t @@ -35,7 +35,7 @@ my $session = WebGUI::Test->session; #---------------------------------------------------------------------------- # Tests -plan tests => 17; # Increment this number for each test you create +plan tests => 23; # Increment this number for each test you create #---------------------------------------------------------------------------- # put your tests here @@ -158,8 +158,74 @@ cmp_deeply( 'deleteCollateral: negative index works', ); -$product->purge; +$product->setCollateral('variantsJSON', 'new', { a => 'alligators', b => 'bursting'}); +$product->setCollateral('variantsJSON', 'new', { a => 'ah', b => 'bay'}); +cmp_deeply( + $product->getAllCollateral('variantsJSON'), + [ + {a => 'aye', b => 'bee' }, + {a => 'alligators', b => 'bursting' }, + {a => 'ah', b => 'bay' }, + ], + 'setup correct for moving collateral', +); +$product->moveCollateralDown('variantsJSON', 1); +cmp_deeply( + $product->getAllCollateral('variantsJSON'), + [ + {a => 'aye', b => 'bee' }, + {a => 'ah', b => 'bay' }, + {a => 'alligators', b => 'bursting' }, + ], + 'moveCollateralDown: worked', +); + +$product->moveCollateralDown('variantsJSON', 3); +cmp_deeply( + $product->getAllCollateral('variantsJSON'), + [ + {a => 'aye', b => 'bee' }, + {a => 'ah', b => 'bay' }, + {a => 'alligators', b => 'bursting' }, + ], + 'moveCollateralDown: can not move out of range collateral item', +); + +$product->moveCollateralUp('variantsJSON', 1); +cmp_deeply( + $product->getAllCollateral('variantsJSON'), + [ + {a => 'ah', b => 'bay' }, + {a => 'aye', b => 'bee' }, + {a => 'alligators', b => 'bursting' }, + ], + 'moveCollateralUp: worked', +); + +$product->moveCollateralUp('variantsJSON', 0); +cmp_deeply( + $product->getAllCollateral('variantsJSON'), + [ + {a => 'ah', b => 'bay' }, + {a => 'aye', b => 'bee' }, + {a => 'alligators', b => 'bursting' }, + ], + 'moveCollateralUp: can not move the first collateral item in the array', +); + +$product->moveCollateralUp('variantsJSON', 5); +cmp_deeply( + $product->getAllCollateral('variantsJSON'), + [ + {a => 'ah', b => 'bay' }, + {a => 'aye', b => 'bee' }, + {a => 'alligators', b => 'bursting' }, + ], + 'moveCollateralUp: out of range index does not do anything', +); + +$product->purge; #---------------------------------------------------------------------------- # Cleanup