Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

Comment on

( #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 # 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

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

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

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

    How do I use this? | Other CB clients
    Other Users?
    Others having an uproarious good time at the Monastery: (5)
    As of 2016-07-24 01:11 GMT
    Find Nodes?
      Voting Booth?
      What is your favorite alternate name for a (specific) keyboard key?

      Results (221 votes). Check out past polls.