Adding missing model object and tests for dao.

This commit is contained in:
sergiotarxz 2022-03-16 18:00:22 +01:00
parent ec313dada3
commit 865c61d036
2 changed files with 390 additions and 0 deletions

View File

@ -0,0 +1,198 @@
package Peace::Model::Application;
use v5.30.0;
use strict;
use warnings;
use Data::Dumper;
use Params::ValidationCompiler qw/validation_for/;
use Types::Standard qw/Str InstanceOf Bool Num HasMethods/;
use DateTime;
{
my $validator = validation_for(
params => {
uuid => { type => Str, optional => 1 },
date_creation => { type => InstanceOf ['DateTime'], optional => 1 },
name => { type => Str },
description => { type => Str },
url => { type => Str },
developer =>
{ type => InstanceOf ['Peace::Model::Developer'], optional => 1 },
developer_uuid => { type => Str, optional => 1 },
dbh => { type => HasMethods ['selectrow_hashref'], optional => 1 },
price => { type => Num },
git_repo => { type => Str },
flatpak_builder_file => { type => Str },
verified => { type => Bool },
}
);
sub new {
my $class = shift;
my %params = $validator->(@_);
die 'developer or developer_uuid should be passed on construct'
unless exists $params{developer}
|| ( exists $params{developer_uuid} && exists $params{dbh} );
my $self = bless {%params}, $class;
return $self;
}
}
{
my $validator =
validation_for( params => [ { type => Str, optional => 1 } ] );
sub uuid {
my $self = shift;
if (@_) {
my ($new_uuid) = $validator->(@_);
$self->{uuid} = $new_uuid;
}
return $self->{uuid};
}
}
{
my $validator =
validation_for(
params => [ { type => InstanceOf ['DateTime'], optional => 1 } ] );
sub date_creation {
my $self = shift;
if (@_) {
my ($new_date_creation) = $validator->(@_);
$self->{date_creation} = $new_date_creation;
}
return $self->{date_creation};
}
}
{
my $validator =
validation_for( params => [ { type => Str, optional => 1 } ] );
sub name {
my $self = shift;
if (@_) {
my ($new_name) = $validator->(@_);
$self->{name} = $new_name;
}
return $self->{name};
}
}
{
my $validator =
validation_for( params => [ { type => Str, optional => 1 } ] );
sub description {
my $self = shift;
if (@_) {
my ($new_description) = $validator->(@_);
$self->{description} = $new_description;
}
return $self->{description};
}
}
{
my $validator =
validation_for( params => [ { type => Str, optional => 1 } ] );
sub url {
my $self = shift;
if (@_) {
my ($new_url) = $validator->(@_);
$self->{url} = $new_url;
}
return $self->{url};
}
}
{
my $validator =
validation_for( params =>
[ { type => InstanceOf ['Peace::Model::Developer'], optional => 1 } ]
);
sub developer {
my $self = shift;
if (@_) {
my ($new_developer) = $validator->(@_);
$self->{developer} = $new_developer;
}
unless ( exists $self->{developer} ) {
my $developer_uuid = $self->{developer_uuid};
my $developer_dao =
Peace::DAO::Developer->new( dbh => $self->_dbh );
$self->{developer} =
$developer_dao->recover_by_uuid( uuid => $developer_uuid );
}
return $self->{developer};
}
}
{
my $validator =
validation_for( params => [ { type => Num, optional => 1 } ] );
sub price {
my $self = shift;
if (@_) {
my ($new_price) = $validator->(@_);
$self->{price} = $new_price;
}
return $self->{price};
}
}
{
my $validator =
validation_for( params => [ { type => Str, optional => 1 } ] );
sub flatpak_builder_file {
my $self = shift;
if (@_) {
my ($new_flatpak_builder_file) = $validator->(@_);
$self->{flatpak_builder_file} = $new_flatpak_builder_file;
}
return $self->{flatpak_builder_file};
}
}
{
my $validator =
validation_for( params => [ { type => Bool, optional => 1 } ] );
sub verified {
my $self = shift;
if (@_) {
my ($new_verified) = $validator->(@_);
$self->{verified} = $new_verified;
}
return $self->{verified};
}
}
{
my $validator =
validation_for( params => [ { type => Str, optional => 1 } ] );
sub git_repo {
my $self = shift;
if (@_) {
my ($new_git_repo) = $validator->(@_);
$self->{git_repo} = $new_git_repo;
}
return $self->{git_repo};
}
}
sub _dbh {
my $self = shift;
return $self->{dbh};
}
1;

