From e017005b17fb29bc105bc728cd062d1bfa186f3a Mon Sep 17 00:00:00 2001 From: JT Smith Date: Mon, 7 Nov 2005 01:54:35 +0000 Subject: [PATCH] added automated code testing framework --- docs/changelog/6.x.x.txt | 1 + sbin/testCodebase.pl | 62 +++++++++++++++ t/Id.t | 56 +++++++++++++ t/SQL.t | 166 +++++++++++++++++++++++++++++++++++++++ t/Utility.t | 109 +++++++++++++++++++++++++ t/_test.skeleton | 43 ++++++++++ 6 files changed, 437 insertions(+) create mode 100644 sbin/testCodebase.pl create mode 100644 t/Id.t create mode 100644 t/SQL.t create mode 100644 t/Utility.t create mode 100644 t/_test.skeleton diff --git a/docs/changelog/6.x.x.txt b/docs/changelog/6.x.x.txt index 875535322..c2667175c 100644 --- a/docs/changelog/6.x.x.txt +++ b/docs/changelog/6.x.x.txt @@ -9,6 +9,7 @@ as much as 100%. See gotcha.txt for details. - Changed macro API which cuts macro memory consumption in half. See migration.txt for details. + - Added automated code testing framework. 6.7.7 diff --git a/sbin/testCodebase.pl b/sbin/testCodebase.pl new file mode 100644 index 000000000..590232f05 --- /dev/null +++ b/sbin/testCodebase.pl @@ -0,0 +1,62 @@ +#------------------------------------------------------------------- +# WebGUI is Copyright 2001-2005 Plain Black Corporation. +#------------------------------------------------------------------- +# Please read the legal notices (docs/legal.txt) and the license +# (docs/license.txt) that came with this distribution before using +# this software. +#------------------------------------------------------------------- +# http://www.plainblack.com info@plainblack.com +#------------------------------------------------------------------- + +$|=1; + +use strict; +use lib '../..'; +use Getopt::Long; + +my $configFile; +my $help; + +GetOptions( + 'configFile=s'=>\$configFile, + 'help'=>\$help + ); + +if ($help || !$configFile) { + print < 2; # increment this value for each test you create +use WebGUI::Id; +use WebGUI::Utility; + +initialize(); # this line is required + +# generate +my $generateId = WebGUI::Id::generate(); +is(length($generateId), 22, "generate() - length of 22 characters"); +my @uniqueIds; +my $isUnique = 1; +for (1..2000) { + last unless $isUnique; + my $id = WebGUI::Id::generate(); + $isUnique = !isIn($id,@uniqueIds); + push(@uniqueIds,$id); +} +ok($isUnique, "generate() - unique"); + +cleanup(); # this line is required + + +# ---- DO NOT EDIT BELOW THIS LINE ----- + +sub initialize { + $|=1; # disable output buffering + my $configFile; + GetOptions( + 'configFile=s'=>\$configFile + ); + exit 1 unless ($configFile); + WebGUI::Session::open("..",$configFile); +} + +sub cleanup { + WebGUI::Session::close(); +} + diff --git a/t/SQL.t b/t/SQL.t new file mode 100644 index 000000000..4f3b91f6b --- /dev/null +++ b/t/SQL.t @@ -0,0 +1,166 @@ +#------------------------------------------------------------------- +# WebGUI is Copyright 2001-2005 Plain Black Corporation. +#------------------------------------------------------------------- +# Please read the legal notices (docs/legal.txt) and the license +# (docs/license.txt) that came with this distribution before using +# this software. +#------------------------------------------------------------------- +# http://www.plainblack.com info@plainblack.com +#------------------------------------------------------------------- + +# ---- BEGIN DO NOT EDIT ---- +use strict; +use lib '../lib'; +use Getopt::Long; +use WebGUI::Session; +# ---- END DO NOT EDIT ---- + + +use Test::More tests => 31; # increment this value for each test you create +use WebGUI::SQL; + +initialize(); # this line is required + +# read +ok(my $sth = WebGUI::SQL->read("select * from settings"), "read()"); + +# array +my @row = $sth->array; +is(@row, 2, "array()"); + +# getColumnNames +my @columnNames = $sth->getColumnNames; +ok($columnNames[0] eq "name" && $columnNames[1] eq "value", "geColumnNames()"); + +# hash +is(scalar($sth->hash), "2/8", "hash()"); + +# hashRef +is(scalar(%{$sth->hashRef}), "2/8", "hashRef()"); + +# rows +ok($sth->rows > 1, "rows()"); + +# finish +ok($sth->finish, "finish()"); + +# unconditionalRead +ok(my $sth = WebGUI::SQL->unconditionalRead("select * from tableThatDoesntExist"), "unconditionalRead()"); + +# errorCode +is($sth->errorCode, "1146" ,"errorCode()"); + +# errorMessage +ok($sth->errorMessage =~ m/Table .*\.tablethatdoesntexist. doesn.t exist/ , "errorMessage()"); + +$sth->finish; + +# quote +is(quote("that's great"), "'that\\\'s great'", "quote()"); + +# quoteAndJoin +my @quoteAndJoin = ("that's great", '"Howdy partner!"'); +is(quoteAndJoin(\@quoteAndJoin), "'that\\\'s great','\\\"Howdy partner!\\\"'", "quoteAndJoin()"); + +# beginTransaction +SKIP: { + skip("Don't know how to test beginTransaction.",1); + ok(undef,"beginTransaction()"); +} + +# commit +SKIP: { + skip("Don't know how to test commit",1); + ok(undef, "commit()"); +} + +# rollback +SKIP: { + skip("Don't know how to test rollback()",1); + ok(undef, "rollback()"); +} + +# prepare +ok(my $sth = WebGUI::SQL->prepare("select value from settings where name=?"), "prepare()"); + +# execute +$sth->execute(['showDebug']); +is($sth->errorCode, undef, "execute()"); + +$sth->finish; + +# quickArray +my ($value) = WebGUI::SQL->quickArray("select value from settings where name='authMethod'"); +ok($value, "quickArray()"); + +# write +WebGUI::SQL->write("delete from incrementer where incrementerId='theBigTest'"); # clean up previous failures +WebGUI::SQL->write("insert into incrementer (incrementerId, nextValue) values ('theBigTest',25)"); +my ($value) = WebGUI::SQL->quickArray("select nextValue from incrementer where incrementerId='theBigTest'"); +is($value, 25, 'write()'); + +# quickCSV +is(WebGUI::SQL->quickCSV("select * from incrementer where incrementerId='theBigTest'"), "incrementerId,nextValue\ntheBigTest,25\n", "quickCSV()"); + +# quickHash +my %quickHash = WebGUI::SQL->quickHash("select * from incrementer where incrementerId='theBigTest'"); +is($quickHash{nextValue}, 25, "quickHash()"); + +# quickHash +my $quickHashRef = WebGUI::SQL->quickHashRef("select * from incrementer where incrementerId='theBigTest'"); +is($quickHashRef->{nextValue}, 25, "quickHashRef()"); + +# quickTab +is(WebGUI::SQL->quickTab("select * from incrementer where incrementerId='theBigTest'"), "incrementerId\tnextValue\ntheBigTest\t25\n", "quickCSV()"); + +# buildArray +my ($buildArray) = WebGUI::SQL->buildArray("select nextValue from incrementer where incrementerId='theBigTest'"); +is($buildArray, 25, "buildArray()"); + +# buildArrayRef +my $buildArrayRef = WebGUI::SQL->buildArrayRef("select nextValue from incrementer where incrementerId='theBigTest'"); +is($buildArrayRef->[0], 25, "buildArrayRef()"); + +# buildHash +my %buildHash = WebGUI::SQL->buildHash("select incrementerId,nextValue from incrementer where incrementerId='theBigTest'"); +is($buildHash{theBigTest}, 25, "buildHash()"); + +# buildHashRef +my $buildHashRef = WebGUI::SQL->buildHashRef("select incrementerId,nextValue from incrementer where incrementerId='theBigTest'"); +is($buildHashRef->{theBigTest}, 25, "buildHashRef()"); + +# getNextId +is(getNextId('theBigTest'), 25, "getNextId()"); +WebGUI::SQL->write("delete from incrementer where incrementerId='theBigTest'"); + +# setRow +my $setRowId = WebGUI::SQL->setRow("incrementer","incrementerId",{incrementerId=>"new", nextValue=>47}); +ok($setRowId ne "", "setRow() - return ID"); +my ($setRowResult) = WebGUI::SQL->quickArray("select nextValue from incrementer where incrementerId=".quote($setRowId)); +is($setRowResult, 47, "setRow() - set data"); + +# getRow +my $getRow = WebGUI::SQL->getRow("incrementer","incrementerId",$setRowId); +is($getRow->{nextValue}, 47, "getRow()"); +WebGUI::SQL->write("delete from incrementer where incrementerId=".quote($setRowId)); + + +cleanup(); # this line is required + + +# ---- DO NOT EDIT BELOW THIS LINE ----- + +sub initialize { + $|=1; # disable output buffering + my $configFile; + GetOptions( + 'configFile=s'=>\$configFile + ); + exit 1 unless ($configFile); + WebGUI::Session::open("..",$configFile); +} + +sub cleanup { + WebGUI::Session::close(); +} + diff --git a/t/Utility.t b/t/Utility.t new file mode 100644 index 000000000..545f307a8 --- /dev/null +++ b/t/Utility.t @@ -0,0 +1,109 @@ +#------------------------------------------------------------------- +# WebGUI is Copyright 2001-2005 Plain Black Corporation. +#------------------------------------------------------------------- +# Please read the legal notices (docs/legal.txt) and the license +# (docs/license.txt) that came with this distribution before using +# this software. +#------------------------------------------------------------------- +# http://www.plainblack.com info@plainblack.com +#------------------------------------------------------------------- + + +# ---- BEGIN DO NOT EDIT ---- +use strict; +use lib '../lib'; +use Getopt::Long; +use WebGUI::Session; +# ---- END DO NOT EDIT ---- + +use Test::More tests => 20; # increment this value for each test you create + +initialize(); # this line is required + + +# commify +is(WebGUI::Utility::commify(10), "10", 'commify() - no comma needed'); +is(WebGUI::Utility::commify(1000), "1,000", 'commify() - single comma'); +is(WebGUI::Utility::commify(10000000), "10,000,000", 'commify() - multiple commas'); + +# formatBytes +is(WebGUI::Utility::formatBytes(10), '10 B', 'formatBytes() - bytes'); +is(WebGUI::Utility::formatBytes(4300), '4 kB', 'formatBytes() - kilobytes'); +is(WebGUI::Utility::formatBytes(1700000), '2 MB', 'formatBytes() - megabytes'); + +# isBetween +ok(WebGUI::Utility::isBetween(0,-1,1), 'isBetween() - negative and positive range'); +ok(WebGUI::Utility::isBetween(11,1,15), 'isBetween() - positive range'); +ok(WebGUI::Utility::isBetween(-5,-10,-2), 'isBetween() - negative range'); + +# isIn +ok(WebGUI::Utility::isIn("webgui", qw(cars trucks webgui trains)), 'isIn()'); + +# makeArrayCommaSafe +my @commaFilledArray = ("this,that", "foo,bar", "x-y"); +WebGUI::Utility::makeArrayCommaSafe(\@commaFilledArray); +my $noCommaFound = 1; +foreach my $row (@commaFilledArray) { + $noCommaFound = 0 if ($row =~ m/,/); +} +ok($noCommaFound, 'makeArrayCommaSafe()'); + +# makeCommaSafe +ok(!(WebGUI::Utility::makeCommaSafe("this,that,foo,,bar") =~ m/,/), 'makeCommaSafe()'); + +# makeTabSafe +ok(!(WebGUI::Utility::makeTabSafe("this\tthat\tfoo\tbar\t") =~ m/\t/), 'makeTabSafe()'); + +# randint +my $number = WebGUI::Utility::randint(50,75); +ok($number >= 50 && $number <= 75, 'randint()'); + +# randomizeArray +SKIP: { + skip("Don't know how to test randomizeArray.",1); + ok(undef, 'randomizeArray()'); + } + +# randomizeHash +SKIP: { + skip("Don't know how to test randomizeHash.",1); + ok(undef, 'randomizeHash()'); + } + +# round +is(WebGUI::Utility::round(47.133984233, 0), 47, 'round() - 0 significant digits'); +is(WebGUI::Utility::round(47.133984233, 3), 47.134, 'round() - multiple significant digits'); + +# sortHash +SKIP: { + skip("Don't know how to test sortHash.",1); + ok(undef, 'sortHash()'); + } + +# sortHashDescending +SKIP: { + skip("Don't know how to test sortHashDescending.",1); + ok(undef, 'sortHashDescending()'); + } + + +cleanup(); # this line is required + + +# ---- DO NOT EDIT BELOW THIS LINE ----- + +sub initialize { + $|=1; # disable output buffering + my $configFile; + GetOptions( + 'configFile=s'=>\$configFile + ); + exit 1 unless ($configFile); + WebGUI::Session::open("..",$configFile); +} + +sub cleanup { + WebGUI::Session::close(); +} + + diff --git a/t/_test.skeleton b/t/_test.skeleton new file mode 100644 index 000000000..2e4ef2b7b --- /dev/null +++ b/t/_test.skeleton @@ -0,0 +1,43 @@ +#------------------------------------------------------------------- +# WebGUI is Copyright 2001-2005 Plain Black Corporation. +#------------------------------------------------------------------- +# Please read the legal notices (docs/legal.txt) and the license +# (docs/license.txt) that came with this distribution before using +# this software. +#------------------------------------------------------------------- +# http://www.plainblack.com info@plainblack.com +#------------------------------------------------------------------- + +# ---- BEGIN DO NOT EDIT ---- +use strict; +use lib '../lib'; +use Getopt::Long; +use WebGUI::Session; +# ---- END DO NOT EDIT ---- + + +use Test::More tests => 1; # increment this value for each test you create + +initialize(); # this line is required + +# put your tests here + +cleanup(); # this line is required + + +# ---- DO NOT EDIT BELOW THIS LINE ----- + +sub initialize { + $|=1; # disable output buffering + my $configFile; + GetOptions( + 'configFile=s'=>\$configFile + ); + exit 1 unless ($configFile); + WebGUI::Session::open("..",$configFile); +} + +sub cleanup { + WebGUI::Session::close(); +} +