Skip to content

Commit

Permalink
added sql execute result check
Browse files Browse the repository at this point in the history
  • Loading branch information
liseen committed Jan 10, 2012
1 parent 7642ec2 commit 0bf52aa
Show file tree
Hide file tree
Showing 2 changed files with 209 additions and 1 deletion.
204 changes: 204 additions & 0 deletions samples/Simple/test/lib/Simple/Test.pm
@@ -0,0 +1,204 @@
package Simple::Test;

use strict;
use warnings;
#use Smart::Comments;
#
use JSON;
use Time::HiRes qw/gettimeofday tv_interval/;
use Test::Deep;
use QAT;

use Test::Base -Base;
our @EXPORT = qw/run_blocks/;

my $json = JSON->new->allow_nonref;

our %DBHCache;
our %PerfStat;

END {
# disconnect database
for my $dbh (values %DBHCache) {
$dbh->disconnect;
}
}

sub check_response ($$$) {
my ($block, $content, $elapsed) = @_;

my $name = $block->name;

my $response = $block->response;
my $response_like = $block->response_like;
my $response_deep = $block->response_deep;
my $response_validator = $block->response_validator;
my $response_elapsed_limit = $block->response_elapsed_limit;


if ($response) {
is($content, $response, "$name response");
}

if ($response_deep) {
my $deep_content = $json->decode($content);
my $deep_exp_content = $json->decode($response_deep);
is_deep($deep_content, $deep_exp_content, "$name response deep equal");

}

if ($response_like) {
my $pat = qr/$response_like/;
if ($content =~ $pat) {
while (my ($k, $v) = each %+) {
$ENV{$k} = $v;
### key: $k
### value: $v
}
ok(1, "$name response like");
} else {
ok(0, "$name response like");
}
}

if ($response_validator) {
my $data = $json->decode($content);
eval {
my $validator = QAT::Validator->new(spec => $response_validator);
$validator->validate($data)
};
ok(!$@, "$name response validator $@");
}

if ($response_elapsed_limit) {
ok($response_elapsed_limit > $elapsed * 1000, "$name response elapsed limit")
}
}

sub run_http_block ($) {
my $block = shift;

my $name = $block->name;

my $url = $block->url;

if (!$url) {
my $host = $block->host || $ENV{QAT_ENV_HOST};
my $port = $block->port || $ENV{QAT_ENV_PORT} || 80;
my $uri = $block->uri;
$url = "http://$host:$port/$uri";
}

my ($res, $elapsed) = QAT::Util::do_http_request({
url => $url,
timeout => $block->timeout || 10,
useragent => $block->useragent || '',
method => $block->method || '',
data => $block->data || '',
data_urlencode => $block->data_urlencode || '',
form => $block->form || '',
});

### $elapsed

#ok($res->is_success, "$name request okay");
my $response_code = $block->response_code;
my $response_header = $block->response_header;

if ($response_code) {
is($res->code, $response_code, "$name response code");
}

if ($response_header) {
my @headers = split /\n/, $response_header;
for my $h (@headers) {
my ($hk, $hv) = split /\s*:\s*/, $h, 2;
next if !$hk;

is($res->header($hk), $hv, "$name response header $hk");
}
}

my $content = $res->content;

check_response($block, $content, $elapsed);
}

sub run_db_block ($) {
my $block = shift;

my $name = $block->name;

my $db_dsn = $block->db_dsn;
my $db_user = $block->db_user;
my $db_password = $block->db_password;

my $sql = $block->sql;

my $dbh;
my $db_key = $db_dsn . $db_user . $db_password;
if (exists $DBHCache{$db_key}) {
$dbh = $DBHCache{$db_key};
} else {
require DBI;
$dbh = DBI->connect($db_dsn, $db_user, $db_password);
}


my $data = [];
my $content = undef;

my $sth = $dbh->prepare($sql);

my $t0 = [ gettimeofday ];
my $res = $sth->execute();
my $elapsed = tv_interval($t0);

ok($res, "$name sql execute result: " . $dbh->errstr);

if ($sth->{NUM_OF_FIELDS}) {
while (my $ref = $sth->fetchrow_hashref()) {
push @$data, $ref;
}
my $r = {ret => JSON::true, data => $data };
$content = $json->encode($r);
}
$sth->finish();


check_response($block, $content, $elapsed);
}

sub run_blocks () {
for my $block (blocks) {
if (!$block->is_filtered) {
$block->run_filters;;
}

if ($block->uri || $block->url) {
run_http_block($block);
} elsif ($block->db_dsn) {
run_db_block($block);
} else {
die "must have uri|url or db_dsn";
}
}
}

package Simple::Test::Filter;

use Test::Base::Filter -base;

sub qat_expand_var {
my $v = shift;

$v =~ s/\$(QAT_[_A-Z0-9]+)/
if (!defined $ENV{$1}) {
die "No environment $1 defined.\n";
}
$ENV{$1}/eg;

$v;
}

1;
6 changes: 5 additions & 1 deletion samples/Simple/test/t/func.t
Expand Up @@ -7,8 +7,12 @@ use FindBin;
use lib "$FindBin::Bin/../../../../inc";
use lib "$FindBin::Bin/../../../../lib";

use lib "$FindBin::Bin/../inc";
use lib "$FindBin::Bin/../lib";

#use QAT::Test::Filter;
use QAT::Test;
#use QAT::Test;
use Simple::Test;

plan tests => 1 * blocks() + 11;

Expand Down

0 comments on commit 0bf52aa

Please sign in to comment.