Skip to content

Commit

Permalink
GLR test target
Browse files Browse the repository at this point in the history
  • Loading branch information
salortiz committed Mar 19, 2016
1 parent bf21be9 commit 0055e68
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 21 deletions.
17 changes: 6 additions & 11 deletions lib/DBDish/TestMock/StatementHandle.pm6
Expand Up @@ -3,23 +3,18 @@ need DBDish;

unit class DBDish::TestMock::StatementHandle does DBDish::StatementHandle;

my @data = (
[<a b c>],
[<d e f>],
);
my @data = (|<a b>, 1), (|<d e>, 2);

has Int $!current_idx = 0;

method execute(*@) {
self!enter-execute;
$!current_idx = 0;
@!column-name = <col1 col2 col3>;
@!column-name = <col1 col2 colN>;
@!column-type = (Str, Str, Int);
self!done-execute(@data.elems, True)
}

method _row {self.fetchrow}

method fetchrow { (@data[$!current_idx++] // ()).list }

method _free { }
method finish { True }
method _row { @data[$!current_idx++] // @ }
method _free { }
method finish { True }
47 changes: 37 additions & 10 deletions t/05-mock.t
@@ -1,31 +1,58 @@
use v6;
use Test;
use DBIish;
plan 9;
plan 22;

my $dbh = DBIish.connect('TestMock');
my $sth = $dbh.prepare('everything');

$sth.execute;
is $sth.fetchrow.join(','), 'a,b,c', 'first row';
is $sth.fetchrow.join(','), 'd,e,f', 'second row';
nok $sth.fetchrow, 'third row is empty';
is $sth.column-names, <col1 col2 colN>, 'Columns';
is $sth.column-types.perl, [Str, Str, Int].perl, 'Types';
is $sth.rows, 2, 'Results';

is $sth.row.join(','), 'a,b,1', 'first row';
is $sth.row.join(','), 'd,e,2', 'second row';
nok $sth.row, 'third row is empty';

$sth.execute;
is $sth.fetchrow.join(','), 'a,b,1', 'first row (legacy)';
is $sth.fetchrow.join(','), 'd,e,2', 'second row (legacy)';
nok $sth.fetchrow, 'third row is empty (legacy)';

$sth.execute;
# Testing the internals
my \a = $sth.allrows;
isa-ok a, Seq, 'allrows returns Seq';
ok my $iter = a.iterator, 'Got iterator';
is $iter.pull-one, ['a', 'b', 1], 'A row';

$sth.execute;
is-deeply [$sth.allrows], [ ['a', 'b', 1], ['d','e', 2]], 'allrows';
$sth.execute;
is-deeply [$sth.fetchall-array], [ ['a', 'b', '1'], ['d', 'e', '2']], 'fetchall-array';

$sth.execute;
is-deeply [$sth.fetchall-array], [ [<a b c>], [<d e f>]], 'fetchall-array';
is-deeply $sth.row :hash, hash(col1 => 'a', col2 => 'b', colN => 1),
'row :hash (1)';
is-deeply $sth.row :hash, hash(col1 => 'd', col2 => 'e', colN => 2),
'row :hash (2)';
is-deeply $sth.row :hash, hash(), 'row :hash (empty)';

$sth.execute;
is-deeply $sth.fetchrow-hash, hash(col1 => 'a', col2 => 'b', col3 => 'c'),
is-deeply $sth.fetchrow-hash, hash(col1 => 'a', col2 => 'b', colN => '1'),
'fetchrow-hash (1)';
is-deeply $sth.fetchrow-hash, hash(col1 => 'd', col2 => 'e', col3 => 'f'),
is-deeply $sth.fetchrow-hash, hash(col1 => 'd', col2 => 'e', colN => '2'),
'fetchrow-hash (2)';
is-deeply $sth.fetchrow-hash, hash(), 'fetchrow-hash (empty)';

$sth.execute;
is-deeply $sth.fetchall-hash, hash(col1 => [<a d>], col2 => [<b e>], col3 => [<c f>]), 'fetchall-HoA';
is-deeply $sth.fetchall-hash, hash(
col1 => [<a d>], col2 => [<b e>], colN => ['1', '2']
), 'fetchall-HoA';

$sth.execute;
is-deeply $sth.fetchall-AoH, (
{col1 => 'a', col2 => 'b', col3 => 'c'},
{col1 => 'd', col2 => 'e', col3 => 'f'},
{col1 => 'a', col2 => 'b', colN => '1'},
{col1 => 'd', col2 => 'e', colN => '2'},
).list, 'fetchall-AoH';

0 comments on commit 0055e68

Please sign in to comment.