Adding more tests and fixing detected errors.
This commit is contained in:
parent
c567043dc2
commit
94eafe55a2
@ -8,6 +8,8 @@ use warnings;
|
|||||||
use Params::ValidationCompiler qw/validation_for/;
|
use Params::ValidationCompiler qw/validation_for/;
|
||||||
use Types::Standard qw/HasMethods InstanceOf Str/;
|
use Types::Standard qw/HasMethods InstanceOf Str/;
|
||||||
|
|
||||||
|
use DateTime::Format::Pg;
|
||||||
|
|
||||||
{
|
{
|
||||||
my $validator = validation_for(
|
my $validator = validation_for(
|
||||||
params => {
|
params => {
|
||||||
@ -43,7 +45,7 @@ INSERT INTO developers (secret_bcrypt, name, surname, email, country, verified)
|
|||||||
RETURNING uuid;
|
RETURNING uuid;
|
||||||
EOF
|
EOF
|
||||||
my $result = $dbh->selectrow_hashref(
|
my $result = $dbh->selectrow_hashref(
|
||||||
$insert, undef, $developer->secret,
|
$insert, undef, $developer->secret_bcrypt,
|
||||||
$developer->name, $developer->surname, $developer->email,
|
$developer->name, $developer->surname, $developer->email,
|
||||||
$developer->country, $developer->verified
|
$developer->country, $developer->verified
|
||||||
);
|
);
|
||||||
@ -70,14 +72,26 @@ EOF
|
|||||||
my $dbh = $self->_dbh;
|
my $dbh = $self->_dbh;
|
||||||
my $result = $dbh->selectrow_hashref( <<'EOF', undef, $uuid );
|
my $result = $dbh->selectrow_hashref( <<'EOF', undef, $uuid );
|
||||||
SELECT uuid, date_creation, secret_bcrypt, name, surname, email, stripe_id, country, verified
|
SELECT uuid, date_creation, secret_bcrypt, name, surname, email, stripe_id, country, verified
|
||||||
FROM customer
|
FROM developers
|
||||||
WHERE uuid = ?;
|
WHERE uuid = ?;
|
||||||
EOF
|
EOF
|
||||||
for my $key (keys %$result) {
|
for my $key (keys %$result) {
|
||||||
delete $result->{$key} unless defined $result->{$key};
|
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);
|
my $developer = Peace::Model::Developer->new(%$result);
|
||||||
return $developer;
|
return $developer;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
sub _dbh {
|
||||||
|
my $self = shift;
|
||||||
|
return $self->{dbh};
|
||||||
|
}
|
||||||
1;
|
1;
|
||||||
|
@ -9,6 +9,8 @@ use DBI;
|
|||||||
use DateTime;
|
use DateTime;
|
||||||
|
|
||||||
use Peace::Model::Customer;
|
use Peace::Model::Customer;
|
||||||
|
use DateTime::Format::Pg;
|
||||||
|
use DateTime;
|
||||||
|
|
||||||
BEGIN {
|
BEGIN {
|
||||||
use_ok 'Peace::DAO::Customer';
|
use_ok 'Peace::DAO::Customer';
|
||||||
@ -29,13 +31,15 @@ EOF
|
|||||||
results => [ ['uuid'], [$uuid], ]
|
results => [ ['uuid'], [$uuid], ]
|
||||||
};
|
};
|
||||||
$sql = <<'EOF';
|
$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
|
EOF
|
||||||
|
my $datetime = DateTime->now;
|
||||||
|
my $formatter = DateTime::Format::Pg->new;
|
||||||
$dbh->{mock_add_resultset} = {
|
$dbh->{mock_add_resultset} = {
|
||||||
sql => $sql,
|
sql => $sql,
|
||||||
results => [
|
results => [
|
||||||
[ 'uuid', 'secret_bcrypt', 'date_creation', 'stripe_id' ],
|
[ 'uuid', 'secret_bcrypt', 'date_creation', 'stripe_id' ],
|
||||||
[ $uuid, 'hola', undef, undef ]
|
[ $uuid, 'hola', $formatter->format_datetime($datetime), undef ]
|
||||||
],
|
],
|
||||||
};
|
};
|
||||||
## WHEN
|
## WHEN
|
||||||
@ -50,9 +54,10 @@ EOF
|
|||||||
my $dbh = DBI->connect( 'DBI:Mock:', '', '' );
|
my $dbh = DBI->connect( 'DBI:Mock:', '', '' );
|
||||||
my $customer_dao = Peace::DAO::Customer->new( dbh => $dbh );
|
my $customer_dao = Peace::DAO::Customer->new( dbh => $dbh );
|
||||||
my $sql = <<'EOF';
|
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
|
EOF
|
||||||
my $uuid = 'hola';
|
my $uuid = 'hola';
|
||||||
|
my $formatter = DateTime::Format::Pg->new;
|
||||||
my $date_creation = DateTime->now;
|
my $date_creation = DateTime->now;
|
||||||
my $secret_bcrypt = 'hola';
|
my $secret_bcrypt = 'hola';
|
||||||
my $stripe_id = undef;
|
my $stripe_id = undef;
|
||||||
@ -61,7 +66,7 @@ EOF
|
|||||||
sql => $sql,
|
sql => $sql,
|
||||||
results => [
|
results => [
|
||||||
[ 'uuid', 'date_creation', 'secret_bcrypt', 'stripe_id' ],
|
[ '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
128
t/00006-developer-dao.t
Normal 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';
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user