Peace/t/00002-customer-dao.t

85 lines
2.3 KiB
Perl

use v5.30.0;
use strict;
use warnings;
use Test::Most tests => 6;
use DBI;
use DateTime;
use Peace::Model::Customer;
use DateTime::Format::Pg;
use DateTime;
BEGIN {
use_ok 'Peace::DAO::Customer';
}
{
## GIVEN
my $sql = <<'EOF';
INSERT INTO customers (secret_bcrypt) VALUES (?) RETURNING uuid;
EOF
my $uuid = 'hola';
my $dbh = DBI->connect( 'DBI:Mock:', '', '' );
my $customer = Peace::Model::Customer->new( secret_bcrypt => 'hola' );
my $customer_dao = Peace::DAO::Customer->new( dbh => $dbh );
$dbh->{mock_add_resultset} = {
sql => $sql,
results => [ ['uuid'], [$uuid], ]
};
$sql = <<'EOF';
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', $formatter->format_datetime($datetime), undef ]
],
};
## WHEN
$customer_dao->create( customer => $customer );
## THEN
is $customer->uuid, $uuid, 'Customer id correctly set after user creation.';
}
{
## GIVEN
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 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;
$dbh->{mock_add_resultset} = {
sql => $sql,
results => [
[ 'uuid', 'date_creation', 'secret_bcrypt', 'stripe_id' ],
[ $uuid, $formatter->format_datetime($date_creation), $secret_bcrypt, $stripe_id ],
]
};
## WHEN
my $customer = $customer_dao->recover_by_uuid( uuid => $uuid );
## THEN
is $customer->uuid, $uuid, 'Uuid is recovered correctly';
is $customer->date_creation, $date_creation,
'Date creation is recovered correctly';
is $customer->secret_bcrypt, $secret_bcrypt,
'Secret is recovered correctly';
is $customer->stripe_id, $stripe_id, 'Secret is recovered correctly';
}