Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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.
#!/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 messag +es. 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::Anydat +a 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 ta +ble fields and data definitions # ---------------------------------------------- +-------------------------------- my @table_fields = qw/ user_id first_name last_n +ame email credits login_id password /; my %table_field_def = ( user_id => 'char(20)', first_name => 'char(20)', last_name => 'char(20)', email => 'char(20)', credits => 'char(20)', login_id => 'char(20)', password => 'char(20)' ); print p(" Table Fields = ", @table_fields) if $ +DEBUG; print p(" Table Fields def = ", %table_field_de +f) if $DEBUG; my $dbh = DBI->connect('dbi:AnyData(RaiseError=> +1):') or die "Can not create database connection"; # build the table using SQL # --------------------------------- $dbh->do ( "CREATE TABLE the_table (" . join(',', map { $_ . ' ' . $table_fiel +d_def{$_} } @table_fields) . ")" ) or die "Can not create table"; $dbh->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"); # Th +is 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 en +tered 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 dataf +ile 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_us +ers_file(); } } # end if if statement for $confirm } else { # if there no parameters print a webform and ask conformati +on to install print hr, start_form; # create a form using CGI.PM print p("Please type in YES (In capitals) to proceed with i +nstallation: ", textfield("confirm")); print submit(-name=>'submit button'); print end_form, hr; +# end the form } print end_html; # this closes the web page properly

In reply to Re: Testing for Beginners by Anonymous Monk
in thread Testing for Beginners by markmoon

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (5)
As of 2024-03-28 23:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found