Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
TestMock: Allow generation from syntetic data
Mimic DBD::Sponge reflexion capabilities.
  • Loading branch information
salortiz committed May 16, 2016
1 parent 82c072f commit 9d13ea0
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 12 deletions.
2 changes: 1 addition & 1 deletion lib/DBDish/TestMock.pm6
@@ -1,7 +1,7 @@
use v6;
need DBDish;

unit class DBDish::TestMock:ver<0.0.2> does DBDish::Driver;
unit class DBDish::TestMock:ver<0.0.3> does DBDish::Driver;
need DBDish::TestMock::Connection;

method connect() { DBDish::TestMock::Connection.new(:parent(self)) }
Expand Down
4 changes: 3 additions & 1 deletion lib/DBDish/TestMock/Connection.pm6
Expand Up @@ -4,5 +4,7 @@ need DBDish;
unit class DBDish::TestMock::Connection does DBDish::Connection;
need DBDish::TestMock::StatementHandle;

method prepare($) { DBDish::TestMock::StatementHandle.new(:parent(self)) }
method prepare($statement) {
DBDish::TestMock::StatementHandle.new(:$statement, :parent(self), |%_)
}
method _disconnect { }
30 changes: 23 additions & 7 deletions lib/DBDish/TestMock/StatementHandle.pm6
Expand Up @@ -3,18 +3,34 @@ need DBDish;

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

my @data = (|<a b>, 1), (|<d e>, 2);
has List $!data = ((|<a b>, 1), (|<d e>, 2));
has int $!current_idx;
has $.statement;

has Int $!current_idx = 0;
submethod BUILD(:$!parent!, :$!statement, :$rows,
:$col-names = <col1 col2 colN>;
:$col-types = (Str, Str, Int);
) {
with $rows {
$!data = $_;
}
@!column-name = @($col-names);
@!column-type = @($col-types);
}

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

method _row { @data[$!current_idx++] // @ }
method _row {
if $!current_idx < $!data.elems {
$!data[$!current_idx++];
} else {
self.finish;
@
}
}
method _free { }
method finish { True }
method finish { $!Finished = True }
10 changes: 7 additions & 3 deletions t/05-mock.t
@@ -1,19 +1,23 @@
use v6;
use Test;
use DBIish;
plan 22;
plan 25;

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

$sth.execute;
nok $sth.Executed, 'Not executed yet';
is $sth.statement, 'mockdata', 'Statement';
is $sth.column-names, <col1 col2 colN>, 'Columns';
is $sth.column-types.perl, [Str, Str, Int].perl, 'Types';

$sth.execute;
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';
ok $sth.Finished, 'Finished';

$sth.execute;
is $sth.fetchrow.join(','), 'a,b,1', 'first row (legacy)';
Expand Down

0 comments on commit 9d13ea0

Please sign in to comment.