Adding more tests and fixing detected errors.

This commit is contained in:
sergiotarxz 2022-03-15 17:34:14 +01:00
parent c567043dc2
commit 94eafe55a2
3 changed files with 153 additions and 6 deletions

View File

@ -8,6 +8,8 @@ use warnings;
use Params::ValidationCompiler qw/validation_for/;
use Types::Standard qw/HasMethods InstanceOf Str/;
use DateTime::Format::Pg;
{
my $validator = validation_for(
params => {
@ -43,7 +45,7 @@ INSERT INTO developers (secret_bcrypt, name, surname, email, country, verified)
RETURNING uuid;
EOF
my $result = $dbh->selectrow_hashref(
$insert, undef, $developer->secret,
$insert, undef, $developer->secret_bcrypt,
$developer->name, $developer->surname, $developer->email,
$developer->country, $developer->verified
);
@ -70,14 +72,26 @@ EOF
my $dbh = $self->_dbh;
my $result = $dbh->selectrow_hashref( <<'EOF', undef, $uuid );
SELECT uuid, date_creation, secret_bcrypt, name, surname, email, stripe_id, country, verified
FROM customer
FROM developers
WHERE uuid = ?;
EOF
for my $key (keys %$result) {
delete $result->{$key} unless defined $result->{$key};
}
if ( exists $result->{date_creation} ) {
my $iso8601 = DateTime::Format::Pg->new;
$result->{date_creation} =
$iso8601->parse_datetime( $result->{date_creation} );
}
my $developer = Peace::Model::Developer->new(%$result);
return $developer;
}
}
sub _dbh {
my $self = shift;
return $self->{dbh};
}
1;

View File

@ -9,6 +9,8 @@ use DBI;
use DateTime;
use Peace::Model::Customer;
use DateTime::Format::Pg;
use DateTime;
BEGIN {
use_ok 'Peace::DAO::Customer';
@ -29,13 +31,15 @@ EOF
results => [ ['uuid'], [$uuid], ]
};
$sql = <<'EOF';
SELECT uuid, date_creation, secret_bcrypt, stripe_id FROM customer WHERE uuid = ?;
SELECT uuid, date_creation, secret_bcrypt, stripe_id FROM customers WHERE uuid = ?;
EOF
my $datetime = DateTime->now;
my $formatter = DateTime::Format::Pg->new;
$dbh->{mock_add_resultset} = {
sql => $sql,
results => [
[ 'uuid', 'secret_bcrypt', 'date_creation', 'stripe_id' ],
[ $uuid, 'hola', undef, undef ]
[ $uuid, 'hola', $formatter->format_datetime($datetime), undef ]
],
};
## WHEN
@ -50,9 +54,10 @@ EOF
my $dbh = DBI->connect( 'DBI:Mock:', '', '' );
my $customer_dao = Peace::DAO::Customer->new( dbh => $dbh );
my $sql = <<'EOF';
SELECT uuid, date_creation, secret_bcrypt, stripe_id FROM customer WHERE uuid = ?;
SELECT uuid, date_creation, secret_bcrypt, stripe_id FROM customers WHERE uuid = ?;
EOF
my $uuid = 'hola';
my $formatter = DateTime::Format::Pg->new;
my $date_creation = DateTime->now;
my $secret_bcrypt = 'hola';
my $stripe_id = undef;
@ -61,7 +66,7 @@ EOF
sql => $sql,
results => [
[ 'uuid', 'date_creation', 'secret_bcrypt', 'stripe_id' ],
[ $uuid, $date_creation, $secret_bcrypt, $stripe_id ],
[ $uuid, $formatter->format_datetime($date_creation), $secret_bcrypt, $stripe_id ],
]
};

128
t/00006-developer-dao.t Normal file
View File

@ -0,0 +1,128 @@
use v5.30.0;
use strict;
use warnings;
use Test::Most tests => 6;
use DBI;
use DateTime;
use Peace::Model::Developer;
use DateTime::Format::Pg;
use DateTime;
BEGIN {
use_ok 'Peace::DAO::Developer';
}
{
## GIVEN
my $sql = <<'EOF';
INSERT INTO developers (secret_bcrypt, name, surname, email, country, verified)
VALUES (?, ?, ?, ?, ?, ?)
RETURNING uuid;
EOF
my $uuid = 'hola';
my $secret_bcrypt = 'hola';
my $name = 'Larry';
my $surname = 'Wall';
my $email = 'larry@perl.org';
my $country = 'US';
my $verified = 0;
my $dbh = DBI->connect( 'DBI:Mock:', '', '' );
my $developer = Peace::Model::Developer->new(
name => $name,
surname => $surname,
email => $email,
country => $country,
secret_bcrypt => $secret_bcrypt,
verified => $verified,
);
my $developer_dao = Peace::DAO::Developer->new( dbh => $dbh );
$dbh->{mock_add_resultset} = {
sql => $sql,
results => [ ['uuid'], [$uuid], ]
};
$sql = <<'EOF';
SELECT uuid, date_creation, secret_bcrypt, name, surname, email, stripe_id, country, verified
FROM developers
WHERE uuid = ?;
EOF
my $datetime = DateTime->now;
my $formatter = DateTime::Format::Pg->new;
$dbh->{mock_add_resultset} = {
sql => $sql,
results => [
[
'uuid', 'name',
'surname', 'email',
'country', 'verified',
'secret_bcrypt', 'date_creation',
'stripe_id'
],
[
$uuid, $name, $surname, $email, $country, $verified,
$secret_bcrypt, $formatter->format_datetime($datetime), undef
]
],
};
## WHEN
$developer_dao->create( developer => $developer );
## THEN
is $developer->uuid, $uuid,
'Developer id correctly set after user creation.';
}
{
## GIVEN
my $dbh = DBI->connect( 'DBI:Mock:', '', '' );
my $developer_dao = Peace::DAO::Developer->new( dbh => $dbh );
my $sql = <<'EOF';
SELECT uuid, date_creation, secret_bcrypt, name, surname, email, stripe_id, country, verified
FROM developers
WHERE uuid = ?;
EOF
my $uuid = 'hola';
my $name = 'Larry';
my $surname = 'Wall';
my $email = 'larry@perl.org';
my $country = 'US';
my $verified = 0;
my $formatter = DateTime::Format::Pg->new;
my $datetime = DateTime->now;
my $date_creation = DateTime->now;
my $secret_bcrypt = 'hola';
my $stripe_id = undef;
$dbh->{mock_add_resultset} = {
sql => $sql,
results => [
[
'uuid', 'name',
'surname', 'email',
'country', 'verified',
'secret_bcrypt', 'date_creation',
'stripe_id'
],
[
$uuid, $name, $surname, $email, $country, $verified,
$secret_bcrypt, $formatter->format_datetime($datetime), undef
]
],
};
## WHEN
my $developer = $developer_dao->recover_by_uuid( uuid => $uuid );
## THEN
is $developer->uuid, $uuid, 'Uuid is recovered correctly';
is $developer->date_creation, $date_creation,
'Date creation is recovered correctly';
is $developer->secret_bcrypt, $secret_bcrypt,
'Secret is recovered correctly';
is $developer->stripe_id, $stripe_id, 'Secret is recovered correctly';
}