Adding application recovery from developer.

This commit is contained in:
sergiotarxz 2022-03-17 02:41:47 +01:00
parent 865c61d036
commit 138e21137a
5 changed files with 271 additions and 9 deletions

View File

@ -5,7 +5,9 @@ use v5.30.0;
use strict;
use warnings;
use Test::Most tests => 2;
use Data::Dumper;
use Test::Most tests => 4;
use DateTime;
@ -56,3 +58,47 @@ use Peace::DAO::Application;
ok $application->uuid, 'Generated uuid.';
ok $application->date_creation > $current_date, 'The date is recent.';
}
{
## GIVEN
my $current_date = DateTime->now;
my $peace = Peace->new;
my $home = $ENV{HOME};
my $config =
$peace->plugin(
JSONConfig => { file => "$home/.config/peace/peace.conf" } );
my $dbh = Peace::DB->dbh( config => $config );
my $secret_bcrypt = 'hola';
my $developer = Peace::Model::Developer->new(
name => 'Larry',
surname => 'Wall',
email => 'larry@perl.org',
country => 'US',
verified => 0,
secret_bcrypt => $secret_bcrypt
);
my $developer_dao = Peace::DAO::Developer->new( dbh => $dbh );
$developer_dao->create( developer => $developer );
my $application = Peace::Model::Application->new(
name => 'Desfronificator',
description => 'Desfronifies the files.',
url => 'desfronificator.example.com',
developer => $developer,
price => 0,
flatpak_builder_file => './resources/com.example.desfronificator.yml',
git_repo =>
'https://git.desfronificator.example.com/larry/desfronificator.git',
verified => 0,
);
my $application_dao = Peace::DAO::Application->new( dbh => $dbh );
$application_dao->create( application => $application );
## WHEN
my $applicatios =
$application_dao->recover_by_developer( developer => $developer );
## THEN
ok @$applicatios, 'Applications was at least a element.';
ok $applicatios->[0]->isa('Peace::Model::Application'),
'Application is made of developer.';
}

View File

