Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

My first attempt to design a module

by paragkalra (Scribe)
on Oct 04, 2010 at 04:15 UTC ( [id://863241]=perlquestion: print w/replies, xml ) Need Help??

paragkalra has asked for the wisdom of the Perl Monks concerning the following question:

Hi All,

I am planning to design a small module that would make use of object oriented concepts of Perl.

Since its my first attempt in the field of Oops, I would request Perl experts to review the code design as and when I would share the new piece of development for this module.

Basic idea of the module: In many projects, qa needs to validate database tables by executing many sql queries (test cases). The category of these sql queries could be:

1. Count sql queries where in count returned by each sql query should be exactly equal to the known count 2. Count sql queries where in count returned by each sql query should be more than known count. 3. Count sql queries where in count returned by each sql query should be less than known count. 4. Minus sql queries (sql1 minus sql2) where in expected output is 'no rows returned' meaning data set of the the sql queries is exactly same.

The test cases i.e. sql queries would be mentioned in configuration file (called cfg) - 1 sql query per line.

The configuration file would be given for processing to appropriate method. This method would then execute all the test cases and generate the detailed results in the result directory specified by the user.

I have attached initial raw draft of the module and also a test file to test the module.

PS: Its just the first version and with only 1 method. I plan to implement the remaining 5-6 methods gradually.

The reason I want to get it reviewed is because if it turns out to be success I would love to release it to CPAN

Please feel free to share your valuable suggestions.

Main Module

# Author: Parag Kalra, paragkalra@gmail.com # Licensed under GNU Public License - http://www.gnu.org/licenses/gpl. +html package QA::Automation; use strict; use warnings; =head1 NAME QA::Automation - This module is used to execute Oracle database test c +ases (sql queries) and validate the automation suite =head1 DESCRIPTION This module provides tools (methods) Automate SQL query execution agai +nst Oracle database To start with, user needs to create the object by providing database c +onnection details, result directory and some other non-mandatory parameters. As of now plan is to provide following methods: 1. create_cfg_from_uncommented_sql : This method creates cfg (configur +ation file) from an uncommented sql query. This cfg forms part of the automation suite that would be executed by +appropriate method 2. execute_exact_count_sqls : This Method would execute all the count +sqls mentioned in the cfg and compare the returned count against the count mentioned for the respective sql quer +y. All the test cases where mismatch would be found in the count, would b +e marked as FAILED or else PASSED Once all the test cases (sql queries) are executed, a detailed result +summary would be created in the result directory specified by the user. 3. execute_minus_match_sqls : This method would execute all the minus +sqls mentioned in the cfg and would check if any data is returned or not. If returned then test cases would be m +arked as FAILED or else PASSED The intention is to check that data set of minus separated queries is +same. Once all the test cases (sql queries) are executed, a detailed result +summary would be created in the result directory specified by the user. Any many more methods are in pipe line =head1 SYNOPSIS use QA::Automation; my $qa_atm = QA::Automation -> new ( uname => "scott", passwd => "tiger", + sid => "orcl", result => "home/my +/dir", ORA_HOME => 1, DETAILED_RESULT => + 1, CREATE_RESULT_DIR +=> 1, ); =head1 REQUIRES DBI DBD::Oracle File::Path =cut # Required modules use DBI; use File::Path; =head1 CONSTRUCTOR AND STARTUP =head2 new =over 4 This method creates the object required for automation of test cas +e execution. It also creates the result directory if it does not exists and use +r wants to create it. =back =cut sub new { my $class = shift; my %args = @_; my $self = \%args; my $result_dir = $self->{'result'}; # if the directory doesn't exists create then create it if ((! -d $result_dir) && ($self->{'CREATE_RESULT_DIR'})){ mkpath ($result_dir); } bless $self, $class or die "Can't bless $class: $!"; return $self; } =head1 METHODS =head2 create_cfg_from_uncommented_sql =over 4 This method prepares the cfg from the sql file provided as input b +y the user. Pre-requisites for this method: 1. All the sql queries mentioned under the sql file should end wit +h a semi-colon (;) 2. There should be no comments in the sql file =back =cut sub create_cfg_from_uncommented_sql { my $self = shift; my $sql = shift; # Declaring variables local $/ = ';'; # Setting the IFS to a semicolon my $input_file; my $output_file = "$self->{'result'}/SQL.out"; # Checking the existence of the input file if ( ! defined $sql){ die "Input SQL file not provided - Usage: qa_atm_obj->create_c +fg_from_uncommented_sql(SQL_File>\n"; } else { $input_file = $sql; } # Processing input/output files open my $input_fh, '<', $input_file or die "Could not open the inp +ut file - $input_file\n"; open my $output_fh, '>', $output_file or die "Could not create the + output file - $output_file\n"; # Looping through the file and removing all the white space and ap +pending a new line at the end while (<$input_fh>){ $_ =~ s/\s+/ /g; $_ =~ s/^\s+//; $_ =~ s/\s$//; print $output_fh "$_\n"; } # Closing the files close $input_fh or print "Could not close the file - $input_file\n +"; close $output_fh or print "Could not close the file - $output_file +\n"; } =head1 AUTHOR Parag Kalra, paragkalra@gmail.com =cut 1;

Test Script

use strict; use warnings; use QA::Automation; my $qa_atm = QA::Automation->new ( uname => "scott", passwd => "tiger", sid => "orcl", result => "home/my +/dir", ORA_HOME => 1, DETAILED_RESULT => + 1, CREATE_RESULT_DIR +=> 1, ); my @tmp_arr = %{$qa_atm}; $qa_atm->create_cfg_from_uncommented_sql('id_pwd.sql');

Replies are listed 'Best First'.
Re: My first attempt to design a module
by morgon (Priest) on Oct 04, 2010 at 06:49 UTC
    In your constructor you don't have to check for a bless to be successful (can't think of any scenario in which that would ever fail), but you should check if the mkdir is successful.

    But otherwise it looks ok (I don't like shifts though, I prefer the my($self, $sql)=@_; style but that is mainly a matter of taste).

Re: My first attempt to design a module
by phaylon (Curate) on Oct 04, 2010 at 20:43 UTC

    You might want to take a peek at Moose as an OO framework. It already takes care of many things you'd otherwise might end up reinventing.

    Other than that, I'd include the $! variable in your error messages for open and close, since that will contain the actual error message. You might also like autodie since it'll take care of this for you. It's in core since 5.10.1.


    Ordinary morality is for ordinary people. -- Aleister Crowley
Re: My first attempt to design a module
by doug (Pilgrim) on Oct 04, 2010 at 13:38 UTC

    You have the snippet

    sub new { my $class = shift; my %args = @_;

    which I do a bit differently.

    sub new { my ($class,%args) = @_; my $package = ref($class) || $class || confess "classless construct +or";

    because I like to allow for

     $something->new(...)

    where $something can be a string (class name) or an existing object. The existing object bit is especially handy when creating a clone.

    - doug

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://863241]
Approved by vinoth.ree
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (7)
As of 2024-04-19 12:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found