<?xml version="1.0" encoding="windows-1252"?>
<node id="343635" title="Re: Testing for Beginners" created="2004-04-08 11:32:56" updated="2005-08-10 14:35:16">
<type id="11">
note</type>
<author id="961">
Anonymous Monk</author>
<data>
<field name="doctext">
As a noob, I am in favour of the test first strategy, I think it can/will save me lots of time in the long-run if I can teach myself to do it!

I am primarily writing web apps, CGI.PM and DBI (using DBD::ANYDATA) so some guidance on writing tests for this sort of thing would be appreciated.

Below is some code I threw together this afternoon to create some CSV datafiles using SQL Perl, etc etc.

It works, I know this because I tested it manually.
How would I write a proper test for this?

And yes I know I should have written the test first, but lets ignore that for now. Any other constructive criticisms would also be appreciated.

&lt;CODE&gt;
#!/usr/bin/perl -wT

# ---------------------------------------------
#
# Description:
# This script is designed to be used once to install the system
#  We will use DBI and DBD::AnyData to create CSV datafiles
#  We use CGI.PM to handle HTML
#
# History:
# ========
# 07 April 2004, - Initial file created, based on e-judo.cgi at e-judo.sourceforge.net
# 08 April 2004, - Wrote the subs to create all the data files. 



    my $DEBUG = 1; #  If this is set to 1 then we see the debug messages.



use strict; # force strict programming controls



use CGI qw(:standard); # use the CGI.PM module
use lib './MyLib'; # use the modules in MyLib, this is the DBD::Anydata used for database activities
use DBI; # This calls the DBI module, which along with the line above allows us to do database activities



# Sub Routines
# ----------------------------------------------------------------

sub create_users_file {

                      # This sub creates the users_csv file
                      print p("Start of create_users_file") if $DEBUG;

                      # create the scalers we need to use in the sql
                      # ----------------------------------------------
                      my $table = "data/users_csv/";
                      print p(" table name = ", $table ) if $DEBUG;
                      # Okay now we must create the database files
                      # here is the DBI/SQL code
                      # ----------------------------------------------------


                      # First create the array and hash to hold the table fields and data definitions
                      # ------------------------------------------------------------------------------
                      my @table_fields = qw/ user_id first_name last_name email credits login_id password /;
                      my %table_field_def = (
                                          user_id =&gt; 'char(20)',
                                          first_name =&gt; 'char(20)',
                                          last_name =&gt; 'char(20)',
                                          email =&gt; 'char(20)',
                                          credits =&gt; 'char(20)',
                                          login_id =&gt; 'char(20)',
                                          password =&gt; 'char(20)' );
                       print p(" Table Fields = ", @table_fields) if $DEBUG;
                       print p(" Table Fields def = ", %table_field_def) if $DEBUG;

                      my $dbh = DBI-&gt;connect('dbi:AnyData(RaiseError=&gt;1):') or die "Can not create database connection";

                      # build the table using SQL
                      # ---------------------------------
                      $dbh-&gt;do (
                               "CREATE TABLE the_table (" .
                                join(',', map { $_ . ' ' . $table_field_def{$_} } @table_fields) .
                                ")" )
                                or die "Can not create table";

                                $dbh-&gt;func( 'the_table', 'CSV', $table, 'ad_export');

                      print p("User table created") if $DEBUG;



                      print p("END of create_users_file") if $DEBUG;

}




# End Sub-Routines
# ------------------





# Main Code starts here
# -----------------------


print header(), start_html("Installation"), h1("Install Script"); # This line uses CGI.PM to to create the webpage


if (param()){  # If there is a parameter(or parameters) then validate, else show the login screen.
# the following lines are excecuted if paramaters HAVE been entered

             my $confirm = param("confirm"); # $confirm is the text entered on the  webpage form entered by the user
             if ($confirm eq "YES") {      # If the user enetered YES (in caps) then run the install

                                     # first, check if the users datafile exists, if not we will create it.
                                     if (-e "data/users_csv"){
                          	                               print p("users_csv exists") if $DEBUG;
                                        } else {
                                                               print p("users_csv does not exist so about to call the create_users_file sub") if $DEBUG;
                                  	                       create_users_file();
                                                          }


                    } # end if if statement for $confirm





} else {   # if there no parameters print a webform and ask conformation to install



       	print hr, start_form;          # create a form using CGI.PM
       	print p("Please type in YES (In capitals) to proceed with installation: ", textfield("confirm"));
       	print submit(-name=&gt;'submit button');
       	print end_form, hr;                                        # end the form






}
print end_html; # this closes the web page properly

&lt;/CODE&gt;

</field>
<field name="root_node">
343595</field>
<field name="parent_node">
343595</field>
<field name="reputation">
18</field>
</data>
</node>