192
t/00008-application-dao.t Normal file
View File

@ -0,0 +1,192 @@
use v5.30.0;
use strict;
use warnings;
use Test::Most tests => 3;
use DBI;
use DateTime;
use Peace::Model::Developer;
use Peace::Model::Application;
use DateTime::Format::Pg;
use DateTime;
BEGIN {
use_ok 'Peace::DAO::Application';
}
{
## GIVEN
my $sql = <<'EOF';
INSERT INTO applications
(name, description, url,
developer, price, git_repo,
flatpak_builder_file, verified)
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
RETURNING uuid;
EOF
### Developer
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:', '', '' );
### Application data
my $app_name = 'Desfronificator';
my $app_description = 'Desfronifies the files.';
my $app_url = 'https://desfronificator.example.com';
my $app_price = '4.20';
my $app_git_repo =
'https://git.desfronificator.example.com/larry/desfronificator.git';
my $app_flatpak_builder_file =
'./resources/com.example.desfronificator.yml';
my $app_verified = 0;
my $developer = Peace::Model::Developer->new(
uuid => $uuid,
secret_bcrypt => $secret_bcrypt,
name => $name,
surname => $surname,
email => $email,
country => $country,
verified => $verified,
);
my $application = Peace::Model::Application->new(
developer => $developer,
name => $app_name,
description => $app_description,
url => $app_url,
price => $app_price,
flatpak_builder_file => $app_flatpak_builder_file,
verified => $app_verified,
git_repo => $app_git_repo,
);
my $application_dao = Peace::DAO::Application->new( dbh => $dbh );
$dbh->{mock_add_resultset} = {
sql => $sql,
results => [ ['uuid'], [$uuid], ]
};
$sql = <<'EOF';
SELECT uuid, date_creation, name,
description, url, developer,
price, git_repo, flatpak_builder_file,
verified
FROM applications
WHERE uuid = ?
EOF
my $datetime = DateTime->now;
my $formatter = DateTime::Format::Pg->new;
$dbh->{mock_add_resultset} = {
sql => $sql,
results => [
[
'uuid', 'date_creation',
'name', 'description',
'url', 'developer',
'price', 'git_repo',
'flatpak_builder_file', 'verified',
],
[
$uuid,
$formatter->format_datetime($datetime),
$app_name,
$app_description,
$app_url,
$developer->uuid,
$app_price,
$app_git_repo,
$app_flatpak_builder_file,
$app_verified
]
],
};
## WHEN
$application_dao->create( application => $application );
## THEN
is $application->uuid, $uuid,
'Application uuid correctly set after user creation.';
}
{
### Developer
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:', '', '' );
### Application data
my $app_name = 'Desfronificator';
my $app_description = 'Desfronifies the files.';
my $app_url = 'https://desfronificator.example.com';
my $app_price = '4.20';
my $app_git_repo =
'https://git.desfronificator.example.com/larry/desfronificator.git';
my $app_flatpak_builder_file =
'./resources/com.example.desfronificator.yml';
my $app_verified = 0;
my $developer = Peace::Model::Developer->new(
uuid => $uuid,
secret_bcrypt => $secret_bcrypt,
name => $name,
surname => $surname,
email => $email,
country => $country,
verified => $verified,
);
my $application_dao = Peace::DAO::Application->new( dbh => $dbh );
my $sql = <<'EOF';
SELECT uuid, date_creation, name,
description, url, developer,
price, git_repo, flatpak_builder_file,
verified
FROM applications
WHERE uuid = ?
EOF
my $datetime = DateTime->now;
my $formatter = DateTime::Format::Pg->new;
$dbh->{mock_add_resultset} = {
sql => $sql,
results => [
[
'uuid', 'date_creation',
'name', 'description',
'url', 'developer',
'price', 'git_repo',
'flatpak_builder_file', 'verified',
],
[
$uuid,
$formatter->format_datetime($datetime),
$app_name,
$app_description,
$app_url,
$developer->uuid,
$app_price,
$app_git_repo,
$app_flatpak_builder_file,
$app_verified
]
],
};
## WHEN
my $application = $application_dao->recover_by_uuid( uuid => $uuid );
## THEN
is $application->uuid, $uuid,
'Application uuid correctly recovered.';
}