added more tests and fixed bugs discovered by tests
This commit is contained in:
parent
66d36242fd
commit
2f8ad97b73
2 changed files with 37 additions and 6 deletions
|
|
@ -402,7 +402,7 @@ sub getAllIterator {
|
||||||
my @objects;
|
my @objects;
|
||||||
my $ids = $class->getAllIds($session, $options, @_);
|
my $ids = $class->getAllIds($session, $options, @_);
|
||||||
my $sub = sub {
|
my $sub = sub {
|
||||||
my ($id) = $ids->array;
|
my ($id) = shift @{$ids};
|
||||||
return if !$id;
|
return if !$id;
|
||||||
my $object = $class->new($session, $id);
|
my $object = $class->new($session, $id);
|
||||||
if (!$object) {
|
if (!$object) {
|
||||||
|
|
@ -453,8 +453,8 @@ sub getAllSql {
|
||||||
|
|
||||||
# limit to our sequence
|
# limit to our sequence
|
||||||
my $sequenceKey = $class->crud_getSequenceKey;
|
my $sequenceKey = $class->crud_getSequenceKey;
|
||||||
unless ($options->{sequenceKeyValue} && $sequenceKey) {
|
if ($options->{sequenceKeyValue} && $sequenceKey) {
|
||||||
$sql .= $dbh->quote_identifier($sequenceKey)."=?";
|
push @where, $dbh->quote_identifier($sequenceKey)."=?";
|
||||||
}
|
}
|
||||||
|
|
||||||
# merge all clauses with the main query
|
# merge all clauses with the main query
|
||||||
|
|
@ -599,9 +599,13 @@ sub reorder {
|
||||||
my $dbh = $db->dbh;
|
my $dbh = $db->dbh;
|
||||||
|
|
||||||
# find all the items in this sequence
|
# find all the items in this sequence
|
||||||
|
my @params = ();
|
||||||
|
if ($sequenceKey) {
|
||||||
|
push @params, $sequenceKeyValue;
|
||||||
|
}
|
||||||
my $clause = ($sequenceKey) ? "where ".$dbh->quote_identifier($sequenceKey)."=?" : '';
|
my $clause = ($sequenceKey) ? "where ".$dbh->quote_identifier($sequenceKey)."=?" : '';
|
||||||
my $current = $db->read("select ".$dbh->quote_identifier($tableKey)." from ".$dbh->quote_identifier($tableName)."
|
my $current = $db->read("select ".$dbh->quote_identifier($tableKey)." from ".$dbh->quote_identifier($tableName)."
|
||||||
$clause order by sequenceNumber", [$sequenceKeyValue]);
|
$clause order by sequenceNumber", \@params);
|
||||||
|
|
||||||
# query to update items in the sequence
|
# query to update items in the sequence
|
||||||
$clause = ($sequenceKey) ? "and ".$dbh->quote_identifier($sequenceKey)."=?" : '';
|
$clause = ($sequenceKey) ? "and ".$dbh->quote_identifier($sequenceKey)."=?" : '';
|
||||||
|
|
@ -614,7 +618,11 @@ sub reorder {
|
||||||
if ($id eq $self->getId) {
|
if ($id eq $self->getId) {
|
||||||
$objectData{id $self} = $i;
|
$objectData{id $self} = $i;
|
||||||
}
|
}
|
||||||
$change->execute([$i, $id, $sequenceKeyValue]);
|
my @params = ($i, $id);
|
||||||
|
if ($sequenceKey) {
|
||||||
|
push @params, $sequenceKeyValue;
|
||||||
|
}
|
||||||
|
$change->execute(\@params);
|
||||||
$i++;
|
$i++;
|
||||||
}
|
}
|
||||||
$db->commit;
|
$db->commit;
|
||||||
|
|
|
||||||
25
t/Crud.t
25
t/Crud.t
|
|
@ -28,7 +28,7 @@ my $session = WebGUI::Test->session;
|
||||||
#----------------------------------------------------------------------------
|
#----------------------------------------------------------------------------
|
||||||
# Tests
|
# Tests
|
||||||
|
|
||||||
plan tests => 29; # Increment this number for each test you create
|
plan tests => 43; # Increment this number for each test you create
|
||||||
|
|
||||||
#----------------------------------------------------------------------------
|
#----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
|
@ -87,6 +87,29 @@ is($record2->get('sequenceNumber'), 3, "demotion from middle works");
|
||||||
$record2->promote;
|
$record2->promote;
|
||||||
is($record2->get('sequenceNumber'), 2, "promotion from middle works");
|
is($record2->get('sequenceNumber'), 2, "promotion from middle works");
|
||||||
|
|
||||||
|
# deleting
|
||||||
|
ok($record2->delete, "deletion reports success");
|
||||||
|
my $copyOfRecord3 = WebGUI::Crud->new($session, $record3->getId);
|
||||||
|
my $copyOfRecord4 = WebGUI::Crud->new($session, $record4->getId);
|
||||||
|
is($copyOfRecord3->get('sequenceNumber'), '2', "deletion of record 2 moved record 3 to sequence 2");
|
||||||
|
is($copyOfRecord4->get('sequenceNumber'), '3', "deletion of record 2 moved record 4 to sequence 3");
|
||||||
|
|
||||||
|
# updating
|
||||||
|
sleep 1;
|
||||||
|
ok($copyOfRecord4->update, "update returns success");
|
||||||
|
isnt($copyOfRecord4->get('lastUpdated'), $copyOfRecord4->get('dateCreated'), "updates work");
|
||||||
|
|
||||||
|
# retrieve data
|
||||||
|
is(WebGUI::Crud->getAllSql($session), "select `id` from `unnamed_crud_table` order by sequenceNumber", "getAllSql() no options");
|
||||||
|
is(WebGUI::Crud->getAllSql($session,{sequenceKeyValue=>1}), "select `id` from `unnamed_crud_table` order by sequenceNumber", "getAllSql() sequence key value with no key specified");
|
||||||
|
is(WebGUI::Crud->getAllSql($session,{limit=>5}), "select `id` from `unnamed_crud_table` order by sequenceNumber limit 5", "getAllSql() with a row limit");
|
||||||
|
is(WebGUI::Crud->getAllSql($session,{limit=>[10,20]}), "select `id` from `unnamed_crud_table` order by sequenceNumber limit 10,20", "getAllSql() with a start and row limit");
|
||||||
|
is(WebGUI::Crud->getAllSql($session,{orderBy=>'lastUpdated'}), "select `id` from `unnamed_crud_table` order by `lastUpdated`", "getAllSql() with a custom order by clause");
|
||||||
|
is(scalar(@{WebGUI::Crud->getAllIds($session)}), 3, "getAllIds()");
|
||||||
|
my $iterator = WebGUI::Crud->getAllIterator($session);
|
||||||
|
while (my $object = $iterator->()) {
|
||||||
|
isa_ok($object, 'WebGUI::Crud');
|
||||||
|
}
|
||||||
|
|
||||||
#----------------------------------------------------------------------------
|
#----------------------------------------------------------------------------
|
||||||
# Cleanup
|
# Cleanup
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue