_verifyId method: initial implementation.
Internal method for use to verify that an id we have really is in the database. For use before other operations.
This commit is contained in:
parent
de7b145ac3
commit
587144f03e
2 changed files with 71 additions and 2 deletions
|
@ -27,6 +27,8 @@ our @EXPORT = qw(
|
|||
|
||||
our $VERSION = '0.02';
|
||||
|
||||
use Scalar::Util qw(looks_like_number);
|
||||
|
||||
######################################################################
|
||||
|
||||
=begin new
|
||||
|
@ -100,10 +102,52 @@ sub addSupporter ($$) {
|
|||
$sth->execute($sp->{ledger_entity_id}, $sp->{display_name}, $sp->{public_ack});
|
||||
my $id = $this->dbh->last_insert_id("","","","");
|
||||
$sth->finish();
|
||||
|
||||
return $id;
|
||||
}
|
||||
|
||||
######################################################################
|
||||
|
||||
=head1 Non-Public Methods
|
||||
|
||||
These methods are part of the internal implementation are not recommended for
|
||||
use outside of this module.
|
||||
|
||||
=over
|
||||
|
||||
=item _verifyId()
|
||||
|
||||
Parameters:
|
||||
|
||||
=over
|
||||
|
||||
=item $self: current object.
|
||||
|
||||
=item $id: A scalar numeric argument that is the to lookup
|
||||
|
||||
|
||||
=back
|
||||
|
||||
Returns: scalar boolean, which is true iff. the $id is valid and already in the supporter database.
|
||||
|
||||
|
||||
=cut
|
||||
|
||||
|
||||
sub _verifyId($) {
|
||||
my($self, $id) = @_;
|
||||
|
||||
die "_verifyId() called with a non-numeric id" unless defined $id and looks_like_number($id);
|
||||
|
||||
my $val = $self->dbh()->selectall_hashref("SELECT id FROM supporter WHERE id = $id", 'id');
|
||||
use Data::Dumper;
|
||||
return (defined $val and defined $val->{$id});
|
||||
|
||||
}
|
||||
|
||||
|
||||
=back
|
||||
|
||||
=cut
|
||||
|
||||
1;
|
||||
__END__
|
||||
|
|
|
@ -6,7 +6,7 @@
|
|||
use strict;
|
||||
use warnings;
|
||||
|
||||
use Test::More tests => 9;
|
||||
use Test::More tests => 13;
|
||||
use Test::Exception;
|
||||
|
||||
use Scalar::Util qw(looks_like_number);
|
||||
|
@ -56,6 +56,31 @@ lives_ok { $id2 = $sp->addSupporter({ display_name => "Donald Drapper",
|
|||
ok( (looks_like_number($id2) and $id2 > $id1),
|
||||
"addSupporter: add works with public_ack set to true and a display_name given");
|
||||
|
||||
=pod
|
||||
|
||||
Tests for internal methods:
|
||||
|
||||
=over
|
||||
|
||||
|
||||
=item _verifyId tests
|
||||
|
||||
=cut
|
||||
|
||||
ok( $sp->_verifyId($id2), "_verifyId: id just added exists");
|
||||
|
||||
dies_ok { $sp->_verifyId(undef); } "_verifyId: dies for undefined id";
|
||||
dies_ok { $sp->_verifyId("String") } "_verifyId: dies for non-numeric id";
|
||||
|
||||
# This is a hacky way to test this; but should work
|
||||
ok(not ($sp->_verifyId($id2 + 10)), "_verifyId: non-existent id is not found");
|
||||
|
||||
=pod
|
||||
|
||||
=back
|
||||
|
||||
=cut
|
||||
|
||||
$dbh->disconnect();
|
||||
###############################################################################
|
||||
#
|
||||
|
|
Loading…
Reference in a new issue