unit test case for this perl code

unit test case for this perl code

am 02.09.2011 10:46:30 von Anirban Adhikary

--0-2074727828-1314953190=:37105
Content-Type: text/plain; charset=iso-8859-1
Content-Transfer-Encoding: quoted-printable

Hi I have write the following code and Now I have been asked to write a uni=
t test case for this code. But neither I have=A0 any idea how to write a un=
it test case nor I have ever experience with it. So in this case I am askin=
g your help. #!/usr/bin/perl ###=0A## The program is called by ma=
in.pl=0A## dependent conf file is the property.conf located=0A## under the =
config folder in the home directory=0A### use strict;=0Ause DBI;=0Ause=
File::Basename;=0Ause Cwd qw(realpath); my $home =3D dirname( realpat=
h( $0 )); my %config =3D &readConfigFile($home."/config/global.conf");=
=0Amy $db =3D &createDbConnection($config{'user'},$config{'passwd'},$confi g=
{'dbalias'}); my $stmt =3D $db->prepare("select job,OCCURRENCE,retenti=
on_days from CONFIG.PROCESS_LOG");=0A$stmt->execute(); my %prunehash;=
=0Awhile(my @row =3D stmt->fetchrow_array) {     $prunehash{$row[0]=
..':~:'.$row[1]} =3D $row[2];=0A}=0A$stmt->finish; foreach (keys %prune=
hash) {     my ($job,$occ) =3D split(':~:',$_);     my $ret=
days =3D $prunehash{$_};     my $delstmt =3D $db->prepare("delete f=
rom LOG.PROCESS_LOG where days (CURRENT_TIMESTAMP - LOG.PROCESS_LOG.WHEN) >=
".$retdays." and LOG.PROCESS_LOG.JOB =3D '".$job."' and LOG.PROCESS_LOG.OC=
CURRENCE =3D ".$occ." ");         $delstmt->execute;  =
   $delstmt->finish;=0A} =0A$db->disconnect; ## SubRoutines.=
sub config {     my ($flname) =3D @_;     my %chash;=
    open(FILE, "<$flname") || return 1; # die ("cannot open");=0A=
    # loading conf from file     while () {  =A0=
=A0   =A0 chop $_ and next if (/^#/ || $_ eq "\n");       =
=A0 my ($name, $value)=3D/(.*)=3D(.*)/;       =A0 next if ((!$n=
ame) || (!$value));   =A0   =A0 $name=3D&trim($name);  =A0=
=A0   =A0 $value=3D&trim($value);   =A0   =A0 next if (( $=
name eq "") ||( $value eq ""));       =A0 $chash{$name} =3D $va=
lue;     }     close(FILE);   =A0 return %chash;=
=0A} sub createDbConnection {     my ($uid,$passwd,$dbalias) =
=3D @_;     return DBI->connect ("dbi:DB2:$dbalias", $uid, $passwd)=
or die 'Unable to connect'     =0A} sub trim {     (m=
y $str)=3D(@_);     $str =3D~ s/^\s+|\s+$//g;     return $s=
tr;=0A}=0A################################################## ######=0Aglobal=
..conf=A0 contents user= =0Apasswd= =0Adbalias= =0A=0 AThans=
& Regards in advance=0ARatul.
--0-2074727828-1314953190=:37105--

Re: unit test case for this perl code

am 03.09.2011 12:28:15 von Sayth Renshaw

On Sep 2, 4:46=A0am, ratul...@yahoo.co.in (anirban adhikary) wrote:
> Hi I have write the following code and Now I have been asked to write a u=
nit test case for this code. But neither I have=A0 any idea how to write a =
unit test case nor I have ever experience with it. So in this case I am ask=
ing your help.
>

> Thans & Regards in advance
> Ratul.

What purpose was your code written to perform? Your unit test case
should demonstrate that it performs the function without side effect
or error.

Sayth


--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: unit test case for this perl code

am 03.09.2011 16:32:55 von Peter Scott

On Fri, 02 Sep 2011 14:16:30 +0530, anirban adhikary wrote:

> Hi I have write the following code and Now I have been asked to write a
> unit test case for this code. But neither I have  any idea how to =
write
> a unit test case nor I have ever experience with it. So in this case I
> am asking your help.

[Snip program that deletes old records from a database]

If someone used the words "unit test" in a request to you then they=20
likely have an existing testing environment and expectations of how tests=
=20
should behave and what scope they have. Or is this the first unit test=20
that the person asking you will have? So find out what "unit test" means=
=20
to them, because there are many possibilities. Assuming they used the=20
term in its usual sense, this test would be one unit out of many others,=20
so what do those other tests do, what protocols do they follow, where do=20
they send their results? Without knowing those things any well-meaning=20
answers you could get here would probably lead you in the wrong=20
direction. Tell us what you find out.

--=20
Peter Scott
http://www.perlmedic.com/ http://www.perldebugged.com/
http://www.informit.com/store/product.aspx?isbn=3D0137001274
http://www.oreillyschool.com/certificates/perl-programming.p hp

--=20
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/