@ -6,7 +6,7 @@ use strict;
use warnings;
use Params::ValidationCompiler qw/validation_for/;
use Types::Standard qw/HasMethods InstanceOf Str/;
use Types::Standard qw/HasMethods InstanceOf Str HashRef/;
{
my $validator = validation_for(
@ -63,6 +63,39 @@ EOF
}
}
{
my $validator = validation_for(
params => {
developer =>
{ type => InstanceOf ['Peace::Model::Developer'], optional => 1 },
}
);
sub recover_by_developer {
my $self = shift;
my %params = $validator->(@_);
my $developer = $params{developer};
my $developer_uuid = $developer->uuid;
my $dbh = $self->_dbh;
if ( !defined $developer_uuid ) {
die "Developer has no uuid yet when recovering applications.";
}
my $result =
$dbh->selectall_arrayref(
<<'EOF', { Slice => {} }, $developer->uuid );
SELECT uuid, date_creation, name,
description, url, developer,
price, git_repo, flatpak_builder_file,
verified
FROM applications
WHERE developer = ?;
EOF
$result = [ map { $self->_select_result_to_application($_) } @$result ];
return $result;
}
}
{
my $validator = validation_for(
params => {
@ -86,6 +119,16 @@ EOF
if ( !defined $result ) {
die "Application with uuid $uuid not found.";
}
return $self->_select_result_to_application($result);
}
}
{
my $validator = validation_for( params => [ { type => HashRef }, ] );
sub _select_result_to_application {
my $self = shift;
my ($result) = $validator->(@_);
if ( exists $result->{developer} ) {
$result->{developer_uuid} = delete $result->{developer};
$result->{dbh} = $self->_dbh;

View File

@ -5,10 +5,12 @@ use strict;
use warnings;
use Params::ValidationCompiler qw/validation_for/;
use Types::Standard qw/Str InstanceOf Bool/;
use Types::Standard qw/Str InstanceOf Bool HasMethods/;
use DateTime;
use Peace::DAO::Application;
{
my $validator = validation_for(
params => {
@ -21,6 +23,7 @@ use DateTime;
stripe_id => { type => Str, optional => 1 },
country => { type => Str },
verified => { type => Bool },
dbh => { type => HasMethods ['selectall_arrayref'], optional => 1 },
}
);
@ -32,6 +35,20 @@ use DateTime;
}
}
sub applications {
my $self = shift;
if (!defined $self->{applications}) {
my $dbh = $self->_dbh;
if (!defined $dbh) {
die "There is no database handle, so no chance to recover applications from the developer.";
}
my $application_dao = Peace::DAO::Application->new( dbh => $dbh );
$self->{applications} = $application_dao->recover_by_developer(developer => $self);
}
return $self->{applications};
}
{
my $validator =
validation_for( params => [ { type => Str, optional => 1 } ] );
@ -158,4 +175,9 @@ use DateTime;
return $self->{verified};
}
}
sub _dbh {
my $self = shift;
return $self->{dbh};
}
1;

View File

@ -3,7 +3,12 @@ use v5.30.0;
use strict;
use warnings;
use Test::Most tests => 5;
use Test::Most tests => 6;
use Test::MockModule;
use DBI;
use Peace::Model::Application;
BEGIN {
use_ok 'Peace::Model::Developer';
@ -58,3 +63,76 @@ BEGIN {
## THEN
is $developer->uuid, $uuid, 'Uuid can be set.';
}
{
## GIVEN
my $application_dao_mock = Test::MockModule->new('Peace::DAO::Application');
### Developer data
my $uuid = 'example';
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_name0 = 'Desfronificator';
my $app_name1 = 'Desfronificator 1.0';
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(
secret_bcrypt => $secret_bcrypt,
name => $name,
surname => $surname,
email => $email,
country => $country,
verified => $verified,
dbh => $dbh,
);
my $applications = [
Peace::Model::Application->new(
name => $app_name0,
description => $app_description,
url => $app_url,
price => $app_price,
git_repo => $app_git_repo,
flatpak_builder_file => $app_flatpak_builder_file,
verified => $app_verified,
developer => $developer,
),
Peace::Model::Application->new(
name => $app_name0,
description => $app_description,
url => $app_url,
price => $app_price,
git_repo => $app_git_repo,
flatpak_builder_file => $app_flatpak_builder_file,
verified => $app_verified,
developer => $developer,
),
];
$application_dao_mock->mock(
recover_by_developer => sub {
return $applications;
}
);
## WHEN
my $new_applications = $developer->applications;
## THEN
is_deeply $new_applications, $applications,
'Applications are correctly recovered from the developer.';
}

View File

@ -3,7 +3,7 @@ use v5.30.0;
use strict;
use warnings;
use Test::Most tests => 3;
use Test::Most tests => 5;
use DBI;
use DateTime;
@ -187,6 +187,79 @@ EOF
my $application = $application_dao->recover_by_uuid( uuid => $uuid );
## THEN
is $application->uuid, $uuid,
'Application uuid correctly recovered.';
is $application->uuid, $uuid, 'Application uuid correctly recovered.';
}
{
## GIVEN
my $sql = <<'EOF';
SELECT uuid, date_creation, name,
description, url, developer,
price, git_repo, flatpak_builder_file,
verified
FROM applications
WHERE developer = ?;
EOF
### Developer data
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(
uuid => $uuid,
secret_bcrypt => $secret_bcrypt,
name => $name,
surname => $surname,
email => $email,
country => $country,
verified => $verified,
);
### Application data
my $app_uuid = 'denuheesuehs';
my $app_date_creation = DateTime->now;
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 $iso8601 = DateTime::Format::Pg->new;
$dbh->{mock_add_resultset} = {
sql => $sql,
results => [
[
'uuid', 'date_creation',
'name', 'description',
'url', 'price',
'git_repo', 'flatpak_builder_file',
'verified', 'developer'
],
[
$app_uuid, $iso8601->format_datetime($app_date_creation),
$app_name, $app_description,
$app_url, $app_price,
$app_git_repo, $app_flatpak_builder_file,
$app_verified, $developer->uuid,
],
]
};
my $application_dao = Peace::DAO::Application->new( dbh => $dbh );
## WHEN
my $applications =
$application_dao->recover_by_developer( developer => $developer );
## THEN
ok @$applications, 'Applications can be recovered from developer.';
ok $applications->[0]->isa('Peace::Model::Application'),
'Application is made of developer.';
}