Permalink
Browse files

added data2row.

  • Loading branch information...
karupanerura committed Aug 30, 2013
1 parent 9fc50fd commit 7fa47a494676e890a29477a9379dd683e9e30cef
Showing with 59 additions and 0 deletions.
  1. +26 −0 lib/Teng.pm
  2. +33 −0 t/001_basic/035_data2row.t
@@ -607,6 +607,32 @@ sub single_by_sql {
);
}

sub data2row {
my ($self, $table_name, $data) = @_;

my $table = $self->{schema}->get_table( $table_name );
Carp::croak("No such table $table_name") unless $table;

return $data if $self->{suppress_row_objects};

$table->{row_class}->new(
{
sql => do {
my @caller = caller(0);
my $level = 0;
while ($caller[0] eq __PACKAGE__ || $caller[0] eq ref $self) {
@caller = caller(++$level);
}
sprintf '/* DUMMY QUERY %s->data2row created from %s line %d */', ref $self, $caller[1], $caller[2];
},
row_data => $data,
teng => $self,
table => $table,
table_name => $table_name,
}
);
}

sub single_named {
my ($self, $sql, $args, $table_name) = @_;

@@ -0,0 +1,33 @@
use t::Utils;
use Mock::Basic;
use Test::More;

my $dbh = t::Utils->setup_dbh;
my $db_basic = Mock::Basic->new({dbh => $dbh});
$db_basic->setup_test_db;

subtest 'data2row method' => sub {
my $raw_data = [
{
id => 1,
name => 'perl',
},
{
id => 2,
name => 'ruby',
},
{
id => 3,
name => 'python',
},
];

my $rows = [ map { $db_basic->data2row(mock_basic => $_) } @$raw_data ];
isa_ok $_, 'Teng::Row' for @$rows;
is $rows->[0]->id, 1;
is $rows->[1]->id, 2;
is $rows->[2]->id, 3;
};

done_testing;

0 comments on commit 7fa47a4

Please sign in to comment.