Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Use .dispose when needed
  • Loading branch information
salortiz committed Mar 24, 2016
1 parent f174fa6 commit dddfba3
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 26 deletions.
27 changes: 19 additions & 8 deletions lib/DBIish/CommonTesting.pm6
Expand Up @@ -136,6 +136,7 @@ method run-tests {

is $rc, 1, "execute one with one integer parameter should return 1 row affected";
is $sth.rows, 1, '$sth.rows for execute one with one integer parameter should report 1 row affected';
$sth.dispose;

ok $sth = $dbh.prepare( "
INSERT INTO nom (price)
Expand All @@ -144,6 +145,7 @@ method run-tests {
ok $rc = $sth.execute(4.85), "execute one with one float parameter";
is $rc, 1, "execute one with one float parameter should return 1 row affected";
is $sth.rows, 1, '$sth.rows for execute one with one float parameter should report 1 row affected';
$sth.dispose;

ok $sth = $dbh.prepare( "
INSERT INTO nom (name, description, quantity, price)
Expand Down Expand Up @@ -337,30 +339,37 @@ method run-tests {
}
else { skip 'fetchrow_arrayref not implemented', 2 }

$sth.finish;
$sth.dispose;

# test quotes and so on
{
$sth = $dbh.prepare(q[INSERT INTO nom (name, description) VALUES (?, ?)]);
my $lived;
lives-ok { $sth.execute("quot", q["';]); $lived = 1 }, 'can insert single and double quotes';
lives-ok {
$sth.execute("quot", q["';]); $lived = 1
}, 'can insert single and double quotes';
$sth.dispose;
if $lived {
$sth = $dbh.prepare(q[SELECT description FROM nom where name = ?]);
lives-ok { $sth.execute('quot') }, 'lived while retrieving result';
$sth = $dbh.prepare(q[SELECT description FROM nom WHERE name = ?]);
lives-ok {
$sth.execute('quot');
}, 'lived while retrieving result';
is $sth.fetchrow.join, q["';], 'got the right string back';
$sth.finish;
$sth.dispose;
}
else {
skip('dependent tests', 2);
}

$lived = 0;
lives-ok { $dbh.do(q[INSERT INTO nom (name, description) VALUES(?, '?"')], 'mark'); $lived = 1}, 'can use question mark in quoted strings';
lives-ok {
$dbh.do(q[INSERT INTO nom (name, description) VALUES(?, '?"')], 'mark'); $lived = 1
}, 'can use question mark in quoted strings';
if $lived {
my $sth = $dbh.prepare(q[SELECT description FROM nom WHERE name = 'mark']);
$sth.execute;
is $sth.fetchrow.join, '?"', 'correctly retrieved question mark';
$sth.finish;
$sth.dispose;
}
else {
skip('dependent test', 1);
Expand All @@ -375,6 +384,7 @@ method run-tests {
my $row = $sth.fetchrow-hash;

ok !?$row, 'a query with no results should have a falsy value';
$sth.dispose;
}

# test that a query that's exhausted its result set has a falsy value
Expand All @@ -386,6 +396,7 @@ method run-tests {
$row = $sth.fetchrow-hash;

ok !?$row, 'a query with no more results should have a falsy value';
$sth.dispose;
}

# test that an integer >= 2**31 still works as an argument to execute
Expand All @@ -402,7 +413,7 @@ method run-tests {
is $row[1], 'many', 'The contents of the row fetched via a large integer are correct';
is $row[2], $large-int, 'The contents of the row fetched via a large integer are correct';

$sth.finish;
$sth.dispose;
}

# Drop the table when finished, and disconnect
Expand Down
47 changes: 29 additions & 18 deletions t/26-mysql-blob.t
Expand Up @@ -2,7 +2,7 @@ v6;
use Test;
use DBIish;

plan 18;
plan 19;

my %con-parms = :database<dbdishtest>, :user<testuser>, :password<testpass>;
my $dbh;
Expand Down Expand Up @@ -31,20 +31,31 @@ lives-ok {
}, 'Table created';
my $blob = Buf.new(^256);
my $query = 'INSERT INTO test VALUES(?, ?)';
ok (my $sth = $dbh.prepare($query)), "Prepared '$query'";
ok $sth.execute(1, $blob), 'Executed with buf';
ok $sth.execute(2, Buf), 'Executed without buf';
ok $sth = $dbh.prepare('SELECT name FROM test WHERE id = ?'), 'SELECT prepared';
ok $sth.execute(1), 'Executed for 1';
ok (my @res = $sth.row), 'Get a row';
is @res.elems, 1, 'One field';
ok (my $data = @res[0]), 'With data at 0';
ok $data ~~ Buf, 'Data is-a Buf';
is $data, $blob, 'Data in Buf match with original';
ok $sth.execute(2), 'Executed for 2';
ok (@res = $sth.row), 'Get a row';
is @res.elems, 1, 'One field';
$data = @res[0];
ok $data ~~ Buf, 'Data is-a Buf';
ok not $data.defined, 'But is NULL';
$dbh.do('DROP TABLE IF EXISTS test');

with $dbh.prepare($query) {
LEAVE { .dispose }
ok $_, "Prepared '$query'";
ok .execute(1, $blob), 'Executed with buf';
ok .execute(2, Buf), 'Executed without buf';
} else { .fail }

with $dbh.prepare('SELECT name FROM test WHERE id = ?') {
LEAVE { .dispose }
ok $_, 'SELECT prepared';
ok .execute(1), 'Executed for 1';
ok (my @res = .row), 'Get a row';
is @res.elems, 1, 'One field';
ok (my $data = @res[0]), 'With data at 0';
ok $data ~~ Buf, 'Data is-a Buf';
is $data, $blob, 'Data in Buf match with original';
ok .execute(2), 'Executed for 2';
ok (@res = .row), 'Get a row';
is @res.elems, 1, 'One field';
$data = @res[0];
ok $data ~~ Buf, 'Data is-a Buf';
ok not $data.defined, 'But is NULL';
} else { .fail }

lives-ok {
$dbh.do('DROP TABLE IF EXISTS test');
}, 'All clean';

0 comments on commit dddfba3

Please sign in to comment.